From b92a8df861cb46fe8b16ca7749c6c91476ce4b12 Mon Sep 17 00:00:00 2001 From: cliffhall Date: Fri, 27 Nov 2026 00:26:57 -0500 Subject: [PATCH 1/2] fix(fetch): GHSA-g7v6-hfx3-8rv6 Co-Authored-By: Claude Opus 5.5 Signed-off-by: cliffhall --- src/fetch/README.md | 8 +++--- src/fetch/src/mcp_server_fetch/server.py | 27 ++++++++++++++++----- src/fetch/tests/test_protocol.py | 31 ++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/fetch/README.md b/src/fetch/README.md index c051833aec..3f6c4a6862 100644 --- a/src/fetch/README.md +++ b/src/fetch/README.md @@ -185,9 +185,11 @@ Optionally, you can add it to a file called `.vscode/mcp.json` in your workspace By default, the server will obey a websites robots.txt file if the request came from the model (via a tool), but not if the request was user initiated (via a prompt). Every redirect hop of a tool fetch is checked against the robots.txt of -the host it leads to, the same way as the requested URL. A path is matched against the rules both as it is spelled and -as an origin typically resolves it (`%2F` and `%2E` decoded, repeated slashes merged, `.` and `..` segments removed), -and the fetch is refused if either is disallowed, so `//private` or `/x/../private` cannot get past `Disallow: /private`. +the host it leads to, the same way as the requested URL. A path is matched against the rules as it is spelled, as an +origin typically resolves it (`%2F` and `%2E` decoded, repeated slashes merged, `.` and `..` segments removed), and as +an origin with Windows or servlet path semantics resolves it (the same, with a backslash or `%5C` also read as a +separator and a `;` parameter dropped from each segment). The fetch is refused if any of these is disallowed, so +`//private`, `/x/../private`, `/x%5C..%5Cprivate` or `/x/..;/private` cannot get past `Disallow: /private`. Matching stays case-sensitive, as RFC 9309 specifies. If the robots.txt request fails to connect, or is answered with 401, 403 or a server error (5xx), the tool refuses the fetch rather than reading the response as rules (RFC 9309 treats a 5xx robots.txt as complete disallow); any other 4xx is treated as no rules. This can be disabled by adding the diff --git a/src/fetch/src/mcp_server_fetch/server.py b/src/fetch/src/mcp_server_fetch/server.py index 696459e620..3e22d41b42 100644 --- a/src/fetch/src/mcp_server_fetch/server.py +++ b/src/fetch/src/mcp_server_fetch/server.py @@ -721,6 +721,9 @@ def handle_starttag( _ENCODED_SLASH = re.compile("%2f", re.IGNORECASE) _ENCODED_DOT = re.compile("%2e", re.IGNORECASE) _REPEATED_SLASHES = re.compile("/{2,}") +# A backslash, literal or escaped, which some origins (IIS, Windows-backed +# servers) read as a path separator. +_BACKSLASH = re.compile(r"\\|%5c", re.IGNORECASE) def _remove_dot_segments(path: str) -> str: @@ -742,7 +745,7 @@ def _remove_dot_segments(path: str) -> str: return "/" + "/".join(resolved) + trailing -def _resolved_path_url(url: str) -> str: +def _resolved_path_url(url: str, *, windows_and_servlet: bool = False) -> str: """``url`` with its path spelled the way a typical origin resolves it. robots.txt rules match the path a crawler requests, but an origin such as @@ -751,9 +754,19 @@ def _resolved_path_url(url: str) -> str: repeated slashes merged and dot segments removed before rules are matched against it, as well as against the path as spelled. The query is kept as it is, and matching stays case-sensitive (RFC 9309 2.2.2). + + With ``windows_and_servlet``, the path is also resolved the way origins + with other path semantics resolve it: a backslash (literal or ``%5C``) is + a separator, as on IIS, and a ``;`` path parameter is dropped from each + segment, as in a Java servlet container. That spelling is matched in + addition to the others rather than in place of one, so it only ever adds + refusals. """ parts = urlsplit(url) path = _ENCODED_DOT.sub(".", _ENCODED_SLASH.sub("/", parts.path)) + if windows_and_servlet: + path = _BACKSLASH.sub("/", path) + path = "/".join(segment.split(";", 1)[0] for segment in path.split("/")) path = _remove_dot_segments(_REPEATED_SLASHES.sub("/", path)) return urlunsplit(parts._replace(path=path)) @@ -804,12 +817,14 @@ async def check_may_autonomously_fetch_url( line for line in robot_txt.splitlines() if not line.strip().startswith("#") ) robot_parser = Protego.parse(processed_robot_txt) - # Refused when either spelling of the path is disallowed: resolving the + # Refused when any spelling of the path is disallowed: resolving the # path only ever adds refusals. - if not ( - robot_parser.can_fetch(url, user_agent) - and robot_parser.can_fetch(_resolved_path_url(url), user_agent) - ): + spellings = ( + url, + _resolved_path_url(url), + _resolved_path_url(url, windows_and_servlet=True), + ) + if not all(robot_parser.can_fetch(spelling, user_agent) for spelling in spellings): raise ToolError( f"The sites robots.txt ({robot_txt_url}), specifies that autonomous fetching of this page is not allowed, " f"{user_agent}\n" diff --git a/src/fetch/tests/test_protocol.py b/src/fetch/tests/test_protocol.py index 5529f0d047..8c11dacefe 100644 --- a/src/fetch/tests/test_protocol.py +++ b/src/fetch/tests/test_protocol.py @@ -475,6 +475,14 @@ async def test_ignore_robots_txt_skips_the_check(web: FakeWeb) -> None: "/%2e/private", "/x/%2E%2E/private", "//private//report", + # An origin that reads a backslash as a separator (IIS) serves + # /private for these. + "/public%5c..%5cprivate", + "/public%5C..%5Cprivate", + # A servlet container drops ;parameters from each segment first. + "/public/..;/private", + "/public;x/..;y/private", + "/;x/private", ], ) async def test_robots_disallow_matches_the_resolved_path( @@ -503,6 +511,11 @@ async def test_robots_disallow_matches_the_resolved_path( "/x/..%2Fprivate", "/x/%2e%2e//private", "/x//%2e%2e/private", + # A redirect carries a literal backslash as it is, where a tool + # argument would have it read as a separator first. + "/public\\..\\private", + "/public%5c..%5cprivate", + "/public/..;/private", ], ) async def test_redirect_to_a_disallowed_path_spelled_otherwise_is_refused( @@ -528,6 +541,8 @@ async def test_redirect_to_a_disallowed_path_spelled_otherwise_is_refused( "/pri%20vate", # Matching stays case-sensitive (RFC 9309 2.2.2). "//Private", + "/public%5cprivate", + "/public;private", ], ) async def test_robots_allows_a_path_that_resolves_outside_the_rule( @@ -543,6 +558,22 @@ async def test_robots_allows_a_path_that_resolves_outside_the_rule( assert web.urls() == [ROBOTS, url] +async def test_robots_disallow_still_matches_the_dot_resolved_spelling( + web: FakeWeb, +) -> None: + # Dropping ;parameters is matched in addition to the dot-resolved + # spelling, so a rule naming a parameter still refuses a path that only + # resolves to it once its dot segments are removed. + url = "https://example.com/x/%2e%2e/a;b" + web.add(ROBOTS, plain("User-agent: *\nDisallow: /a;b")) + web.add(url, plain("private-page")) + async with connect() as (session, _): + result = await call(session, {"url": url}) + assert wire(result)["isError"] is True + assert "private-page" not in text_of(result) + assert web.urls() == [ROBOTS] + + async def test_robots_disallow_still_matches_the_literal_spelling( web: FakeWeb, ) -> None: From e6131710e7d4d7cc3944035e57fcb6b57e4023d2 Mon Sep 17 00:00:00 2001 From: cliffhall Date: Fri, 27 Nov 2026 00:40:33 -0500 Subject: [PATCH 2/2] fix(fetch): GHSA-g7v6-hfx3-8rv6 Co-Authored-By: Claude Opus 5.5 Signed-off-by: cliffhall --- src/fetch/README.md | 7 ++++--- src/fetch/src/mcp_server_fetch/server.py | 23 ++++++++++++++--------- src/fetch/tests/test_protocol.py | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/fetch/README.md b/src/fetch/README.md index 3f6c4a6862..545e04ca52 100644 --- a/src/fetch/README.md +++ b/src/fetch/README.md @@ -186,9 +186,10 @@ Optionally, you can add it to a file called `.vscode/mcp.json` in your workspace By default, the server will obey a websites robots.txt file if the request came from the model (via a tool), but not if the request was user initiated (via a prompt). Every redirect hop of a tool fetch is checked against the robots.txt of the host it leads to, the same way as the requested URL. A path is matched against the rules as it is spelled, as an -origin typically resolves it (`%2F` and `%2E` decoded, repeated slashes merged, `.` and `..` segments removed), and as -an origin with Windows or servlet path semantics resolves it (the same, with a backslash or `%5C` also read as a -separator and a `;` parameter dropped from each segment). The fetch is refused if any of these is disallowed, so +origin typically resolves it (`%2F` and `%2E` decoded, repeated slashes merged, `.` and `..` segments removed), as an +origin with Windows path semantics resolves it (the same, with a backslash or `%5C` also read as a separator), and as a +servlet container resolves it (the same, with a `;` parameter dropped from each segment). The fetch is refused if any +of these is disallowed, so `//private`, `/x/../private`, `/x%5C..%5Cprivate` or `/x/..;/private` cannot get past `Disallow: /private`. Matching stays case-sensitive, as RFC 9309 specifies. If the robots.txt request fails to connect, or is answered with 401, 403 or a server error (5xx), the tool refuses the fetch rather than reading the response as rules (RFC 9309 diff --git a/src/fetch/src/mcp_server_fetch/server.py b/src/fetch/src/mcp_server_fetch/server.py index 3e22d41b42..54c269a6b1 100644 --- a/src/fetch/src/mcp_server_fetch/server.py +++ b/src/fetch/src/mcp_server_fetch/server.py @@ -745,7 +745,9 @@ def _remove_dot_segments(path: str) -> str: return "/" + "/".join(resolved) + trailing -def _resolved_path_url(url: str, *, windows_and_servlet: bool = False) -> str: +def _resolved_path_url( + url: str, *, backslash_separator: bool = False, drop_parameters: bool = False +) -> str: """``url`` with its path spelled the way a typical origin resolves it. robots.txt rules match the path a crawler requests, but an origin such as @@ -755,17 +757,19 @@ def _resolved_path_url(url: str, *, windows_and_servlet: bool = False) -> str: against it, as well as against the path as spelled. The query is kept as it is, and matching stays case-sensitive (RFC 9309 2.2.2). - With ``windows_and_servlet``, the path is also resolved the way origins - with other path semantics resolve it: a backslash (literal or ``%5C``) is - a separator, as on IIS, and a ``;`` path parameter is dropped from each - segment, as in a Java servlet container. That spelling is matched in - addition to the others rather than in place of one, so it only ever adds - refusals. + Origins with other path semantics are covered by further spellings, each + matched in addition to the others rather than in place of one, so each + only ever adds refusals: with ``backslash_separator`` a backslash (literal + or ``%5C``) is a separator, as on IIS; with ``drop_parameters`` a ``;`` + path parameter is dropped from each segment, as in a Java servlet + container. They are separate spellings because no origin is assumed to + do both. """ parts = urlsplit(url) path = _ENCODED_DOT.sub(".", _ENCODED_SLASH.sub("/", parts.path)) - if windows_and_servlet: + if backslash_separator: path = _BACKSLASH.sub("/", path) + if drop_parameters: path = "/".join(segment.split(";", 1)[0] for segment in path.split("/")) path = _remove_dot_segments(_REPEATED_SLASHES.sub("/", path)) return urlunsplit(parts._replace(path=path)) @@ -822,7 +826,8 @@ async def check_may_autonomously_fetch_url( spellings = ( url, _resolved_path_url(url), - _resolved_path_url(url, windows_and_servlet=True), + _resolved_path_url(url, backslash_separator=True), + _resolved_path_url(url, drop_parameters=True), ) if not all(robot_parser.can_fetch(spelling, user_agent) for spelling in spellings): raise ToolError( diff --git a/src/fetch/tests/test_protocol.py b/src/fetch/tests/test_protocol.py index 8c11dacefe..e618b6d5b1 100644 --- a/src/fetch/tests/test_protocol.py +++ b/src/fetch/tests/test_protocol.py @@ -574,6 +574,28 @@ async def test_robots_disallow_still_matches_the_dot_resolved_spelling( assert web.urls() == [ROBOTS] +@pytest.mark.parametrize( + ("rule", "path"), + [ + # An origin that reads a backslash as a separator keeps ;parameters, + # and one that drops ;parameters keeps backslashes. + ("/a;b", "/x%5c..%5ca;b"), + ("/a%5Cb", "/x/..;/a%5Cb"), + ], +) +async def test_robots_disallow_matches_each_resolution_on_its_own( + web: FakeWeb, rule: str, path: str +) -> None: + url = f"https://example.com{path}" + web.add(ROBOTS, plain(f"User-agent: *\nDisallow: {rule}")) + web.add(url, plain("private-page")) + async with connect() as (session, _): + result = await call(session, {"url": url}) + assert wire(result)["isError"] is True + assert "private-page" not in text_of(result) + assert web.urls() == [ROBOTS] + + async def test_robots_disallow_still_matches_the_literal_spelling( web: FakeWeb, ) -> None: