Skip to content

Skills URI validation accepts traversal segments in dynamic skill/directory URIs and trailing '?' / '#' delimiters #3884

Open

Description

@0718lol

Initial Checks

Release line

2.x (current stable)

Description

In src/mcp/shared/skills.py, there are two validation gaps in skill and directory URI checks (distinct from #3685, which covers percent-encoded dot-segments in child URIs inside _validate_resource_uri_in_skill / validate_directory_result):

  1. Dot-segment traversal accepted in skill root URIs (when resources="dynamic") and directory URIs:

    • skill_name_from_uri() (lines 221-243) and parse_directory_uri() (lines 263-275) never check path segments for . or ...
    • In Skill._check_conformance() (lines 127-128), if self.resources == "dynamic": return self returns early before _validate_resource_uri_in_skill() ever runs, so a dynamic Skill with uri="skill://org/../my-skill/SKILL.md" is accepted without error.
    • Similarly, parse_directory_uri("skill://org/../../etc") (used by Skills._handle_read_directory to gate incoming resources/directory/read requests) accepts .. segments.
  2. Empty ? / # delimiters bypass query/fragment rejection:

    • skill_name_from_uri(), _validate_resource_uri_in_skill(), parse_directory_uri(), and validate_directory_result() check if not parts.scheme or parts.query or parts.fragment:.
    • Because urllib.parse.urlsplit() returns empty strings ("") for a bare trailing ? or #, URIs such as skill://my-skill/SKILL.md?, skill://my-skill/SKILL.md#, skill://my-skill?, and skill://my-skill# pass validation.

Example Code

from mcp.shared.skills import Skill, parse_directory_uri, skill_name_from_uri

# 1. Dynamic skill accepts '..' traversal segments in its own URI
skill = Skill(
    uri="skill://org/../my-skill/SKILL.md",
    frontmatter={"name": "my-skill", "description": "example"},
    resources="dynamic",
)
print("1. Dynamic skill accepted:", skill.uri)

# 2. parse_directory_uri accepts '..' traversal segments
print("2. parse_directory_uri accepted:", parse_directory_uri("skill://org/../../etc"))

# 3. Empty '?' / '#' delimiters bypass query/fragment checks
for uri in ("skill://my-skill/SKILL.md?", "skill://my-skill/SKILL.md#"):
    print(f"3a. skill_name_from_uri({uri!r}) ->", skill_name_from_uri(uri))
for uri in ("skill://my-skill?", "skill://my-skill#"):
    print(f"3b. parse_directory_uri({uri!r}) ->", parse_directory_uri(uri))

Observed output:

1. Dynamic skill accepted: skill://org/../my-skill/SKILL.md
2. parse_directory_uri accepted: ('skill', 'org', '/../../etc')
3a. skill_name_from_uri('skill://my-skill/SKILL.md?') -> my-skill
3a. skill_name_from_uri('skill://my-skill/SKILL.md#') -> my-skill
3b. parse_directory_uri('skill://my-skill?') -> ('skill', 'my-skill', '')
3b. parse_directory_uri('skill://my-skill#') -> ('skill', 'my-skill', '')

Expected behavior: all of the above URIs should raise ValueError (and ValidationError on Skill), rejecting dot-segments (literal or percent-encoded, aligning with #3685) and any ? or # delimiter in the URI string.

I would be happy to submit a PR with regression tests if assigned.

Python & MCP Python SDK

  • Python: 3.14.8
  • mcp: 2.3.1.dev20+77001c96 (main @ 77001c9)

AI disclosure: AI assistance was used to help investigate and draft this issue; the reproduction and observed output were verified locally.

Activity

  1. added
    bug
    v2
    on Nov 21, 2026
  2. visy-ani commented on Nov 22, 2026

    @visy-ani

    can i take this? i'd resolve the path and check it stays inside the skill dir, and reject trailing ? / # before parsing. happy to add tests for the traversal cases too.

  3. industriverse commented on Nov 23, 2026

    @industriverse

    Confirmed both gaps locally against main (src/mcp/shared/skills.py):

    1. skill_name_from_uri() and parse_directory_uri() accept literal and percent-encoded dot-segments (.., ., %2e%2e, %2E), allowing dynamic Skill roots and resources/directory/read requests to bypass traversal checks.
    2. Bare trailing ? and # pass all four validators because urllib.parse.urlsplit() parses empty query/fragment strings as "".

    Adding _has_query_or_fragment() ("?" in uri or "#" in uri) and _is_dot_or_traversal_segment() (checking raw + unquote() segments across all four validators) closes both vectors cleanly with 74/74 tests passing in tests/shared/test_skills.py (and 30/30 CI checks green on #3899 if maintainers decide to assign and reopen).

  4. MrSpy00 commented on Nov 23, 2026

    @MrSpy00

    I have a tested fix ready for this (rejecting path traversal segments and empty query/fragment delimiters in skill URIs, 29/29 tests passing). Could you assign this issue to me so I can reopen the PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugv2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions