Learning center

When a reviewer says a log line leaks a token, read the value

Decide by what the logged value contains at runtime and who reads that log. If it can carry a credential or personal data, change the line and add a test that asserts the secret is absent from captured logs; if it holds only identifiers, status codes or an error body you have checked, dismiss with what you checked.

Find what the value contains#

A reviewer sees a variable name and guesses its contents. The guess is often wrong in both directions. A variable called token may hold a token count, and a harmless-looking exception may carry a full request URL. Before you answer, find out what the logged expression turns into as a string.

Exceptions are the usual surprise. In aiohttp, a ClientResponseError prints the request URL, including its query string. Running this against aiohttp 3.14.5 with a made-up key:

from aiohttp import ClientResponseError, RequestInfo
from multidict import CIMultiDict, CIMultiDictProxy
from yarl import URL

url = URL("https://monitoringapi.example/site/1/overview.json?api_key=FAKEKEY123")
ri = RequestInfo(url, "GET", CIMultiDictProxy(CIMultiDict()), url)
print(ClientResponseError(ri, (), status=503, message="Service Unavailable"))

prints 503, message='Service Unavailable', url='https://monitoringapi.example/site/1/overview.json?api_key=FAKEKEY123'. Any API that takes its key as a query parameter leaks it through that string.

Then ask where the log goes. OWASP's list of data that "should usually not be recorded directly in the logs" includes "Access tokens", "Authentication passwords", "Session identification values" and "Sensitive personal data and some forms of personally identifiable information (PII)". OWASP Logging Cheat Sheet For a self-hosted application, the log's readers include everyone the user pastes it to in a bug report.

A key that was in the exception text#

That aiohttp behavior was a real Home Assistant bug. An issue reported "SolarEdge: API key is written to the log in plain text when an API request fails". Issue #188395 The fix explained that on a failed request "the raised aiohttp.ClientResponseError propagated to DataUpdateCoordinator and was logged verbatim." It caught the error in each Monitoring API data service and raised an UpdateFailed carrying only the service name and status code, with from None. Pull request Python's raise ... from None suppresses the chained context, so the traceback no longer prints the original exception and its URL.

Copilot reviewed the change on 25 October 2026 and flagged a gap in the tests: "The security regression test covers only one of six independent error-sanitization handlers." Its open item read "Cover all Monitoring API sanitization handlers in regression tests." Review

The author answered with commit 9ac05c1, which extended the secret-leak test to every API method the fix covered. The merged test is parametrized over all six API methods; each raises a ClientResponseError whose URL contains the key, and each asserts:

assert API_KEY not in caplog.text
assert "api_key" not in caplog.text

Merged diff The pull request merged on 27 October 2026. The reviewer's comment was right, and the right response was a test. Six handlers written the same way can each be broken separately, and a test that covers one of them proves nothing about the other five.

A leak that depends on a hypothetical#

Eight months earlier, Copilot reviewed a Cloudflare DDNS pull request in the same repository. The code logged a failed record creation with a _LOGGER.warning call whose arguments included resp.status and await resp.text(). Copilot wrote: "The API token is included in the Authorization header and the response text is logged in error messages." It went on: "If the Cloudflare API includes sensitive information in error responses (such as echoing back parts of the request)," "this could expose the API token in logs." Review comment

The pull request was closed without merging on 25 February 2026, so this thread has no resolution to follow. It still shows how to read the claim. The token travels in a request header; the log records the response body. The leak exists only if the API echoes request headers into error bodies, which is a claim about Cloudflare's API, not about this code. Settle it by reading the API's documented error format or capturing a real failed response in a test environment. This guide did not check Cloudflare's error format, so it does not say whether the claim was right. If the body turns out to hold only error codes and messages, dismiss and cite the format. If you cannot find out, logging the status and the API's error codes costs little.

Decide from what the value holds#

What the log line recordsEvidence to checkResponse
A credential, session ID or passwordThe value at runtime, including how its type printsChange: drop or mask it, and add a test on captured logs
An exception or object whose string form you have not checkedIts __str__ or toString output, with a fake secretTest first, then change if the secret appears
One of several handlers already fixedWhether the test covers each handlerAdd the missing test cases
A response body from an external APIThe API's documented error format, or a captured responseDismiss with the format, or log codes only
Personal data such as names or addressesWho can read the log, and for how longChange, or mask to what debugging needs
Identifiers, counts or status codesThe values are not secretsDismiss, naming what the value is

A test on captured logs is cheap and keeps the answer after the next refactor. When a reviewer returns to the same line on a later push, the follow-through guide covers how to count the repeat.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading