Logging sensitive data in plaintext

Logging sensitive data in plaintext

Description

Passwords, tokens, API keys and session IDs written to the console can reach dashboards, log collectors and error reports.

Potential impact

Logs may outlive application data and reach more people when shared during an incident. A valid credential can give a log reader access to the account and its privileges.

Remediation

  • Allow-list the fields in structured logs. Use a shared filter to remove password, token, secret, apiKey, authorization and cookie before output, including direct writes to stdout or stderr.
  • If diagnostics need a value, use a fixed marker or a separate tracking ID. Limit who can read logs and how long copies are kept.

Examples

Implement getTokenId to return an ID that cannot authenticate a request.

Before

javascript
function login(req) {
  const token = req.headers.authorization;
  console.info("login token", token);
}

After

javascript
function login(req) {
  const tokenId = getTokenId(req.headers.authorization);
  console.info("login token accepted", { tokenId });
}

The example logs tokenId. Verify the token before recording that the login was accepted.

References