Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

fix(server): confine image_url local reads and stop leaking the path

Diego Moraes2201ee2443f1
diff --git a/c/openai_server.py b/c/openai_server.py
index ebed9d31..bfb9436c 100644
--- a/c/openai_server.py
+++ b/c/openai_server.py
@@ -1597,7 +1597,16 @@ GLM53_IMAGE_OPEN, GLM53_IMAGE, GLM53_IMAGE_CLOSE = (
def _image_bytes_from_url(url):
- """data: URI, file:// o percorso sul disco -> i byte dell'immagine."""
+ """data: URI, file:// o percorso sul disco -> i byte dell'immagine.
+
+ A local path is read with the server process's own permissions. On a
+ server that binds beyond loopback (which already requires an API key),
+ an authenticated client could otherwise read any file the process can
+ reach -- e.g. "file:///etc/passwd". Two guards without breaking the
+ documented loopback single-user case: '..' is refused outright (never
+ needed for a real image path), and if COLI_IMAGE_ROOT is set the resolved
+ path must stay inside it, mirroring serve_static's relative_to() check.
+ Errors stay generic so the reply never confirms a path or its permissions."""
if not isinstance(url, str) or not url:
raise APIError(400, "image_url.url must be a non-empty string.", "messages")
if url.startswith("data:"):
@@ -1615,12 +1624,21 @@ def _image_bytes_from_url(url):
raise APIError(400, "remote image URLs are not fetched; send the image "
"as a base64 data: URI or a path on this machine.",
"messages")
- path = url[7:] if url.startswith("file://") else url
+ raw = url[7:] if url.startswith("file://") else url
+ if ".." in Path(raw).parts:
+ raise APIError(400, "image path is not allowed.", "messages")
try:
- with open(path, "rb") as handle:
+ target = Path(raw).resolve()
+ image_root = os.environ.get("COLI_IMAGE_ROOT")
+ if image_root:
+ target.relative_to(Path(image_root).resolve())
+ except (ValueError, OSError):
+ raise APIError(400, "image path is not allowed.", "messages")
+ try:
+ with open(target, "rb") as handle:
return handle.read()
- except OSError as problem:
- raise APIError(400, f"cannot read image {path}: {problem}", "messages")
+ except OSError:
+ raise APIError(400, "cannot read the requested image.", "messages")
# Qwen3.8 splices images as <|vision_start|> + N x <|image_pad|> + <|vision_end|>,
diff --git a/c/tests/test_openai_server.py b/c/tests/test_openai_server.py
index 845c0565..08379ba9 100644
--- a/c/tests/test_openai_server.py
+++ b/c/tests/test_openai_server.py
@@ -2,6 +2,7 @@ import http.client
import io
import json
import math
+import os
import socket
import tempfile
import threading
@@ -17,7 +18,7 @@ from pathlib import Path
from openai_server import (APIError, APIHandler, APIServer, ClientCancelled,
DEFAULT_CHAT_STOP_SEQUENCES, END, GenerationScheduler,
READY, Engine, InklingStreamSplit, StopFilter, ThinkingStreamSplit,
- _engine_error, cap_for_arch, conversation_cache_slot, model_arch,
+ _engine_error, _image_bytes_from_url, cap_for_arch, conversation_cache_slot, model_arch,
generation_options, parse_tool_calls, parse_dsv4_tool_calls,
parse_arch_tool_calls, parse_k3_tool_calls, parse_qwen38_tool_calls,
read_engine_turn, render_chat, render_chat_kimi, render_chat_olmoe,
@@ -2567,5 +2568,48 @@ class ReasoningEffortTest(unittest.TestCase):
self.assertNotIn("Reasoning Effort", text)
+class ImageUrlPathGuard(unittest.TestCase):
+ """image_url.url points at a local file read with the server's rights.
+ A '..' path is refused; COLI_IMAGE_ROOT confines reads; errors stay
+ generic so a reply never confirms a path or its permissions."""
+
+ def setUp(self):
+ self._saved = os.environ.pop("COLI_IMAGE_ROOT", None)
+
+ def tearDown(self):
+ os.environ.pop("COLI_IMAGE_ROOT", None)
+ if self._saved is not None:
+ os.environ["COLI_IMAGE_ROOT"] = self._saved
+
+ def test_reads_a_plain_file_by_default(self):
+ with tempfile.TemporaryDirectory() as root:
+ img = Path(root) / "pic.png"
+ img.write_bytes(b"\x89PNG\r\n")
+ self.assertEqual(_image_bytes_from_url(str(img)), b"\x89PNG\r\n")
+ self.assertEqual(_image_bytes_from_url("file://" + str(img)),
+ b"\x89PNG\r\n")
+
+ def test_dotdot_is_refused(self):
+ with self.assertRaises(APIError) as caught:
+ _image_bytes_from_url("/var/data/../../etc/passwd")
+ self.assertEqual(caught.exception.status, 400)
+ self.assertNotIn("passwd", str(caught.exception))
+
+ def test_image_root_confines_reads(self):
+ with tempfile.TemporaryDirectory() as root, \
+ tempfile.NamedTemporaryFile() as outside:
+ os.environ["COLI_IMAGE_ROOT"] = root
+ inside = Path(root) / "ok.png"
+ inside.write_bytes(b"ok")
+ self.assertEqual(_image_bytes_from_url(str(inside)), b"ok")
+ with self.assertRaises(APIError):
+ _image_bytes_from_url(outside.name)
+
+ def test_error_does_not_leak_the_path(self):
+ with self.assertRaises(APIError) as caught:
+ _image_bytes_from_url("/no/such/secret-name.png")
+ self.assertNotIn("secret-name", str(caught.exception))
+
+
if __name__ == "__main__":
unittest.main()