Anh Tran dev notes

Production bugs from mobile and backend work, written up with the fix and how it was checked.

Note 14 / Security / NestJS / Backend

Your HTTP access log is a credential store: refresh tokens, OAuth codes and Bearer headers in pino-http

pino-http logs the full URL and every header by default. How that put live refresh tokens in two NestJS backends' logs, and the redaction and whitelist fixes that emptied them.

By
Trần Thế Anh, Software Engineer
Published
Work done
October 2026, AI Note Taker and Dramma Short (NestJS, pino-http)
Stack
NestJS 10 and 11, nestjs-pino, pino-http, Express 5, supertest, Jest
Reading
7 min, 1,421 words

A tester on the Dramma Short backend ran the server locally for a while and then searched its log for the word Bearer. The count was 44,773 lines. Next to it sat a response-info.log of 230 MB that held accessToken and refreshToken values from response bodies. Nothing had been misconfigured on purpose. The logger was running on its defaults, which is the point of this note.

A week earlier an independent check of the AI Note Taker backend found a smaller version of the same problem with a worse consequence. Version 1.0.0 to 1.0.3 of that app refreshed its session with GET /auth/refresh-token?refreshToken=<jwt>, where the JWT lives 30 days. Every refresh wrote a working 30-day credential into the access log as part of the URL.

What the defaults write

pino-http builds each request line from a req serializer. Its README documents that the default for req is pino.stdSerializers.req. That serializer records the method, the URL with its query string, the query object and all request headers. So Authorization: Bearer <access token> and Cookie: refreshToken=<jwt> are in every line, and so is anything carried in the URL: refresh tokens, OAuth ?code=&state= on callbacks, a ?handoff= code on a web sign-in flow.

A comment in the AI Note Taker logger options records how this was confirmed: a request with a made-up header, then a search of the log for that exact string. The string was there. The comment stays in the file because the next person to tidy it will ask why the config is so fussy.

Whoever can read the log can then act as the user. In a hosting dashboard, a log file on disk, or a log shipper added later, that is more people than should hold a 30-day session. The OWASP Logging Cheat Sheet lists access tokens and session identifiers under data to exclude from logs, or at least to remove, mask, hash or encrypt. A URL is also a poor place for a secret for a second reason: it is copied by proxies and crash reporters on the way, which is why the refresh route moved to a POST body. Moving the token fixes new traffic. The old app versions on the stores still send it in the query, so the log had to be fixed on its own.

Two different leaks need two different tools

Headers and URLs are not the same problem, and the pino tool that fits one does not fit the other.

redact in pino takes a list of paths into the log object and replaces the values. It fits headers, because their paths are known in advance:

pinoHttp: {
  redact: {
    paths: [
      'req.headers.authorization',
      'req.headers.cookie',
      'req.headers["x-api-key"]',
      'res.headers["set-cookie"]',
    ],
    censor: '[hidden]',
  },
}

The last path matters. A refresh token is issued in the response’s Set-Cookie, so masking the request side and forgetting the response side leaves the new token in the log on every sign-in. The pino redaction documentation describes the path syntax.

redact cannot help with the URL. The secret is not at a known key of the log object. It is inside one string, req.url, at a position that depends on the route. For that I replaced the req serializer and rewrote the query part of the URL:

function redactUrlQuery(url: string): string {
  const [path, query] = url.split('?', 2);
  if (!query) return url;
  const params = new URLSearchParams(query);
  for (const [key, value] of params) {
    if (SENSITIVE_KEYS.has(key.toLowerCase()) || SECRET_PATTERNS.some(p => p.test(value))) {
      params.set(key, '[hidden]');
    }
  }
  return `${path}?${params}`;
}

serializers: {
  req(req) { // req comes from pino's standard serializer
    req.url = redactUrlQuery(req.url);
    if (req.query) req.query = redactSensitive(req.query);
    return req;
  },
}

A harmless parameter such as client=app stays visible, which keeps the logs useful for debugging. The decision to hide a value comes from two checks. One is the key name. The other is the shape of the value: a JWT, an X-Amz-Signature= presigned URL, or a provider API-key prefix gets hidden under any key name, so a new parameter called tk does not slip through.

One list of sensitive keys, not two

The service already had a function that redacted request bodies for a different log line. The URL fix reuses its key list instead of adding a second one. Two lists drift: someone adds handoff to the body list and the URL list never learns. The version in the code has one SENSITIVE_KEYS set that both paths import. It holds the obvious names (password variants, access, refresh and identity tokens, authorization, cookie, secret, apikey) and the ones that are easy to forget: code, state, nonce, authorizationCode, handoff, signature, and content fields such as transcript, text and email. A comment on the set says what it is: a safety net, not permission to log bodies. A response interceptor in that service once relied on “the body will be redacted later” and nobody checked, so a password, an access token and a transcript sat in the log together before it was caught.

The cheaper answer: whitelist what you log

On Dramma Short I did not mask. I removed. The request serializer there returns only the request id, the method and the path with the query cut off. The response serializer returns only the status code. Both header objects are redacted wholesale. The response interceptor, which used to deep-clone and write every request and response body, now logs timestamp, id, method, IP, user agent and path.

serializers: {
  req: (req) => ({ id: req.id, method: req.method, path: (req.url ?? '').split('?')[0] }),
  res: (res) => ({ statusCode: res.statusCode }),
},
redact: ['req.headers', 'res.headers'],

This is the safer shape because a field added next year to the request is not logged until someone decides it should be. A blacklist fails open on every future field. A whitelist fails closed. It was also cheaper to run. The commit that made the change is titled as a performance change, and a comment in the logger options says that logging every header cost CPU under load, measured on 12 October 2026. I do not have a throughput figure from that measurement, so I give none.

The AI Note Taker service keeps the mask approach and not the whitelist because its access log is also used to debug mobile clients, where seeing client=app and the other harmless parameters pays off. I still think a whitelist is the better default for a new service.

How I checked, and how to sweep for the class

For Dramma Short the check was blunt. After calling the home route and the refresh route, grep -c Bearer on the server log returned 0, and grep -rc eyJ logs/ returned 0 (eyJ is how a base64 JWT header starts). Before the change the first count was 44,773.

For AI Note Taker the check is a test in the repository. It builds a real Express app with pino-http and the application’s own logger options, sends /api/auth/refresh-token?refreshToken=<jwt>&client=app through supertest, and reads what the logger wrote. The token must be absent and client=app present; the OAuth callback ?code=&state= and ?handoff= get the same treatment. Removing the serializer turns both cases red. I also ran the built server against a real Postgres and a temporary Redis, called the legacy route with two refresh tokens, and found each token 0 times in the output.

To find other writers of URLs, I ran a scan for any logger call that mentions originalUrl, req.url, request.url or .query. It returned no matches outside the specs. Another place that writes request data is the exception filter. It already logs the path and query through the same redaction function.

What I have not verified

These fixes were not committed to a release branch when I wrote the lessons, and I did not re-run the test suites while writing this article. The figures above come from my lesson notes and from reading the specs and code. Dramma Short’s two commits (c2e9f83 and 6e6aee7, 12 October 2026) are in its history. I have not checked whether log files written before the fix, on the machines that ran the old build, were deleted; the notes say old logs were removed for Dramma Short and say nothing for the other service. Redaction by key name and value pattern is a heuristic. A secret under an unfamiliar key and in an unfamiliar format would pass through the mask path, which is why the whitelist remains the stronger choice.

Sources: pino-http README, serializers option (https://github.com/pinojs/pino-http); pino redaction documentation (https://github.com/pinojs/pino/blob/main/docs/redaction.md); OWASP Logging Cheat Sheet, “Data to exclude” (https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html).