-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[v1.x] fix: reject trailing newline in tool-name validation #3086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maxisbey
wants to merge
2
commits into
v1.x
Choose a base branch
from
fix/tool-name-trailing-newline-v1x
base: v1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+22
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟣 Pre-existing issue, not introduced by this PR: the template-to-regex conversion on line 88 never
re.escape()s the literal (non-parameter) segments, so regex metacharacters in a template are interpreted as regex syntax — e.g. templatetest://a.b/{x}matchestest://aXb/foobecause.is a wildcard, and literals containing(raisere.errorat match time. Since this PR already tightens this exact line viare.fullmatch, a natural follow-up is to split the template on{param}boundaries andre.escape()the literal chunks before joining them with the named-group patterns.Extended reasoning...
The bug
ResourceTemplate.matches()builds its regex by naive string replacement:Only the
{/}delimiters are transformed — the literal (non-parameter) segments of the URI template are spliced into the regex verbatim, with nore.escape(). Any regex metacharacter in a literal segment (.,+,?,*,(,),[,|, ...) is therefore interpreted as regex syntax instead of being matched literally.Step-by-step proof
Take the template
test://a.b/{x}:.replace()calls produce the patterntest://a.b/(?P<x>[^/]+)..ina.bis an unescaped regex wildcard, so it matches any character, not just a literal dot.re.fullmatch('test://a.b/(?P<x>[^/]+)', 'test://aXb/foo')succeeds —.consumesX.matches()returns{'x': 'foo'}, and the URI is dispatched to a handler whose template it does not actually match.Two worse variants were verified empirically:
test://a+b/{x}compilesa+as "one or morea", so it matchestest://ab/fooandtest://aab/foo— and, depending on the surrounding literal, can fail to match its own literal formtest://a+b/foo.(in a literal segment (e.g.test://a(b/{x}) raisesre.error: missing ), unterminated subpatternat match time, turning a registration-time template typo into a runtime exception on everyread_resourcelookup.Why nothing prevents it
There is no escaping or validation anywhere on this path:
ResourceTemplate.from_functionstoresuri_templateas a plain string, andmatches()is the only place it is interpreted. The wire path's pydanticAnyUrlnormalization does not help here — it normalizes the incoming URI, not the server-side template, and dots/plus signs are perfectly legal URL characters that survive normalization.Relation to this PR
This is squarely pre-existing: the naive
.replace()construction on line 88 is untouched by this PR, which only switched the adjacentre.match(f"^{pattern}$", uri)tore.fullmatch(pattern, uri)— a strictly tighter change that neither introduces nor worsens the escaping gap. It's worth noting only because the PR modifies this exact function and fixes the same bug class (overly-permissive template matching), so a follow-up here would be a natural companion.How to fix
Split the template on
{param}boundaries and escape the literal chunks before reassembling, e.g.:This keeps the existing
[^/]+parameter semantics while making literal segments match literally, and eliminates there.errorcrash for templates containing regex metacharacters.