Skip to content

ADR-0048: Signed upload links for agents: one single-use PUT per governed createupload, proxied through the API, the one upload-link route

Context

An agent that has a file on disk can add it to a site only through upload_asset, which carries the bytes as base64 inside the tool call (up to 4 MB). Every byte is model output: a 3 MB image costs about 4 MB of tokens, and larger files cannot be sent at all (#250). The agent usually has a shell that could send the file directly, but agents are refused on REST (RFC-0005 §10.1) and hold no REST credential in that shell. #299 added an optional sha256 to upload_asset; it does not change the cost.

Decision

  1. Two MCP tools. create_upload (scope asset:upload, gated by modes and approvals exactly as upload_asset) declares filename, bytes, mediaType, a required sha256 and the asset’s text fields, and returns a PUT link. complete_upload (same scope, same key only, no second approval) creates the asset node and answers what upload_asset answers. A declared SHA-256 already in the library answers existing with no link. Both schemas follow the strict structured-output profile (every property required, optional values null). upload_asset is unchanged.
  2. The link is PUT <admin origin>/api/sites/<siteId>/assets/agent-uploads/<uploadId>?sig=…. sig is HMAC-SHA-256 over purpose, uploadId, siteId, actor, method, bytes, mediaType, sha256 and expiresAt, with a key derived under the label cloudhal agent upload v1 (Studio’s tickets use cloudhal upload ticket v1). The secret is the optional CLOUDHAL_UPLOAD_SECRET, else the preview secret or MCP state key. There is no kid: links live 10 minutes, so rotation voids at most that window.
  3. The media_uploads row is the authority. The link is bound to one site, one upload, one method, the exact size, the declared type and the exact SHA-256, and credited to the key that minted it. The PUT must start within 10 minutes; received bytes must be completed within 60 minutes (completeBy). States open → receiving → received → completed (or abandoned) move by CAS; a failed verification reopens the row, at most 3 attempts.
  4. The PUT is proxied through the API on every host (the admin Worker on Cloudflare and on workerd, the Node API of cloudhal studio). It runs the RFC-0004 §5.2 pipeline up to the blob write and leases the blob until completeBy; it never creates a node. complete_upload inserts the node in one guarded batch() that asserts the blob is still stored and leased by this upload. An uncompleted upload loses its lease and is collected by the existing media garbage collection. No direct R2 or S3 presigned URLs in v1.
  5. I15 is amended. REST stays default-deny. One route requirement, upload-link, is a capability rather than an actor: it is declared only on PUT …/assets/agent-uploads/:uploadId, the route guard reads no session for it, and the handler authorizes against the single-use row. The route-enumeration test pins upload-link to that one route, so a second use needs another RFC. RFC-0005 §10.1’s “agents remain refused on REST” gains the same exception.
  6. Limits. 25 MB per file by default (media.agentUploadBytes, up to 100 MB and the type’s cap), 10 open uploads per key and 50 per site, dailyLimit (100 files, 500 MB a day, counting open uploads’ declared bytes), the site quota with declared bytes reserved. Refused PUTs are not audited; accepted PUTs write asset.upload-received; logs and audit rows never hold sig.
  7. Capability. get_mode.uploads reports {signedUrl, maxBytes, linkSeconds}. signedUrl is false on stdio cloudhal mcp, on a Worker without a shared secret, and when an operator sets media.agentUploads: false; the tools are always listed and answer signed_upload_unsupported there. The playground denies the route. No new cloudhal command in v1.

Alternatives considered

Alternative Pros Cons Why not
Direct presigned PUT to R2 No Worker body limit, up to 7 days The binding cannot presign, so the Worker would hold bucket-wide S3 credentials; S3 presigned URLs are reusable until expiry; size and SHA-256 are not enforced at write; only the r2.cloudflarestorage.com host; CORS Loses single use and write-time binding, adds a powerful secret (RFC-0048 open question 4: not in v1)
Presigned PUT to the self-host S3 store Same Exposes the cluster-private store; signed size and checksum support unverified for SeaweedFS and RustFS Proxying keeps the store private
Chunked base64 parts No shell, no REST exception Every byte is still model output; more failure points Does not fix #250
fetch_asset_url from a local tunnel No server change Third-party tool, public exposure while open Heavier and less safe; already possible
The agent’s own key on the PUT No capability exception The shell usually lacks the credential (OAuth clients keep it) Does not work for the main case; a key, when sent, must still match
Do nothing Zero work Cost and the 4 MB limit stay #250 stays open

Consequences

  • Positive. Files up to the plan’s body limit reach the library without passing through the model, under the same scope, mode, approval, budget and quota as upload_asset. One code path serves every host.
  • Negative. The first REST route an agent uses directly, and the first whose credential is the URL. A new migration on media_uploads (purpose, attempts, complete_by, received, asset_id, an index). Self-host reverse proxies must allow bodies up to the agent cap.
  • Risks. A leaked, unused link lets whoever holds it burn the upload’s attempts (a denial of service on that upload; it can write only the declared bytes). A key holder can fill a site’s quota up to the daily and site limits, as with upload_asset today. Files above the Workers plan’s body limit still cannot be uploaded by agents.