Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596"""Path values remain data within an operation's declared route."""
from functools import partialfrom http.server import SimpleHTTPRequestHandler, ThreadingHTTPServerfrom pathlib import Pathfrom threading import Thread
import httpx2import pytestfrom jsonschema_path import SchemaPath
from fastmcp import Client, FastMCPfrom fastmcp.exceptions import ToolErrorfrom fastmcp.utilities.openapi.director import RequestDirector
@pytest.mark.parametrize( "value", [ ".", "..", "../other", "other/../x", r"other\..\x", "%2e%2e", "%252e%252e", "..%2fother", "other%5c..%5cx", ],)def test_path_value_rejects_dot_segments(value: str): with pytest.raises(ValueError, match="dot segments"): RequestDirector(SchemaPath.from_dict({}))._build_url( "/items/{id}/record", {"id": value}, "https://api.example.com" )
@pytest.mark.parametrize( "value", ["item.1", ".hidden", "..name", "a/b", "a\\b", "100%", 42])def test_path_value_preserves_ordinary_data(value: str | int): url = RequestDirector(SchemaPath.from_dict({}))._build_url( "/items/{id}/record", {"id": value}, "https://api.example.com" ) assert url.startswith("https://api.example.com/items/") assert url.endswith("/record")
async def test_path_value_is_checked_before_backend_request(tmp_path: Path): (tmp_path / "api" / "users").mkdir(parents=True) (tmp_path / "api" / "admin.txt").write_text("private-record") handler = partial(SimpleHTTPRequestHandler, directory=str(tmp_path)) backend = ThreadingHTTPServer(("127.0.0.1", 0), handler) worker = Thread(target=backend.serve_forever, daemon=True) worker.start() spec = { "openapi": "3.0.0", "info": {"title": "API", "version": "1"}, "paths": { "/api/users/{id}/admin.txt": { "get": { "operationId": "get_record", "parameters": [ { "name": "id", "in": "path", "required": True, "schema": {"type": "string"}, } ], "responses": {"200": {"description": "OK"}}, } } }, } try: async with httpx2.AsyncClient( base_url=f"http://127.0.0.1:{backend.server_port}" ) as http: server = FastMCP.from_openapi(spec, client=http) async with Client(server) as client: with pytest.raises(ToolError, match="dot segments"): await client.call_tool("get_record", {"id": ".."}) finally: backend.shutdown() backend.server_close() worker.join()
def test_path_value_rejects_excessive_encoding_layers(): value = "%" + "25" * 40 + "2e" with pytest.raises(ValueError, match="too many encoding layers"): RequestDirector(SchemaPath.from_dict({}))._build_url( "/items/{id}", {"id": value}, "https://api.example.com" )