fix(server): escape regex special chars in resource template literals#2963
Closed
Bartok9 wants to merge 1 commit into
Closed
fix(server): escape regex special chars in resource template literals#2963Bartok9 wants to merge 1 commit into
Bartok9 wants to merge 1 commit into
Conversation
Closes modelcontextprotocol#2961 ResourceTemplate.matches() built its regex via plain string replacement ("{" -> "(?P<", "}" -> ">[^/]+)"), leaving regex-special characters in the literal parts of the URI template (".", "?", "+", etc.) unescaped. They were then interpreted as metacharacters, causing false matches: data://.well-known/{name} matched data://Xwell-known/hello (. = any char) data://items/{id}.json matched data://items/123Xjson (. = any char) Escape the whole template with re.escape() first, then substitute the escaped "{param}" markers with named capture groups so literal characters are matched literally while parameters still capture. Adds a regression test that fails without the fix.
Member
|
You've opened a duplicated pull request, please search opened PRs before creating new ones. Duplicated from #2749. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ResourceTemplate.matches()now treats regex-special characters in the literal parts of a URI template (.,?,+, etc.) as literals instead of regex metacharacters.data://.well-known/{name}matched URIs it should not.Motivation
Closes #2961.
matches()converted the URI template to a regex with plain string replacement:This left regex-special characters in the literal portions unescaped, so they acted as metacharacters and produced incorrect matches:
data://.well-known/{name}data://Xwell-known/hello(.= any char)data://items/{id}.jsondata://items/123Xjson(.= any char)Fix
Escape the whole template with
re.escape()first, then substitute the escaped{param}markers with named capture groups:Literal characters are now matched literally; parameters still capture
[^/]+as before.Verification
uv run pytest tests/server/mcpserver/resources/test_resource_template.py— 16 passeduv run ruff check/ruff format --check— cleanReal behavior proof
Regression test
test_template_matches_escapes_regex_special_charsreproduces the issue cases. It fails without the source fix and passes with it: