buzz icon indicating copy to clipboard operation
buzz copied to clipboard

fix(cli): let the relay decide which files may be uploaded

Open nvisionlife opened this issue 3 months ago • 1 comments

Summary

buzz upload file rejected anything outside a five-entry MIME allowlist before making a request, so XLSX, PDF, CSV and every other document failed locally with unsupported file type — even though the relay's PUT /upload already routes generic files to validate_file_content. The client was enforcing a stale copy of relay policy, and human Desktop users could upload files that agents could not.

This makes the relay authoritative. It sniffs the body, ignores the advisory Content-Type for routing, and applies its configured max_file_bytes; the CLI now enforces only the bounds it actually owns.

  • Delete ALLOWED_MIMES. Detected MIME selects client resource behaviour — size cap, timeout, advisory Content-Type — and never acceptance. Deliberately not replaced with a copy of the relay's BLOCKED_FILE_MIME_TYPES: a duplicated denylist is two policies that drift, and recreates this same bug for the next format a newer relay learns to accept.
  • Keep the established media caps for the five types whose CLI/relay semantics are stable (media_size_cap). Generic files get no client policy cap — max_file_bytes is per-deployment configuration the client cannot discover, so guessing it would reject files the relay accepts.
  • Add MAX_BUFFERED_UPLOAD_BYTES (500 MB) as an explicit client memory ceiling. It is a separate constant from MAX_VIDEO_BYTES on purpose: same initial value, different concern, and they diverge the moment uploads are streamed. Its error reads too large to buffer safely … not a relay policy limit so it cannot be mistaken for a refusal by the server.
  • Preflight against metadata.len(), and sniff the type from a bounded 8 KiB prefix (infer::get_from_path) instead of the whole file. Previously std::fs::read ran before every check — an 800 MB file cost ~843 MB RSS just to print unsupported file type.

Known consequences

Both are unavoidable while the relay's limits are undiscoverable, so they are stated rather than hidden:

  1. Unsupported non-video media now costs a transfer. The relay sniffs 4 KiB but its non-video path buffers the whole bounded body before validating, so a .mp3/.mov/.tiff that used to fail instantly and locally now uploads and comes back 415. Measured against production with ID3-signature payloads at 0.5, 2, 10 and 25 MB: full body pushed every time. Documents not working at all is the worse failure, but this is a real cost in a common path.
  2. Generic files above the relay's max_file_bytes waste work. They are fully allocated client-side, partially transferred, and rejected with 413. Against production with a 100 MB cap, the transfer stopped anywhere between 0.8 MB and ~102 MB depending on where the bound tripped — the client allocation is certain, the transfer volume is not.

Follow-ups, not in this change

  • Stream the body and stream-hash it so nothing is buffered and the memory guard can go away. This needs with_retry_body to reopen the file per attempt instead of cloning Bytes, which is the retry-sensitive part — worth separating from this fix.
  • BUD-06 HEAD /upload preflight, so the client can learn relay policy before sending a byte. That is the thing that fixes both consequences above at once. Not currently available: an unauthenticated production probe on 2026-08-02 returned 405 with Allow: PUT, and NIP-11 advertises no media limits.
  • Relay-side: the bounded to_bytes failure path constructs FileTooLarge { size: 0, max }, so every over-cap generic upload tells the user their file is "0 bytes". Separately, no early declared-size check exists on the generic path today — adding one would be a new capability, not a fix to a misreporting one.
  • #2964 (large OOXML detected as application/zip) is untouched. Note the client-side sniff here has the same shape of limit at 8 KiB, so a large .xlsx will be sent with Content-Type: application/octet-stream. That is advisory only — the relay re-sniffs — so it does not affect acceptance, but it does not fix #2964 either.
  • desktop/src-tauri/src/commands/media.rs:307 carries its own unsupported file type gate. Out of scope here; flagging it as the same class of client-side policy.

Related issue

Fixes #2963. Fixes #3083 (same root cause, filed independently). Related to #2964, which this does not fix — see above.

Testing

cargo test -p buzz-cli — 284 passed at a6b5cce, including 10 new tests. cargo clippy -p buzz-cli --all-targets and cargo fmt --all --check clean.

The new tests drive upload_file against a mock Blossom server and assert on observable behaviour — which endpoint was called, what bytes arrived, and which failures happen before any request:

Test Locks
xlsx_reaches_the_upload_endpoint_byte_identically the reported bug; real 1.5 KB workbook fixture, byte-identical body
pdf_reaches_the_upload_endpoint / opaque_bytes_reach_the_upload_endpoint generic types are no longer refused client-side
jpeg_still_uploads_unchanged existing media path untouched
relay_415_is_surfaced_without_retry_or_legacy_fallback relay's explanation reaches the user verbatim; a 415 is not retried and not replayed against /media/upload
upload_404_falls_back_to_the_legacy_endpoint old-relay compatibility preserved
generic_file_above_the_image_cap_is_not_capped_by_the_client 50 MB image cap does not leak onto documents
file_above_the_buffer_guard_fails_before_any_request over-guard sparse file fails before allocation, zero HTTP requests
image_above_the_image_cap_is_still_rejected_before_any_request media caps still enforced, now before the read
media_size_cap_covers_only_the_established_media_types the cap table itself

The 415-surfacing and 404-fallback cases already behave correctly at 28ae6cd2 — no new code was needed for either. They are regression locks on behaviour a future refactor would plausibly break.

crates/buzz-cli/testdata/minimal.xlsx is a genuine 1,572-byte OOXML workbook (five parts, deflated), added so the round-trip test uses a real file rather than a zip-shaped approximation.

Design was reviewed adversarially before implementation; the production measurements quoted above are from that review, not from this branch.

nvisionlife avatar Aug 02 '26 11:08 nvisionlife

+1 on making the relay the authority here.

I have a related open PR (#3060) that takes a slightly different angle: mirror the relay's BLOCKED_FILE_MIME_TYPES denylist client-side (fast-fail only the permanent security denylist) and raise the non-media size cap to the relay's 100MB default, rather than expanding an allowlist. Same end state for agents (PDF/xlsx/csv/etc. stop dying client-side).

Happy for either approach to land — if this one merges first I'll close #3060 as superseded.

maikunari avatar Aug 07 '26 09:08 maikunari