feat(security): stream uploads end to end, bound request bodies (#258) #299

Merged
sharang merged 1 commits from feat/streamed-uploads into main 2026-09-21 11:37:19 +00:00
Owner

Second half of #258 (MVP-0.5); with #297 merged, this completes it. Closes #258.

The problem

A file upload of up to 512 MiB was held in memory about five times over:

Hop Before Now
Browser file read into WASM memory (read_bytes) only the file handle is kept; the File itself is the request body (Dioxus FileStream, browser-native streaming)
Browser → dashboard JSON server function carrying Vec<u8> — one JSON number per byte, roughly 4× the file POST /api/uploads/{id}?kind&plc_format, raw application/octet-stream
Dashboard → agent a second copy for the multipart request the body is forwarded as the multipart file part via reqwest::Body::wrap_stream
Agent whole field buffered (field.bytes()), then written synchronously written to disk chunk by chunk, 413 past 512 MiB

A few concurrent uploads could exhaust an 8 GB node. Now no hop holds the file.

Cleanup on failure: on the agent, the stored file is owned by a guard until the artifact record references it. An oversized, malformed or kind-less upload leaves nothing on disk.

Request-body limits

The 512 MiB limit sat on every route of both services, so any JSON endpoint could be made to buffer half a gigabyte. It is now scoped:

Route Limit
agent POST /api/v1/targets/{id}/artifacts/upload 512 MiB + multipart framing (streamed)
agent POST /api/v1/werkbank/jobs/complete 32 MiB (a job's findings as JSON; behind a runner key)
dashboard webhook proxy 25 MB (GitHub's push-payload cap)
everything else axum's 2 MiB default

How it was decided

At the design fork I asked for a structured second opinion (TypeSafe Jev, neutral description, no code): stream through the dashboard, agent writes chunks to disk scored 0.98, over a public direct-to-agent upload endpoint or fixing only the agent. The agent-only fix scored 0.10, because the dashboard's 4× copy was the bigger problem. Splitting this out of #297 scored 0.65, a weak yes, which matched my own call.

Verification

  • End to end in a real browser: agent + dashboard (dx build) + Chromium via Playwright. A 3 MiB firmware image went through the onboarding wizard as content-type: application/octet-stream, content-disposition: attachment; filename="firmware-test.bin", x-content-size: 3145728, with no JSON encoding, and landed on disk byte-identical (SHA-256 match). The wizard then showed the firmware scans as applicable, and detection ran on the file.
  • New integration tests, against a live Mongo:
    • a 3 MiB upload, over the old 2 MiB default, streams and attaches with the exact size;
    • a 3 MiB JSON body to an ordinary route now gets 413;
    • an upload without kind returns 400 and leaves no file behind.
  • Unchanged suites: cargo test --lib 357 + 71 + 9 + 28 and dashboard 21 pass. cargo clippy -D warnings is clean for agent, core, MCP, werkbank-exec, control-map, and both dashboard builds (server and WASM).
  • Also fixed: the two manual_checked_ops lints in attack_chain/view.rs that newer clippy reports, so dashboard clippy is clean locally.

Two notes

  • The e2e suite is stale on main: 14 of 15 red in parallel, 9 red single-threaded. It's test rot from before per-tenant databases, not these changes, and CI doesn't run it. Filed as #298. The onboarding tests, including the three new ones, pass single-threaded.
  • Existing wizard behaviour: the review step lists only reference artifacts, not staged files, although they upload fine after creation. Not touched here.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p

Second half of #258 (MVP-0.5); with #297 merged, this completes it. Closes #258. ## The problem A file upload of up to 512 MiB was held in memory about five times over: | Hop | Before | Now | |---|---|---| | Browser | file read into WASM memory (`read_bytes`) | only the file handle is kept; the `File` itself is the request body (Dioxus `FileStream`, browser-native streaming) | | Browser → dashboard | JSON server function carrying `Vec<u8>` — one JSON number per byte, roughly 4× the file | `POST /api/uploads/{id}?kind&plc_format`, raw `application/octet-stream` | | Dashboard → agent | a second copy for the multipart request | the body is forwarded as the multipart `file` part via `reqwest::Body::wrap_stream` | | Agent | whole field buffered (`field.bytes()`), then written synchronously | written to disk chunk by chunk, **413** past 512 MiB | A few concurrent uploads could exhaust an 8 GB node. Now no hop holds the file. **Cleanup on failure:** on the agent, the stored file is owned by a guard until the artifact record references it. An oversized, malformed or `kind`-less upload leaves nothing on disk. ## Request-body limits The 512 MiB limit sat on **every route** of both services, so any JSON endpoint could be made to buffer half a gigabyte. It is now scoped: | Route | Limit | |---|---| | agent `POST /api/v1/targets/{id}/artifacts/upload` | 512 MiB + multipart framing (streamed) | | agent `POST /api/v1/werkbank/jobs/complete` | 32 MiB (a job's findings as JSON; behind a runner key) | | dashboard webhook proxy | 25 MB (GitHub's push-payload cap) | | everything else | axum's 2 MiB default | ## How it was decided At the design fork I asked for a structured second opinion (TypeSafe Jev, neutral description, no code): *stream through the dashboard, agent writes chunks to disk* scored 0.98, over a public direct-to-agent upload endpoint or fixing only the agent. The agent-only fix scored 0.10, because the dashboard's 4× copy was the bigger problem. Splitting this out of #297 scored 0.65, a weak yes, which matched my own call. ## Verification - **End to end in a real browser:** agent + dashboard (`dx build`) + Chromium via Playwright. A 3 MiB firmware image went through the onboarding wizard as `content-type: application/octet-stream`, `content-disposition: attachment; filename="firmware-test.bin"`, `x-content-size: 3145728`, with no JSON encoding, and landed on disk **byte-identical** (SHA-256 match). The wizard then showed the firmware scans as applicable, and detection ran on the file. - **New integration tests**, against a live Mongo: - a 3 MiB upload, over the old 2 MiB default, streams and attaches with the exact size; - a 3 MiB JSON body to an ordinary route now gets **413**; - an upload without `kind` returns 400 and leaves no file behind. - **Unchanged suites:** `cargo test --lib` 357 + 71 + 9 + 28 and dashboard 21 pass. `cargo clippy -D warnings` is clean for agent, core, MCP, werkbank-exec, control-map, and **both dashboard builds** (server and WASM). - **Also fixed:** the two `manual_checked_ops` lints in `attack_chain/view.rs` that newer clippy reports, so dashboard clippy is clean locally. ## Two notes - **The e2e suite is stale on `main`:** 14 of 15 red in parallel, 9 red single-threaded. It's test rot from before per-tenant databases, not these changes, and CI doesn't run it. Filed as **#298**. The onboarding tests, including the three new ones, pass single-threaded. - **Existing wizard behaviour:** the review step lists only reference artifacts, not staged files, although they upload fine after creation. Not touched here. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang added 1 commit 2026-09-21 11:19:12 +00:00
feat(security): stream uploads end to end, bound request bodies (#258)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 7m9s
3a4919f0f6
The second half of #258. A file upload of up to 512 MiB used to be held in
memory about five times over: the browser read it into WASM memory, sent it to
the dashboard as a JSON byte array inside a server function (roughly 4x the
file), the dashboard copied it into a multipart request, and the agent buffered
the whole field before writing it. Several concurrent uploads could exhaust an
8 GB node.

Now nothing holds the file:

- Browser: the wizard keeps the picked file's handle, not its bytes, and passes
  it as a Dioxus `FileStream`, which sends the `File` itself as the request body
  (browser-native streaming from disk).
- Dashboard: `POST /api/uploads/{id}?kind&plc_format` forwards that body to the
  agent as the multipart `file` part via `reqwest::Body::wrap_stream`.
- Agent: the `file` field is written to disk chunk by chunk, refused with 413
  past 512 MiB, and owned by a guard that deletes it on any early return — an
  oversized, malformed or kind-less upload leaves nothing behind.

Request bodies: the 512 MiB limit applied to every route on both services, so
any JSON endpoint could be made to buffer half a gigabyte. It is now scoped:
the upload route (streamed) and the runner's job-result route (32 MiB) on the
agent, the webhook proxy (25 MB, GitHub's cap) on the dashboard; everything
else keeps axum's 2 MiB default.

Also fixes the two `manual_checked_ops` lints in `attack_chain/view.rs` that
newer clippy reports, so dashboard clippy runs clean locally.

Verified end to end in a real browser (agent + dashboard + Chromium): a 3 MiB
firmware image went through the wizard as `application/octet-stream` with no
JSON encoding and landed on disk byte-identical (SHA-256 match).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang merged commit 592f947de7 into main 2026-09-21 11:37:19 +00:00
sharang deleted branch feat/streamed-uploads 2026-09-21 11:37:19 +00:00
Sign in to join this conversation.