| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
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.
With re.match, a $-anchored pattern also matches just before a single trailing newline, so tool-name validation accepted "name\n". Switch the tool-name and URI-template varname checks to re.fullmatch, which puts the whole-string requirement at the call site, and fold the regression cases into the existing invalid-character parametrizations.
|
Thanks @Otis0408 — nice catch, and thanks for filing #3084 to track it per CONTRIBUTING.md. I pushed two small changes on top: switched the fix from \Z to re.fullmatch() (same behavior — it puts the whole-string requirement at the call site and matches how the rest of the codebase validates), and applied the same fix to the URI-template varname check in uri_template.py, which had the same $-with-.match pattern. Merging once CI is green. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Closes #3084
Summary
TOOL_NAME_REGEX in src/mcp/shared/tool_name_validation.py is 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-\n trailing 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 on main (is_valid=True) and passes with the fix; the file's existing 29 tests are unchanged (no valid-name fixture ends in a newline).