Skip to content

Commit b5d2748

Browse files
committed
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
1 parent 77001c9 commit b5d2748

2 files changed

Lines changed: 56 additions & 13 deletions

File tree

‎src/mcp/shared/uri_template.py‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,22 +1079,30 @@ def _scan_prefix(
10791079
assert isinstance(nxt, _Lit)
10801080

10811081
if atom.ifemp:
1082-
# RFC §3.2.7 ifemp: ;name=val for non-empty, bare ;name for
1083-
# empty. Decide which form is present without falling through
1084-
# to the stop-char scan when the value is empty.
1082+
# RFC §3.2.7 ifemp: ;name=val for a non-empty value, bare
1083+
# ;name for an empty one. The '=' that opens the value form is
1084+
# also the first character of a following literal that begins
1085+
# with '=', so the two forms collide when the value's text
1086+
# starts with that literal (e.g. {;a}=end with a="end" expands
1087+
# to ;a=end=end). Prefer the value form: locate the following
1088+
# literal after the '='; only treat the value as empty when it
1089+
# cannot be found there.
1090+
if pos < limit and uri[pos] == "=":
1091+
value_start = pos + 1
1092+
latest = value_start
1093+
while latest < limit and uri[latest] not in stops:
1094+
latest += 1
1095+
end = uri.find(nxt.text, value_start, latest + len(nxt.text))
1096+
if end != -1:
1097+
result[var.name] = unquote(uri[value_start:end])
1098+
pos = end
1099+
continue
1100+
# No '=', or the '=' belongs to the following literal: the
1101+
# value is empty iff the following literal starts here.
10851102
if uri.startswith(nxt.text, pos):
1086-
# Following literal begins immediately: value is empty.
1087-
# Checked before '=' so a literal that itself starts
1088-
# with '=' is not mistaken for the ifemp separator.
10891103
result[var.name] = ""
10901104
continue
1091-
if pos < limit and uri[pos] == "=":
1092-
pos += 1 # value follows; fall through to the scan
1093-
else:
1094-
# The following literal does not start here and there is
1095-
# no '=': the URI's name continued past the template's
1096-
# (e.g. ;keys vs ;key) — no parse.
1097-
return None
1105+
return None
10981106

10991107
# Latest valid end: the var stops at the first stop-char or
11001108
# the scan limit, whichever comes first.

‎tests/shared/test_uri_template.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,41 @@ def test_match_prefix_ifemp_empty_before_non_stop_literal():
747747
assert t.match("api;keyZX/tail") is None
748748

749749

750+
def test_match_prefix_ifemp_value_collides_with_eq_literal() -> None:
751+
# Regression for #3852: an ifemp (";") capture whose non-empty value
752+
# collides with the following literal that begins with "=" must not be
753+
# misread as empty. "{;a}=end{+rest}" with a="end" expands to
754+
# ";a=end=end/x": the leading "=end" is a="end" and the trailing "=end"
755+
# is the template literal feeding the greedy {+rest}.
756+
t = UriTemplate.parse("{;a}=end{+rest}")
757+
variables = {"a": "end", "rest": "/x"}
758+
assert t.expand(variables) == ";a=end=end/x"
759+
assert t.match(t.expand(variables)) == variables
760+
761+
# Same collision with a leading literal; the greedy var forces the
762+
# prefix scan that contains the defect.
763+
t2 = UriTemplate.parse("/api{;mode}=fast{+rest}")
764+
variables2 = {"mode": "fast", "rest": "/42"}
765+
assert t2.match(t2.expand(variables2)) == variables2
766+
767+
# --- Boundary conditions: valid inputs are unaffected ---
768+
# Empty value before a literal starting with "=" is still detected empty.
769+
assert t.match(";a=end/x") == {"a": "", "rest": "/x"}
770+
# A value that does not collide with the literal is unchanged.
771+
assert t.match(";a=v=end/x") == {"a": "v", "rest": "/x"}
772+
# Name continuation with no "=" is still rejected, not mis-parsed.
773+
assert t.match(";aZ=end/x") is None
774+
# No-greedy / suffix path ("{;a}=end") round-trips both value forms.
775+
t3 = UriTemplate.parse("{;a}=end")
776+
assert t3.match(t3.expand({"a": "end"})) == {"a": "end"}
777+
assert t3.match(t3.expand({"a": ""})) == {"a": ""}
778+
# A '=' with no following literal and no literal at pos is a non-match.
779+
assert t.match(";a=xyz") is None
780+
# Empty value when the following literal does NOT start with '='.
781+
t4 = UriTemplate.parse("p{;a}LIT{+r}")
782+
assert t4.match(t4.expand({"a": "", "r": "x"})) == {"a": "", "r": "x"}
783+
784+
750785
def test_match_large_uri_against_greedy_template():
751786
# Large payload against a greedy template — the scan visits each
752787
# character once for the suffix anchor and once for the greedy

0 commit comments

Comments
 (0)