Skip to content

feat(storage): presigned PUT for uploads on R2, so cover bytes bypass the Worker #139

Open

Description

@aRustyDev

Context

Follow-up of #44 (closed), whose asset stage (S28, S29, S31, S33) builds the upload as Plane's app expects it: S28 answers upload_data: {method: "PUT", url, fields: {}, headers}, the browser PUTs the file, and S29 confirms. Deviation D-035 (1) records that on every target the url is an address of this API, PUT /api/assets/v2/upload/{asset}/{token}/, and not a presigned URL of an object store. The token in the path is the credential (the browser sends no cookies): 256 random bits kept as a SHA-256, good for one use and an hour. The owner decided this for #44 and said the R2 presign is a follow-up. D-035 is still marked pending the owner.

Why that was the choice: no target has an object store the browser can reach with a signed URL today. A presign for R2 needs R2 API credentials stored as Worker secrets, which nothing here has (D-035; ADR-0047 and ADR-0080 say the same), and the self-hosted set's blob store is local disk or memory, which has no SigV4 endpoint.

What the plan already says about the end state:

  • ADR-0080: BlobStore has presign(op, key, opts) -> Url, "SigV4; works in wasm32". The built trait (crates/thrust-ports/src/lib.rs, BlobStore) has put, head, get, delete and list, and no presign. ADR-0080 marks "SigV4 presign from Rust/wasm32" as unverified.
  • ADR-0081 and ADR-0047: Option 4, a hybrid. Small files go through the API, and attachments use presigned PUT where the adapter supports it. The Worker buffers a request body and bounds it at 8 MiB, so anything larger than that cannot go through the Worker at all.
  • R2 limits that bite: a presigned URL works only on the S3 API domain (<account>.r2.cloudflarestorage.com), not on a custom domain; the browser must PUT (no POST forms); the bucket needs CORS rules for the app's origin, and the storage hostname becomes visible to the browser.

Problem

  • Every browser upload is carried by the Worker today. That fits the 5 MiB default (CoreConfig.max_attachment_bytes; the compat upload route has its own body limit), but nothing above the Worker's 8 MiB bound can be uploaded, and each upload costs Worker CPU and wall time for bytes it only forwards.
  • The upload route (D-035 (1)) is a deviation from the pin's shape: the pin's url is the store's. The parity tests compare it through a projection (tests/common/mod.rs); with a real presigned URL the projection gets closer to the pin's.
  • S28's token is one-use; a presigned PUT is reusable until it expires, as the pin's is (S3Storage.generate_presigned_put, an hour). S29's check of the object (D-035 (2): size and type must be the asset's) is then the only thing that stops a different file being uploaded, so it must stay.

Proposal

  • Add presign (PUT, and GET if the exports of ADR-0081 want it; that belongs to feat(jobs): exports and seeding as queue jobs with a jobs table (no workflow engine) #56) to BlobStore, with a SigV4 signer that builds for native and wasm32-unknown-unknown (spike 0001 left this unverified, so the first task is a spike result: which signer crate, or a small one of our own, compiles and runs in workerd).
  • thrust-adapters-cloudflare implements it with R2 API credentials (access key id and secret, account id, bucket) read from Worker secrets and the generated wrangler config; the other adapters answer "cannot presign", as ADR-0081 says.
  • S28 asks the store: when it can presign, upload_data.url is the store's signed URL, fields stays {}, and the signed headers carry Content-Type and Content-Length, as the pin's do; when it cannot, the API-hosted PUT of D-035 (1) stays. Both flows are contract-tested on the targets that have them; worker-cf against a real bucket is the only target that can exercise the first.
  • S29 keeps head verification of size and type.
  • Bucket CORS for the app's origin (PUT, the signed headers) is part of the deploy, and the infra side (bucket, token, CORS) is the owner's; say what it needs and where the secret lives before building.
  • Revise D-035 (1) and the plan row; G-27's "an R2 presign that takes the bytes off the Worker" moves from "not built" to what is covered.

Acceptance criteria

  • A known-answer test of the signer against AWS's published SigV4 test vectors (an independent oracle, AGENTS.md rule 9(c)), with a control: a changed header, key or expiry changes the signature.
  • The signer builds and runs in workerd (wasm32), shown by a worker-cf test, not only natively.
  • A real-R2 smoke run, which needs an owner-provided account and bucket (as scripts/stage2-contract.sh did for the budget, G-24): a browser-style PUT to the presigned URL succeeds; the same URL with a different Content-Length, a different key or after expiry is refused by R2; S29 then confirms, and refuses an object that is not the asset's. Record the run (date, commit, what was torn down) in docs/src/testing/status.md, as G-24 did.
  • The cover upload in a real browser (extend e2e/journey.spec.ts, test(e2e): Playwright journey suite on the stock React app as the UI oracle #45) goes to the store's URL on worker-cf with CORS in place, and to the API address on the targets that cannot presign.
  • An upload above the Worker's 8 MiB bound succeeds through the presigned path (with max_attachment_bytes raised) on the real bucket, and is refused as today on the API-hosted path.
  • D-035 and G-27 are revised.

Out of scope

The reaper of assets that were never uploaded (ADR-0047's Scheduler), deletion of an asset, the nine other asset kinds, multipart uploads, a disk blob store for the native server, and the S3-compatible adapter for MinIO and S3 (P1). A MinIO testcontainer would give a local SigV4 check once that adapter exists.

References

Blocked by: the owner's R2 credentials and bucket for the smoke run (the signer and its tests are not blocked)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions