fix: reject trailing newline in tool-name validation#3076
Open
Otis0408 wants to merge 1 commit into
Open
Conversation
TOOL_NAME_REGEX was end-anchored with $, which in Python's default mode also
matches just before a single trailing newline. So validate_tool_name("x\n")
returned is_valid=True with no warning, and a 127-char name plus "\n" (len
128) slipped past both the length and character checks. Anchor with \Z so a
trailing newline is treated as the disallowed character it is.
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.
Summary
TOOL_NAME_REGEXinsrc/mcp/shared/tool_name_validation.pyis end-anchored with$:In Python's default (non-
MULTILINE) mode,$matches at end-of-string or immediately before a single trailing\n. So a tool name ending in exactly one newline passes validation:The length guard uses
len()(which counts the\n), so"a" * 127 + "\n"(length 128) also slips past both the length and the character check. Embedded and non-\ntrailing control chars are already rejected correctly — only the single-trailing-newline case leaks.Fix
Anchor the end with
\Z(strict end-of-string) instead of$. No valid name is affected —re.match(r"^[A-Za-z0-9._-]{1,128}\Z", "abc")still matches; only"abc\n"now correctly fails.Tests
Adds
test_validate_tool_name_rejects_trailing_newline(parametrized over a trailing-newline name and a 128-char name whose last char is\n). Verified it fails onmain(is_valid=True) and passes with the fix; the file's existing 29 tests are unchanged (no valid-name fixture ends in a newline).