From b5d2748ec2f1c03322b70a45150f066fc324cd0b Mon Sep 17 00:00:00 2001 From: LeonxLJX <51880185+LeonxLJX@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:37:04 +0800 Subject: [PATCH] fix(shared): correct ifemp match when following literal begins with '=' UriTemplate.match() misparsed a ';' (ifemp) capture whose non-empty value collided with the following literal that begins with '='. In _scan_prefix, uri.startswith(nxt.text, pos) was checked before uri[pos] == '=', so a value equal to the stem of that literal (e.g. {;a}=end{+rest} with a="end" -> ";a=end=end/x") was read as empty and the duplicate "=end" leaked into the greedy capture. Prefer the value form: when a '=' follows the name, locate the following literal after a value within the operator's stop-char window; only fall back to the empty-value (startswith) check when no boundary is found. The suffix / no-greedy path was already correct and is unchanged. Adds test_match_prefix_ifemp_value_collides_with_eq_literal covering the collision plus boundary cases: empty value before an '=' literal, a non-colliding value, name-continuation rejection, and the no-greedy round-trip. Closes #3852 --- src/mcp/shared/uri_template.py | 34 ++++++++++++++++++------------ tests/shared/test_uri_template.py | 35 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/mcp/shared/uri_template.py b/src/mcp/shared/uri_template.py index 20d6fa9c2e..9ef006f8c5 100644 --- a/src/mcp/shared/uri_template.py +++ b/src/mcp/shared/uri_template.py @@ -1079,22 +1079,30 @@ def _scan_prefix( assert isinstance(nxt, _Lit) if atom.ifemp: - # RFC §3.2.7 ifemp: ;name=val for non-empty, bare ;name for - # empty. Decide which form is present without falling through - # to the stop-char scan when the value is empty. + # RFC §3.2.7 ifemp: ;name=val for a non-empty value, bare + # ;name for an empty one. The '=' that opens the value form is + # also the first character of a following literal that begins + # with '=', so the two forms collide when the value's text + # starts with that literal (e.g. {;a}=end with a="end" expands + # to ;a=end=end). Prefer the value form: locate the following + # literal after the '='; only treat the value as empty when it + # cannot be found there. + if pos < limit and uri[pos] == "=": + value_start = pos + 1 + latest = value_start + while latest < limit and uri[latest] not in stops: + latest += 1 + end = uri.find(nxt.text, value_start, latest + len(nxt.text)) + if end != -1: + result[var.name] = unquote(uri[value_start:end]) + pos = end + continue + # No '=', or the '=' belongs to the following literal: the + # value is empty iff the following literal starts here. if uri.startswith(nxt.text, pos): - # Following literal begins immediately: value is empty. - # Checked before '=' so a literal that itself starts - # with '=' is not mistaken for the ifemp separator. result[var.name] = "" continue - if pos < limit and uri[pos] == "=": - pos += 1 # value follows; fall through to the scan - else: - # The following literal does not start here and there is - # no '=': the URI's name continued past the template's - # (e.g. ;keys vs ;key) — no parse. - return None + return None # Latest valid end: the var stops at the first stop-char or # the scan limit, whichever comes first. diff --git a/tests/shared/test_uri_template.py b/tests/shared/test_uri_template.py index cddbb82b0b..8715d02ef4 100644 --- a/tests/shared/test_uri_template.py +++ b/tests/shared/test_uri_template.py @@ -747,6 +747,41 @@ def test_match_prefix_ifemp_empty_before_non_stop_literal(): assert t.match("api;keyZX/tail") is None +def test_match_prefix_ifemp_value_collides_with_eq_literal() -> None: + # Regression for #3852: an ifemp (";") capture whose non-empty value + # collides with the following literal that begins with "=" must not be + # misread as empty. "{;a}=end{+rest}" with a="end" expands to + # ";a=end=end/x": the leading "=end" is a="end" and the trailing "=end" + # is the template literal feeding the greedy {+rest}. + t = UriTemplate.parse("{;a}=end{+rest}") + variables = {"a": "end", "rest": "/x"} + assert t.expand(variables) == ";a=end=end/x" + assert t.match(t.expand(variables)) == variables + + # Same collision with a leading literal; the greedy var forces the + # prefix scan that contains the defect. + t2 = UriTemplate.parse("/api{;mode}=fast{+rest}") + variables2 = {"mode": "fast", "rest": "/42"} + assert t2.match(t2.expand(variables2)) == variables2 + + # --- Boundary conditions: valid inputs are unaffected --- + # Empty value before a literal starting with "=" is still detected empty. + assert t.match(";a=end/x") == {"a": "", "rest": "/x"} + # A value that does not collide with the literal is unchanged. + assert t.match(";a=v=end/x") == {"a": "v", "rest": "/x"} + # Name continuation with no "=" is still rejected, not mis-parsed. + assert t.match(";aZ=end/x") is None + # No-greedy / suffix path ("{;a}=end") round-trips both value forms. + t3 = UriTemplate.parse("{;a}=end") + assert t3.match(t3.expand({"a": "end"})) == {"a": "end"} + assert t3.match(t3.expand({"a": ""})) == {"a": ""} + # A '=' with no following literal and no literal at pos is a non-match. + assert t.match(";a=xyz") is None + # Empty value when the following literal does NOT start with '='. + t4 = UriTemplate.parse("p{;a}LIT{+r}") + assert t4.match(t4.expand({"a": "", "r": "x"})) == {"a": "", "r": "x"} + + def test_match_large_uri_against_greedy_template(): # Large payload against a greedy template — the scan visits each # character once for the suffix anchor and once for the greedy