Skip to main content

secretmask

Typed, zero-dependency secret masker: redact API keys, tokens, emails and card numbers in strings and nested data before they reach your logs.

Install

pip install secretmask

Usage

Mask a single string:

from secretmask import mask_text

mask_text("Authorization: Bearer abc123def456ghi")
# 'Authorization: Bearer ***'

mask_text("contact jane.doe@example.com, card 4111 1111 1111 1111")
# 'contact ***, card ***'

Mask a whole structure before logging it. The value of any sensitive key is hidden wholesale, string values are pattern-redacted, and lists and nested mappings are walked for you. The input is never mutated:

import logging
from secretmask import mask

payload = {
    "user": "ada",
    "password": "hunter2",
    "headers": {"Authorization": "Bearer eyJ..."},
    "note": "ping me at ada@example.com",
}

logging.info("request %s", mask(payload))
# {'user': 'ada', 'password': '***',
#  'headers': {'Authorization': '***'}, 'note': 'ping me at ***'}

Configure the placeholder, add your own patterns or key names, keep an allowlist, or opt out of the built-ins. Build a SecretMask once and reuse it so the patterns compile a single time:

from secretmask import MaskConfig, SecretMask

config = MaskConfig(
    placeholder="[hidden]",
    extra_keys=("iban",),
    extra_patterns=(("ticket", r"TCK-\d{4}"),),
    allowlist=("support@lianes8studio.com",),  # never mask this one value
    label=True,  # show what was hidden: <redacted:email>
)
masker = SecretMask(config)
masker.mask_text("ref TCK-1234 for a@b.io")
# 'ref <redacted:ticket> for <redacted:email>'

Strategies

Choose how a secret is rendered with Strategy (or the string form):

from secretmask import MaskConfig, Strategy, mask_text

mask_text("Bearer abc123def456ghi", config=MaskConfig(strategy=Strategy.PARTIAL, keep_last=4))
# 'Bearer ***********6ghi'  (keep the tail so two tokens stay distinguishable)

mask_text("mail a@b.io", config=MaskConfig(strategy="hash"))
# 'mail <sha256:0f3306f4>'  (deterministic, correlatable, never readable)
  • full replaces the secret with the placeholder (the default).
  • label names the detector, for example <redacted:jwt>.
  • partial keeps the last keep_last characters and masks the rest.
  • hash replaces the secret with a short deterministic SHA-256 digest.

Masking is idempotent: masking an already-masked value changes nothing.

JSON

from secretmask import mask_json

mask_json('{"password": "p", "user": "ada", "note": "ping a@b.io"}')
# '{"password": "***", "user": "ada", "note": "ping ***"}'

Command line

$ secretmask "Authorization: Bearer abc123def456ghi"
Authorization: Bearer ***

$ echo '{"password": "p", "user": "ada"}' | secretmask --json
{"password": "***", "user": "ada"}

$ secretmask --file app.log --strategy partial --keep-last 2

Run secretmask --help for every option. See GUIDE.md for a full walk-through.

What it detects out of the box

  • JWTs and PEM private-key blocks
  • Bearer <token> headers (the label is kept, the token goes)
  • AWS access key ids, OpenAI, Stripe, GitHub, Slack and Google API keys
  • Passwords inside URLs (scheme://user:pass@host) and connection strings (Password=...;), where only the password is hidden
  • Generic key = value / key: value secret assignments in log lines
  • Email addresses
  • Credit-card numbers, validated with the Luhn checksum so ordinary 16-digit references are left alone
  • The values of sensitive keys (password, token, secret, authorization, api_key, ...), matched case-insensitively and ignoring separators

Why

logging in the standard library will happily write a bearer token or a password straight to disk, and there is no built-in way to scrub it. The maintained options do not quite fill the gap: misprint is typed and dependency-free but only masks the exact strings or regexes you hand it, with no built-in patterns and no recursive key masking; loggingredactor is tied to the logging-filter machinery, matches keys case-sensitively, and ships no patterns for JWTs, cloud keys or card numbers; scrubadub pulls in NLP dependencies for PII detection, which is a different and much heavier job.

secretmask is the small piece in between: one typed, zero-dependency function that both redacts a useful set of secret patterns in free text and recursively masks the values of sensitive keys in nested data, so you can clean a payload in one call before it is logged. It is a plain function, not a framework: no logging handler to install, nothing to configure before it works, and no dependencies to pull in. When you do want control, MaskConfig lets you add patterns and key names or swap the placeholder without giving up the built-ins.

License

MIT. See LICENSE.

Metadata

Release files for secretmask 0.2.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 secretmask 0.2.0
File Size Uploaded Provenance Actions
secretmask-0.2.0.tar.gz 21.2 kB Without provenance Details for secretmask-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for secretmask 0.2.0
File Interpreter ABI Platform Actions
secretmask-0.2.0-py3-none-any.whl Python 3 none any Details for secretmask-0.2.0-py3-none-any.whl

Total release size: 34.4 kB

Release files / secretmask-0.2.0.tar.gz

Download URL secretmask-0.2.0.tar.gz
Size 21.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c845567b08b681ee7e5ef98e27cf5452ab66c1583b0c38cf4b7ce72b64293a22
BLAKE2b-256 checksum
How to use checksums
e6a14856073f7475cf1327066674f0efb20d8a46328a0477eaddf5480286c523
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / secretmask-0.2.0-py3-none-any.whl

Download URL secretmask-0.2.0-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b2e23340025705834fe885c0f3a304573d7e826c9de1ac956447977143fd7f91
BLAKE2b-256 checksum
How to use checksums
413368cea044a49b871de37de3c2d05322dd61a65e02a02dc544c182c7afefc4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for secretmask 0.2.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.2.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