Clear-text logging of sensitive data

Remove or redact sensitive values such as passwords and tokens from logs.

Description

Production collectors often capture Python logs, stdout and stderr. A password, token, session ID or API key printed to any of them can reach the same destinations.

Potential impact

Log readers may gain access to accounts, sessions or internal configuration. Copies in security monitoring systems (SIEMs), backups and external services can be difficult to remove.

Remediation

Use a shared filter or redaction helper to remove sensitive fields, including Authorization headers, before writing to logs or the console. To correlate events, use a separate tracking ID (or its hash) or a server-side reference.

Examples

Before

python
import logging

def bad_parameterized_token(token):
    logging.info("token=%s", token)

After

python
import logging

def safe_masked_token(token):
    logging.info("token=%s", "[REDACTED]")

logging.info substitutes the argument for %s. The revised call passes the fixed string [REDACTED].

References