From c8760362bb429f7ab1d5f9c90797930cf6d6cc89 Mon Sep 17 00:00:00 2001 From: Nikhil Pareek Date: Wed, 4 Nov 2026 17:48:22 -0800 Subject: [PATCH 1/2] fix(mcp-use): redact home paths in file URIs Handle URI percent encoding, JSON slash escapes and local file URI forms without changing benign URLs or ordinary tracing. Cover provider streams, stdio tools and OTLP with content enabled and hidden. Co-Authored-By: Claude Code --- .../mcp-use/tests/test_mcp_use_file_uri.py | 156 ++++++++++++++++++ .../mcp-use/traceai_mcp_use/_text.py | 22 +++ 2 files changed, 178 insertions(+) create mode 100644 python/frameworks/mcp-use/tests/test_mcp_use_file_uri.py diff --git a/python/frameworks/mcp-use/tests/test_mcp_use_file_uri.py b/python/frameworks/mcp-use/tests/test_mcp_use_file_uri.py new file mode 100644 index 000000000..39cc086bf --- /dev/null +++ b/python/frameworks/mcp-use/tests/test_mcp_use_file_uri.py @@ -0,0 +1,156 @@ +"""File-URI privacy regressions: synthetic paths, real agent IO and OTLP.""" + +from __future__ import annotations + +import asyncio +import json +from urllib.parse import quote + +import pytest +from _mcp_use_support import ChatFake, answer, mcp_client, tool_call, wire +from fi_instrumentation import TraceConfig +from mcp_use import MCPAgent +from test_mcp_use_contract import Receiver, _attrs, _register +from test_mcp_use_provider import provider_spans + +from traceai_mcp_use import FutureAGICallback +from traceai_mcp_use._text import scrub_local + +HOME = "/data/synthetic-tenant-user" + + +@pytest.mark.parametrize("uri", [ + "file://" + HOME + "/models/local.gguf", + "file:" + HOME + "/models/local.gguf", + "FILE://" + HOME + "/models/local.gguf", + "file://localhost" + HOME + "/models/local.gguf", + "file://LOCALHOST" + HOME + "/models/local.gguf", + "file://" + HOME, + "file://" + HOME + "/", + "file://" + HOME + "/models/local.gguf?mode=read#weights", + "file:///data/%73ynthetic-tenant-user/models/local.gguf", + "file:///data%2Fsynthetic-tenant-user%2fmodels/local.gguf", + "file:///%64ata/synthetic-tenant-user/models/local.gguf", + ("file://" + HOME + "/models/local.gguf").replace("/", r"\/"), + "file:///home/synthetic-other/private.txt", + "file:///Users/synthetic-other/private.txt", + "file:///tmp/synthetic-model.gguf", + "file:///C:/Users/synthetic-other/private.txt", + "file:///C%3A%5CUsers%5Csynthetic-other%5Cprivate.txt", +]) +def test_file_uri_sensitive_paths_are_scrubbed(monkeypatch, uri): + monkeypatch.setenv("HOME", HOME) + assert scrub_local(uri) == "[path]" + assert scrub_local("ECHO-RESULT:" + uri) == "ECHO-RESULT:[path]" + # Keep surrounding text and JSON intact, rather than discarding content. + assert scrub_local("model='" + uri + "', done") == "model='[path]', done" + assert json.loads(scrub_local('{"model":"' + uri + '"}')) == {"model": "[path]"} + + +@pytest.mark.parametrize("home", ["/custom.mount/synthetic user", "/data/synthetic+user", "/data/synthetic-\u00e9"]) +def test_percent_encoded_home_file_uris(monkeypatch, home): + monkeypatch.setenv("HOME", home) + uri = "file://" + quote(home + "/model with spaces.gguf") + assert scrub_local(uri) == "[path]" + + +@pytest.mark.parametrize("text", [ + "https://example.com" + HOME + "/models/local.gguf", + "http://example.com/home/public/model.gguf", + "https://example.com/data/%73ynthetic-tenant-user/model.gguf", + "file:///data/public/model.gguf", + "file:///data/synthetic-tenant-user-other/model.gguf", + "file:///data/synthetic-tenant-user2/model.gguf", + "file:///data/synthetic-tenant-user.bak/model.gguf", + "file:///data/synthetic-tenant-user%2Dother/model.gguf", + "file:///api/v1/items", + "file://files.example/data/public/model.gguf", + "file:relative/model.gguf", + "profile:///data/synthetic-tenant-user/model.gguf", + "https://example.com/file:///data/synthetic-tenant-user/model.gguf", + "ordinary model identifier", +]) +def test_non_sensitive_uri_content_is_preserved(monkeypatch, text): + monkeypatch.setenv("HOME", HOME) + assert scrub_local(text) == text + + +def test_file_uri_uses_current_home_and_root_is_not_a_catch_all(monkeypatch): + uri = "file:///data/synthetic-second/model.gguf" + monkeypatch.setenv("HOME", HOME) + assert scrub_local(uri) == uri + monkeypatch.setenv("HOME", "/data/synthetic-second/") + assert scrub_local(uri) == "[path]" + monkeypatch.setenv("HOME", "/") + assert scrub_local("file:///api/v1/items") == "file:///api/v1/items" + + +@pytest.mark.parametrize("method", ["run", "stream", "stream_events"]) +@pytest.mark.parametrize("hidden", [False, True]) +def test_provider_file_uri_models_are_private(monkeypatch, method, hidden): + monkeypatch.setenv("HOME", HOME) + spans = provider_spans(method, hidden, model="file://" + HOME + "/models/local.gguf") + assert HOME not in wire(spans) + llm, = [s for s in spans if s.attributes.get("gen_ai.span.kind") == "LLM"] + assert llm.name == "chat [path]" + assert llm.attributes["gen_ai.request.model"] == "[path]" + assert llm.attributes["gen_ai.response.model"] == "[path]" + assert llm.attributes["gen_ai.usage.total_tokens"] == 75 + + +@pytest.mark.parametrize("hidden", [False, True]) +def test_file_uri_privacy_reaches_otlp_without_hiding_benign_content(monkeypatch, hidden): + monkeypatch.setenv("HOME", HOME) + monkeypatch.setenv("FI_HIDE_INPUTS", str(hidden).lower()) + monkeypatch.setenv("FI_HIDE_OUTPUTS", str(hidden).lower()) + uri = "file://localhost" + HOME + "/data/private.txt" + control = "https://example.com/public/model.gguf" + with Receiver() as receiver: + provider = _register(receiver, monkeypatch) + handler = FutureAGICallback( + tracer_provider=provider, + config=TraceConfig(hide_inputs=hidden, hide_outputs=hidden), + ) + + async def run(): + client = mcp_client("stdio") + try: + agent = MCPAgent( + llm=ChatFake( + script=[tool_call("echo", {"text": uri + " " + control}), answer("done")], + model_name="file://" + HOME + "/models/local.gguf", + ), + client=client, + callbacks=[handler], + ) + assert await agent.run("hello") == "done" + finally: + await client.close_all_sessions() + + try: + asyncio.run(run()) + assert provider.force_flush(timeout_millis=10_000) + finally: + provider.shutdown() + spans = receiver.spans() + assert receiver.requests() + + assert len(spans) == 4 + assert HOME not in json.dumps(spans) + agent, = [s for s in spans if _attrs(s)["gen_ai.span.kind"] == "AGENT"] + llms = [s for s in spans if _attrs(s)["gen_ai.span.kind"] == "LLM"] + assert len(llms) == 2 + for llm in llms: + assert llm["name"] == "chat [path]" + assert _attrs(llm)["gen_ai.request.model"] == "[path]" + assert int(_attrs(llm)["gen_ai.usage.total_tokens"]) == 18 + assert all(s["parentSpanId"] == agent["spanId"] for s in spans if s is not agent) + tool, = [s for s in spans if _attrs(s)["gen_ai.span.kind"] == "TOOL"] + if hidden: + assert "gen_ai.tool.call.arguments" not in _attrs(tool) + assert "gen_ai.tool.call.result" not in _attrs(tool) + assert _attrs(tool)["output.value"] == "__REDACTED__" + else: + assert "ECHO-RESULT:[path] " + control in _attrs(tool)["gen_ai.tool.call.result"] + assert control in _attrs(tool)["gen_ai.tool.call.arguments"] + assert not handler._runs and not handler._roots diff --git a/python/frameworks/mcp-use/traceai_mcp_use/_text.py b/python/frameworks/mcp-use/traceai_mcp_use/_text.py index 5ffacabad..e49dfa24c 100644 --- a/python/frameworks/mcp-use/traceai_mcp_use/_text.py +++ b/python/frameworks/mcp-use/traceai_mcp_use/_text.py @@ -17,6 +17,7 @@ import re import socket from typing import Iterable, List, Optional, Sequence, Tuple +from urllib.parse import unquote, urlsplit from fi_instrumentation import REDACTED_VALUE from fi_instrumentation.instrumentation.pii_redaction import redact_pii_in_string @@ -70,6 +71,26 @@ def cap(value: str, limit: int) -> str: r"(?|:*?]+" r"|/(?:Users|home|root|private|var|tmp|opt|usr|srv|mnt|app|workspace|etc|Volumes)/[^\s\"'<>|:*?,;)\]\\]+)" ) +# Unlike HTTP(S) paths, file-URI paths refer to a filesystem. Decode only +# these candidates for matching; leave non-sensitive URI text byte-for-byte. +_FILE_URI = re.compile(r"(?|,;)\]]+", re.I) + + +def _scrub_file_uri(match: "re.Match[str]", home: str) -> str: + uri = match.group(0) + try: + # JSON may escape forward slashes; percent escapes belong to the URI. + path = unquote(urlsplit(uri.replace(r"\/", "/")).path) + except ValueError: + return uri + if home.startswith("/") and (path == home or path.startswith(home + "/")): + return PATH_REMOVED + # Windows drive URIs use /C:/... rather than the bare C:\\... form. + if re.match(r"^/[A-Za-z]:[/\\]", path): + path = path[1:].replace("/", "\\") + return PATH_REMOVED if _LOCAL_PATH.match(path) else uri + + # Account names too generic to replace inside ordinary text. _GENERIC_NAMES = frozenset( "root admin administrator user users app ubuntu runner node nobody guest test dev " @@ -132,6 +153,7 @@ def scrub_local(text: str) -> str: # whole paths only, never siblings or paths embedded in URLs/API routes. # An unset or root HOME must not turn this into a catch-all for /api/... home = os.path.expanduser("~").rstrip("/") + text = _FILE_URI.sub(lambda match: _scrub_file_uri(match, home), text) if home.startswith("/"): text = re.sub( r"(? Date: Wed, 4 Nov 2026 18:34:03 -0800 Subject: [PATCH 2/2] fix(mcp-use): scrub URI delimiters within home paths Match the complete known HOME before URI punctuation boundaries, preserving surrounding text and benign URLs. Cover literal and encoded delimiters in provider streaming and actual OTLP exports. Co-Authored-By: Claude Code --- .../tests/test_mcp_use_uri_delimiters.py | 153 ++++++++++++++++++ .../mcp-use/traceai_mcp_use/_text.py | 22 +++ 2 files changed, 175 insertions(+) create mode 100644 python/frameworks/mcp-use/tests/test_mcp_use_uri_delimiters.py diff --git a/python/frameworks/mcp-use/tests/test_mcp_use_uri_delimiters.py b/python/frameworks/mcp-use/tests/test_mcp_use_uri_delimiters.py new file mode 100644 index 000000000..c184ed6b4 --- /dev/null +++ b/python/frameworks/mcp-use/tests/test_mcp_use_uri_delimiters.py @@ -0,0 +1,153 @@ +"""Literal URI sub-delimiters in HOME, through real MCP IO and OTLP.""" + +from __future__ import annotations + +import asyncio +import json +from urllib.parse import quote + +import pytest +from _mcp_use_support import ChatFake, answer, mcp_client, tool_call, wire +from fi_instrumentation import TraceConfig +from mcp_use import MCPAgent +from test_mcp_use_contract import Receiver, _attrs, _register +from test_mcp_use_provider import provider_spans + +from traceai_mcp_use import FutureAGICallback +from traceai_mcp_use._text import Secrets, clean, scrub_local + +HOMES = ["/data/synthetic,tenant-user", "/data/synthetic;tenant-user", "/data/synthetic)tenant-user"] +PUBLIC = "https://example.com/public/model.gguf" + + +def _uri(home, encoded): + path = home + "/models/local.gguf" + return "file://" + (quote(path, safe="/") if encoded else path) + + +@pytest.mark.parametrize("home", HOMES + [ + "/data/synthetic,tenant;user)directory", + "/home/synthetic,tenant-user", + "/data/synthetic-tenant,", +]) +@pytest.mark.parametrize("form", ["literal", "encoded", "mixed", "escaped", "localhost", "short"]) +def test_complete_home_is_scrubbed_before_uri_text_delimiters(monkeypatch, home, form): + monkeypatch.setenv("HOME", home) + uri = _uri(home, form == "encoded") + if form == "mixed": + uri = uri.replace("synthetic", "%73ynthetic").replace("/data/", "/%64ata%2f") + elif form == "escaped": + uri = uri.replace("/", r"\/") + elif form == "localhost": + uri = uri.replace("file://", "FILE://localhost") + elif form == "short": + uri = uri.replace("file://", "file:") + assert scrub_local(uri) == "[path]" + assert clean("chat " + uri, Secrets(), 256) == "chat [path]" + assert scrub_local("model='" + uri + "', done") == "model='[path]', done" + assert json.loads(scrub_local('{"model":"' + uri + '"}')) == {"model": "[path]"} + for separator in [",", ";", ")"]: + # Do not solve the URI bug by swallowing unrelated adjacent content. + assert scrub_local("(" + uri + separator + PUBLIC) == "([path]" + separator + PUBLIC + assert scrub_local(uri + separator + uri) == "[path]" + separator + "[path]" + + +@pytest.mark.parametrize("home", HOMES) +@pytest.mark.parametrize("encoded", [False, True]) +def test_bare_home_and_benign_uri_controls(monkeypatch, home, encoded): + monkeypatch.setenv("HOME", home) + root = "file://" + (quote(home, safe="/") if encoded else home) + assert scrub_local(root) == "[path]" + assert scrub_local("(" + root + "), done") == "([path]), done" + for value in [ + PUBLIC, + "https://example.com" + home + "/model.gguf", + root + "-other/model.gguf", + root + ".bak/model.gguf", + "file:///data/public/model.gguf", + "file:///api/v1/items", + "https://example.com/" + root, + "ordinary benign answer: content remains visible", + ]: + assert scrub_local(value) == value + # Both orders must preserve the benign URI and still scrub the private one. + public_file = "file:///data/public/model.gguf" + for separator in [",", ";", ")"]: + assert scrub_local(root + separator + public_file) == "[path]" + separator + public_file + assert scrub_local(public_file + separator + root) == public_file + separator + "[path]" + + +@pytest.mark.parametrize("home", HOMES) +@pytest.mark.parametrize("encoded", [False, True]) +@pytest.mark.parametrize("hidden", [False, True]) +@pytest.mark.parametrize("method", ["run", "stream", "stream_events"]) +def test_provider_subdelimiter_models_are_private(monkeypatch, home, encoded, hidden, method): + monkeypatch.setenv("HOME", home) + spans = provider_spans(method, hidden=hidden, model=_uri(home, encoded)) + assert home not in wire(spans) + llm, = [span for span in spans if span.attributes.get("gen_ai.span.kind") == "LLM"] + assert llm.name == "chat [path]" + assert llm.attributes["gen_ai.request.model"] == "[path]" + assert llm.attributes["gen_ai.response.model"] == "[path]" + assert llm.attributes["gen_ai.usage.total_tokens"] == 75 + + +@pytest.mark.parametrize("home", HOMES) +@pytest.mark.parametrize("encoded", [False, True]) +@pytest.mark.parametrize("hidden", [False, True]) +def test_subdelimiter_privacy_in_actual_otlp(monkeypatch, home, encoded, hidden): + monkeypatch.setenv("HOME", home) + monkeypatch.setenv("FI_HIDE_INPUTS", str(hidden).lower()) + monkeypatch.setenv("FI_HIDE_OUTPUTS", str(hidden).lower()) + uri = _uri(home, encoded) + with Receiver() as receiver: + provider = _register(receiver, monkeypatch) + handler = FutureAGICallback( + tracer_provider=provider, + config=TraceConfig(hide_inputs=hidden, hide_outputs=hidden), + ) + + async def run(): + client = mcp_client("stdio") + try: + agent = MCPAgent( + llm=ChatFake( + script=[tool_call("echo", {"text": uri + " " + PUBLIC}), answer("done")], + model_name=uri, + ), + client=client, + callbacks=[handler], + ) + assert await agent.run("hello") == "done" + finally: + await client.close_all_sessions() + + try: + asyncio.run(run()) + assert provider.force_flush(timeout_millis=10_000) + finally: + provider.shutdown() + spans = receiver.spans() + assert receiver.requests() + + assert len(spans) == 4 + # Exported protobuf-decoded spans, not an in-memory callback approximation. + print(json.dumps({"home": home, "encoded": encoded, "hidden": hidden, "spans": spans})) + assert home not in json.dumps(spans) + agent, = [span for span in spans if _attrs(span)["gen_ai.span.kind"] == "AGENT"] + llms = [span for span in spans if _attrs(span)["gen_ai.span.kind"] == "LLM"] + assert len(llms) == 2 + assert all(span["parentSpanId"] == agent["spanId"] for span in spans if span is not agent) + for llm in llms: + assert llm["name"] == "chat [path]" + assert _attrs(llm)["gen_ai.request.model"] == "[path]" + assert int(_attrs(llm)["gen_ai.usage.total_tokens"]) == 18 + tool, = [span for span in spans if _attrs(span)["gen_ai.span.kind"] == "TOOL"] + if hidden: + assert "gen_ai.tool.call.arguments" not in _attrs(tool) + assert "gen_ai.tool.call.result" not in _attrs(tool) + assert _attrs(tool)["output.value"] == "__REDACTED__" + else: + assert "ECHO-RESULT:[path] " + PUBLIC in _attrs(tool)["gen_ai.tool.call.result"] + assert PUBLIC in _attrs(tool)["gen_ai.tool.call.arguments"] + assert not handler._runs and not handler._roots diff --git a/python/frameworks/mcp-use/traceai_mcp_use/_text.py b/python/frameworks/mcp-use/traceai_mcp_use/_text.py index e49dfa24c..56284ed4d 100644 --- a/python/frameworks/mcp-use/traceai_mcp_use/_text.py +++ b/python/frameworks/mcp-use/traceai_mcp_use/_text.py @@ -76,6 +76,26 @@ def cap(value: str, limit: int) -> str: _FILE_URI = re.compile(r"(?|,;)\]]+", re.I) +def _home_file_uri(home: str) -> "re.Pattern[str]": + # Match the entire known HOME before treating punctuation as surrounding + # text. Commas, semicolons and ')' are legal URI path characters: stopping + # at one inside HOME hides the prefix from the filesystem-path check. + # Each character may be literal or percent-encoded (including mixed forms). + parts = [] + for char in home: + encoded = "".join("%{:02x}".format(byte) for byte in os.fsencode(char)) + forms = [re.escape(char), encoded] + if char == "/": + forms.append(r"\\/") # JSON-escaped forward slash + parts.append("(?:" + "|".join(forms) + ")") + return re.compile( + r"(?|?#]*)?" + + "".join(parts) + + r"(?=$|[/\s\"'<>|?#,;)\]]|%2f|\\/)[^\s\"'<>|,;)\]]*", + re.I, + ) + + def _scrub_file_uri(match: "re.Match[str]", home: str) -> str: uri = match.group(0) try: @@ -153,6 +173,8 @@ def scrub_local(text: str) -> str: # whole paths only, never siblings or paths embedded in URLs/API routes. # An unset or root HOME must not turn this into a catch-all for /api/... home = os.path.expanduser("~").rstrip("/") + if home.startswith("/") and any(char in home for char in ",;)"): + text = _home_file_uri(home).sub(lambda match: _scrub_file_uri(match, home), text) text = _FILE_URI.sub(lambda match: _scrub_file_uri(match, home), text) if home.startswith("/"): text = re.sub(