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(api): refuse blocking special files as local images

Rudy Celeklia2723f09a563
diff --git a/c/openai_server.py b/c/openai_server.py
index 2838036f..eaa923ff 100644
--- a/c/openai_server.py
+++ b/c/openai_server.py
@@ -13,6 +13,7 @@ import os
import select
import queue
import signal
+import stat
import socket
import subprocess
import sys
@@ -2733,8 +2734,20 @@ def _image_bytes_from_url(url):
except (ValueError, OSError):
raise APIError(400, "image path is not allowed.", "messages")
try:
- with open(target, "rb") as handle:
- return handle.read()
+ # An allowed directory can also contain a FIFO or device. Opening a
+ # FIFO in ordinary blocking mode would hold an API thread before the
+ # image decoder can reject it. Inspect the opened descriptor, rather
+ # than a pre-open path check, and never wait for a special-file writer.
+ fd = os.open(target, os.O_RDONLY | getattr(os, "O_NONBLOCK", 0))
+ try:
+ if not stat.S_ISREG(os.fstat(fd).st_mode):
+ raise APIError(400, "local image paths must name regular files.", "messages")
+ with os.fdopen(fd, "rb") as handle:
+ fd = None # the file object now owns the descriptor
+ return handle.read()
+ finally:
+ if fd is not None:
+ os.close(fd)
except OSError:
raise APIError(400, "cannot read the requested image.", "messages")
diff --git a/c/tests/test_image_regular_file.py b/c/tests/test_image_regular_file.py
new file mode 100644
index 00000000..f65d282c
--- /dev/null
+++ b/c/tests/test_image_regular_file.py
@@ -0,0 +1,53 @@
+"""An allowed image directory must not expose blocking special files to API clients."""
+import os
+from pathlib import Path
+import tempfile
+import threading
+import unittest
+from unittest.mock import patch
+
+from openai_server import APIError, _image_bytes_from_url
+
+
+class LocalImageFileTest(unittest.TestCase):
+ @unittest.skipUnless(hasattr(os, "mkfifo"), "host has no FIFO files")
+ def test_fifo_is_refused_without_waiting_for_a_writer(self):
+ with tempfile.TemporaryDirectory() as home, \
+ patch.dict(os.environ, {"COLI_IMAGE_ROOT": home}):
+ fifo = Path(home) / "picture.png"
+ os.mkfifo(fifo)
+ done = threading.Event()
+ results = []
+
+ def read():
+ try:
+ results.append(_image_bytes_from_url(str(fifo)))
+ except Exception as error:
+ results.append(error)
+ finally:
+ done.set()
+
+ reader = threading.Thread(target=read, daemon=True)
+ reader.start()
+ try:
+ self.assertTrue(done.wait(0.5), "API image path blocks waiting for a FIFO writer")
+ self.assertIsInstance(results[0], APIError)
+ self.assertEqual(results[0].status, 400)
+ finally:
+ # Release the original blocking implementation after recording
+ # the failure, without leaving a blocked test thread behind.
+ if not done.is_set():
+ fd = os.open(fifo, os.O_WRONLY | os.O_NONBLOCK)
+ os.close(fd)
+ reader.join(timeout=2)
+
+ def test_regular_file_still_reads_all_bytes(self):
+ with tempfile.TemporaryDirectory() as home, \
+ patch.dict(os.environ, {"COLI_IMAGE_ROOT": home}):
+ image = Path(home) / "picture.png"
+ image.write_bytes(b"image bytes")
+ self.assertEqual(_image_bytes_from_url(str(image)), b"image bytes")
+
+
+if __name__ == "__main__":
+ unittest.main()