Escape restored values spliced into streamed tool arguments - #91
Merged
Conversation
A streamed tool-call argument fragment is JSON text one level deeper than the SSE record that carries it: the client concatenates every partial_json and parses the result. restoreJsonText escapes a restored value for its string context precisely because of this, but restoreToolArgText returned the value raw. So a restored value containing a newline, `"`, or `\` produced invalid JSON in the reconstructed tool input. A PEM key, a quoted password, or any multi-line file content the agent was writing back would corrupt the call — `JSON.parse` fails with "Bad control character in string literal". Under the default `detected` policy this reaches exactly the values that are supposed to round-trip: content-derived detections the agent already read locally. Escape in restoreToolArgText, and route tool fragments through it under every policy. `all` previously skipped the withhold bookkeeping and fell through to the blanket restore, so it corrupted the same way; escaping is a property of the JSON context, not of the withhold policy. The existing tool-argument tests all used values with no JSON-special characters, which is why this went unnoticed. Both new tests parse the reconstructed argument rather than substring-matching it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Open
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.
@coderabbitai ignore
Confirms and fixes the corruption hazard I flagged as unconfirmed earlier in this series. It was real, and the mechanism is a one-line asymmetry between two sibling functions.
The bug
A streamed tool-call argument fragment is JSON text one level deeper than the SSE record carrying it — the client concatenates every
partial_jsonand parses the result.restoreJsonTextescapes a restored value for its string context precisely because of this (vault.ts, response-body path).restoreToolArgTextreturned the value raw.So a restored value containing a newline,
", or\produced invalid JSON in the reconstructed tool input:A PEM key, a quoted password, or any multi-line file content the agent was writing back would corrupt the call. Under the default
detectedpolicy this hits exactly the values that are supposed to round-trip — content-derived detections the agent already read locally.Why it went unnoticed
Every existing tool-argument test used values with no JSON-special characters (
alpha-registry-eu-west-1x,bravo-content-hostname-9z). The escaping was never exercised. Both new testsJSON.parsethe reconstructed argument instead of substring-matching it.Fix
Escape in
restoreToolArgText, and route tool fragments through it under every policy.allis the second half of this: it setswithhold: policy !== "all", so it skipped the withhold bookkeeping entirely and fell through to the blanket restore — corrupting the same way by a different path. Escaping is a property of the JSON context, not of the withhold policy, so it can't hang off the withhold flag.withheldstaysundefinedunderallso the deep-sweep behaviour below is unchanged.Scope
Streaming path only. The buffered path already went through
restoreJson→restoreJsonTextand was correct. Registry secrets are unaffected under the default policy because they're withheld rather than restored.pnpm check: 51 files, 565 tests, all pass (563 + 2 new).🤖 Generated with Claude Code