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

未關閉 適合新手
#3,884 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@visy-ani 已經在處理了。

開始於 2026年11月22日。

  • #3899 來自 @industriverse —— 已關閉,未合併
  • #3900 來自 @MrSpy00 —— 已關閉,未合併
  • #3915 來自 @EnesKartal —— 已關閉,未合併
  • #3941 來自 @Echolonius —— 已關閉,未合併

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
74/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python
領域
security

研究方向

閱讀 src/mcp/shared/skills.py 中 Skill._check_conformance 附近(第 127-128 行)、skill_name_from_uri(221-243)、parse_directory_uri(263-275)、_validate_resource_uri_in_skill 和 validate_directory_result;針對 resources == "dynamic" 的提前回傳就是那裡跳過驗證的原因。查看相關的 #3685,了解百分比編碼的 dot-segment 是如何處理的,以便兩個修正保持一致。用 issue 中的程式碼片段重現,然後讓列出的每個 URI 拋出 ValueError/ValidationError,並在現有 skills URI 測試旁邊新增回歸測試;完成意味著該程式碼片段會拋出,且測試通過。

由索引模型根據 Issue 內容生成。

描述

bug v2
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 @ 77001c96)

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

主要語言
Python
星號
25k
分支
4.2k
平均合併
10 分鐘
30 天內合併 PR
1

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

modelcontextprotocol/python-sdk 的其他 Issue

查看 modelcontextprotocol/python-sdk 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。