Repository navigation
fix: close the second audit round across auth, usage and upstream - #18
Merged
Merged
Conversation
An independent four-way audit of the gateway found one exploitable defect, three silent-wrong-behaviour bugs and smaller spec mismatches. Security: authentication classified a request by its raw URL, so /%61dmin/config was authenticated as a general route while the router still dispatched GET /admin/config. With OPENCODE_ADMIN_API_KEYS set, any tenant key could restart the shared upstream, wipe the sticky-session cache, refresh the catalogue and read /admin/config. Classification now uses the matched route, for authentication, the rate-limit bucket and the Anthropic error envelope. Correctness: - OpenAI prompt_tokens/input_tokens reported the non-cached input only, so prompt_tokens_details.cached_tokens could exceed prompt_tokens. The cache buckets are now folded into the prompt, matching the upstream's own cost arithmetic; /v1/messages keeps the Anthropic convention. - The event subscription is per session, so a terminal frame still in flight from a turn the gateway had interrupted could finalise the next turn on the same sticky session with an empty answer reported as a normal stop. Frames are now scoped to the turn by the envelope stamp, and only when it is recognisably epoch milliseconds: an assistant message id cannot scope a turn because one turn spans one assistant message per step. - Media on a non-user message was downloaded, charged to the media budget and then silently dropped, because only user turns forward attachments. It is now a 400 invalid_request naming that surface's field. - A multi-element /v1/completions prompt was silently joined into one completion; it is now rejected instead of returning an answer that matches no prompt. - An empty or non-object 2xx upstream body (including a literal null) became a bare TypeError and an HTTP 500 outside the ApiError taxonomy; it is now an upstream_invalid_json failure. - A non-object JSON SSE frame crashed the shared event stream, dropped every frame behind it and was mislabelled as an idle reconnect. Smaller items, each with a regression test: Anthropic output_config maps to reasoning and the response format; the thinking signature is keyed on the gateway message id on both paths so streamed and buffered agree; json_schema strict and an invalid reasoning_effort are rejected instead of ignored; /v1/responses echoes text.format and rejects an empty model; /v1/completions accepts top_p; service_tier is recorded and named in x_opencode.unapplied; SSE field values are CR/LF-safe; isPrivateHost handles IPv6 zone ids, unmapped IPv6 literals and trailing-dot names; a remote attachment origin no longer retains credentials or the query string; OPENCODE_REASONING_FIELD warns on a typo; the docs-consistency guard pins the documented service and version; and docs/VERIFICATION.md gains a current-state addendum. No dependency was added and no security limit was widened.
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.
What changed
Closes an independent four-way audit (adapters, upstream runtime, HTTP/auth/media,
tests/CI/docs) of
mainatb8d6941. The security finding isP1; the others aresilent-wrong-behaviour bugs and spec mismatches.
request.url, which keeps the caller's bytes, while Fastify's router matches onthe percent-decoded path.
/%61dmin/configtherefore dispatched toGET /admin/configbut was authenticated as a general route: withOPENCODE_ADMIN_API_KEYSset, any tenant key could restart the shared upstream,clear the session cache, refresh the catalogue and read
/admin/config. Allclassification (auth, rate-limit bucket, Anthropic error envelope) now uses the
matched route.
inputas the non-cachedcount; the OpenAI surfaces reported it as
prompt_tokens, soprompt_tokens_details.cached_tokenscould exceedprompt_tokens. The cachebuckets are folded into the prompt now (verified against the installed CLI's own
cost helper).
/v1/messageskeeps the Anthropic convention.still in flight from an interrupted turn could finalise the next turn on the same
sticky session with an empty answer reported as a normal
stop. Frames are nowscoped to the turn by the envelope stamp.
the budget and silently dropped; it is now
400 invalid_request./v1/completionsprompts (medium). A multi-element array was silently joinedinto one completion; it is now
400 unsupported_parameter.upstream_invalid_jsoninstead of aTypeError/HTTP 500; a non-object SSE frameno longer tears down the shared stream; an abort during
resolve()no longerissues a billed turn.
output_config;streamed/buffered thinking-signature agreement;
json_schema.strictand invalidreasoning_effortrejected instead of ignored;/v1/responsesechoestext.formatand rejects an emptymodel;/v1/completionsacceptstop_p;service_tierrecorded and named inx_opencode.unapplied; SSE field valuesCR/LF-safe;
isPrivateHosthandles zone ids, unmapped IPv6 literals andtrailing-dot names; a remote attachment
originno longer retains credentials orthe query;
OPENCODE_REASONING_FIELDwarns on a typo; the docs-consistency guardpins the documented
service/version.No dependency was added and no security limit was widened.
How it was verified
Every fixed item has a regression test that fails on the pre-change code; the
adapter workstream confirmed this by stashing its
src/changes and re-running thetargeted files (25 failures), and the new
test/auth-surface.test.tsreproduces theadmin-tier escalation directly (general key on
/%61dmin/config→401, was200).Checklist
pnpm typecheckpasses.pnpm buildpasses.pnpm testpasses.pnpm lint:commits(Conventional Commits).docs/ARCHITECTURE.mdand the relevant reference doc are updated when acontract, an environment variable or a wire format changed.