99
1010import re
1111from typing import Annotated , Any , Literal
12- from urllib .parse import urlsplit
12+ from urllib .parse import unquote , urlsplit
1313
1414from mcp_types import CacheableResult , PaginatedRequestParams , PaginatedResult , Request , RequestParams , Resource
1515from pydantic import AfterValidator , BaseModel , ConfigDict , Field , model_validator
@@ -218,6 +218,20 @@ class ReadDirectoryRequest(Request[ReadDirectoryParams, Literal["resources/direc
218218 params : ReadDirectoryParams
219219
220220
221+ def _has_query_or_fragment (uri : str ) -> bool :
222+ """Return True if `uri` contains a `?` or `#` delimiter, including bare trailing ones."""
223+ return "?" in uri or "#" in uri
224+
225+
226+ def _is_dot_or_traversal_segment (segment : str ) -> bool :
227+ decoded = unquote (segment )
228+ return segment in ("." , ".." ) or decoded in ("." , ".." ) or "/" in decoded
229+
230+
231+ def _has_traversal_segment (path : str ) -> bool :
232+ return any (_is_dot_or_traversal_segment (segment ) for segment in path .split ("/" ))
233+
234+
221235def skill_name_from_uri (uri : str ) -> str :
222236 """Return the skill `name` encoded in a `SKILL.md` resource URI.
223237
@@ -226,11 +240,14 @@ def skill_name_from_uri(uri: str) -> str:
226240 prefix) that segment is the authority.
227241
228242 Raises:
229- ValueError: If `uri` is not an absolute URI ending in `/SKILL.md`.
243+ ValueError: If `uri` is not an absolute URI ending in `/SKILL.md`, or
244+ contains query/fragment delimiters or traversal path segments.
230245 """
231246 parts = urlsplit (uri )
232- if not parts .scheme or parts . query or parts . fragment :
247+ if not parts .scheme or _has_query_or_fragment ( uri ) :
233248 raise ValueError (f"skill URI { uri !r} is not a valid absolute resource URI" )
249+ if _has_traversal_segment (parts .path ):
250+ raise ValueError (f"skill URI { uri !r} contains a traversal segment" )
234251 if not parts .path .endswith ("/SKILL.md" ):
235252 raise ValueError (f"skill URI { uri !r} must end in /SKILL.md" )
236253 directory = parts .path [: - len ("/SKILL.md" )].strip ("/" )
@@ -247,7 +264,7 @@ def _validate_resource_uri_in_skill(skill_uri: str, resource_uri: str) -> None:
247264 """Raise `ValueError` unless `resource_uri` names a file within `skill_uri`'s directory."""
248265 skill_parts = urlsplit (skill_uri )
249266 resource_parts = urlsplit (resource_uri )
250- if not resource_parts .scheme or resource_parts . query or resource_parts . fragment :
267+ if not resource_parts .scheme or _has_query_or_fragment ( resource_uri ) :
251268 raise ValueError (f"resource URI { resource_uri !r} is invalid" )
252269 if resource_uri .endswith ("/" ):
253270 raise ValueError (f"resource URI { resource_uri !r} names a directory, not a file" )
@@ -256,22 +273,24 @@ def _validate_resource_uri_in_skill(skill_uri: str, resource_uri: str) -> None:
256273 root = skill_parts .path [: - len ("/SKILL.md" )]
257274 if resource_parts .path != skill_parts .path and not resource_parts .path .startswith (root + "/" ):
258275 raise ValueError (f"resource URI { resource_uri !r} is outside the skill root { skill_uri !r} " )
259- if any ( segment in ( "." , ".." ) for segment in resource_parts .path . split ( "/" ) ):
276+ if _has_traversal_segment ( resource_parts .path ):
260277 raise ValueError (f"resource URI { resource_uri !r} contains a traversal segment" )
261278
262279
263280def parse_directory_uri (uri : str ) -> tuple [str , str , str ]:
264281 """Split a directory resource URI into `(scheme, netloc, path)`.
265282
266283 Raises:
267- ValueError: If `uri` has a trailing slash, or is otherwise not a valid
268- absolute resource URI.
284+ ValueError: If `uri` has a trailing slash, traversal segments, query or
285+ fragment delimiters, or is otherwise not a valid absolute resource URI.
269286 """
270287 if uri .endswith ("/" ):
271288 raise ValueError (f"directory URI { uri !r} must not have a trailing slash" )
272289 parts = urlsplit (uri )
273- if not parts .scheme or parts . query or parts . fragment :
290+ if not parts .scheme or _has_query_or_fragment ( uri ) :
274291 raise ValueError (f"directory URI { uri !r} is not a valid absolute resource URI" )
292+ if _has_traversal_segment (parts .path ):
293+ raise ValueError (f"directory URI { uri !r} contains a traversal segment" )
275294 return parts .scheme , parts .netloc , parts .path
276295
277296
@@ -291,12 +310,12 @@ def validate_directory_result(uri: str, result: ReadDirectoryResult) -> None:
291310 prefix = parent_path .rstrip ("/" ) + "/" if parent_path .rstrip ("/" ) else "/"
292311 for resource in result .resources :
293312 child = urlsplit (resource .uri )
294- if not child .scheme or child . query or child . fragment :
313+ if not child .scheme or _has_query_or_fragment ( resource . uri ) :
295314 raise ValueError (f"directory { uri !r} child has invalid URI { resource .uri !r} " )
296315 if child .scheme != scheme or child .netloc != netloc :
297316 raise ValueError (f"resource { resource .uri !r} is not a child of directory { uri !r} " )
298317 relative = child .path .removeprefix (prefix )
299- if relative == child .path or not relative or "/" in relative or relative in ( "." , ".." ):
318+ if relative == child .path or not relative or "/" in relative or _is_dot_or_traversal_segment ( relative ):
300319 raise ValueError (f"resource { resource .uri !r} is not a direct child of directory { uri !r} " )
301320 if resource .uri in seen_uris :
302321 raise ValueError (f"directory { uri !r} contains a duplicate child { resource .uri !r} " )
0 commit comments