From d1a68f60c100d124414dc9cb9dce3b6a6a4d789a Mon Sep 17 00:00:00 2001 From: SDNick484 Date: Tue, 10 Nov 2026 10:15:17 -0800 Subject: [PATCH 1/2] logsafe: redact() masks e-mail addresses (u***@example.com) 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 / _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 --- src/mcp_shared/logsafe.py | 20 ++++++++-- src/mcp_shared/remote.py | 5 ++- templates/tests/test_logsafe.py | 66 ++++++++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 7 deletions(-) diff --git a/src/mcp_shared/logsafe.py b/src/mcp_shared/logsafe.py index e4ba204..3764225 100644 --- a/src/mcp_shared/logsafe.py +++ b/src/mcp_shared/logsafe.py @@ -12,6 +12,7 @@ 2001:db8::1, fe80::1ff:fe00:1 -> x:x::1 (IPv6: last group kept, likewise) AA:BB:CC:DD:EE:FF -> xx:xx:xx:xx:EE:FF AABBCCDDEEFF, 0009b0f76cfd -> xxxxxxxxEEFF, xxxxxxxx6cfd (bare hex, as in MQTT topics) + user@example.com -> u***@example.com (domain kept: it says which IdP) mqtt://user:secret@host -> mqtt://user:***@host http://host/x?token=abc&key=def -> http://host/x?token=***&key=*** /cb?access_token=abc&code=def -> /cb?access_token=***&code=*** (OAuth callbacks) @@ -26,6 +27,9 @@ Loopback and unspecified addresses (127.x, 0.0.0.0, ::1, ::) are left alone: they say nothing about your network and matter when debugging the simulator. +So are things shaped like an e-mail address that aren't one: URL userinfo +(ssh://git@host, mqtt://user:***@host), scp-style remotes (git@github.com:x), +decorators, and hosts without a letters-only TLD (user@localhost, admin@10.0.0.1). Each server's CLI turns redaction off with --no-redact or its _LOG_UNREDACTED=1, e.g. when you are the only reader. Secrets (JWTs, @@ -61,6 +65,14 @@ # groups out of a longer colon run such as an eight-group IPv6 address. _MAC = re.compile(r"(? None: def mask_email(address: str) -> str: """``user@example.com`` -> ``u***@example.com``: enough to tell identities apart, not enough to paste. - For messages that name an identity on purpose (an Access login that isn't on the - allow-list) rather than for ``redact()``, which leaves e-mail addresses alone: they - are rare in library output, and a server's own messages decide what they say. - Anything without an ``@`` comes back unchanged. + ``redact()`` applies this to every address it finds in a log line; call it directly + for text that isn't logged (an error returned to a client) or that is logged + with address redaction off. Anything without an ``@`` comes back unchanged. """ if not address or not isinstance(address, str) or "@" not in address: return address @@ -266,6 +277,7 @@ def redact(text: str) -> str: text = redact_secrets(text) for pattern, replacement in tuple(_IDENTIFIER_PATTERNS): text = pattern.sub(replacement, text) + text = _EMAIL.sub(lambda m: mask_email(m.group(0)), text) # IPv6 before MACs: an eight-group address of two-digit groups is an address, not a MAC. text = _IPV6.sub(_ipv6, text) text = _IPV4.sub(_ip, text) diff --git a/src/mcp_shared/remote.py b/src/mcp_shared/remote.py index 1976b53..cc050cd 100644 --- a/src/mcp_shared/remote.py +++ b/src/mcp_shared/remote.py @@ -296,8 +296,9 @@ async def verify(self, token: str) -> dict[str, Any]: email = str(claims.get("email", "")).lower() if email not in self.policy.emails: # The identity is somebody Access let through but this server doesn't want; name it - # only enough to tell logins apart. logsafe doesn't redact e-mail addresses (and this - # module can't import it: shared modules stay self-contained), so mask it here. + # only enough to tell logins apart. logsafe's redact() would mask it in a log line, but + # not with redaction off, and this module can't import logsafe's mask_email (shared + # modules stay self-contained), so mask it here the same way. local, _, domain = email.partition("@") shown = f"{local[:1]}***@{domain}" if domain else "this identity" raise AccessDenied(f"{shown} is not in MCP_ALLOWED_EMAILS") diff --git a/templates/tests/test_logsafe.py b/templates/tests/test_logsafe.py index 227723c..5a2b72f 100644 --- a/templates/tests/test_logsafe.py +++ b/templates/tests/test_logsafe.py @@ -199,6 +199,52 @@ def test_mask_email(): assert mask_email("") == "" +@pytest.mark.parametrize( + ("line", "expected"), + [ + ("denied: user@example.com", "denied: u***@example.com"), + ("from=first.last+tag@mail.example.co.uk,", "from=f***@mail.example.co.uk,"), + ("", ""), + ("send to user@example.com.", "send to u***@example.com."), + ("user@example.com: not allowed", "u***@example.com: not allowed"), + ("mailto:user@example.com", "mailto:u***@example.com"), + ('{"email": "user@example.com"}', '{"email": "u***@example.com"}'), + ("MCP_ALLOWED_EMAILS=a@example.com,bob@example.com", "MCP_ALLOWED_EMAILS=a***@example.com,b***@example.com"), + ("https://example.com/users/user@example.com/", "https://example.com/users/u***@example.com/"), + ], +) +def test_email_addresses_are_masked(line, expected): + assert redact(line) == expected + + +@pytest.mark.parametrize( + ("line", "expected"), + [ + # URL userinfo: a password is a secret (_URL_PASSWORD); a bare user name isn't an address. + ("mqtt://user:hunter2@broker.example.com:1883", "mqtt://user:***@broker.example.com:1883"), + ("ssh://git@git.example.com/repo.git", "ssh://git@git.example.com/repo.git"), + ("https://user@example.com/path", "https://user@example.com/path"), + # scp-style git remotes. + ("git@github.com:owner/repo.git", "git@github.com:owner/repo.git"), + # Decorators in a traceback, and a JWT that happens to sit next to an @. + (' @app.route("/x")', ' @app.route("/x")'), + ("@functools.wraps(fn)", "@functools.wraps(fn)"), + ("eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxIn0.c2lnbmF0dXJl@example.com", "[jwt redacted]@example.com"), + ("dGVzdA==@example.com", "dGVzdA==@example.com"), + # Not a domain: no letters-only TLD. + ("admin@192.0.2.5:22", "admin@x.x.x.5:22"), + ("user@localhost", "user@localhost"), + ], +) +def test_email_rule_leaves_look_alikes_alone(line, expected): + assert redact(line) == expected + + +def test_email_addresses_show_when_address_redaction_is_off(): + # An address identifies a person; it isn't a credential, so --no-redact shows it like an IP. + assert redact_secrets("denied: user@example.com") == "denied: user@example.com" + + # --- property-based: the rules must be idempotent and leave no address behind ------------------ # hypothesis is optional for the siblings (it isn't in their dev dependencies): without it only # these tests are skipped, never the module (a module-level importorskip would skip everything). @@ -214,7 +260,7 @@ def test_mask_email(): @needs_hypothesis @_settings - @hypothesis.given(st.text(alphabet=st.sampled_from("0123456789abcdefABCDEF:.-x /?&=@eyJ,;"), max_size=40)) + @hypothesis.given(st.text(alphabet=st.sampled_from("0123456789abcdefABCDEF:.-x /?&=@eyJ,;+_*"), max_size=40)) def test_redact_is_idempotent(text): """Redacting a redacted line changes nothing: a line can safely pass through two handlers.""" once = redact(text) @@ -235,6 +281,24 @@ def test_no_ipv6_survives_redact(ip, around): hypothesis.assume(not (ip.is_loopback or ip.is_unspecified)) assert str(ip) not in redact(f"{around} {ip} {around}") + @needs_hypothesis + @_settings + @hypothesis.given(st.emails(), st.sampled_from([" ", ", ", "<", "(", "=", ": ", '"'])) + def test_no_email_local_part_survives_redact(email, sep): + local, _, domain = email.rpartition("@") + hypothesis.assume(len(local) > 1 and "." in domain and domain.rsplit(".", 1)[1].isalpha()) + hypothesis.assume(all(c.isalnum() or c in "._%+-" for c in local)) + once = redact(f"to{sep}{email} ok") + assert f"{local}@" not in once + assert redact(once) == once + + @needs_hypothesis + @_settings + @hypothesis.given(st.text(alphabet=st.sampled_from("ab.@:/+-_=*xyz.com"), max_size=40)) + def test_email_rule_is_idempotent(text): + once = redact(text) + assert redact(once) == once + def test_identifier_patterns(custom_patterns): register_identifier_pattern(r"device-[0-9a-f]{4}") From cd092c04c05ac3f6c7a18c452aff8ceca4a6fa27 Mon Sep 17 00:00:00 2001 From: SDNick484 Date: Tue, 10 Nov 2026 10:15:25 -0800 Subject: [PATCH 2/2] logsafe: anchor the URL-password rule (quadratic on long tokens); fix 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 (? --- src/mcp_shared/logsafe.py | 4 +++- templates/tests/test_logsafe.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/mcp_shared/logsafe.py b/src/mcp_shared/logsafe.py index 3764225..4ee33f0 100644 --- a/src/mcp_shared/logsafe.py +++ b/src/mcp_shared/logsafe.py @@ -73,7 +73,9 @@ _EMAIL = re.compile( r"(?