fix(server): public endpoints stay public when --api-prefix is set#87
Merged
Merged
Conversation
Bug-hunt round 4 finding on tools/server/server-http.cpp.
The api-key middleware's allowlist (/health, /v1/health, /models,
/v1/models, /, /index.html, /bundle.js, /bundle.css) was matched
against req.path verbatim. With --api-prefix /llama, the routes are
registered at "/llama/health" etc. (handlers attach to
path_prefix + path in server_http_context::{get,post}), so req.path
arrives as "/llama/health" and never matches the un-prefixed
allowlist. Health probes and the static webui assets then 401 under
--api-key, defeating the point of marking them public.
Capture path_prefix into the middleware lambda and strip it from
req.path before the allowlist lookup.
Caller audit:
- Only public_endpoints lookup needs the strip; the api-key validity
check itself is correct regardless of prefix.
- The unrelated bundle-of-tests fix in utils.py adapts the health
poll in ServerProcess.start() so tests can actually use api_prefix.
Tests in test_security.py:
- test_api_prefix_keeps_public_endpoints_public — drives all 4
prefix+public endpoints with --api-key + --api-prefix and asserts
no 401.
- test_api_prefix_still_requires_key_for_private_routes — guards the
inverse: private endpoints under prefix MUST still 401 without a key.
20/20 security tests pass locally (was 18 before).
This was referenced Jun 6, 2026
This was referenced Jun 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Bug-hunt round 4 on `tools/server/server-http.cpp`. The api-key middleware's allowlist for public endpoints (`/health`, `/v1/health`, `/models`, `/v1/models`, `/`, `/index.html`, `/bundle.js`, `/bundle.css`) matched against `req.path` verbatim. With `--api-prefix /llama`, the routes register at `/llama/health` etc. (handlers attach to `path_prefix + path` in `server_http_context::{get,post}`), so `req.path` arrives as `/llama/health` and never matches the un-prefixed allowlist — health probes and the static webui assets then 401 under `--api-key`, defeating the point of marking them public.
Repro
```sh
llama-server --api-key foo --api-prefix /llama --model tiny.gguf
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8080/llama/health # was 401, now 200
```
Fix
Capture `path_prefix` into the middleware lambda alongside `api_keys` and strip it from `req.path` before the allowlist lookup. Three-line change in the middleware, plus the obvious test fixture wiring (`api_prefix` field on `ServerProcess` + health-probe path adapts to the prefix).
Test plan
🤖 Generated with Claude Code