FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: reject URL-encoded path separators in resource template parameters by sebastionoss · Pull Request #2353 · modelcontextprotocol/python-sdk · GitHub

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
22 changes: 21 additions & 1 deletion src/mcp/server/mcpserver/resources/templates.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
from mcp.server.context import LifespanContextT, RequestT
from mcp.server.mcpserver.context import Context

# Regex used for each URI template parameter segment.
# Decoded values must still satisfy this constraint to prevent
# encoded path-separator injection (e.g. ``%2F`` → ``/``).
_SEGMENT_RE = re.compile(r"[^/]+")


class ResourceTemplate(BaseModel):
"""A template for dynamically creating resources."""
Expand Down Expand Up @@ -86,13 +91,28 @@ def matches(self, uri: str) -> dict[str, Any] | None:
"""Check if URI matches template and extract parameters.

Extracted parameters are URL-decoded to handle percent-encoded characters.
After decoding, each value is re-validated to ensure it does not contain
a ``/`` character, which would indicate an encoded path separator bypass
(e.g. ``%2F``). Rejecting such values prevents path-traversal attacks
where an attacker could send ``..%2F..%2Fetc%2Fpasswd`` to escape the
intended path segment.
"""
# Convert template to regex pattern
pattern = self.uri_template.replace("{", "(?P<").replace("}", ">[^/]+)")
match = re.match(f"^{pattern}$", uri)
if match:
# URL-decode all extracted parameter values
return {key: unquote(value) for key, value in match.groupdict().items()}
decoded = {key: unquote(value) for key, value in match.groupdict().items()}

# Reject any decoded value that would not have matched the
# original ``[^/]+`` segment constraint. This blocks encoded
# slash injection (``%2F`` → ``/``) which could allow path
# traversal when the parameter is used in file-system operations.
for value in decoded.values():
if not _SEGMENT_RE.fullmatch(value):
return None

return decoded
return None

async def create_resource(
Expand Down
Loading

Back | FazBrowse Home | New Git URL