mirror of
https://github.com/hasso5703/alzeo.git
synced 2026-12-06 12:54:19 +01:00
Without the identity wall, the engine's key is the port's one gate (README), and a request without it still had its body read whole and, when large, counted by the engine on the proxy's own key before the engine's 401. The proxy now applies the engine's own rule first (measured on the live engine: bearer in any case, one space, the key exactly) and answers the engine's own 401, before the body and before an Expect: 100-continue invitation; /health and /metrics stay open. The test harnesses decide which key their proxy holds, whatever HOME holds: those that give it one present it, as every real client does, and those whose clients send none give it none (the CI's offline step gives HOME a key). The cockpit knows the new outcome. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
118 lines
5.0 KiB
Python
118 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
|
"""A refusal the proxy sends before reading a body reaches the client.
|
|
|
|
The 413 for a body past MAX_BODY_BYTES went out without the body being read, and the
|
|
socket closed at once: the kernel answered the rest of the upload with a reset, and the
|
|
reset destroyed the 413 on its way, so a client sending 20 MB past a 1 MB cap saw a broken
|
|
pipe five times in five (found in review, 2026-09-24). And a client that asks first
|
|
(Expect: 100-continue) was invited to send it all before being refused.
|
|
|
|
The same holds for the 401 the proxy answers since v6.34 when a request does not carry the
|
|
key the engine admits: it is sent before the body is read, so it must arrive whole too, and
|
|
a client that asks first must be refused before it sends."""
|
|
import http.client
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import socket
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
|
|
REPO = pathlib.Path(__file__).resolve().parents[1]
|
|
CAP = 1024 * 1024
|
|
KEY = "k-test" # the key this harness gives the proxy
|
|
|
|
|
|
class TheRefusalArrives(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.home = pathlib.Path(tempfile.mkdtemp(prefix="kp-413-"))
|
|
(cls.home / ".config" / "qwen38").mkdir(parents=True)
|
|
(cls.home / ".config" / "qwen38" / "api-key").write_text(KEY + "\n")
|
|
with socket.socket() as s:
|
|
s.bind(("127.0.0.1", 0))
|
|
cls.port = s.getsockname()[1]
|
|
env = {"PATH": os.environ["PATH"], "HOME": str(cls.home), "PROXY_BIND": "127.0.0.1",
|
|
"UPSTREAM": "http://127.0.0.1:9", "MAX_BODY_BYTES": str(CAP)}
|
|
cls.proxy = subprocess.Popen([sys.executable, str(REPO / "keepalive-proxy.py"), str(cls.port)], env=env,
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
end = time.time() + 20
|
|
while time.time() < end:
|
|
try:
|
|
socket.create_connection(("127.0.0.1", cls.port), timeout=0.5).close()
|
|
break
|
|
except OSError:
|
|
time.sleep(0.1)
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
cls.proxy.terminate()
|
|
cls.proxy.wait(10)
|
|
shutil.rmtree(cls.home, ignore_errors=True)
|
|
|
|
def test_a_client_that_just_sends_reads_the_413(self):
|
|
body = b"x" * (20 * CAP)
|
|
for _ in range(5):
|
|
c = http.client.HTTPConnection("127.0.0.1", self.port, timeout=20)
|
|
try:
|
|
c.request("POST", "/v1/chat/completions", body, {"Content-Type": "application/json",
|
|
"Authorization": f"Bearer {KEY}"})
|
|
r = c.getresponse()
|
|
self.assertEqual(r.status, 413)
|
|
self.assertIn(b"body_too_large", r.read())
|
|
finally:
|
|
c.close()
|
|
|
|
def test_a_client_that_asks_first_is_refused_before_sending(self):
|
|
s = socket.create_connection(("127.0.0.1", self.port), timeout=10)
|
|
try:
|
|
s.sendall(b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\n"
|
|
b"Authorization: Bearer " + KEY.encode() + b"\r\n"
|
|
b"Expect: 100-continue\r\nContent-Length: %d\r\n\r\n" % (20 * CAP))
|
|
head = b""
|
|
while b"\r\n\r\n" not in head:
|
|
piece = s.recv(4096)
|
|
if not piece:
|
|
break
|
|
head += piece
|
|
finally:
|
|
s.close()
|
|
self.assertTrue(head.startswith(b"HTTP/1.1 413"), head[:80])
|
|
|
|
def test_a_client_without_the_key_that_just_sends_reads_the_401(self):
|
|
"""Under the cap, so only the key decides: the refusal goes out before the body is
|
|
read, and the client, still sending, reads it whole, five times in five."""
|
|
body = b"x" * (CAP - 1)
|
|
for _ in range(5):
|
|
c = http.client.HTTPConnection("127.0.0.1", self.port, timeout=20)
|
|
try:
|
|
c.request("POST", "/v1/chat/completions", body, {"Content-Type": "application/json"})
|
|
r = c.getresponse()
|
|
self.assertEqual(r.status, 401)
|
|
self.assertEqual(r.read(), b'{"error":"Unauthorized"}')
|
|
finally:
|
|
c.close()
|
|
|
|
def test_a_client_without_the_key_that_asks_first_is_refused_before_sending(self):
|
|
s = socket.create_connection(("127.0.0.1", self.port), timeout=10)
|
|
try:
|
|
s.sendall(b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\n"
|
|
b"Authorization: Bearer wrong\r\n"
|
|
b"Expect: 100-continue\r\nContent-Length: %d\r\n\r\n" % (CAP - 1))
|
|
head = b""
|
|
while b"\r\n\r\n" not in head:
|
|
piece = s.recv(4096)
|
|
if not piece:
|
|
break
|
|
head += piece
|
|
finally:
|
|
s.close()
|
|
self.assertTrue(head.startswith(b"HTTP/1.1 401"), head[:80])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|