Skip to content

fix: close the second audit round across auth, usage and upstream - #18

Merged
dinhdev666 merged 1 commit into
mainfrom
fix/close-the-second-audit-round
Dec 5, 2026
Merged

dinhdev666 merged 1 commit into
mainfrom
fix/close-the-second-audit-round

Conversation

@dinhdev666

Copy link
Copy Markdown
Owner

What changed

Closes an independent four-way audit (adapters, upstream runtime, HTTP/auth/media,
tests/CI/docs) of main at b8d6941. The security finding is P1; the others are
silent-wrong-behaviour bugs and spec mismatches.

  • Auth path classification (high). Authentication classified a request by
    request.url, which keeps the caller's bytes, while Fastify's router matches on
    the percent-decoded path. /%61dmin/config therefore dispatched to
    GET /admin/config but was authenticated as a general route: with
    OPENCODE_ADMIN_API_KEYS set, any tenant key could restart the shared upstream,
    clear the session cache, refresh the catalogue and read /admin/config. All
    classification (auth, rate-limit bucket, Anthropic error envelope) now uses the
    matched route.
  • Token accounting (high). The upstream reports input as the non-cached
    count; the OpenAI surfaces reported it as prompt_tokens, so
    prompt_tokens_details.cached_tokens could exceed prompt_tokens. The cache
    buckets are folded into the prompt now (verified against the installed CLI's own
    cost helper). /v1/messages keeps the Anthropic convention.
  • Turn scoping (high). The event stream is per session, so a terminal frame
    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 now
    scoped to the turn by the envelope stamp.
  • Media roles (medium). Media on a non-user message was downloaded, charged to
    the budget and silently dropped; it is now 400 invalid_request.
  • /v1/completions prompts (medium). A multi-element array was silently joined
    into one completion; it is now 400 unsupported_parameter.
  • Upstream robustness (medium). An empty or non-object 2xx body is now
    upstream_invalid_json instead of a TypeError/HTTP 500; a non-object SSE frame
    no longer tears down the shared stream; an abort during resolve() no longer
    issues a billed turn.
  • Smaller items, each with a regression test: Anthropic output_config;
    streamed/buffered thinking-signature agreement; json_schema.strict and invalid
    reasoning_effort rejected instead of ignored; /v1/responses echoes
    text.format and rejects an empty model; /v1/completions accepts top_p;
    service_tier recorded and named in x_opencode.unapplied; SSE field values
    CR/LF-safe; isPrivateHost handles zone ids, unmapped IPv6 literals and
    trailing-dot names; a remote attachment origin no longer retains credentials or
    the query; OPENCODE_REASONING_FIELD warns on a typo; the docs-consistency guard
    pins the documented service/version.

No dependency was added and no security limit was widened.

How it was verified

pnpm typecheck                          # exit 0
pnpm build                              # exit 0, 78 files in dist/
pnpm test                               # 30 files, 767 passed | 2 skipped
pnpm exec vitest run test/docs-consistency.test.ts   # 16 passed
pnpm smoke:dist                         # dist/index.js answered /health with version 1.0.0
pnpm lint:commits                       # exit 0

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 the
targeted files (25 failures), and the new test/auth-surface.test.ts reproduces the
admin-tier escalation directly (general key on /%61dmin/config → 401, was 200).

Checklist

  • pnpm typecheck passes.
  • pnpm build passes.
  • pnpm test passes.
  • Commits pass pnpm lint:commits (Conventional Commits).
  • New behaviour is covered by a test; a bug fix has a regression test.
  • docs/ARCHITECTURE.md and the relevant reference doc are updated when a
    contract, an environment variable or a wire format changed.
  • No secrets, no debug logging, no commented-out code.

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.
@dinhdev666
dinhdev666 merged commit a53eef9 into main Dec 5, 2026
5 checks passed
@dinhdev666
dinhdev666 deleted the fix/close-the-second-audit-round branch December 5, 2026 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant