Skip to content

feat(pr-verify): log the upstream error type for a refused proxy request - #17024

Merged
kyle-sexton merged 2 commits into
mainfrom
feat/key-proxy-log-upstream-error
Nov 18, 2026
Merged

kyle-sexton merged 2 commits into
mainfrom
feat/key-proxy-log-upstream-error

Conversation

@kyle-sexton

Copy link
Copy Markdown
Member

Refs: #16564

Summary

The pr-verify key proxy logged only the status of an upstream error. In every capture and whole-map run since 2026-11-18T02:09Z, the driven session's typed terminal turns got status 400 (the screen showed "Credit balance too low") while print-mode requests on the same key got 200, and nothing readable says why. This logs why an upstream request was refused, and prints it in the two workflows' report step.

The owner approved this on #16564 (Q23, "Yes, go with recommendations."): for non-2xx upstream answers only, the error type, the first 120 characters of the message, and the request's model, anthropic-beta names and max_tokens.

Fix

  • key-proxy.mjs: one extra log line per non-2xx upstream answer, on both listeners and for the count_tokens preflight. It has no status key, so the report's request counts and token totals do not change. The client still receives the upstream answer byte for byte; the capture reads at most 8,192 bytes beside the relay.
  • The message is untrusted text: both keys and any sk-ant- run are replaced by [key] and any UUID by [id] before the cut; control, format and line-separator characters become spaces; :: and ## are broken; then it is cut to 120 code points. error.type, model and each beta name must match a conservative pattern or are logged as unrecognized or dropped; max_tokens only when it is a number. A body that is not the expected JSON shape logs error body unreadable and nothing from it.
  • Both workflows' "Report the key proxy's usage" step prints up to 10 refused requests per listener and how many more there were, filtered again to printable ASCII with :: and ## broken. Each line starts with key proxy. The proxy's own log never reaches the Actions log, so without this step the new line has no reader.
  • Comments that said the log holds no part of a body now say what it holds, including that an upstream message may quote request content.

Not built, on the security review's advice: masking for encoded or escaped forms of the key (the key travels only in the upstream request header and no client holds it), and passing the client key to the socket listener.

Verification

  • node --test --test-concurrency=4 on key-proxy.test.mjs, workflow.test.mjs, whole-map/workflow.test.mjs, check-kill-switch.test.mjs and resolve-config/callers.test.mjs: 499 pass, 0 fail (reported by the builder; not rerun by the session that opened this).
  • New cases: a 400 with a normal body; a hostile message (::error::, ##[group], both keys, an sk-ant- string, control and bidi characters, 5,000 characters); non-JSON, wrong-shape and over-bound bodies; no line for a 2xx; a streamed 529 reaching the client unchanged; a UUID in a 429 message; a key straddling the 120-character cut; and the report step's jq, read out of the workflow and run against a hostile sample log.
  • An independent security review of the first commit found no path that logs a key, no workflow-command injection, no forged count and no change to forwarding, accounting or caps. Its lower-severity findings are what the second commit fixes.

Left out: a real run. The workflows install the proxy from the base branch, so the new line appears only in runs after this merges; the next capture with a typed turn is what shows the cause.

Related

Remaining

🤖 Generated with Claude Code

kyle-sexton and others added 2 commits November 17, 2026 22:36
For every non-2xx upstream answer, on the socket and the loopback
listener alike, key-proxy.mjs writes one more JSON line to its log, for
the main request and for its count_tokens preflight:

- method, path, upstreamPath and upstreamStatus;
- errorType: the body's error.type when it matches ^[a-z_]{1,64}$,
  else "unrecognized";
- errorMessage: the first 120 characters of error.message, after the
  real key, the client key and any sk-ant- run are replaced by [key],
  every control, format and line or paragraph separator character by a
  space, "::" by ": :" and "##" by "# #";
- model when it matches ^[A-Za-z0-9][A-Za-z0-9._[\]-]{0,79}$, else
  "unrecognized";
- betas: the anthropic-beta names matching ^[a-z0-9][a-z0-9._-]{0,79}$,
  others dropped;
- maxTokens only when max_tokens is a number.

Only the first 8,192 bytes of an error body are kept, beside the relay;
a longer body, or one that is not UTF-8 JSON with string error.type and
error.message, logs upstreamError "error body unreadable" instead of
the two error fields. The client still gets the upstream bytes
unchanged. The line has no status field, so the usage report's counts
are unchanged.

The proxy log never reached the Actions log: the usage report printed
only numbers. Both workflows' "Report the key proxy's usage" step now
also prints the fields of the first 20 such lines, cut to printable
ASCII with "::" and "##" broken again.

Refs: #16564

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- key-proxy.mjs inert(): every UUID in an upstream error message is
  replaced by [id] before the 120-character cut, since a rate-limit
  message may name the organization id and the logs are public.
- Both workflows' "Report the key proxy's usage" jq (kept identical):
  up to 10 refused-request lines per listener instead of 20 overall,
  then "N more refused upstream requests not shown" when any were left
  out; a betas value that is not an array prints as empty instead of
  stopping jq.
- Comments: the key-proxy.mjs header says the logged message may quote
  request content; pr-verify-capture.yml's "Start the key proxy" note
  says the log also holds refused requests' error fields; the report
  step's note gives the per-listener limit.
- Tests: a 429 message with lower- and upper-case UUIDs; a real key
  and a client key straddling the 120-character cut leave no prefix
  of either; workflow.test.mjs runs the report jq read out of the
  workflow against a log with hostile fields, a non-array betas and 12
  lines on one listener, asserting single inert lines starting with
  "key proxy", the per-listener limit and the left-out count.

Refs: #16564

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kyle-sexton
kyle-sexton marked this pull request as ready for review November 18, 2026 03:49
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T03:51:26.421970Z 19adc9f Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

claude-review reviewed 19adc9fe9e6c885249a9ee901435a7db4db3e5dc: read 5 of 5 changed file(s); 0 finding(s), 0 as inline comments.

Code review: no findings

This PR makes the pr-verify key proxy write one extra log line for every non-2xx upstream answer. That covers the main forward path and the count_tokens preflight, on both listeners. The line has the error type, the message cut to 120 characters with keys and UUIDs masked, and the request's model, beta names and max_tokens. Both workflows' "Report the key proxy's usage" steps then print those lines, filtered to inert ASCII.

I read the whole diff and the parts of key-proxy.mjs the changed lines call:

  • decoder, defined at line 278, is a fatal UTF-8 decoder, and the parse is wrapped in a try/catch.
  • ctx.body and ctx.headers are set before forward or the preflight run.
  • The listener field is added by the wrapping log (lines 728 and 736).
  • The existing usage-report jq keeps only entries with a numeric .status. The new entries carry upstreamStatus and no status, so the request counts and token totals don't change.

The bounds match on both paths. errorCapture overflows only above 8,192 bytes, and the preflight path uses the same raw.length > ERROR_BODY_BYTES check. The client still receives the relayed body byte for byte. The capture only reads alongside the relay, and the new test with a chunked 529 covers this.

The jq report step is the same in both workflows. The existing shared-step check in whole-map/workflow.test.mjs (line 95) enforces that, so the capture-side jq test, which runs programs[1] against a hostile log, covers both copies. Masking, single-line output and the ::/## breaking are checked by tests; I traced the line-173 expectation by hand against inert(). I ran nothing. The 499-pass result in the PR body is the author's claim and I have not confirmed it.

The PR refs #16564 but closes no issue, so there are no acceptance criteria to check. Security is out of scope here because a separate security lane runs. The PR body notes that a logged upstream message may quote request content; the owner approved that on #16564.

Spend: none beyond the existing runs. The change adds no lane runs, turns or model changes.

Loosened-check verdict: fail (default; the prompt gave no loosened_check_verdict: line); patterns: 0. No loosening found: the test changes only add cases and move the CLIENT_KEY constant to the top of the file.

Verifier dropped 0 of 0 candidates.

@github-actions

Copy link
Copy Markdown
Contributor

claude-security-review reviewed 19adc9fe9e6c885249a9ee901435a7db4db3e5dc: read 5 of 5 changed file(s); 0 finding(s), 0 as inline comments.

Security review: no security issues found

This PR adds one log line to the pr-verify key proxy for each non-2xx upstream answer, and prints those lines in the "Report the key proxy's usage" step of pr-verify-capture.yml and maintenance-verify-whole-map.yml. I read all five changed files in full. The two workflow files match the trusted-path globs, so their changed steps got extra scrutiny.

What I checked:

  • Key leak. In inert() (key-proxy.mjs L421-424), the upstream key and the client key are replaced literally, and so is any sk-ant- run. UUIDs are masked too. All of this happens before the 120-code-point cut, so no part of a key can survive the cut. The key travels only in the upstream request header and no client holds it, so upstream text cannot echo it in some altered form. The model, anthropic-beta and max_tokens fields come from the client request. Each is checked against a strict pattern or a number test, or it is dropped or logged as unrecognized. No other header value is logged.
  • Workflow-command injection into the Actions log. Both jq programs (pr-verify-capture.yml L1568-1572, maintenance-verify-whole-map.yml L1216-1220) run gsub("[^ -~]"; " ") on every output string, which turns newlines and control characters into spaces. They also break :: and ##. Every line starts with key proxy, so no line can begin a workflow command. The step keeps continue-on-error: true, and its if: is unchanged. The proxy also strips control and bidi characters at the source. The log stays in the proxy's own 0750 directory, which the runner user cannot write.
  • Forwarding and accounting. The client still gets the upstream answer byte for byte: errorBody.feed runs after res.write, and the capture is capped at 8,192 bytes. The new line has no status key, so request counts, token metering, caps and count_tokens preflight relay decisions are unchanged.
  • Trust controls. No trusted-trigger check, token request, kill switch, merge hold or permissions: block changes. The diff deletes no instruction-surface rule.
  • Exposure. A refused request's message can quote up to 120 characters of request content. That goes into a public Actions log after key and UUID masking. The owner approved exactly this scope on Add the manual-dispatch workflow for the whole-map live verification pass #16564 (Q23). No credential found in the tree reaches request content.

No manifest <field>: lines: the diff changes no plugin manifest. Spend: none; the change only adds logging and a jq report step.

critical-findings: 0

@kyle-sexton
kyle-sexton added this pull request to the merge queue Nov 18, 2026
Merged via the queue into main with commit 8dcba38 Nov 18, 2026
84 of 85 checks passed
@kyle-sexton
kyle-sexton deleted the feat/key-proxy-log-upstream-error branch November 18, 2026 05:05
@github-actions github-actions Bot added the merge-path: owner-agent Post-merge accounting record (README.md, Merge paths) label Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-path: owner-agent Post-merge accounting record (README.md, Merge paths)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant