From 226194ef336cedd3142b361b6bbfa96848a46ef0 Mon Sep 17 00:00:00 2001 From: Hasan Basbunar Date: Fri, 13 Nov 2026 16:46:12 +0100 Subject: [PATCH] v1.48.0: the lowercase bearer the door admits reaches llama.cpp spelled as the engine spells it (proxy v6.56) Measured on the live lane 2026-11-12: "bearer " passes the door (its rule is SGLang's, the scheme read in any case, measured 2026-10-08) and llama-server compares the "Bearer " prefix exactly and 401s it, while every SGLang and vLLM lane served the same client. _upstream_auth now normalizes the scheme after the door has byte-for-byte compared the key, and only for the shape the door admits: one space, the key after it. Test: tests/test_proxy_llamacpp_guard.py (its fake llama-server now checks the prefix as the real one does; the new test fails 401 without the fix, passes with it). prod_checks (live tool) expects the one door rule on every engine again. Co-Authored-By: Claude Code --- CHANGELOG.md | 5 +++++ keepalive-proxy.py | 21 +++++++++++++++-- tests/test_proxy_llamacpp_guard.py | 36 +++++++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b5f32b8..2f5866ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,11 @@ its `/health`, its boot read in its container's log, its requests and its draft' its name (llama.cpp has neither route; closing the connection stops a generation, which the proxy does, as on vLLM), and System One points at the SGLang lanes, as the proxy does. Its mark is Unsloth's. +**Proxy v6.56.** A client's bearer reaches the engine spelled the way the engine spells it: the door reads the scheme +in any case, as SGLang's own check does (measured 2026-10-08), and llama.cpp compares the `Bearer ` prefix exactly, so +a lowercase `bearer ` was 401 on the llama.cpp lane alone while every SGLang and vLLM lane served it (measured +2026-11-12). The door has already compared the key byte for byte, so the relay normalizes the scheme and nothing else. + **Proxy v6.55.** llama.cpp's engine is told by its `owned_by` (`llamacpp`): a client that leaves closes the upstream, the window is read from `/v1/models` (`meta.n_ctx`, one request's) and the pool is its slots' windows together (`/props` `total_slots`), so no prompt is taken for a wedge where the engine refuses past its window itself. A prompt is counted diff --git a/keepalive-proxy.py b/keepalive-proxy.py index a3231460..a89decdf 100755 --- a/keepalive-proxy.py +++ b/keepalive-proxy.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Keepalive proxy in front of SGLang, vLLM or llama.cpp (v6.55). No content logging, and the only +"""Keepalive proxy in front of SGLang, vLLM or llama.cpp (v6.56). No content logging, and the only rewriting is the tool-schema guard (role 4); two routes, POST /v1/systemone (role 5) and the operator's POST /proxy/stop (role 7), are answered here instead of relayed. @@ -60,6 +60,13 @@ Ten roles, nothing else: shared: a conversation stays on the box that holds its prefix, a new one goes to the box with the fewest requests in flight (v6.52, docs/pair.md). +v6.56: a client's bearer reaches every engine spelled the way the engine spells it. The door reads the scheme in any +case, as SGLang's own check does (measured 2026-10-08: "bearer " admitted) and v6.38 only stripped the whitespace +around the value, but llama.cpp compares the "Bearer " prefix exactly: the same well-formed key sent lowercase came +back 401 "Invalid API Key" on the Unsloth lane (measured 2026-11-12) while every SGLang and vLLM lane served it. The +door has already compared the key byte for byte (engine_admits), so the relay normalizes the scheme alone, and only +when the value is exactly what the door admits: one space, the key after it. + v6.55: the Qwen3.8 Flash-Next Unsloth GGUF lane (v1.48), served by llama.cpp's llama-server, which names itself "llamacpp" on /v1/models. Like vLLM it has none of SGLang's info, load or abort routes, and it stops a request whose connection closes, streamed or not (its server-context.cpp and server-queue.cpp, b12291): closing is its abort there too @@ -1228,7 +1235,17 @@ def _upstream_auth(handler): value = handler.headers.get("Authorization") # v6.38: without the whitespace around it, which is not part of a header's value (RFC 9110, # 5.5): SGLang reads it so, vLLM compares the value as it comes - return value.strip(" \t") if value else value + if not value: + return value + value = value.strip(" \t") + # v6.56: llama.cpp compares the "Bearer " prefix exactly (measured on the Unsloth lane, + # 2026-11-12), where the door's rule, SGLang's, admits the scheme in any case. The door has + # already compared the key byte for byte (engine_admits: one space, the key after it), so + # the scheme's spelling is the one thing still free here: name it as the engine spells it. + head, sep, rest = value.partition(" ") + if sep and rest and " " not in rest and head != "Bearer" and head.lower() == "bearer": + value = "Bearer " + rest + return value # The one route the identity wall leaves open: monitoring. Every other route needs a diff --git a/tests/test_proxy_llamacpp_guard.py b/tests/test_proxy_llamacpp_guard.py index 656e63f5..c5d96929 100644 --- a/tests/test_proxy_llamacpp_guard.py +++ b/tests/test_proxy_llamacpp_guard.py @@ -52,7 +52,7 @@ class Engine(http.server.BaseHTTPRequestHandler): """llama-server as the proxy meets it: its models, props and metrics, its two count routes, and generations.""" protocol_version = "HTTP/1.1" window, slots = 262144, 2 - generated, counted = [], [] + generated, counted, auths = [], [], [] def log_message(self, *a): pass @@ -95,6 +95,15 @@ class Engine(http.server.BaseHTTPRequestHandler): self._json(200, {"input_tokens": tokens(text_of(body.get("messages") or [])), "object": "response.input_tokens"}) return + auth = self.headers.get("Authorization") + if auth != "Bearer test-key": + # llama.cpp compares the "Bearer " prefix exactly (measured on the v1.48 lane, + # 2026-11-12: a lowercase "bearer ", which the door admits as SGLang's own + # rule does, came back 401 "Invalid API Key") + self._json(401, {"error": {"code": 401, "message": "Invalid API Key", + "type": "authentication_error"}}) + return + Engine.auths.append(auth) Engine.generated.append(self.path) self._json(200, {"id": "x", "object": "chat.completion", "model": SERVED, "choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"}, @@ -131,15 +140,16 @@ class LlamaCppGuard(unittest.TestCase): Engine.window, Engine.slots = 262144, 2 Engine.generated.clear() Engine.counted.clear() + Engine.auths.clear() self.mod.FLASH_PROMPT_CEILING_TOKENS = 0 self.mod.invalidate_pool() def share(self, pool): return int(pool * (1.0 - self.mod.OVERSIZE_MARGIN_FRAC)) - def post(self, path, doc): + def post(self, path, doc, auth="Bearer test-key"): req = urllib.request.Request(f"http://127.0.0.1:{self.port}{path}", data=json.dumps(doc).encode(), - headers={"Content-Type": "application/json", "Authorization": "Bearer test-key"}) + headers={"Content-Type": "application/json", "Authorization": auth}) try: with urllib.request.urlopen(req, timeout=30) as r: return r.status, r.read() @@ -214,6 +224,26 @@ class LlamaCppGuard(unittest.TestCase): finally: self.mod._SERVED["names"] = saved + def test_a_lowercase_bearer_scheme_reaches_the_engine_as_the_engine_spells_it(self): + # the door reads the scheme in any case (its rule is SGLang's, measured 2026-10-08: + # "bearer " admitted), and v6.38 only stripped the whitespace around the value. + # llama.cpp is stricter than SGLang there: the same well-formed key sent as + # "bearer " answered 401 on the live lane (2026-11-12), where every other lane + # served it. The door has already compared the key byte for byte (engine_admits), so + # the one thing the relay may still change is the scheme's spelling: name it the way + # the engine spells it, which is what the door accepted. + st, out = self.post("/v1/chat/completions", + {"model": SERVED, "messages": [{"role": "user", "content": "hi"}]}, + auth="bearer test-key") + self.assertEqual(st, 200, out[:120]) + self.assertEqual(Engine.auths[-1], "Bearer test-key") + # a wrong key under any spelling stays refused at the door, before the relay + st, _ = self.post("/v1/chat/completions", + {"model": SERVED, "messages": [{"role": "user", "content": "hi"}]}, + auth="bearer wrong") + self.assertEqual(st, 401) + self.assertEqual(Engine.generated, ["/v1/chat/completions"], "only the admitted one reached the engine") + if __name__ == "__main__": unittest.main()