fix(resources): escape literal regex metacharacters in ResourceTemplate.matches#2749
Open
vidigoat wants to merge 1 commit into
Open
fix(resources): escape literal regex metacharacters in ResourceTemplate.matches#2749vidigoat wants to merge 1 commit into
vidigoat wants to merge 1 commit into
Conversation
ResourceTemplate.matches() built its regex with a naive string
substitution:
pattern = self.uri_template.replace('{', '(?P<').replace('}', '>[^/]+)')
The literal portions of the template were never re.escape-d, so regex
metacharacters in the template text were interpreted as operators. A
template like 'api://v1.0/{version}' treated '.' as 'any character' and
wrongly matched 'api://v1X0/abc' (false positive routing a URI to the
wrong template), while a template with '+', '(', '[' etc. in a literal
segment failed to match its own valid URIs (false negative).
Tokenize the template into literal/placeholder parts, re.escape the
literals, and turn '{param}' into named capture groups. Adds a
regression test.
Signed-off-by: Vidit Patankar <vidit.patankar16@gmail.com>
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
ResourceTemplate.matches()(src/mcp/server/mcpserver/resources/templates.py) builds its matching regex by naive string substitution:The literal portions of the template are never
re.escape-d, so any regex metacharacter in the template text is interpreted as a regex operator. This causes both:api://v1.0/{version}treats.as "any character" and wrongly matchesapi://v1X0/abc, routing a URI to a template it shouldn't match (returning{'version': 'abc'}instead ofNone).+,(,[, etc. in a literal segment (e.g.res://a+b/{x}) fails to match its own valid URI.Fix
Tokenize the template into literal/placeholder parts,
re.escapethe literal text, and turn each{param}into a named capture group:Verification
ruff checkandruff format --checkpass on both changed files. Adds a regression test asserting metacharacters in literal segments are matched literally.