Install
consolepip install redactscope
Use
pythonfrom redactscope import SecretLeak, assert_clean, audit, redact
clean = redact(tool_result) # a copy, never the original
findings = audit(tool_result) # [Finding(path="metadata.api_key", kind="secret-key", …)]
assert_clean(tool_result) # raises SecretLeak, for a test or a gate
Wrapping a tool so nothing raw ever reaches the context:
pythondef safe_tool(function):
def wrapper(*args, **kwargs):
result = function(*args, **kwargs)
findings = audit(result)
if findings:
log.warning("redacted %s", [str(finding) for finding in findings])
return redact(result)
return wrapper
console$ redactscope message.json > clean.json # redacted JSON on stdout
$ redactscope trace.log # plain text works too
$ redactscope message.json --check # exit 1 if anything was found
$ redactscope message.json --keys-only # mask keys, leave other strings alone
What is caught
| kind | example |
|---|---|
secret-key | any key whose name contains password, token, secret, api_key, authorization, credential, session, cookie, signature, … |
openai-key | sk-… (8+ characters after the prefix) |
anthropic-key | sk-ant-… |
github-token | ghp_, gho_, ghu_, ghs_, ghr_, github_pat_ |
slack-token | xoxb-, xoxp-, xoxa-, … |
aws-access-key | AKIA… / ASIA… + 16 characters |
google-api-key | AIza… + 35 characters |
jwt | three dot-separated base64url segments starting eyJ |
bearer / basic-auth | Bearer …, Basic … |
url-credentials | postgres://user:password@host |
private-key-block | -----BEGIN … PRIVATE KEY----- … -----END … |
audit reports a path for each finding — messages[0].content, metadata.api_key — and a preview that never contains the value itself.
Rules
redactreturns a copy. Dict keys are kept, values are replaced with***.- A string that is not a secret is returned unchanged; matching is on shapes that are
unlikely in prose (sk- needs 8 more characters, AKIA needs 16).
- Pass
markers=(...)to mask your own key names,patterns=(...)to narrow or extend
the value patterns, mask="[redacted]" to change the replacement.
- Tuples become lists, because JSON has no tuples and a message usually ends up as JSON.
--keys-onlyandpatterns=()are the same decision: trust the key names and leave
the strings alone.
Limits
- This is a safety net, not a guarantee. A secret with no recognisable shape, in prose,
under an innocent key will pass — nothing pattern-based can fix that.
- Patterns are chosen to avoid false positives, so a short custom token
(token=abc123) is caught by its key, not by its value.
- No entropy analysis and no semantic detection: that is a different product with a
different false-positive budget.
- Redaction is not a substitute for not collecting the secret in the first place.
Development
consolepython -m venv .venv
.venv/bin/pip install -e ".[test]"
.venv/bin/pytest
The suite covers every line of the package (pytest --cov --cov-branch), and the examples in the docstrings run as tests through --doctest-modules.
License
MIT.
Other libraries here
One problem each, standard library only, no runtime dependencies.
- argrepair — turn a bad tool call into a message the model can act on
- backoff-headers — turn a rate-limit header into the exact time to wait
- columncheck — find the CSV defect before the pipeline does
- dir-digest — one hash for a directory tree, identical on every OS
- docanchor — catch every broken heading anchor in your docs
- envfile-diff — diff two .env files without printing the secrets
- fsync-replace — replace a file so a crash cannot leave it half-written
- header-date — HTTP dates parsed strictly, formatted correctly
- junit-merge — merge JUnit XML from every job into one honest report
- npm-semver — npm semver ranges, parsed the way npm parses them
- outputcap — keep a huge tool result out of the context window
- parse-size — byte sizes in and out, MB and MiB never confused
- path-containment — keep untrusted paths inside their directory
- runbudget — a per-run cost cap that warns before it stops
- testflakes — rank your flaky tests from the history you already have
- tool-loop-guard — catch an agent repeating itself while it is still cheap
- toolcall-ledger — a hash-chained record of every tool call
- toolcircuit — stop retrying a tool that is down
- toolquota — client-side rate limits your agent loop can wait on
- wheeldeps — read a wheel's dependencies offline, markers included
- zip-deterministic — byte-identical archives, build after build