Skip to content

Survive binary tunnel frames, bad telemetry timestamps and unbounded fs_read - #370

Open
t11z wants to merge 5 commits into
mainfrom
claude/hopeful-newton-np020m
Open

t11z wants to merge 5 commits into
mainfrom
claude/hopeful-newton-np020m

Conversation

@t11z

@t11z t11z commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

What & why

Scheduled crash-fuzzing run. This run targeted surfaces the earlier runs did not reach: hostile frames on /agent/ws (binary messages, mutated register / telemetry / log / response frames) followed by a sweep of every GET /api route, the MCP tools with junk arguments and a fake agent answering with mutated fixtures, and the Rust agent's handlers, frame handling and parsers. The session may push to one branch only, so the three fixes share this PR, one commit each.

1. A binary WebSocket message on /agent/ws raises KeyError

Starlette's receive_text assumes a text message:

KeyError: 'text'
  tunnel.py  _handshake_conn / _handshake_signed / _serve
  starlette/websockets.py  receive_text

This is reachable before authentication (first frame, and the reply to the challenge) and from any connected agent. The exception left the endpoint unhandled.

Fix: a small _receive_text helper returns None for a binary message. A binary first frame closes 4400, a binary auth reply closes 4401, and a binary frame on an established tunnel is dropped like any other unparseable frame.

2. A telemetry frame with a bad collected_at breaks the fleet views

collected_at was stored as whatever string the agent sent.

  • An unparseable value (" ", "x", "2026-02-30T00:00:00Z"), or one with no UTC equivalent ("0001-01-01T00:00:00+23:59"), made GET /api/fleet and GET /api/agent/{id}/availability answer 500 for every host: ValueError / OverflowError in availability._parse, called on MIN(collected_at).
  • A value without a UTC offset inside the history window made GET /api/agent/{id}, /api/fleet/trend and /api/today answer 500: TypeError: can't subtract offset-naive and offset-aware datetimes in health_rules._rule_win_update.

Both last until the row ages out of retention.

Fix: the Telemetry model fails validation unless collected_at is an RFC 3339 timestamp with an offset, so the tunnel drops the frame and keeps the connection (its existing behaviour for an invalid frame). Rows already stored are tolerated on read: availability.load_many ignores an unusable first-seen candidate, and evaluate_snapshot reads an offset-less now as UTC. docs/protocol.md states the format. The agent already emits this form (util::now_rfc3339), and no frame shape changes.

3. Agent: fs_read is unbounded; file-size sums overflow

  • fs_read called std::fs::read and applied READ_CAP afterwards. A 1 GiB file cost 1 GiB of memory to return 256 KiB. /dev/zero grew until the process was OOM-killed. A FIFO with no writer blocked the call forever. Fix: refuse anything that is not a regular file (bad_args) and read at most one byte past the cap.
  • cleanup.rs (reclaimed bytes) and web_activity.rs (merged hits) added values that come from the file system and from a user-writable browser database with unchecked +=. Three 8 EiB sparse files in a swept tmpfs root, or a visit_count near i64::MAX, panic in debug builds and wrap in release. Fix: saturating_add.

Regression tests

Fixed inputs only; no fuzz harness is committed.

  • test_tunnel_malformed_frame.py: three binary-frame cases through the real ASGI app.
  • test_hostile_input_no_500.py: rejected and accepted collected_at values, plus stored-row cases for both read paths.
  • Agent: read_stops_at_the_cap_whatever_the_file_size, read_refuses_a_directory, read_refuses_a_device_that_never_ends, reclaimed_bytes_saturate_on_huge_sparse_files, merged_hits_saturate.

The server tests and the two saturation tests were checked to fail without the fix. The fs_read tests were not run against the old code, because there they exhaust memory.

Found, not fixed here

  • Synchronous tool handlers (fs_*, diag_*, net_config, screen_capture, telemetry_collect) run on tokio worker threads in dispatch::run. As many blocked calls as the host has cores stall the heartbeat and the writer. Moving them to spawn_blocking touches every dispatch arm, so it is left for a separate change.
  • web_activity reads a browser history database without a row cap and drops the whole profile on one malformed row; browser_extensions reads manifests without a size cap.
  • A policy frame's regexes are compiled on the tunnel read loop with no bound on count or size.
  • webfilter fails on a hosts file that is not valid UTF-8.

Fuzzing summary

Checklist

  • Tests pass: pytest -q (server): 4274 passed, 7 skipped. cargo test (agent): 593 passed
  • Lint/format clean: ruff check on kenny_server and the touched tests, cargo fmt --check, cargo clippy --all-targets -- -D warnings
  • If the wire contract changed: no frame or tool shape changed and PROTOCOL_VERSION is not bumped. docs/protocol.md gains one paragraph stating the collected_at format the fixtures and the agent already use. /contract-check was not run
  • If the change is architectural: n/a, bug fixes with no boundary moved
  • Docs updated where relevant: docs/protocol.md
  • Commits are signed off (DCO): not signed off. The maintainer needs to add the sign-off

Generated by Claude Code

Starlette's receive_text raises KeyError on a binary message. On /agent/ws
that was an unhandled exception, reachable before authentication (first
frame, auth reply) and from any connected agent. A binary first frame now
closes 4400, a binary auth reply closes 4401, and a binary frame on an
established tunnel is dropped like any other unparseable frame.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QcqxZNsBPqj6rX5XZujCNu
…nstant

collected_at was stored as whatever string the agent sent. One snapshot
with an unparseable value made GET /api/fleet and
/api/agent/{id}/availability answer 500 for every host (ValueError or
OverflowError in availability._parse on MIN(collected_at)); one without a
UTC offset made /api/agent/{id}, /api/fleet/trend and /api/today answer 500
(naive minus aware datetime in the health rules).

The telemetry frame now fails validation on such a value, so the tunnel
drops it and keeps the connection. Rows already stored are tolerated on
read: availability ignores an unusable first-seen candidate and the health
rules read an offset-less instant as UTC. docs/protocol.md states the
format; no frame shape changes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QcqxZNsBPqj6rX5XZujCNu
fs_read read the whole target into memory before applying READ_CAP and
opened whatever the path named: a multi-GB file cost its full size, a
character device such as /dev/zero grew until the agent was OOM-killed, and
a FIFO blocked the call forever. It now refuses anything that is not a
regular file (bad_args) and reads at most one byte past the cap.

The cleanup sweep's reclaimed-byte total and web_activity's merged hit
count summed values taken from the file system and from a user-writable
browser database with unchecked addition, which panics in debug builds and
wraps in release. Both saturate.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QcqxZNsBPqj6rX5XZujCNu
…ropped

The health_rules.py change in this branch moves no threshold and no status:
it reads an offset-less evaluation instant as UTC. Nothing in the dashboard
views or their screenshots changes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QcqxZNsBPqj6rX5XZujCNu
wait_times_out_while_another_process_runs_target copied /bin/sleep to a
file named kenny-wait-test-<pid> and ran the copy. The Linux CI job fails
that test on every run that reaches it (also on an unrelated dependency
PR): the child is gone before the wait looks for it. Where sleep is an
applet of a multi-call binary, the binary picks the applet by the name it
is run as, so the renamed copy exits at once.

The copy now keeps the name `sleep` inside a private directory, which runs
the same on a standalone sleep and on a multi-call one. Test-only; no
agent behaviour changes. [skip-doc-drift]

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QcqxZNqpHNGk5hJzgn6CWt
Signed-off-by: Claude <noreply@anthropic.com>
(cherry picked from commit 3ab1a26)

t11z commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

The kenny-agent (Rust) job failed on handlers::agent_update::tests::wait_times_out_while_another_process_runs_target ("must report not-free while a process still runs target"). This PR's diff does not touch agent_update.rs; the other 592 agent tests passed in that run, including the five added here.

The same test already fails on the Linux runner for other PRs, and #364 carries a fix for it (the test copied sleep under a different name, which exits at once where sleep is a multi-call applet). I cherry-picked that commit (3ab1a26, test-only) onto this branch as 4096aaf so CI can go green here. It is the identical change, so whichever of the two PRs merges first, the other merges cleanly. cargo test locally with it: 593 passed.


Generated by Claude Code

This branch has not been deployed

No deployments
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.

2 participants