Skip to content

logsafe: redact() masks e-mail addresses; anchor the quadratic URL-password rule - #28

Merged
SDNick484 merged 2 commits into
mainfrom
audit/logsafe-email-rule
Nov 10, 2026
Merged

SDNick484 merged 2 commits into
mainfrom
audit/logsafe-email-rule

Conversation

@SDNick484

Copy link
Copy Markdown
Owner

Refs #16 (the open item "e-mail masking as a general redact() identifier rule").

Non-breaking, but it changes default log output in all 9 siblings: an e-mail address that used to appear in full now appears as u***@example.com.

1. E-mail rule (d1a68f6)

mask_email() existed, but nothing applied it outside remote.py's Access denial. redact() now masks every address the way mask_email() does: the first character of the local part, ***, and the domain kept, since the domain says which IdP.

It is an identifier rule (like IPs and MACs), not a secret. With --no-redact / <NAME>_LOG_UNREDACTED=1, addresses show. redact_secrets() is unchanged.

Input Output
denied: user@example.com denied: u***@example.com
MCP_ALLOWED_EMAILS=a@example.com,bob@example.com …=a***@example.com,b***@example.com
mqtt://user:hunter2@broker.example.com mqtt://user:***@broker.example.com (_URL_PASSWORD, unchanged)
ssh://git@git.example.com/x, https://user@example.com/ unchanged (URL userinfo: the rule never starts right after ://)
git@github.com:owner/repo.git unchanged (: + non-space after the domain = scp remote)
@app.route(...), @functools.wraps(fn) unchanged (no local part)
eyJ….…@example.com [jwt redacted]@example.com (the JWT rule runs first)
dGVzdA==@example.com, user@localhost, admin@192.0.2.5:22 unchanged (no local part touching @; no letters-only TLD)
  • Idempotent. The masked local part u*** can't match again.
  • Property tests. They use the template's existing guard (try-import + skipif), not pytest.importorskip. A module-level importorskip would skip the whole file in siblings, which don't install hypothesis. The new tests check that no local part longer than one character survives (st.emails()) and that the rule is idempotent over an @./:-heavy alphabet. The existing idempotence alphabet gains +_*.
  • Linear time. The quantifiers are possessive (Python ≥ 3.11), so a long token can't make the rule backtrack.
  • remote.py's denial comment is updated. It still masks by hand because shared modules can't import each other.

2. Found while testing: _URL_PASSWORD was quadratic (cd092c0)

_URL_PASSWORD started with an unanchored \w+, so on a long word with no :// (a hex dump, a base64 blob) the engine rescanned from every offset. A 100 KB blob took 20 s, and 200 KB took 78 s. Logging is synchronous on the event loop, so one such debug line stalls the server.

The rule now starts only at a word boundary ((?<!\w), possessive scheme). What it matches is unchanged. I timed every logsafe rule over 23 token shapes × 6 tails at 20 KB, and this was the only slow one. New test: four 100 KB blobs must redact in < 2 s. The old rule fails three of them, taking 68 s in total.

The same commit fixes a flaky property test that was already on main. test_no_ipv6_survives_redact failed whenever hypothesis drew ::ffff:0.0.0.1-style addresses, whose 0.x/127.x tail is kept on purpose. The test now makes the same assumption test_no_ipv4_survives_redact already does.

Checks

345 passed; coverage 99%; ruff check / ruff format --check clean, including --no-respect-gitignore tests; mypy clean.

🤖 Generated with Claude Code

SDNick484 and others added 2 commits November 10, 2026 10:15
mask_email() existed, but only remote.py's Access denial used it (by hand),
so an e-mail address anywhere else in a log line survived: a library's error
message, a repr of a config, MCP_ALLOWED_EMAILS logged at debug.

redact() now masks every address it finds the way mask_email() does: the
first character of the local part, then ***, and the domain kept (it tells
which IdP). It is an identifier rule, like IPs and MACs, not a secret: with
--no-redact / <NAME>_LOG_UNREDACTED=1 addresses show.

Left alone, each with a test: URL userinfo (ssh://git@host, https://user@host;
mqtt://user:pass@host is still _URL_PASSWORD's), scp-style git remotes
(git@github.com:owner/repo), decorators in tracebacks, a JWT next to an @ (it
is already [jwt redacted]), base64 padding before an @, and "domains" without
a letters-only TLD (user@localhost, admin@192.0.2.5:22).

redact() stays idempotent: the masked local part "u***" can't match again.
Property tests (skipped where hypothesis isn't installed, as in the siblings):
no local part longer than one character survives, and the rule is idempotent
over an @/./:-heavy alphabet; the existing idempotence test's alphabet gains
+, _ and *. Possessive quantifiers keep the rule linear on long tokens.

Default log output changes for all 9 siblings: an address that used to show
in full now shows as u***@Domain. Nothing else changes; no sibling test
asserts an unmasked address in a redacted log line.

Refs #16

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… a flaky property test

_URL_PASSWORD began with an unanchored \w+, so on a long run of word
characters with no "://" (a hex dump, a base64 blob) the engine rescanned
from every offset: 0.8 s for 20 KB, 20 s for 100 KB, 78 s for 200 KB.
Logging is synchronous on the event loop, so one such debug line stalls the
server. It now starts only at a word boundary (?<!\w) and the scheme is
possessive; what it matches is unchanged (a later start inside the same word
reaches the same "://" or fails the same way). Every other rule was timed on
the same shapes and is linear. New test: four 100 KB blobs redact in < 2 s
(the old rule fails three of them).

test_no_ipv6_survives_redact failed when hypothesis drew an address with a
dotted 0.x/127.x tail ("::ffff:0.0.0.1"): the IPv4 rule keeps those tails on
purpose, as it keeps bare 0.x/127.x. The test now assumes them away, as
test_no_ipv4_survives_redact already does. Pre-existing on main.

Refs #16

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@SDNick484
SDNick484 merged commit 115574a into main Nov 10, 2026
4 checks passed
@SDNick484
SDNick484 deleted the audit/logsafe-email-rule branch November 10, 2026 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant