Skip to content

fix(gateway): validate provider streams before committing responses - #442

Open
inter0925 wants to merge 1 commit into
astaxie:mainfrom
inter0925:fix/non-sse-provider-stream-redaction
Open

inter0925 wants to merge 1 commit into
astaxie:mainfrom
inter0925:fix/non-sse-provider-stream-redaction

Conversation

@inter0925

Copy link
Copy Markdown

Summary

When a client requests a streaming completion, the gateway forwards the provider's event stream to the client. If the provider answers with a 2xx status but the body is not an SSE stream (for example a single JSON error envelope), the gateway currently:

  • treats the 2xx response as a stream, because only statuses of 400 and above are rejected;
  • forwards the provider's raw bytes to the client, because the SSE decoder keeps lines without an SSE field as raw bytes and credential redaction is only applied to the decoded data of error frames;
  • commits 200 text/event-stream to the client, while the attempt is recorded as a failure (or, after a clean EOF, as a success).

Some providers include sensitive request details in such error bodies, including the provider credential used for the request. In that case the credential configured in TokenHub can be returned to any client that is allowed to call the model. A related case exists for non-streaming chat: a 2xx JSON error envelope is returned to the caller as a successful completion.

This change validates the framing of provider streams before anything is written to the client, never forwards raw provider bytes that are not SSE, and masks provider credentials in every byte that is forwarded.

Related Issue

N/A

Changes

  • Before the first byte is written to the client, check that the first meaningful line of a provider stream is an SSE field. Leading blank lines, a UTF-8 BOM, CRLF line endings, comment heartbeats and common vendor extension fields are accepted. The decision is based on the body, not on Content-Type, so valid SSE labelled as application/json keeps working.
  • A body that is not SSE is rejected with a gateway error (502, existing provider_upstream_error code, fixed message). The client never sees provider text. A bounded, credential-masked copy of the body is kept only in the attempt log; bodies over the bound are masked entirely rather than truncated.
  • After the stream has started, lines without an SSE field are dropped. Provider credential representations are masked in the raw bytes of normal frames, error frames, the final frame at EOF, partially read frames, and output produced by protocol conversions, plugins and stream-transform hooks.
  • The Responses passthrough now commits response headers on the first successful write instead of before reading the provider body. An empty write or a flush before the first write no longer commits a 200. The playground started event is sent with the first output event.
  • Non-streaming Chat Completions, Responses and Anthropic Messages: a 2xx body is treated as an error only when the protocol result field (choices, output, content) is absent and the body carries a structured failure signal (non-null top-level error, success: false, or a numeric code of 400 or above). Bodies that contain the result field are returned unchanged.
  • Failover and health behaviour is kept as close to the previous behaviour as possible. Passthrough streams and non-streaming error envelopes use a neutral, non-retrying disposition, because they previously did not count as provider failures. Translated streams keep their existing retry-on-empty behaviour. Responses keeps its existing committed-stream disposition. Authentication state is never inferred from the provider's message text. No public error codes were added.
  • The subscription-provider stream filter that already dropped unframed lines now uses the same shared guard.
  • Covered paths: OpenAI-compatible, Azure and Kronk chat streams; Responses passthrough and SSE media responses; native and translated Anthropic; Gemini; Dify; Codex-compatible conversions; subscription accounts and the subscription worker; the playground; provider plugins and stream-transform hooks. Binary transports such as audio are not affected, and the check is not placed in the shared HTTP sender.

Type of Change

  • Bug fix
  • New feature
  • Refactor or maintenance
  • Documentation
  • Deployment or configuration

Verification

All commands were run on the branch head, with Go 1.26.9 and golangci-lint v2.12.2 as pinned in CI.

  • gofmt -l . in backend/: no output.
  • go vet ./... in backend/: passed.
  • go test -timeout 30m ./... in backend/: passed (24 packages, 0 failures).
  • golangci-lint run ./... in backend/ (v2.12.2): 0 issues.
  • node --test tools/*.test.mjs: 205/205 passed.
  • node tools/check-source-lines.mjs, node tools/check-env-contract.mjs, node tools/check-ui-translations.mjs, node tools/check-doc-translations.mjs --base v0.11.0 --head HEAD: passed.
  • git diff --check: passed.
  • New tests use in-process fake upstreams and cover:
    • rejection on all three public streaming endpoints, for both text/event-stream and application/json, with and without trailing newlines;
    • the fixed client error, and masked credentials in the attempt log;
    • a successful follow-up request to the same provider;
    • valid SSE labelled with the wrong Content-Type; BOM, heartbeats, CRLF and extension fields; unframed lines inside a stream;
    • usage accounting and [DONE];
    • structured error detection, and precedence of the protocol result field.
  • Two existing assertions that expected an empty Codex stream to return 200 now expect the uncommitted failure. The quota release, TTFB and interruption metric assertions are unchanged and pass.
  • On Go 1.26.3, two SMTP tests fail on the unchanged v0.11.0 base as well, because they expect diagnostics from a newer standard library. They pass on Go 1.26.9 and are unrelated to this change.
  • Frontend checks and e2e were not run because no frontend files changed.

Compatibility, Security, and Operations

  • Security: prevents provider credentials and raw provider error bodies from reaching API clients through streaming and non-streaming responses.
  • API behaviour: for a 2xx provider response that is not SSE, a streaming client now receives a 502 JSON error before any stream bytes, instead of a 200 event stream containing the provider's raw body. A non-streaming 2xx error envelope without a result field now returns 502 instead of 200. Valid streams and completions are unchanged.
  • The Responses passthrough commits 200 on the first provider output instead of before reading the provider body. Clients that relied on receiving headers before the first event will receive them slightly later.
  • No configuration, environment variable, database or migration changes. Rollback is a normal revert.

Checklist

  • The PR title and body are written in English.
  • Tests were added or updated for behavior changes, or the reason they are unnecessary is documented.
  • No credentials, local .env files, databases, backups, or runtime logs are included.
  • Environment variable changes are synchronized across examples, Compose, start.sh, and deployment documentation where applicable. (No environment variable changes.)
  • Shared user-facing behavior is documented consistently in English, Simplified Chinese, and Japanese where applicable. (No user-facing documentation changes.)
  • data/model-catalog.yaml remains tracked and catalog changes were reviewed where applicable. (Not changed.)
  • git diff --check passes.

Check SSE framing before forwarding provider output and mask credentials
in passthrough and rewritten frames. Keep bounded diagnostics in attempt
logs and reject JSON error envelopes that lack a protocol result field.
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