From bb15d1d387c8bb45441e2cef96115e9449bebc87 Mon Sep 17 00:00:00 2001 From: Your Open Source Tooling Specialist <121191527+bettercallcaleb@users.noreply.github.com> Date: Tue, 20 Oct 2026 00:40:55 -0400 Subject: [PATCH] fix(server): accept localhost authority in local file URIs (#758) Resolve the parsed URI path without prepending localhost, while rejecting non-local authorities and retaining the media root check. Add regression cases for localhost authority spellings and remote hosts. Refs FlashML-org/FreeToken#757 --- python/freetoken/mm/media.py | 5 ++++- tests/server/test_mm_input.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/python/freetoken/mm/media.py b/python/freetoken/mm/media.py index 5c3c4ef7..40c782ee 100644 --- a/python/freetoken/mm/media.py +++ b/python/freetoken/mm/media.py @@ -66,7 +66,10 @@ def _load_local_media(url: str, config: ServerArgs) -> bytes: if not root: raise ValueError("cannot load local files without --allowed-local-media-path") spec = urlparse(url) - filepath = Path(url2pathname((spec.netloc or "") + (spec.path or ""))) + # RFC 8089: empty authority and localhost identify the local machine. + if spec.netloc and spec.netloc.lower().rstrip(".") != "localhost": + raise ValueError("file:// URLs must have no host or use localhost") + filepath = Path(url2pathname(spec.path or "")) # resolve() follows symlinks, so a link inside the root escaping it is rejected too resolved = filepath.resolve() if Path(root).resolve() not in resolved.parents: diff --git a/tests/server/test_mm_input.py b/tests/server/test_mm_input.py index 19d9f865..bfc9b16d 100644 --- a/tests/server/test_mm_input.py +++ b/tests/server/test_mm_input.py @@ -119,6 +119,16 @@ def test_local_media_requires_allowlisted_root(tmp_path): config = _config(allowed_local_media_path=str(tmp_path)) assert asyncio.run(fetch_image_bytes([{"kind": "url", "data": url}], config)) == [b"fakepng"] + # RFC 8089 local authority refers to the same file as file:///... . + for authority in ("localhost", "LOCALHOST", "localhost."): + local_url = f"file://{authority}{img}" + assert asyncio.run(fetch_image_bytes([{"kind": "url", "data": local_url}], config)) == [b"fakepng"] + + # Non-local authorities must not be interpreted as filesystem paths. + remote_url = f"file://remote.invalid{img}" + with pytest.raises(ValueError, match="no host or use localhost"): + asyncio.run(fetch_image_bytes([{"kind": "url", "data": remote_url}], config)) + # a path outside the root is rejected even with the gate on with pytest.raises(ValueError, match="subpath"): asyncio.run(fetch_image_bytes([{"kind": "url", "data": "file:///etc/hostname"}], config))