Files
alzeo/tests/test_proxy_client_keys.py
T
Hasan BasbunarandClaude Opus 5 9d6967687c proxy: refuse the request fields SGLang dies on, and close three holes of this route's own (v6.20)
Reading logprobs is what /v1/systemone does, so an audit went looking at what
else can ask for them. OpenAI documents top_logprobs as "an integer between 0
and 20"; SGLang declares it Optional[int] with no constraint and hands it to
logprobs.topk(max_k), where a number past the vocabulary raises "selected index
k out of range" INSIDE the scheduler. One request from one client ends the
engine for everybody, and this box needs about nine minutes to come back
(sglang#40076, open). It was then reproduced the hard way here, by relaying one
such request during a test of the refusal meant to prevent it.

sglang#31597 catalogued the same family in July with reproductions, and both
PRs that bounded them were closed without being merged, which is why the first
had to be reported again in September. Checked one by one in the served
v0.5.19: stop_token_ids indexes a scatter_add_ out of bounds whenever
min_new_tokens > 0; input_ids indexes the embedding, and the validator that
would stop it has zero callers; n becomes parallel_sample_num unbounded. All
reachable from an ordinary chat request.

So the proxy refuses them, on the three relayed routes that carry the logprob
count under three names, element by element when a batch sends a list. The two
id fields need the vocabulary, which the engine publishes nowhere, so it is
asked for the only way it is offered: an out-of-range logit_bias is refused
with a message that names it. One probe an hour, built to be refused, never
reaching the scheduler; it matched the checkpoint's config.json to the digit.
A negative id is refused whatever happens, and a probe that fails stands the
rest of the guard down rather than refuse traffic it cannot judge.

Three defects of this route's own, found in the same audit and in review:
  * the retry that asks a second, wider time went straight to the engine
    without the door, and ignored SYSTEMONE_TOP_K_MAX, the clamp an operator
    sets because their build refuses a wide top-k
  * a percent-encoded non-ASCII byte in any path decoded to something
    http.client encodes as ASCII, raising UnicodeEncodeError, a ValueError no
    arm here catches: an empty reply and a traceback, before any key is checked
  * a lone surrogate outside the state cost a full fan-out and returned 500;
    the check now runs over the whole parsed request, before any inference, and
    never echoes the text it cannot encode
  * a branch still queued for a slot was aborted with a rid the engine had
    never seen, then sent anyway when a slot freed

And one trap written down rather than fixed, because the code was already right
and nothing said why: on the speculative path this lane runs, the engine
divides the logprobs it returns by the request's temperature, and on the
ordinary path it does not. Only the 1.0 this route sends makes the two agree.

keepalive-proxy.py at 84% branch coverage, floor raised to 83.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-22 10:22:37 +02:00

281 lines
11 KiB
Python

#!/usr/bin/env python3
"""v6.16's per-client identity wall: QWEN38_CLIENT_KEYS_FILE turns the proxy
into the component that knows WHO sent the request, or refuses to pretend.
The bug class here is the wall that is silently absent: a keys file that is
missing, empty or malformed must not degrade into "no wall, carry on", and a
request without a listed bearer must not reach the engine riding somebody
else's journal line. The 401 speaks the dialect of the caller it is refusing
(a Claude client parses authentication_error, an OpenAI client parses
error.type), and /health stays open: a monitoring endpoint behind an
identity wall stops being monitoring.
The proxy is imported fresh per scenario (environment first, then exec_module),
and every request crosses a real socket into the real handler: the module
executes where coverage can see it, which matters, because the wall is
exactly the kind of code a floor was set to keep covered.
v6.17's lesson is the engine below: unlike the other proxy test engines, this
one enforces a key, because the real one does. v6.16's wall identified every
labeled client and admitted none (their tokens met the engine's own key check
verbatim), and no test could see it against an engine that lets everything
through. The admission scenarios at the bottom exist so that hole cannot
reopen unnoticed.
"""
import http.server
import importlib.util
import io
import json
import os
import socket
import sys
import tempfile
import threading
import time
import unittest
import urllib.error
import urllib.request
from pathlib import Path
REPO = Path(__file__).resolve().parents[1]
BODY = json.dumps({"model": "x", "messages": []}).encode()
class Engine(http.server.BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
# Unlike the other proxy test engines, this one enforces a key, because
# the real one does: v6.16's tests passed against an engine that lets
# everything through, which is exactly why the verbatim-forwarding hole
# survived them. require_key names the engine's key; last_auth records
# what the proxy actually sent upstream on the last request.
require_key = None
last_auth = None
last_path = None # the path the proxy sent upstream, decoded or not
def log_message(self, *a):
pass
def _admit(self):
Engine.last_auth = self.headers.get("Authorization")
Engine.last_path = self.path
if Engine.require_key and Engine.last_auth != "Bearer " + Engine.require_key:
body = json.dumps({"error": {"message": "engine: bad key",
"type": "invalid_request_error"}}).encode()
self.send_response(401)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
return False
return True
def do_POST(self):
if not self._admit():
return
n = int(self.headers.get("Content-Length") or "0")
self.rfile.read(n)
body = json.dumps({"id": "chatcmpl-test", "choices": []}).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_GET(self):
if not self._admit():
return
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.send_header("Content-Length", "2")
self.end_headers()
self.wfile.write(b"OK")
_fresh = [0]
def fresh_proxy_module(upstream_port, keys_path=None, upstream_key=None):
"""Import the proxy with the environment the scenario calls for.
Module import is when the keys wall is built, so the scenario has to
exist before the exec, and the SystemExit of a refused start surfaces
right here: fail closed is observable at import, not only in a journal.
"""
_fresh[0] += 1
spec = importlib.util.spec_from_file_location(
"kproxy_keys_%d" % _fresh[0], REPO / "keepalive-proxy.py")
mod = importlib.util.module_from_spec(spec)
names = ("UPSTREAM", "QWEN38_CLIENT_KEYS_FILE", "QWEN38_UPSTREAM_API_KEY")
saved = [os.environ.get(k) for k in names]
os.environ["UPSTREAM"] = "http://127.0.0.1:%d" % upstream_port
if keys_path is None:
os.environ.pop("QWEN38_CLIENT_KEYS_FILE", None)
else:
os.environ["QWEN38_CLIENT_KEYS_FILE"] = keys_path
if upstream_key is None:
os.environ.pop("QWEN38_UPSTREAM_API_KEY", None)
else:
os.environ["QWEN38_UPSTREAM_API_KEY"] = upstream_key
try:
spec.loader.exec_module(mod)
finally:
for key, value in zip(names, saved):
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
return mod
def post(port, path, token=None):
headers = {"Content-Type": "application/json"}
if token:
headers["Authorization"] = "Bearer " + token
req = urllib.request.Request("http://127.0.0.1:%d%s" % (port, path), data=BODY, headers=headers)
try:
with urllib.request.urlopen(req, timeout=10) as r:
return r.status, r.read()
except urllib.error.HTTPError as e:
return e.code, e.read()
def get(port, path):
try:
with urllib.request.urlopen("http://127.0.0.1:%d%s" % (port, path), timeout=10) as r:
return r.status
except urllib.error.HTTPError as e:
return e.code
class ClientKeys(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.engine = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Engine)
threading.Thread(target=cls.engine.serve_forever, daemon=True).start()
cls.eport = cls.engine.server_address[1]
@classmethod
def tearDownClass(cls):
cls.engine.shutdown()
cls.engine.server_close()
def setUp(self):
Engine.require_key = None
Engine.last_auth = None
def keys_file(self, text):
fd, path = tempfile.mkstemp(suffix=".json", text=True)
with open(fd, "w") as f:
f.write(text)
self.addCleanup(os.unlink, path)
return path
def live(self, keys_path=None, upstream_key=None):
mod = fresh_proxy_module(self.eport, keys_path, upstream_key)
srv = mod.Server(("127.0.0.1", 0), mod.H)
threading.Thread(target=srv.serve_forever, daemon=True).start()
self.addCleanup(srv.shutdown)
self.addCleanup(srv.server_close)
deadline = time.time() + 10
while time.time() < deadline:
try:
socket.create_connection(("127.0.0.1", srv.server_address[1]), 0.5).close()
break
except OSError:
time.sleep(0.1)
return srv.server_address[1]
def test_the_wall_is_off_by_default_anonymous_still_relayed(self):
port = self.live()
status, _ = post(port, "/v1/chat/completions")
self.assertEqual(status, 200)
def test_unlisted_key_is_401_in_the_openai_dialect(self):
port = self.live(self.keys_file('{"tok-alice":"alice"}'))
for tok in (None, "wrong"):
status, body = post(port, "/v1/chat/completions", tok)
self.assertEqual(status, 401)
self.assertIn("client key", json.loads(body)["error"]["message"])
def test_unlisted_key_is_401_in_the_anthropic_dialect(self):
port = self.live(self.keys_file('{"tok-alice":"alice"}'))
status, body = post(port, "/v1/messages")
self.assertEqual(status, 401)
doc = json.loads(body)
self.assertEqual(doc["type"], "error")
self.assertEqual(doc["error"]["type"], "authentication_error")
def test_listed_key_is_200_and_the_label_rides_the_journal(self):
port = self.live(self.keys_file('{"tok-alice":"alice"}'))
capture = io.StringIO()
real = sys.stderr
sys.stderr = capture
try:
for path in ("/v1/chat/completions", "/v1/messages"):
status, _ = post(port, path, "tok-alice")
self.assertEqual(status, 200)
finally:
sys.stderr = real
self.assertIn("key=alice", capture.getvalue())
def test_an_escaped_path_does_not_walk_past_the_wall(self):
"""SGLang decodes percent-escapes before it routes (checked live: GET /%76%31/models
answers with the model list), so "/%76%31/chat/completions" reached the engine while
this proxy's checks, which all match the raw string, saw a path that was not /v1/.
Without a listed key, and with the engine's own key attached by the proxy."""
Engine.require_key = "engine-K"
Engine.last_auth = None
port = self.live(self.keys_file('{"tok-alice":"alice"}'), upstream_key="engine-K")
for path in ("/v1/chat/completions", "/%76%31/chat/completions",
"/v1/%63%68%61%74/completions", "/%76%31/chat/completions?stream=false"):
status, body = post(port, path)
self.assertEqual(status, 401, (path, body))
self.assertIn("client key", json.loads(body)["error"]["message"])
self.assertIsNone(Engine.last_auth, "not one of them reached the engine")
status, _ = post(port, "/%76%31/chat/completions", "tok-alice")
self.assertEqual(status, 200, "a listed key still gets through the decoded path")
self.assertEqual(Engine.last_path, "/v1/chat/completions",
"and the engine is sent the path this proxy checked")
def test_a_path_that_hides_a_query_separator_is_refused(self):
port = self.live(self.keys_file('{"tok-alice":"alice"}'))
status, body = post(port, "/v1/chat/completions%3Fstream=true", "tok-alice")
self.assertEqual(status, 400, body)
self.assertIn("encoded query", json.loads(body)["error"]["message"])
def test_health_stays_open_without_a_key(self):
port = self.live(self.keys_file('{"tok-alice":"alice"}'))
self.assertEqual(get(port, "/health"), 200)
def test_upstream_key_admits_named_client_as_engine_key(self):
Engine.require_key = "engine-K"
port = self.live(self.keys_file('{"tok-alice":"alice"}'), upstream_key="engine-K")
status, _ = post(port, "/v1/chat/completions", "tok-alice")
self.assertEqual(status, 200)
self.assertEqual(Engine.last_auth, "Bearer engine-K")
def test_verbatim_without_upstream_key_named_client_meets_engine_check(self):
Engine.require_key = "engine-K"
port = self.live(self.keys_file('{"tok-alice":"alice"}'))
status, body = post(port, "/v1/chat/completions", "tok-alice")
self.assertEqual(status, 401)
self.assertNotIn("client key", json.loads(body)["error"]["message"])
self.assertEqual(Engine.last_auth, "Bearer tok-alice")
def test_upstream_key_stays_dormant_when_wall_off(self):
Engine.require_key = "engine-K"
port = self.live(upstream_key="engine-K")
status, _ = post(port, "/v1/chat/completions", "engine-K")
self.assertEqual(status, 200)
self.assertEqual(Engine.last_auth, "Bearer engine-K")
def test_missing_empty_broken_keys_files_refuse_to_be_imported(self):
for path, text in ((None, "/nonexistent/keys.json"), ("{}", "{}"), ("{not json", "{not json")):
real = "/nonexistent/keys.json" if path is None else self.keys_file(text)
with self.assertRaises(SystemExit, msg=repr(text)):
fresh_proxy_module(self.eport, real)
if __name__ == "__main__":
unittest.main(verbosity=2)