Skip to content

FreeToken brings datacenter-scale model serving to your desktop. Run massive models locally, fast and efficiently.

fix(server): accept localhost authority in local file URIs (#758)

Your Open Source Tooling Specialistbb15d1d387c8
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))