Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/mcp_shared/logsafe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
<NAME>_LOG_UNREDACTED=1, e.g. when you are the only reader. Secrets (JWTs,
Expand Down Expand Up @@ -61,7 +65,17 @@
# groups out of a longer colon run such as an eight-group IPv6 address.
_MAC = re.compile(r"(?<![0-9A-Fa-f:])((?:[0-9A-Fa-f]{2}[:-]){4})([0-9A-Fa-f]{2}[:-][0-9A-Fa-f]{2})(?![0-9A-Fa-f:])")
_BARE_MAC = re.compile(r"(?<![0-9A-Za-z-])([0-9A-Fa-f]{8})([0-9A-Fa-f]{4})(?![0-9A-Za-z-])")
_URL_PASSWORD = re.compile(r"(\w+://[^/\s:@?#]+):[^@/\s?#]+@")
# An e-mail address. The local part can't start inside a longer token (the first lookbehind) or
# right after "://", where "user@host" is URL userinfo, not an address. The domain needs a
# letters-only TLD, so "user@localhost" and "admin@10.0.0.1" don't count, and it can't be followed
# by ":" and a non-space, which is an scp-style git remote (git@github.com:owner/repo). Possessive
# quantifiers (++) because a log line can hold a long token: no backtracking into it.
_EMAIL = re.compile(
r"(?<![\w.%+-])(?<!://)([A-Za-z0-9._%+-]++)@((?:[A-Za-z0-9-]++\.)+[A-Za-z]{2,63})(?![A-Za-z0-9-]|\.[A-Za-z0-9]|:\S)"
)
# Anchored at the start of a word: unanchored, a long run of word characters (a hex or base64 blob)
# was rescanned from every offset, quadratic in the length of the line (20 s for 100 KB).
_URL_PASSWORD = re.compile(r"(?<!\w)(\w++://[^/\s:@?#]+):[^@/\s?#]+@")
# The names whose value is a credential wherever they appear: as a query parameter, as a key in a
# JSON object or a Python dict/headers repr, or as a header line. One vocabulary, so a name added
# for one shape (an OAuth callback's ?access_token=) is caught in the others too. `pin` and `psk`
Expand Down Expand Up @@ -147,10 +161,9 @@ def clear_identifier_patterns() -> 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
Expand Down Expand Up @@ -266,6 +279,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)
Expand Down
5 changes: 3 additions & 2 deletions src/mcp_shared/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
79 changes: 78 additions & 1 deletion templates/tests/test_logsafe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import logging
import re
import time

import pytest

Expand Down Expand Up @@ -199,6 +200,61 @@ 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,"),
("<User_1@Example.COM>", "<U***@Example.COM>"),
("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


@pytest.mark.parametrize("blob", ["a" * 100_000, "a" * 100_000 + "@", "0" * 100_000 + "://", "a." * 50_000 + "@"])
def test_a_long_token_is_redacted_in_linear_time(blob):
# Logging runs on the event loop: a rule that backtracks over a long hex/base64 blob stalls the
# server. 100 KB took 20 s with the old unanchored URL-password rule; linear rules take ms.
start = time.perf_counter()
redact(f"payload {blob} end")
assert time.perf_counter() - start < 2.0


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).
Expand All @@ -214,7 +270,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)
Expand All @@ -233,8 +289,29 @@ def test_no_ipv4_survives_redact(pre, ip, post):
@hypothesis.given(st.ip_addresses(v=6), st.text(alphabet="abcxyz ,.;", max_size=8))
def test_no_ipv6_survives_redact(ip, around):
hypothesis.assume(not (ip.is_loopback or ip.is_unspecified))
# A dotted tail of 0.x or 127.x ("::ffff:0.0.0.1") is kept on purpose, as the bare 0.x and
# 127.x addresses are (test_no_ipv4_survives_redact makes the same assumption).
hypothesis.assume(not str(ip).rsplit(":", 1)[1].startswith(("127.", "0.")))
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}")
Expand Down
Loading