Secrets and PII in logs, noise and retention
Passwords, tokens, full card numbers and personal data must not be written to logs without need, because many people read logs and they are kept long. They are protected by masking at the logger or Collector, an internal id instead of an email and an allowlist of fields. QA checks this with a unique marker, such as a password with a random suffix. Noise hides real errors.
Logs · Security
You see a full card number and a user’s password in the logs. Is that already a leak?
A developer added a "temporary" log of a request body to debug login. A week later fifty people read the logs, they are copied into tickets, they land in a store with long retention and in a third-party analytics service. A password in a log is already out of control. So logs are treated as a separate leak surface, not as safe diagnostics. This lesson covers what must not be logged, how it is protected, how QA checks it, and how not to drown in noise and eternal logs.
How it works and why. Logs are read by many people, kept for long and copied into other systems, so whatever lands there leaks. Do not log passwords, tokens (access, refresh, API keys), full card numbers and CVV, full bodies of authentication requests, or personal data (email, phone, address, a document) unless there is a direct need and a legal basis. For cards, PCI DSS applies: security codes are not stored after authorization, and a number, if needed, is shown masked. A similar list is given by the OWASP Logging Cheat Sheet, and CWE-532 describes the weakness "sensitive information in a log file".
The same log
Everything as is
Email, password, a full card with CVV and a token in the clear. Everyone who reads the log sees them.
Masking and an id
An internal id instead of an email, field values replaced, a card with only the last digits.
Example: what is in the log and what should be. A fragment with defects:
INFO login ok user=anna@example.com
INFO POST /auth/login body={"email":"anna@example.com","password":"S3cret!"}
INFO charge card=4111111111111111 cvv=123
DEBUG headers Authorization: Bearer eyJhbGciOi...
The same fragment after the fix:
INFO login ok userId=u_48213
INFO POST /auth/login body={"email":"[redacted]","password":"[redacted]"}
INFO charge card=**** 1111
DEBUG headers Authorization: [redacted]
What is done for this:
- Masking: at the logger level or in the OpenTelemetry Collector. Libraries can "redact" fields by path, for example
redactin pino:
import pino from 'pino';
const logger = pino({
redact: {
// paths to fields that must not reach the log
paths: ['req.headers.authorization', '*.password', '*.cardNumber'],
censor: '[redacted]',
},
});
- Log the user’s internal id, not an email or a name.
- An allowlist of fields for requests and responses instead of "log everything and cut later": a new field does not get into the log by itself.
- DEBUG switched off in production and access control for logs.
The QA role. Check that after login, payment and password reset sensitive values do not appear in logs and error bodies. An automated test does this with a unique marker: a password with a random suffix that must then not be found in the collected server log:
import { execSync } from 'node:child_process';
test('a password and a token do not reach the log', async ({ page }) => {
const password = 'S3cret!-' + Date.now();
await loginAs(page, 'anna@example.com', password);
// collect this run's log, not the file from the CI step
const log = execSync('docker compose logs --no-color api').toString();
expect(log).not.toContain(password);
expect(log).not.toMatch(/Bearer\s+[\w-]+\.[\w-]+\.[\w-]+/);
});
The log is collected inside the test, after the action: a file that CI writes after the tests is still missing or stale here. The unique suffix matters: a plain "password" is found in field names and the test would fail falsely. In CI logs GitHub Actions masks the values of registered secrets, but not values derived from them: a base64 version, a substring or a value inside a JSON-structured secret may go unrecognised. So secrets are not printed even "for debugging".
Noise. If there are hundreds of ERRORs every day and nobody looks at them, a real problem will not be noticed. Signs of noise: expected exceptions at ERROR level (for example "user not found" on a wrong login); the same entry thousands of times a second (a retry loop); healthcheck requests per pod. It is fixed with the level, deduplication or a write rate limit, not by switching logging off.
Retention. Logs are kept for a limited time: because of cost and privacy requirements (the storage limitation principle in GDPR: no longer than needed for the purpose). This is automated with index lifecycle policies (in Elasticsearch ILM: hot, warm, cold, frozen, delete). Audit logs may have a longer period. For QA this means that logs and artifacts of a failed run must be read or saved before they vanish, and also that personal data must not stay in logs longer than needed.
Limits and alternatives. Masking by field name does not catch a secret that landed in arbitrary message text ("failed for token abc123"), so it is complemented with secret scanners in CI and regular log checks. Another risk: user input with a line break can forge an entry in a text log (log injection, CWE-117), so control characters are escaped or a structured format is used. Deletion under the right to erasure is hard if PII is spread across logs, and the best protection is not to write it.
Typical mistakes.
- A "temporary" request-body log that was forgotten.
- A test checks only for the absence of the word "password", not of a specific value.
- Copying a log with a token into a public ticket or chat.
- Leaving DEBUG on in production "for a couple of days".
Check yourself
A test checks that the server log has no word "password" and fails on every run. What is wrong and how do you check correctly?
Answer
The word "password" occurs in field names and messages ("password reset"), which is not a leak. Check a specific value: enter a unique password with a random suffix and make sure this exact value (and tokens by pattern) is not in the log.
Why is logging an internal id better than a user’s email?
Answer
An id does not reveal a person by itself and still lets you link entries, whereas an email is personal data that then leaks through log copying, requires limited retention and is hard to delete. Only someone with access to the users database can tell who it is.