mirror of
https://github.com/hasso5703/alzeo.git
synced 2026-12-05 12:24:28 +01:00
Every refused key was {"error":"Unauthorized"}: none sent, another box's,
one with quotes or two spaces, one under another scheme. Claude Code showed
"Failed to authenticate. API Error: 401 {"error":"Unauthorized"}", opencode
"Error: Unauthorized" (2026-11-30). The answer names the cause and the next
step, in each route's dialect, with WWW-Authenticate: Bearer, and never a byte
of the key nor of what was sent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
4.7 KiB
Python
112 lines
4.7 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 json
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import socket
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
|
import localports # noqa: E402
|
|
|
|
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")
|
|
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, cls.port = localports.spawn(lambda port: subprocess.Popen(
|
|
[sys.executable, str(REPO / "keepalive-proxy.py"), str(port)], env=env,
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL))
|
|
|
|
@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.assertIn("carries none", json.loads(r.read())["error"]["message"])
|
|
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)
|