-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[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
1
commit 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.
+6
−1
Open
Changes from all commits
Commits
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
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 same
$-with-re.matchtrailing-newline bug fixed here for tool names also exists on v1.x inResourceTemplate.matches()(src/mcp/server/fastmcp/resources/templates.py:89), where'resource://user/123\n'matches template'resource://user/{id}'with the newline captured into the extracted parameter. The wire path is protected by pydanticAnyUrlnormalization, butFastMCP.read_resource/Context.read_resourceaccept rawstr, so consider a follow-up switching it tore.fullmatch— note this also means the PR description's claim that the URI half of #3076 doesn't apply to v1.x isn't quite complete.Extended reasoning...
The bug
This PR fixes the classic Python regex pitfall —
$matches not only at end-of-string but also just before a single trailing\n, sore.match(r'^...$', s)accepts strings with a trailing newline — forTOOL_NAME_REGEXintool_name_validation.py. The identical pattern remains on v1.x inResourceTemplate.matches():Step-by-step proof
For template
resource://user/{id}and input'resource://user/123\n':^resource://user/(?P<id>[^/]+)$.[^/]+matches any non-slash character — including\n— so it greedily consumes123\n.$(withoutre.MULTILINE) matches at end-of-string or just before a trailing newline at end-of-string; here it matches at the very end after[^/]+consumed the newline.match.groupdict()returns{'id': '123\n'}— the URI is accepted and the trailing newline is smuggled into the extracted parameter, which is then passed as an argument to the resource function.(Even if
[^/]+were non-greedy, step 3 would let$match before the newline — either way the input is accepted when it should be rejected.)Why the impact is limited
On the wire-protocol path,
ReadResourceRequestParams.uriis a pydanticAnyUrl, and pydantic-core normalization strips a raw trailing newline ('resource://user/123\n'validates to'resource://user/123'), so a remote client sending a compliantresources/readrequest cannot trigger this. However, the server-side entry pointsFastMCP.read_resource,Context.read_resource, andResourceManager.get_resourceall acceptAnyUrl | strand pass a plainstrthrough unnormalized, so in-process/programmatic callers still hit the bug.Relation to this PR
This PR does not touch
templates.py, so this is squarely pre-existing and should not block merge. It is worth noting, though, because the PR description states that the URI half of #3076 doesn't apply to v1.x (sinceuri_template.pyis v2-only) — but this separate fastmcp instance of the same bug class does exist on v1.x, so the backport leaves one instance unfixed.How to fix
Mirror this PR: use
re.fullmatch(pattern, uri)instead ofre.match(f'^{pattern}$', uri). Alternatively, anchor with\A...\Z— whichsrc/mcp/server/experimental/task_scope.py:31-34already does, with a comment explicitly warning about this exact$/trailing-newline hazard, so the fix direction is already established in this codebase.