Skip to content
Closed
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
34 changes: 21 additions & 13 deletions src/mcp/shared/uri_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 35 additions & 0 deletions tests/shared/test_uri_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading