Skip to main content

log-leak

Fail your test run when personal data or secrets show up in your logs.

This line passes code review and every test, then sends your users' email addresses to Datadog for the next two years:

log.info(f"created user {user.email}")

log-leak is a pytest plugin. While your tests run, it reads every log record your code writes and checks it for emails, phone numbers, NIC numbers, card numbers, passwords, tokens and API keys. Your tests already go through the code paths that log, so they find the leaks for you:

$ pytest
........................................                                 [100%]
=================================== log-leak ===================================
app/users.py:6  LL009 email  An email address was logged.
    in create_user(), logger 'app.users' at INFO
    message: created user <email>
    first seen in: tests/test_users.py::test_create
    also in 3 more tests
    fix: Log an internal user ID instead.
app/auth.py:31  LL006 credential  A value under a key like password, secret or token was logged.
    in login(), logger 'app.auth' at WARNING
    message: login failed for {'username': 'amal', 'password': '<credential>'}
    template: login failed for %s
    first seen in: tests/test_auth.py::test_wrong_password
    fix: Log only the fields you need, or mask sensitive keys before logging.
log-leak: found 2 leaks at 2 call sites, logged by 5 tests.
40 passed in 1.31s

The report shows where the log call is, never the leaked value itself, so the CI log doesn't become a second leak. Fake test data counts as a leak: if a test logs amal@example.com, the same line logs real addresses in production.

Install

pip install pytest-log-leak     # or: uv add --dev pytest-log-leak

That's it. pytest loads the plugin on its own, so the next pytest run is checked.

What it finds

ID Name Finds
LL001 private-key PEM private key blocks
LL002 jwt JSON Web Tokens
LL003 api-key AWS, GitHub, Stripe, Slack, OpenAI, Anthropic, Google and SendGrid keys
LL004 url-credentials Passwords inside URLs, e.g. postgresql://app:secret@db/prod
LL005 auth-header Bearer and Basic Authorization header values
LL006 credential Values under keys like password, secret, token, api_key
LL007 card-number Card numbers (checked against card prefixes and the Luhn checksum)
LL008 nic Sri Lankan NIC numbers, old and new format
LL009 email Email addresses
LL010 phone Sri Lankan mobile numbers, and any number written as +<country code>...

It checks the log message, the messages of any exception logged with log.exception(...), and fields passed with extra={...}.

When a leak is fine

Some logged values are meant to be public, like a support address. Silence one call with a comment on its first line, or alone on the line above:

log.info("contact %s", SUPPORT_EMAIL)  # log-leak: ignore[LL009]

# log-leak: ignore -- every detector, for this call only
log.info(
    "sent receipt to %s",
    order.email,
)

pytest --log-leak-explain LL009 prints why a detector exists and how to fix what it finds. --log-leak-explain all lists them.

Adopting it in an existing project

A big project may have dozens of leaks on day one. Record them in a baseline file and fix them over time:

pytest --log-leak-update-baseline     # writes .log-leak-baseline.json; commit it
pytest                                # now only new leaks fail the run

Baseline entries are keyed by file, function, logger and detector, not line number, so editing code above a log call doesn't make an old leak look new. When an entry stops showing up, log-leak tells you, and the next --log-leak-update-baseline after a full run drops it.

Configure

In pytest.ini, or [tool.pytest.ini_options] in pyproject.toml:

[pytest]
# Lowest level checked. Production usually logs INFO.
log_leak_level = INFO
# false: report leaks without failing the run.
log_leak_fail = true
# Detectors to skip everywhere.
log_leak_ignore = LL010
# Loggers to skip, with their children.
log_leak_ignore_loggers = sqlalchemy.engine
# Where the baseline lives.
log_leak_baseline = .log-leak-baseline.json

Turn it off for one run with pytest -p no:log_leak.

GitHub Actions

Nothing to set up. When GITHUB_ACTIONS=true, log-leak also prints each leak as an annotation, so it shows up on the pull request diff at the logging line.

How accurate is it

A checker that fails your build has to be almost never wrong, or people turn it off. The detectors are tuned against Loghub: 32,000 real log lines from 16 systems, including Hadoop, Spark, OpenSSH, Android, macOS and Windows.

The first run gave 79 hits. 67 were false positives: HDFS block IDs that pass the card checksum, times followed by process IDs that looked like phone numbers, compact timestamps that looked like NIC numbers, and object descriptions like token=Token{a64f992}. Each is fixed and kept as a regression test. The other 12 hits are real email addresses: two personal ones in the macOS logs (one inside an iCloud URL, logged 10 times) and Red Hat's build address in a Linux kernel banner.

Run it yourself with uv run python bench/loghub.py. Loghub contains almost no secrets, so it measures false positives, not how many real leaks are caught. That side is covered by the test suite.

Limits

  • pytest-xdist is not supported yet. With -n, log-leak prints a warning and checks nothing.
  • It sees records that reach the root logger. A logger with propagate = False is not checked.
  • It finds what your tests exercise. Code paths with no test are not checked.
  • caplog.set_level(...) in a test raises the level for that test, so lower-level records in it aren't created and can't be checked.

License

MIT

Metadata

Release files for pytest-log-leak 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-log-leak 0.1.0
File Size Uploaded Provenance Actions
pytest_log_leak-0.1.0.tar.gz 29.5 kB Without provenance Details for pytest_log_leak-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-log-leak 0.1.0
File Interpreter ABI Platform Actions
pytest_log_leak-0.1.0-py3-none-any.whl Python 3 none any Details for pytest_log_leak-0.1.0-py3-none-any.whl

Total release size: 46.6 kB

Release files / pytest_log_leak-0.1.0.tar.gz

Download URL pytest_log_leak-0.1.0.tar.gz
Size 29.5 kB
Tags Source
SHA-256 checksum
How to use checksums
919044ff16ac934256d0507efa01d2f2f027dd50e46c8f107ad70645b94af3e3
BLAKE2b-256 checksum
How to use checksums
4ed1e81dd305c2855a8f1a240aeb767dc6bf5fc4e7da624924da7a6eb2ed7601
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pytest_log_leak-0.1.0-py3-none-any.whl

Download URL pytest_log_leak-0.1.0-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
32ed8bc7efca5bb7aafda14e088fbfc625b6f0013d4e8d8dcfeaeebb8267fe1e
BLAKE2b-256 checksum
How to use checksums
57aab0c7d0789bfeb09d07276a2a9d57d07bc0da1d9e02e2686af15cbe742bd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for pytest-log-leak 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page