Uploads
Binding for implementation. An agent can stage a file once, as an upload that belongs to one identity,
and then attach it to sends, replies, reply-alls, forwards and drafts by its ID instead of carrying the
bytes as base64 inside every JSON request. Today every attachment travels as content_base64 inside a
request body of at most 7 MiB, which inflates it by a third, and an MCP client must make the model emit
those bytes as text. An upload is sent to the Worker once, verified for size and SHA-256 while it streams
into R2, classified with the same core::attach rules as inbound mail, and then copied into every message
that uses it. Uploads are not a drive: they have no paths, no folders, no download route and a lifetime of
at most 7 days. Pylota Identity never fetches an attachment from a URL (§1).
| Requirements | FR-UPL-1 to FR-UPL-12 (this page), FR-OUT-10, FR-IN-5, FR-BILL-8, FR-PRV-1, FR-PRV-3 |
| Edge cases | Q65–Q108 |
| Code | crates/core/src/uploads.rs (create validation, the state machine, the upload-token payload, expiry arithmetic; pure), crates/core/src/attach.rs (classify_name, used again with the final filename), crates/core/src/compose.rs (encoded_size, the link plan from sizes alone), crates/worker/src/uploads/{mod.rs, put.rs, classify.rs, copy.rs}, handlers/uploads.rs (four routes), handlers/links.rs (PUT /v1/links/{token}), mailbox/uploads.rs (the uploads table, its MailboxRequest operations and alarm:uploads), the attachment hooks in mailbox/submit.rs (policy step 12, steps 6 and 7 of the reservation), the uploads rows in jobs/erasure.rs, the up/ exclusion in jobs/backup.rs, mcp/tools.rs, crates/cli/src/commands/uploads.rs; the R2 additions to BlobStore in crates/platform/src/lib.rs and cf/glue.rs |
| Tables | Mailbox uploads (new), attachments.upload_id (new column), meta key alarm:uploads; D1: none (Data model) |
| Contracts | POST, GET (list and one) and DELETE on /v1/identities/{identity_id}/uploads…, and PUT /v1/links/{token} (REST API); attachments[].upload_id on send, reply, reply-all and forward, and attachments on forward; Message.attachments[].upload_id; the permissions messages:send or drafts:write (either one); the signed-link purpose upload; the receipt field uploads_deleted; the errors 404 upload_not_found, 409 upload_not_ready, 409 upload_in_progress, 409 upload_in_use, 410 upload_expired, 400 upload_size_mismatch, 400 upload_checksum_mismatch, 422 upload_limit_reached and 422 attachment_risky (Errors); the MCP tools mail_create_upload and mail_get_upload; no new event and no new policy field |
| Limits | Limits › Uploads: 1–26,214,400 bytes per upload; 100 live uploads and 524,288,000 declared bytes per identity; lifetime 900–604,800 seconds, default 86,400; upload URL 3,600 seconds; attachments per message 26,214,400 decoded bytes in total |
| Built in | M33 |
1. What uploads are and are not
The problem. A send carries each attachment as content_base64 (Outbound › Attachments and size).
The request body is at most 7 MiB (413 payload_too_large, Outbound › Handler step 4),
so the largest file a send can carry is about 5.25 MiB, and with large_attachments: "link" the signed
link that replaces an oversized file can never be bigger than that either. An MCP client is worse off:
tools/call carries its arguments as a JSON object, and the protocol has no binary input type, so a
file reaches a tool only as text the model writes out
(MCP specification 2026-07-28, Tools,
read 2026-10-17). A 3 MiB PDF is four million base64 characters of model output.
What an upload is. A file staged by one identity: declared up front (filename, content type, size,
SHA-256), sent once to a signed URL on the Worker, verified, classified, and then attachable by
{ "upload_id": "upl_…" } until it expires. Every message that uses it gets its own copy of the bytes,
so expiring or deleting the upload never changes a sent message (§5.4).
Pylota Identity never fetches attachments from URLs. AgentMail accepts url attachments and fetches them
at send time. Pylota Identity does not, for three reasons:
- SSRF. A fetch on behalf of a request would be an outbound request to an address chosen by the caller. Every outbound destination Pylota Identity calls today is fixed by the operator and passes the SSRF guard of Security § 9; a per-request URL would turn every send key into a way to probe internal or link-local addresses, follow redirects into them, or make the Worker download from a host that serves something different to Cloudflare’s addresses.
- Content integrity. The bytes at a URL can change between the moment an agent or a person decides to send them and the moment the Worker fetches them, and again on a retry. An upload’s SHA-256 is declared before the bytes arrive and checked by R2; a replayed send can only ever attach the same bytes.
- Prompt injection. Agents read untrusted mail. “Attach the file at https://…” in a message is an instruction an agent should not follow; with no URL attachments, following it cannot make Pylota Identity fetch and forward anything.
Not a drive. AgentMail’s Agent Drive keeps files under paths with listing, download links and per-file expiry. Uploads have none of that: no path, no folder, no rename, no download route (the bytes are only ever read back by the Worker to classify and copy them), no sharing between identities, and a lifetime of at most 7 days. Long-lived files belong to the integrator’s own storage (for Pylota, its own object store); an upload is the staging step of a send.
Not a presigned R2 URL. R2 presigned URLs need an R2 API token’s Access Key ID and Secret Access Key
and S3 SigV4 signing, are bearer URLs valid for 1 second to 7 days, and work only on the S3 API domain
<ACCOUNT_ID>.r2.cloudflarestorage.com
(R2 presigned URLs, updated 2026-08-22,
read 2026-10-17). Using them would add a long-lived S3 credential that can sign for the whole bucket,
expose the account ID in every upload URL, and let bytes land in R2 with no Worker in the path: no check
of a tenant suspension or identity pause at upload time, no attempt counting, and no classification until
a separate “complete” call. A Worker URL keeps the one credential the deployment already has (the link
signing key, Security § 7.3) and does every check in the request that
carries the bytes.
Why the Worker can take the bytes. A Worker accepts request bodies of up to 100 MB on the Free and Pro
account plans, 200 MB on Business and up to 5 GB on Enterprise, and answers 413 above that
(Workers limits, read 2026-10-17). The
upload maximum of 26,214,400 bytes (25 MiB, a Pylota Identity choice) is below every plan’s limit, so it
is a fixed number rather than a setting. It is also the largest message Cloudflare Email Routing delivers
inbound (Limits › Mail), so an agent can forward or re-send anything it
can receive, and it bounds the memory classification needs (§4.3).
On Workers Paid an HTTP request has no wall-time limit while the client stays connected
(Workers limits, read 2026-10-17), so a slow
client is not cut off; the upload URL’s one-hour lifetime is checked when the request starts, not while
it streams.
2. The upload
{
"id": "upl_01JAC3Q7W9X2M5N6P8R0T1Y4ZB",
"identity_id": "idn_01J9Z4K8V4QW7X2M5N6P8R0T1Y",
"filename": "FNOL-7781.pdf",
"content_type": "application/pdf",
"size": 9437184,
"sha256": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae",
"status": "pending",
"status_reason": null,
"sniffed_type": null,
"risk": null,
"scan_status": "skipped",
"use_count": 0,
"last_used_at": null,
"created_at": "2026-10-17T09:00:00Z",
"expires_at": "2026-10-18T09:00:00Z",
"upload_url": "https://mail.example.com/v1/links/bDE6YTp1cGxvYWQ6dGVuXzAxSjk…",
"upload_url_expires_at": "2026-10-17T10:00:00Z"
}
| Field | Meaning |
|---|---|
id | upl_ + ULID. This is the value a send passes as attachments[].upload_id |
filename | Sanitised as attachment filenames are (no path components, no control characters, at most 255 bytes, Inbound › Risk classification). Untrusted content |
content_type | As declared: a valid type/subtype, at most 127 bytes |
size | Declared bytes, 1–26,214,400. The bytes must match it exactly |
sha256 | Declared SHA-256 of the bytes, 64 hexadecimal characters; stored and returned lower case |
status | pending, uploading, ready, rejected or expired (below) |
status_reason | For rejected: risk or infected. For expired: expired, too_many_attempts or object_missing. Otherwise null |
sniffed_type, risk | Set when the bytes are classified (Inbound › Sniffing); risk is one of the six AttachmentRisk values or null |
scan_status | skipped (no scanner configured), clean, infected or error |
use_count, last_used_at | Accepted messages that copied this upload, and when the last one did |
expires_at | When the upload and its bytes are deleted, whether or not it was used |
upload_url, upload_url_expires_at | Present only in the create response and in GET of one upload while it is pending or uploading; null otherwise and never in lists. A new URL is minted on every such response (§3.2) |
PUT claims bytes verified, classified clean
create ──▶ pending ─────────▶ uploading ─────────────────────────────▶ ready ──┐
▲ │ │ │
└── PUT failed ────┘ └── quarantining risk or infected ──▶ rejected
(or lease lapsed; (object deleted) │
10th failure ──▶ expired) │
pending, uploading, ready ── expires_at passes ──────────────────────────▶ expired
rejected, expired ── 7 days later ─▶ row deleted (then 404 upload_not_found)
DELETE (any state, unless a send holds it) ─▶ row and object deleted
pending. Created; no verified bytes. Only aPUTwith this upload’s token moves it on.uploading. OnePUTholds the claim (put_token, a lease of 120 seconds that thePUTrenews every 60 seconds). A crashedPUTloses its claim when the lease lapses; the nextPUTtakes it.ready. The bytes in R2 have the declared size and SHA-256, were classified, and carry no quarantining risk (encrypted_documentis the one non-quarantining risk, and it is allowed). Onlyreadyuploads can be attached.rejected. The bytes carry a quarantining risk (executable,macro,archive_bomb,encrypted_archive,type_mismatch) or the scanner saidinfected. The object is deleted at once; the row stays as a tombstone so that a later attach gets422 attachment_riskyinstead of404.expired.expires_atpassed, tenPUTattempts failed, or the object was found missing (after a restore). The object is deleted; the row stays as a tombstone for 7 days so that a late attach gets410 upload_expired.
3. Routes
All four …/uploads routes are identity-scoped (Scope::IdentityPath) and need either
messages:send or drafts:write: the key must hold at least one. They are available to every key
level within its scope (an identity key for its own identity). Rate limits: RL_API and, for tenant and
identity keys, RL_TENANT, as for every route.
3.1 Create — POST /v1/identities/{identity_id}/uploads
{
"filename": "FNOL-7781.pdf",
"content_type": "application/pdf",
"size": 9437184,
"sha256": "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae",
"expires_in": 86400
}
201 with the upload object (status pending, with upload_url). Idempotency-Key is optional; a replay
returns the original status (201) with Idempotent-Replayed: true, and, like every replay that carries
a resource, it re-reads the upload and mints a fresh upload_url (the stored record keeps the upload
ID, never a URL). In order:
- Authentication, permission (
messages:sendordrafts:write), level and owner checks (Security § 5.2); a missing or foreign identity is404 identity_not_found. - Body:
filename1–255 bytes after sanitising (empty after sanitising →400 invalid_request),content_typea validtype/subtypeof at most 127 bytes,sizean integer 1–26,214,400,sha256matching^[0-9A-Fa-f]{64}$,expires_inan integer 900–604,800 (default 86,400). Each failure is400 invalid_requestwithdetails.errors[]. - D1 facts (one batch, the same reads as the send handler’s step 6 for identity and tenant): identity
deletingordeleted→404 identity_not_found; tenantsuspended→403 tenant_suspended; identitypaused→409 identity_pausedwithdetails.reason; the tenant’s effectiveretention.message_daysand the identity’sretention.message_hours(Parity additions), for the expiry cap in step 6. - Storage gate:
QuotaRequest::GetUsageon the tenant’sTenantQuota; whenstorage_gbhas agrantedvalue andused ≥ granted→402 billing_limitwithdetails.feature = "storage_gb"(Plans, metering and billing › Storage). Skipped forbillingexemptordisabled. MailboxRequest::CreateUpload(§5.2) checks, inside one transaction, that the identity has fewer than 100 live uploads (pending,uploading,ready) and that their declared sizes plus this one are at most 524,288,000 bytes; otherwise422 upload_limit_reachedwithdetails.limit(countorbytes),details.usedanddetails.max. It inserts the row.expires_at = created_at + expires_in, capped atcreated_at + retention.message_days daysand atcreated_at + retention.message_hours hourswhen those are set. The response always shows the effective value.- Mint the upload URL (§3.2) and return
201.
3.2 Sending the bytes — PUT /v1/links/{token}
PUT /v1/links/bDE6YTp1cGxvYWQ6dGVuXzAxSjk… HTTP/1.1
Host: mail.example.com
Content-Type: application/octet-stream
Content-Length: 9437184
<9,437,184 bytes>
200 with the upload object (status ready, upload_url: null). The URL needs no API key: it is a signed
link in the format of Security § 7.3 with a new purpose:
payload = "l1:{kid}:upload:{tenant_id}:{identity_id}:{upload_id}:{expires_unix_s}"
token = base64url(payload || HMAC-SHA256(link key {kid}, payload)[0..16])
expires = min(now + 3,600 s, upload.expires_at)
The token is a capability for exactly one upload, whose size and SHA-256 were fixed at create; it cannot
put different bytes. It is a bearer URL, so it is short-lived (one hour) and a new one is minted on every
create response and every GET of a pending or uploading upload. An agent can hand the URL to a tool
that holds the file but no API key (a code sandbox, a shell, a browser automation step).
Purposes are separate: a PUT with an att or export token, or a GET with an upload token, gets
404 with the code of the token’s own target (upload_not_found for an upload token, as for a bad MAC),
so a link never reveals why it failed. /v1/links/* answers only on the API host, sends no CORS headers
and is never called by the console.
Rules in order. “Count a failure” means MailboxRequest::FailUpload { counted: true }, which returns the
upload to pending and increments failed_puts; the tenth counted failure sets expired
(too_many_attempts) and deletes the object:
- Token. Split, look up the link key by kid (current, or inside its verify window), recompute the
MAC in constant time, check purpose
uploadand expiry. Any failure →404 upload_not_found. - Rate limit.
RL_TENANTkeyed by the token’s tenant ID (1,800 per 60 seconds) →429 rate_limited. - D1 facts (one batch, 5-second deadline,
503 unavailableon failure): the identity’sstatusandmailbox_do_id, the tenant’sstatusand partner. Identitydeletingordeleted, or tenanterasingorerased→404 upload_not_found; tenantsuspended→403 tenant_suspended; the tenant’s partner suspended →403 partner_suspended; identitypaused→409 identity_paused; a restore freeze or aread_onlycontainment →503 unavailablewithdetails.reasonfrozenorread_only(Errors). - Framing, before reading any byte. The handler reads the upload with
GetUpload(no row →404 upload_not_found).Content-Lengthmust be present and equal the declared size; otherwise400 upload_size_mismatchwithdetails.reason = "content_length",details.expectedanddetails.received(nullwhen absent). AContent-Encodingother thanidentity→400 upload_size_mismatchwithdetails.reason = "encoding". The request’sContent-Typeis ignored: the type was declared at create. Neither case is counted: nothing was read. - Claim.
MailboxRequest::BeginUpload { id, put_token, now }with a fresh randomput_token:pending, oruploadingwhose lease has lapsed → claimed:status = 'uploading',lease_until = now + 120 s;uploadingwith a live lease →409 upload_in_progress(retryable),Retry-After= the seconds left on the lease (at least 1);ready→200with the upload, without reading the body (a retriedPUTafter a lost response is a success, not an error);rejected→422 attachment_riskywithdetails.riskordetails.reason = "infected";expired→410 upload_expiredwithdetails.reason; aready,pendingoruploadingupload whoseexpires_athas passed is setexpiredhere and answers the same;- no row →
404 upload_not_found.
- Resume check.
BLOBS.head(r2_key). An object that exists with the declared size and asha256custom metadata value equal to the declared digest was written by an earlier attempt whose response was lost: step 7 writes that value with theput, and R2 stores the object only after checking thesha256option, so the pair proves a verified write. Skip steps 7 and 8. Whenheadalso returnschecksums.sha256, it must agree. An object that exists and does not match is deleted first. The R2 Workers reference (updated 2026-07-31, read 2026-10-18) saysheadreturns the object’s metadata and that a checksum given toput()is on the objectput()returns; it does not say whetherheadreturns checksums, so the check does not depend on them, andit::uploads::v_put_stream_contractrecords whatheadreturns. Until that test runs, this is unverified. - Stream into R2. Pipe the request body through a
FixedLengthStreamof the declared size: an identity transform stream that accepts exactly that number of bytes and no more (Workers Request › Set the Content-Length header, read 2026-10-17). The page does not say how R2’sputtreats a stream that ends early or how it learns the length; the first task of M33 is a real-runtime test against workerd’s R2 (it::uploads::v_put_stream_contract) that pins both before the handler is written, and steps 7 and 8 below hold whichever way it answers. Then callBLOBS.put(r2_key, stream, { sha256: declared, customMetadata })withcustomMetadatatenant,identity,uploadandsha256(the declared digest, read by step 6). R2 uses thesha256option to check the received object’s integrity (R2 Workers API reference › R2PutOptions, updated 2026-07-31, read 2026-10-17) and fails theputwith10037 BadDigeston a mismatch, so the Worker never hashes and never buffers the file: memory stays at the stream’s chunk size, whatever the upload’s size. The handler renews the lease every 60 seconds (RenewUpload). R2’s error code is at the end of the error message (R2 error codes, updated 2026-07-31, read 2026-10-17):10037 BadDigest→ count a failure;400 upload_checksum_mismatchwithdetails.expected(the declared SHA-256). Nothing is stored.10013 IncompleteBody, aFixedLengthStreamerror, or the client disconnecting → count a failure;400 upload_size_mismatchwithdetails.reason = "incomplete"(the client may never see it). Nothing is stored.10058 TooManyRequests(R2 allows one write per second to the same key, and concurrent writes to one key get429, R2 limits, read 2026-10-17), or any other R2 error →FailUpload { counted: false };503 unavailable(retryable). A503caused by the service is never counted against the client.
- Verify what was stored. The
putresult’ssizeandchecksums.sha256must equal the declared values; a mismatch (defence in depth: it should be impossible after step 7) deletes the object, counts a failure and answers as10037. - Classify and scan (§4.3).
- Complete.
MailboxRequest::CompleteUpload { id, put_token, sniffed_type, risk, scan_status }. Inside one transaction it re-checks thatput_tokenstill holds the claim and thatexpires_athas not passed: thenready, orrejectedwithobject_state = 'deleting'. A lost claim (the lease lapsed and anotherPUTtook over) answers409 upload_in_progress; the otherPUTfinishes the job. After commit, a rejected upload’s object is deleted and the response is422 attachment_risky; otherwise200.
No complete call. AgentMail’s Drive and S3-style flows need a third call because the bytes bypass
the API. Here the request that carries the bytes is the one that verifies and classifies them, so a
client makes two calls: create, then PUT. A client that lost the PUT response repeats the PUT
(step 5 answers 200 for a ready upload, step 6 skips re-sending bytes already stored) or reads the
upload with GET.
3.3 Read — GET /v1/identities/{identity_id}/uploads and GET …/uploads/{upload_id}
The list returns the identity’s uploads, newest first, with the standard limit and cursor
(REST API › Pagination) and an optional status filter (one of the
five values). Tombstones (rejected, expired) are listed while they exist. Lists never carry
upload_url. GET of one returns the upload; while it is pending or uploading it carries a freshly
minted upload_url. A missing, foreign or deleted upload is 404 upload_not_found.
There is no route that returns an upload’s bytes. The bytes are read back only by the Worker, to classify
them and to copy them into messages; a sent message’s attachments are read with the existing
GET …/messages/{message_id}/attachments/{attachment_id} (attachments:read).
3.4 Delete — DELETE /v1/identities/{identity_id}/uploads/{upload_id}
204. Deletes the row and, after commit, the object, in any state. A send that is between validating
the upload and committing holds an in-memory pin on it (§6); a delete
then gets 409 upload_in_use (retryable, Retry-After: 5). A PUT in progress loses its claim: its
CompleteUpload finds no row, deletes the object it wrote and answers 404 upload_not_found. Deleting a
missing upload is 404 upload_not_found. Messages that already copied the upload keep their copies.
3.5 Attaching an upload
Send, reply and reply-all take attachment items of two shapes; forward gains the same attachments
array next to include_attachments:
"attachments": [
{ "upload_id": "upl_01JAC3Q7W9X2M5N6P8R0T1Y4ZB", "filename": "FNOL-7781.pdf", "disposition": "attachment" },
{ "filename": "logo.png", "content_type": "image/png", "content_base64": "iVBORw0KGgo…",
"disposition": "inline", "content_id": "logo" }
]
- An item has exactly one of
content_base64andupload_id; neither or both →400 invalid_request(details.errors[0].path = "attachments[{i}]"). - With
upload_id:filenameis optional (the upload’s filename by default) and sanitised;content_typeis not accepted (400 invalid_request, “taken from the upload”);dispositionandcontent_idare as for inline items (aninlinepart needscontent_id). - The upload must belong to the identity that sends. An upload of another identity, of another tenant,
or one that does not exist is
404 upload_not_foundwithdetails.index, the same answer in every case. - Drafts store attachments in the same two shapes (Drafts, approvals and scheduled send), checked when a revision is saved exactly as §4.1 checks them (states, final filename, inline classification, the total); a draft must not depend on the upload once a revision is saved (§5.4).
Message.attachments[] gains upload_id: the upload the attachment was copied from, or null (inline
base64, inbound, forwarded original). It survives the upload’s expiry and deletion: it records where
the bytes came from, not a live reference.
4. The send path with uploads
4.1 Policy step 12, extended
Step 12 of the policy pipeline (“Content”) gains these checks, in this order, after the existing ones (text or html present, subject, at most 32 attachments, labels and metadata):
- Item shape (§3.5) →
400 invalid_request. - For each
upload_iditem, in index order, against the mailbox’suploadstable (this runs insideSubmit, after the idempotency lookup):- no row →
404 upload_not_found,details.index; pendingoruploading→409 upload_not_ready(retryable),details.index,details.status;rejected→422 attachment_risky,details.indexanddetails.risk(ordetails.reason = "infected");expired, orreadywithexpires_at ≤ now→410 upload_expired,details.index,details.reason;- the final filename (the item’s, sanitised, or the upload’s) is checked again with
core::attach::classify_name(final_filename, sniffed_type): the executable-extension list, the macro extensions, andtype_mismatchbetween the new extension and the sniffed family. A risk →422 attachment_risky,details.index,details.risk. Renamingreport.pdftoreport.exeafter upload never bypasses the rules; - pin the upload (§6).
- no row →
- Inline base64 items are classified too.
core::attach::sniffandclassify_riskrun on each decodedcontent_base64item, with its filename and declared type. A quarantining risk →422 attachment_risky,details.index,details.risk. This is new for inline attachments: without it, an agent could avoid the upload checks by switching to base64. The malware scanner is not called for inline items, because the submit path has a 30-second deadline and the scanner may take 30 seconds per file (Inbound › Malware scanner hook); uploads are scanned when their bytes arrive. - Total size. The decoded sizes of all attachments (inline bytes and uploads’
size) must be at most 26,214,400 bytes (a Pylota Identity choice: the upload maximum, so one maximal upload always fits); otherwise413 message_too_largewithdetails.reason = "attachments_total",details.sizeanddetails.max, whateverlarge_attachmentssays. The existing limit of 32 attachments still applies.
A failure at any of these steps unpins every upload pinned so far.
4.2 Step 17: size, inline or linked
Step 17 (“Size after composition”) is decided from sizes alone, before any upload byte is read:
core::compose::encoded_size(size) gives each part’s base64 size with 76-character lines, and the
composed size is the headers and bodies plus those parts. When the result is above
5,234,688 bytes (5,242,880 − 8,192, Outbound › Attachments and size):
large_attachments: "refuse"→413 message_too_large,details.reason = "transport_limit";large_attachments: "link"→ the existing rule: attachments are replaced by signed links, largest first, until the message fits. Uploads and inline items are treated alike.
The result is a plan: which attachments are embedded in the MIME and which are linked. A dry run
(?dry_run=true) stops here: it validates the uploads and computes the plan, reads no upload bytes,
copies nothing and pins nothing past the request.
4.3 Classification and the scanner
Used by step 9 of the PUT (§3.2):
- Memory budget. Classification reads the stored object back into memory:
core::attachinspects the first 8 KiB, ZIP central directories, OLE directories and PDF trailers (Inbound › Sniffing), which can sit anywhere in the file. A Worker isolate has 128 MB, shared by every request it is running (Workers limits, read 2026-10-17), so each isolate keeps a byte budget of 33,554,432 bytes (32 MiB, a Pylota Identity choice: a quarter of the isolate) for upload bytes held for classification. A classification waits up to 10 seconds for room; then thePUTgets503 unavailable(retryable, not counted), the object stays in R2, and the retry skips streaming (§3.2 step 6). - Classify.
core::attach::sniffandclassify_riskwith the declared filename and content type: the rules of Inbound › Risk classification, unchanged. - Scan. When
PM_SCANNER_URLis set and there is no quarantining risk, the bytes are posted to the scanner with the contract of Inbound › Malware scanner hook (headersX-Pylota-Attachment-Sha256,X-Pylota-Sniffed-Type; 30-second timeout; no redirects; response read up to 4 KB; the SSRF guard). Every upload is scanned, up to the 26,214,400-byte maximum (the inbound rule scans attachments up to 20 MB; uploads are scanned whole because they are the files an agent chose to send).clean→scan_status = 'clean';infected→rejectedwithstatus_reason = 'infected';error, a timeout, a non-2xx status or an unparseable body →scan_status = 'error', the upload becomesready, andupload_scanner_error_totalis incremented. This is the inbound rule (fail open on scanner errors), so a scanner outage never stops sending. - Release the memory budget.
4.4 Steps 6 and 7 of the reservation
Reservation inside the mailbox, with uploads:
- Step 4 (composition in memory). The embedded uploads are read from R2 (
BLOBS.get); by step 17 they total at most the composed limit, so they fit in memory as inline base64 attachments already do. A missing object (only possible after a restore, Q100) sets the uploadexpired(object_missing), releases the reservations and returns410 upload_expired. - Step 6 (sent copy). Besides
out/{msg}.eml, every attachment that came from an upload is copied tot/{ten}/i/{idn}/out/{msg}/a/{att}, the existing key of outbound attachments: R2 to R2, streamed through aFixedLengthStreamof the upload’s size with thesha256option, so the copy is verified like the upload was and never held in memory. A linked attachment’s link points at that copy. Any failure takes the existing path: everything written so far is deleted, the reservations are released, and the send gets503 unavailable; a copy that runs past the submit deadline gets504 timeoutbefore anything is committed, so a retry with the same key is safe. - Step 7 (one transaction). Re-check that each pinned upload is still
ready(it must be: the pin blocks delete and expiry), insert theattachmentsrows withupload_id,sha256andsniffed_typefrom the upload, andUPDATE uploads SET use_count = use_count + 1, last_used_at = ?nowfor each distinct upload. If the transaction fails, the copies are deleted with the sent copy (best effort, I9) anduse_countis unchanged. - Content-Type of the part. The declared type, except that a declared
application/octet-streamwith a recognisedsniffed_typeis sent as the sniffed type. - Every exit path unpins.
Idempotency comes first. A replay of an accepted send returns the stored response before step 12 runs, so it succeeds even after the upload expired or was deleted (Q89): the message already holds its own copies.
5. Storage
5.1 Mailbox table
Added to the IdentityMailbox schema with the idempotent statements of
Design § 4, rule 6, in mailbox schema v1 while nothing is built
(Data model):
CREATE TABLE IF NOT EXISTS uploads (
id TEXT PRIMARY KEY, -- upl_
filename TEXT NOT NULL, -- sanitised: no path, ≤ 255 bytes
content_type TEXT NOT NULL, -- as declared, ≤ 127 bytes
size INTEGER NOT NULL CHECK (size BETWEEN 1 AND 26214400),
sha256 TEXT NOT NULL, -- lower-case hex, 64 characters
status TEXT NOT NULL CHECK (status IN ('pending','uploading','ready','rejected','expired')),
status_reason TEXT CHECK (status_reason IN ('risk','infected','expired','too_many_attempts','object_missing')),
sniffed_type TEXT,
risk TEXT CHECK (risk IN ('executable','macro','encrypted_archive','archive_bomb',
'type_mismatch','encrypted_document')),
scan_status TEXT NOT NULL DEFAULT 'skipped' CHECK (scan_status IN ('skipped','clean','infected','error')),
r2_key TEXT NOT NULL, -- t/{ten}/i/{idn}/up/{upl}
object_state TEXT NOT NULL DEFAULT 'absent' CHECK (object_state IN ('absent','present','deleting')),
put_token TEXT, -- the PUT holding the claim (status uploading)
lease_until INTEGER, -- the claim lapses after this time
failed_puts INTEGER NOT NULL DEFAULT 0, -- counted failures; the 10th expires the upload
use_count INTEGER NOT NULL DEFAULT 0,
last_used_at INTEGER,
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
ended_at INTEGER -- when it became rejected or expired
);
CREATE INDEX IF NOT EXISTS uploads_status_expiry ON uploads(status, expires_at);
CREATE INDEX IF NOT EXISTS uploads_created ON uploads(created_at, id);
-- attachments gains the upload a sent attachment was copied from (NULL for inline base64 and inbound):
ALTER TABLE attachments ADD COLUMN upload_id TEXT; -- upl_; not a foreign key: the upload may be gone
SQLite has no ADD COLUMN IF NOT EXISTS, so the migration runs the ALTER TABLE only when
pragma_table_info('attachments') has no upload_id row, which keeps the migration idempotent under
rule 6. meta gains alarm:uploads (the earliest pending upload wake-up). Every column has a writer and
a reader: the routes and CompleteUpload write the row; the routes, step 12, the alarm, the storage
report and erasure read it; attachments.upload_id is written at step 7 and read by the Message object
and by erasure (§9).
R2 gains one key, added to Data model § 4:
| Key | Content | Custom metadata | Deleted by |
|---|---|---|---|
t/{ten}/i/{idn}/up/{upl} | Upload bytes, verified against the declared SHA-256 | tenant, identity, upload, sha256 | Rejection, expiry, DELETE, erasure; the orphan sweep after 15 days when no row names it |
5.2 Mailbox operations
New MailboxRequest variants; each runs one transaction_sync:
| Operation | Does |
|---|---|
CreateUpload { new_upload, max_live: 100, max_live_bytes: 524288000 } | The limit checks and the insert (§3.1 step 5); arms alarm:uploads for expires_at → Created(Upload) or LimitReached { limit, used, max } |
GetUpload { id }, ListUploads { status, after, limit } | Reads |
DeleteUpload { id } | InUse when pinned; otherwise deletes the row and returns the key for the caller to delete after commit → Deleted { r2_key }, InUse, NotFound |
BeginUpload { id, put_token, now } | §3.2 step 5 → Claimed(Upload), InProgress { retry_after }, Ready(Upload), Rejected(Upload), Expired(Upload), NotFound |
RenewUpload { id, put_token, now } | lease_until = now + 120 s while put_token holds the claim → Renewed or Lost |
FailUpload { id, put_token, counted } | Back to pending; failed_puts + 1 when counted; the 10th sets expired (too_many_attempts, object_state = 'deleting') |
CompleteUpload { id, put_token, sniffed_type, risk, scan_status, now } | §3.2 step 10 → Ready(Upload), Rejected(Upload), Expired(Upload), Lost, NotFound |
UploadPlan { after, limit }, EraseUploads { ids } | For erasure of a held identity (§9): lists the uploads with their keys, then deletes the rows → count |
The Submit path reads and updates uploads inside the mailbox; it needs no new operation. Drafts, which
also live in the mailbox, copy an upload with the internal function uploads::copy_to(id, dest_key)
(the step 6 copy), not through an RPC, holding the upload’s pin (§6) and a draft_att marker on the
destination for the length of the copy (Drafts § 7).
5.3 The alarm:uploads purpose
The mailbox’s single alarm (Design § 4, rule 5) gains
alarm:uploads, set to the earliest of: the expires_at of a pending, uploading or ready upload,
ended_at + 7 days of a tombstone, and now + 60 s while any row has object_state = 'deleting'. The
handler:
- sets
expired(status_reason = 'expired',ended_at = now,object_state = 'deleting'when the object may exist) on every live upload whoseexpires_athas passed and that is not pinned (a pinned one is retried 60 seconds later); - after commit, deletes the objects of
deletingrows and sets themabsent(a failed delete staysdeletingand is retried); - deletes tombstones whose
ended_atis more than 7 days old and whose object isabsent; - re-arms.
The daily alarm:maintenance runs the same handler, so a missed alarm is caught within a day, and
MailboxRequest::Hygiene (the tenant retention job’s hygiene step,
Privacy § 5.2) runs it too.
5.4 Reuse: copy, never reference
A ready upload can be attached by any number of sends of its identity until it expires. Each accepted
message gets its own copy (§4.4), and each draft revision that references an upload copies it when the
revision is saved, so:
- deleting or expiring an upload never changes a sent message, a queued message waiting for transport, or a saved draft;
- erasing a message erases its copy without touching other messages that used the same upload;
storage_gbcounts each copy, which is what is stored.
The cost is storage: one 25 MiB file sent to ten separate threads is stored ten times (plus the upload until it expires). A shared, reference-counted object would save that, but it would couple the lifetime of every message to every other message that used the file, and an erasure of one message could not delete bytes another message still points to. Pylota Identity chooses the simpler rule.
6. Concurrency and idempotency
- One mailbox, one writer. Every state change is a
transaction_syncinside the identity’sIdentityMailbox, so two creates racing for the 100th place, or aDELETEracing aPUT, are ordered. - One
PUTat a time per upload. The claim (put_token,lease_until) admits onePUT; a second gets409 upload_in_progresswithRetry-After. R2 would refuse concurrent writes to one key anyway (R2 limits, read 2026-10-17); the claim makes the answer deterministic instead of a race between two streams. - A lost claim. If a
PUT’s lease lapses (the Worker was evicted mid-stream) and anotherPUTclaims the upload, the first one’sRenewUploadorCompleteUploadanswersLost; it stops and answers409 upload_in_progress. If both wrote the object, both wrote the same verified bytes (R2 checked the same declared SHA-256), so whichever completes leaves a correct object. - Retried
PUTs. Afterready:200, body not read. After a write whose response was lost: theheadcheck (§3.2 step 6) finds the verified object and skips the stream. - Pins. The mailbox keeps
upload_pins: HashMap<String, u32>in memory. Step 12 pins each upload a send uses; every exit path ofSubmitunpins (a guard, as forin_flight).DeleteUploadanswersInUsefor a pinned upload, and the expiry handler skips it. A Durable Object has one live instance and loses its memory only together with every request it was running (Reservation inside the mailbox), so a pin can never outlive its send. - Create idempotency.
Idempotency-Keyon create uses the existing per-key, per-tenant records (REST API › Idempotency): the record stores the upload ID; a replay re-reads the upload and mints a new URL; the same key with a different body gets409 idempotency_conflict. Without a key, a retried create makes a secondpendingupload that expires unused. - Send idempotency. Unchanged: the key is checked before step 12 (§4.4), and the fingerprint covers
the request body, which names the
upload_id, so a retry with the same key and body attaches the same bytes. A draft sent withdraft:{draft_id}:{revision}attaches the revision’s copies.
7. Failure handling
| Failure | Behaviour |
|---|---|
D1 unavailable (create or PUT) | 503 unavailable; nothing written; a PUT is not counted |
TenantQuota unavailable (create’s storage gate) | 503 unavailable; nothing written |
| Mailbox call fails or times out (create) | 503 unavailable; a create that committed but whose answer was lost leaves a pending upload that expires unused, or that an Idempotency-Key replay returns |
Client disconnects mid-PUT | R2 stores nothing (IncompleteBody or the stream error); the failure is counted; the upload returns to pending |
Worker evicted mid-PUT | The lease lapses after at most 120 seconds; the next PUT claims; an object that was fully written is found by the head check |
R2 refuses the write (10058, 5xx) | 503 unavailable, not counted |
R2 digest mismatch (10037) | 400 upload_checksum_mismatch, counted, nothing stored |
| Classification has no memory room within 10 s | 503 unavailable, not counted; object kept; the retry skips the stream |
| Scanner error, timeout, non-2xx, bad body | scan_status = 'error', upload ready, upload_scanner_error_total incremented |
CompleteUpload fails after the object was written | 503 unavailable; the upload is still uploading until the lease lapses, then pending; the retry’s head check finds the object and only classification runs again |
Object delete fails (rejection, expiry, DELETE) | object_state stays deleting (for DELETE, the row is gone and the object is left to the orphan sweep after 15 days); the alarm retries every 60 seconds |
| Upload object missing when a send reads or copies it | The upload is set expired (object_missing); the send gets 410 upload_expired; uploads_expired_total{reason=object_missing} |
| R2 copy fails or times out at submit step 6 | The existing step 6 rule: everything written is deleted, reservations released, 503 unavailable or 504 timeout; nothing committed |
| Step 7 transaction fails | Copies and sent copy deleted (best effort, I9); use_count unchanged |
8. Security and visibility
- Permissions. Either
messages:sendordrafts:write.RouteSpecgainsany_permissions: &'static [Permission](when not empty, the key must hold at least one of them), checked at step 2 of Security § 5.2 likepermissions; a key with neither gets403 permission_deniedwithdetails.required = ["messages:send", "drafts:write"]anddetails.any_of = true. The check reads only the key and the path, so it leaks nothing. - Key levels. Every level can use the routes within its scope. An identity key reaches only its own
identity’s uploads; another identity’s upload ID in the path or in an attachment item is
404 upload_not_found, as for a missing one. A partner key reaches uploads only of its own tenants’ identities; a tenant of another partner answers as missing (Security › Partner keys). - Same identity only. An upload is attachable only by sends of the identity that owns it, whatever key level sends. A tenant key cannot attach identity A’s upload to a send from identity B; it uploads again for B. This keeps every upload inside one mailbox, one erasure scope and one storage report.
- The upload URL. A bearer capability for one upload’s declared bytes, valid for at most one hour,
signed by the
linkkey (Security § 7.3). It cannot read anything, cannot write other bytes than those declared, and stops working when the upload leavespendingoruploading. Rotating the link key invalidates outstanding upload URLs after the key’s verify window, as for attachment links;GETof the upload mints a new one. The URL is never logged (the token is replaced by its first 8 characters in logs, as for other signed links). - What the
PUTchecks without an API key. Everything a keyed write checks about the target: the identity and tenant state, the partner’s suspension, the freeze and containment states (§3.2 step 3). A suspended tenant cannot finish an upload begun before the suspension. - Untrusted content.
filenameis untrusted: it is sanitised, never used as an R2 key, path, header value or log field, and is returned like an attachment filename. The R2 key uses only IDs. - Classification before use. No upload byte reaches a recipient before
core::attach(and the scanner, when configured) has seen it. A rejected upload’s bytes are deleted at once. Inline base64 attachments are classified too (§4.1). - No fetches. The Worker never makes an outbound request on behalf of an upload except to the
operator’s scanner (
PM_SCANNER_URL, fixed by the operator, under the SSRF guard). - Attack suite. The four routes and
PUT /v1/links/{token}joinit::security::cross_tenant_matrix(Testing § 7): a foreign upload ID in every route and in an attachment item answers exactly as a missing one; an upload token of tenant A replayed with an upload ID of tenant B fails the MAC.
9. Privacy
Inventory. One new store: mailbox uploads rows and their R2 objects under t/{ten}/i/{idn}/up/.
They hold file contents the agent chose to send, and filenames. Each lives at most 7 days (less when the
tenant’s or identity’s retention is shorter, §3.1 step 6), plus a 7-day tombstone that holds metadata
only (filename, size, digest, state), never bytes. Privacy § 2 gains the
row.
Retention. Uploads are not mail, so the message retention cutoffs do not select them; their own expiry
deletes them (§5.3), and the expiry is capped by retention.message_days and retention.message_hours
so an upload never outlives the mail retention its identity has. The tenant retention job’s hygiene
step runs the same sweep as a backstop.
Erasure. Every scope deletes uploads, and the receipt counts them in a new field
uploads_deleted (rows deleted); their objects count in r2_objects_deleted as today.
| Scope | What happens to uploads |
|---|---|
| Message, thread | ErasurePlan also returns, for each message to erase, the r2_key of every upload its attachments were copied from (attachments.upload_id) that still exists; the job deletes those objects with the message’s keys, and EraseRows deletes those upload rows in the same transaction and returns their count. The upload is deleted even if other messages used it: their copies are their own (§5.4), and a file sent to the person whose data is erased should not stay staged for more sends |
| Counterparty | As message scope, for every message that matches the counterparty |
| Identity (no holds) | The prefix delete of t/{ten}/i/{idn}/ removes the objects; CountAll counts the uploads rows for the receipt before WipeAll |
| Identity (holds) | Uploads are never held: a legal hold covers threads, and an upload belongs to none. After the held-thread pass, UploadPlan and EraseUploads delete every upload of the identity and its objects |
| Tenant | Covered by the identity steps and sweep_r2 |
| People (console accounts) | No upload is tied to a person |
A message erased by any scope also loses its own attachment copies (out/{msg}/a/{att}), as today.
Legal holds. Not applicable to uploads (above). A held thread keeps its messages’ attachment copies, which is where sent bytes live.
Subject-access export. Uploads are not exported: they are a staging area that empties within 7 days, not correspondence, and the bytes of every upload that was sent are in the exported messages’ attachments. Privacy § 11 lists unsent uploads as not exported.
Backup bucket. The nightly backup job skips keys under …/up/
(Privacy § 5.4): restoring an upload is never useful, and a copy
would outlive the upload’s expiry. Retention and erasure deletes still go to both buckets, as for every
t/ key.
Orphans. The mailbox’s rolling orphan sweep (Inbound › Orphan objects)
names uploads.r2_key among the keys that keep an object alive; an object under up/ that no row names
is deleted once it is 15 days old.
Restore. A point-in-time restore can bring back uploads rows whose objects were deleted since. The
first PUT, attach or alarm that finds the object missing sets the upload expired
(object_missing); nothing is sent with missing bytes. A re-applied erasure
(Privacy § 6) deletes restored upload rows by the same rules.
Logs. Upload ID, size, status, result codes and durations; never the filename, the token or the digest.
10. Billing and limits
- Storage. Upload bytes count toward
storage_gb. The mailbox’s storage report (Plans, metering and billing › Storage) addsSUM(size)ofuploadsrows whoseobject_stateispresentordeleting.CompleteUploadsetspresentforready; rejection, expiry and deletion setdeletinguntil the object is gone. - Gate. Create is refused with
402 billing_limit(storage_gb) while storage is at or over its allowance (§3.1 step 4); aPUTis not gated, because its create already was and the bytes were declared. Sends with attachments are gated at policy step 18 as today. - No new allowance. Uploads are not a plan feature; there is no count to buy.
- Limits (Pylota Identity choices). 26,214,400 bytes per upload; 100 live uploads and 524,288,000
declared bytes of live uploads per identity (
422 upload_limit_reached);expires_in900–604,800 seconds, default 86,400; upload URL 3,600 seconds; 10 counted failedPUTs; 26,214,400 decoded attachment bytes per message; tombstones 7 days. They are fixed, not policy: they bound memory, storage and work per identity, and no tenant needs them higher for staging. - Test tenants. Uploads behave the same; the send’s recipients are already limited to the simulator and loopback (policy step 8).
11. Events and audit
No event and no audit row. An upload is a step inside a client’s own request flow: the client learns
every outcome from the PUT response or a GET, and the result that matters to others (a sent message
with attachments) is already evented as message.sent. Uploads can be created at a high rate, so audit
rows would bury the decisions the audit log exists for. The send’s own events and the message’s
attachments[].upload_id record which upload a message used.
12. Console
No uploads screen. Uploads are a programmatic staging step with a lifetime of hours, and the console never
sends mail with attachments. Their bytes appear in the workspace’s storage meter on /console/plan and the
Overview through the storage report. A sent message’s attachments are shown and downloaded as today.
13. MCP and CLI
MCP. A model cannot stream a file through MCP: tool arguments are JSON, and a file is base64 text the model must produce (§1). So:
mail_sendandmail_replyaccept attachment items of both shapes (oneOf), andmail_forwardgainsattachmentswith both shapes. Small files the model already holds as text (a generated CSV, a short note) can still go inline. The MCP body limit stays 7 MiB.mail_create_upload(messages:sendordrafts:write,RL_API): argumentsidentity,filename,content_type,size,sha256, optionalexpires_inandidempotency_key; callsPOST /v1/identities/{id}/uploads. Its description tells the model how to finish: “Then send the file bytes with an HTTP PUT to upload_url (for examplecurl -sS -T FILE -H 'Content-Type: application/octet-stream' "$UPLOAD_URL") from a tool that can read the file, and pass upload_id in attachments. The URL works for one hour and only for these exact bytes.” Annotations:readOnlyHint: false,destructiveHint: false,idempotentHint: false(trueonly whenidempotency_keyis given; MCP annotations are per tool, so it is declaredfalse),openWorldHint: false.mail_get_upload(messages:sendordrafts:write,RL_API): argumentupload_id(andidentity); callsGET …/uploads/{upload_id}; read-only annotations. It returns a freshupload_urlwhile the upload ispendingoruploading.- No tool deletes an upload: an unused upload expires on its own, and a model has no reason to delete one.
- In
tools/list, both tools appear when the key holdsmessages:sendordrafts:write(MCP § 3).
CLI.
idvy uploads create FILE [--identity ID] [--filename NAME] [--content-type TYPE] [--expires-in SECONDS]
idvy uploads list [--identity ID] [--status STATUS]
idvy uploads get UPLOAD_ID [--identity ID]
idvy uploads delete UPLOAD_ID [--identity ID]
create reads the file, computes its size and SHA-256 locally, creates the upload, PUTs the bytes
(retrying the PUT on 503 and 409 upload_in_progress after Retry-After, at most 5 times), and
prints the upload ID and expires_at. The content type defaults to the file extension’s
(application/octet-stream when unknown). idvy send --attach FILE (and reply, forward) sends files
of at most 1,048,576 bytes inline and uploads larger ones first (a Pylota Identity choice: below 1 MiB the
extra round trips cost more than the base64).
14. Observability
New metrics (Observability § 3.2):
| Metric | Kind | Labels | Emitted by |
|---|---|---|---|
uploads_created_total | counter | – | handlers/uploads.rs |
upload_puts_total | counter | result: ready, already_ready, rejected_risk, rejected_infected, size_mismatch, checksum_mismatch, incomplete, in_progress, expired, not_found, unavailable | handlers/links.rs |
upload_bytes | observation | – | handlers/links.rs, per ready upload |
upload_put_ms | observation | – | handlers/links.rs: request start → response |
upload_classify_wait_total | counter | result: immediate, waited, timed_out | uploads/classify.rs |
upload_scanner_error_total | counter | – | uploads/classify.rs |
uploads_expired_total | counter | reason: expired, too_many_attempts, object_missing | mailbox/uploads.rs |
upload_attachments_total | counter | mode: inline, link | mailbox/submit.rs |
upload_copy_ms | observation | – | uploads/copy.rs, per copied upload |
Alert (Observability § 5.3): upload_object_missing (class B, ticket)
when uploads_expired_total{reason=object_missing} > 0 while no restore freeze is in effect: an object
went missing without a restore, which means a delete bug. Runbook: compare the mailbox’s uploads rows
with BLOBS.list of the identity’s up/ prefix and the orphan sweep’s log.
The Outbound and delivery dashboard gains: upload_puts_total by result, upload_put_ms and
upload_copy_ms p50/p95/p99, and inline against linked attachments.
15. Pylota agentic OS
Who uses uploads. Pylota’s senders that carry files, all from its API server, never from an agent
directly (Pylota’s integration spec, docs/plans/pylota-mail-integration.md in the Pylota repository,
§ 7.3): row 10 (recovery letters, a generated PDF), row 16 (FNOL packs to insurers: forms and
photos), row 20 (insurer invoices). They run under the operator identity keys K3 (identity level,
messages:send, which is enough) or the platform tenant key K5 for Pylota-owned identities. Spec § 7.6
says no sender carries attachments over 5 MiB today; FNOL packs with photos are the first that will.
How. The send service in Pylota’s API server, for each file over 1 MiB: computes SHA-256 from the
bytes it already holds in its own store, creates the upload with an idempotency key
pm:v1:upload:{pendingActionId}:{fileSha256[0..16]}, PUTs the bytes, then sends with
{ "upload_id": … } and the sender’s existing Idempotency-Key. Pylota stores the upload ID and
expires_at on its pending action (mail_outbound_messages row or the pending-action record), and
re-uses the upload for a retry of the same action until it expires.
Agents. Specialists never handle bytes or upload URLs: they pass Pylota file references to Pylota’s
capability route, which uploads server-side. The public-webchat coordinator never reaches uploads at all,
and because Pylota Identity fetches no URLs, text from a webchat or an email can never make the service fetch
and attach a file. An MCP client of Pylota’s own (an operator using Claude with Pylota Identity directly) uses
mail_create_upload and its code tool for the PUT.
Failures in Pylota. 422 attachment_risky fails the pending action with a visible reason in the app
(“attachment refused: {risk}”), never retried. 410 upload_expired and 404 upload_not_found re-upload
once and resend with the same send key: a refused send records nothing, so the key is still free even
though the body now names a new upload_id
(Reservation inside the mailbox).
413 message_too_large on an operator tenant with large_attachments: "refuse" is shown as “attachment
too large for email” with the option to send as links. 409 upload_not_ready never happens in Pylota’s
flow, because it sends only after the PUT returned ready.
What Pylota’s integration spec must add (for its owner):
- § 7.1 (router contract): uploads for files over 1 MiB, the create,
PUTand send order, the upload idempotency key, and the error mapping above (attachment_risky,upload_expired,upload_not_found,upload_not_ready,upload_limit_reached,413 message_too_largewithdetails.reason). - § 7.6: replace “attachments over 5 MiB: none today” with the decision on
large_attachmentsper tenant kind (operator tenants and the platform tenant), since insurers may not accept links that expire afterlink_ttl_hours. - § 4.6 (what Pylota stores): the upload ID and
expires_aton the pending action. - § 6: no key change (K3 and K5 already hold
messages:send); K7 needs none (the auth tenant sends no files). - § 9 (erasure): a renter counterparty erasure deletes the uploads its erased messages were copied from
(receipt
uploads_deleted), and the drafts addressed to the renter with their attachment copies. An upload staged and not yet sent names no recipient, so the renter’s address does not find it; it expires on its own within its lifetime (at most 7 days, §2). Pylota’s own file store is erased by Pylota. - W5.5 tests: an FNOL pack over 5 MiB sent with links, a risky file refused, a retry after expiry.
16. Functional requirements
To add to PRD § 6 as a new group, “Uploads”:
- FR-UPL-1 The service must let a key holding
messages:sendordrafts:writecreate an upload for an identity in its scope, with a filename, a content type, a size of 1–26,214,400 bytes and a SHA-256, and return the upload’s ID, a signed upload URL valid for at most one hour, and itsexpires_at. - FR-UPL-2 The service must accept an upload’s bytes only through its signed upload URL on the
Worker, require a
Content-Lengthequal to the declared size, stream the bytes into R2 without holding the file in memory, and make the uploadreadyonly after R2 has verified the declared SHA-256 and the stored size matches. - FR-UPL-3 The service must classify every upload with the inbound attachment rules and, when a
scanner is configured, scan it before it can be attached; a quarantining risk or an
infectedverdict must reject the upload, delete its bytes and refuse every later use with422 attachment_risky. - FR-UPL-4 The service must let send, reply, reply-all, forward and drafts attach a
readyupload of the sending identity byupload_id, re-check the risk of the final filename, and copy the bytes into the message; an upload of another identity or tenant must be refused exactly as a missing one (404 upload_not_found). - FR-UPL-5 The service must apply the transport limit to attachments from uploads as to inline
ones (
413 message_too_large, or signed links withlarge_attachments: "link"), and must refuse a message whose attachments total more than 26,214,400 decoded bytes. - FR-UPL-6 The service must admit one
PUTper upload at a time, answer a repeatedPUTof areadyupload with success without reading its body, resume an upload whose bytes were stored but whose response was lost, and expire an upload after ten failed attempts caused by the client. - FR-UPL-7 The service must delete every upload and its bytes at its
expires_at(default 24 hours, at most 7 days, never later than the identity’s mail retention), used or not, and deleting or expiring an upload must never change a message or a draft that used it. - FR-UPL-8 The service must limit each identity to 100 live uploads and 524,288,000 declared
bytes, count upload bytes toward
storage_gb, and refuse a create with402 billing_limitwhile storage is at or over its allowance. - FR-UPL-9 The service must delete uploads in every erasure scope, including the uploads that
erased messages were copied from, count them in the receipt as
uploads_deleted, leave them out of subject-access exports and the R2 backup copy, and delete upload objects that no row names. - FR-UPL-10 The service must classify inline base64 attachments of every send with the same rules, so that sending base64 never bypasses the checks an upload gets.
- FR-UPL-11 The service must never fetch attachment content from a URL, and must not offer uploads as a file store: no paths, no download route and no sharing between identities.
- FR-UPL-12 The MCP server must offer
mail_create_uploadandmail_get_uploadand acceptupload_iditems inmail_send,mail_replyandmail_forward; the CLI must offeridvy uploads create|list|get|deleteand use uploads for--attachfiles over 1 MiB.
17. Edge cases
Section Q of the edge-case register (rows Q65–Q108):
| # | Case | Required behaviour | Owner | Test |
|---|---|---|---|---|
| Q65 | Invalid create: size 0 or above 26,214,400; sha256 not 64 hexadecimal characters; filename empty after sanitising or above 255 bytes; content_type not type/subtype or above 127 bytes; expires_in outside 900–604,800 | 400 invalid_request with details.errors[]; nothing stored | S | core::uploads::validate_create, it::uploads::q65_invalid_create |
| Q66 | PUT body ends early: the client disconnects or sends fewer bytes than its Content-Length | R2 stores nothing; the failure is counted; the upload returns to pending; the client (if still connected) gets 400 upload_size_mismatch with reason = "incomplete"; a full PUT afterwards succeeds | S | it::uploads::v_put_stream_contract, it::uploads::q66_partial_body |
| Q67 | PUT without Content-Length (chunked), with a Content-Length other than the declared size, or with a Content-Encoding | 400 upload_size_mismatch (reason content_length or encoding, details.expected, details.received) before any byte is read; not counted | S | it::uploads::q67_framing |
| Q68 | The bytes do not match the declared SHA-256 | R2 refuses the write (10037); 400 upload_checksum_mismatch with details.expected; nothing stored; counted | S | it::uploads::v_put_stream_contract, it::uploads::q68_checksum_mismatch |
| Q69 | Two PUTs of one upload at once | One claims; the other gets 409 upload_in_progress with Retry-After ≥ 1; the stored object has the declared digest | S | core::uploads::state_machine, it::uploads::q69_concurrent_puts |
| Q70 | PUT repeated after the upload is ready (the first response was lost) | 200 with the upload; the body is not read; no second classification or scan | S | it::uploads::q70_put_after_ready |
| Q71 | PUT after expires_at, after the tombstone was purged, or with a URL older than one hour | Past expires_at: the upload is set expired and the PUT gets 410 upload_expired; purged tombstone or expired token: 404 upload_not_found | S | it::uploads::q71_put_after_expiry |
| Q72 | A token that is altered, signed by an unknown or retired kid, of purpose att or export on PUT, or of purpose upload on GET | 404 with the token’s target code (upload_not_found for an upload token), the same body whatever the cause | S | core::uploads::token_purpose, it::uploads::scope_matrix |
| Q73 | The Worker is evicted mid-PUT | The claim lapses within 120 seconds; the next PUT claims it; an object stored complete before the eviction is found by the head check and not streamed again | S | it::uploads::q73_crash_and_resume |
| Q74 | R2 refuses the write: one write per second per key (10058), concurrent writes, or a server error | 503 unavailable (retryable); not counted against the client | S | it::uploads::q74_r2_errors |
| Q75 | No classification memory room for 10 seconds | 503 unavailable, not counted; the object stays; the retry skips the stream and only classifies | S | it::uploads::q75_classify_budget |
| Q76 | The bytes carry a quarantining risk (executable, macro, archive_bomb, encrypted_archive, type_mismatch) | rejected (risk), object deleted at once; the PUT gets 422 attachment_risky with details.risk; every later attach gets the same | S | core::attach::b10_*, it::uploads::q76_risky_rejected |
| Q77 | The bytes are an encrypted_document | ready with risk = "encrypted_document"; attachable and sent | S | it::uploads::q76_risky_rejected |
| Q78 | The scanner answers infected | rejected (infected), object deleted; 422 attachment_risky with details.reason = "infected" | S | it::uploads::q78_scanner_verdicts |
| Q79 | The scanner errors, times out, answers non-2xx or an unparseable body | ready with scan_status = "error"; upload_scanner_error_total incremented; sending is not blocked | S | it::uploads::q78_scanner_verdicts |
| Q80 | A send attaches an upload of another identity, of another tenant, or one that does not exist | 404 upload_not_found with details.index, the same body in all three cases; nothing stored | S | it::uploads::q80_cross_identity_attach |
| Q81 | A send attaches an upload that is pending or uploading | 409 upload_not_ready (retryable) with details.index and details.status | S | it::uploads::q81_attach_states |
| Q82 | A send attaches an expired upload, a rejected one, or a ready one whose expires_at passed before the alarm ran | 410 upload_expired with details.reason; 422 attachment_risky; 410 upload_expired | S | it::uploads::q81_attach_states |
| Q83 | The attach item renames the upload into a risky name (report.pdf → report.exe, .docm, or an extension of a different family from the sniffed one) | 422 attachment_risky with details.index and details.risk; nothing sent | S | core::attach::v19_final_name_reclassified, it::uploads::q83_rename_refused |
| Q84 | An item with both content_base64 and upload_id, with neither, or with upload_id and content_type | 400 invalid_request with details.errors[0].path naming the item | S | it::uploads::q84_attachment_item_shape |
| Q85 | The same upload twice in one send | Both attached; each is its own copy and attachment ID; use_count rises by 1 | S | it::uploads::q84_attachment_item_shape |
| Q86 | Attachments total more than 26,214,400 decoded bytes | 413 message_too_large with reason = "attachments_total", also with large_attachments: "link" | S | it::uploads::q86_totals_and_links |
| Q87 | The composed message is above the transport limit | refuse: 413 message_too_large (transport_limit). link: the largest attachments become links until it fits; the linked copy is verified by SHA-256; the link serves bytes whose digest equals the upload’s | S | core::compose::v23_link_plan_from_sizes, it::uploads::q86_totals_and_links |
| Q88 | DELETE, or expiry, while a send between step 12 and its commit holds the upload | DELETE gets 409 upload_in_use (retryable); expiry waits 60 seconds; the send completes with the bytes | S | it::uploads::q88_pins |
| Q89 | A send replayed with the same key and body after its upload expired or was deleted | The original response with deduplicated: true; nothing re-checked, nothing sent again | S | it::uploads::q89_replay_after_expiry |
| Q90 | A send refused after its copies were written (the step 7 transaction fails, or the thread lock times out) | The copies and sent copy are deleted; use_count unchanged; the upload stays ready and attachable | S | it::uploads::q90_refused_send_cleanup |
| Q91 | Create repeated with the same Idempotency-Key | Same body: 201 with the same upload, a new upload_url and Idempotent-Replayed: true. Different body: 409 idempotency_conflict | S | it::uploads::q91_create_idempotency |
| Q92 | The 101st live upload of an identity, or live declared bytes above 524,288,000, including two creates racing for the last place | 422 upload_limit_reached with details.limit, used and max; never more than the limit stored | S | it::uploads::q92_limits |
| Q93 | Storage at or over the plan’s storage_gb | Create gets 402 billing_limit (storage_gb); a PUT of an existing upload is not gated; exempt and disabled billing skip the gate; upload bytes appear in the storage report | S | it::uploads::q93_storage_gate |
| Q94 | Tenant suspended, partner suspended, identity paused, identity deleting, tenant erasing; a suspension that lands while a PUT streams | Create and PUT get 403 tenant_suspended, 403 partner_suspended, 409 identity_paused; deleting or erasing: create 404 identity_not_found, PUT 404 upload_not_found; the in-flight PUT’s CompleteUpload still completes (the state is checked at the start), and the upload cannot be sent while the suspension lasts | S | it::uploads::q94_suspension_and_pause |
| Q95 | A restore freeze or a read_only containment | Create, PUT and DELETE get 503 unavailable with details.reason frozen or read_only; nothing written | S | it::uploads::q95_freeze_and_read_only |
| Q96 | A key with neither messages:send nor drafts:write; an identity key naming another identity; a partner key naming another partner’s tenant | 403 permission_denied with details.required listing both and any_of: true; 404 upload_not_found (or identity_not_found) for the scope cases, as for missing IDs | S | it::uploads::scope_matrix |
| Q97 | Identity erasure or identity deletion, with and without legal holds | Every upload row and object of the identity is deleted; uploads_deleted in the receipt; held threads keep their messages’ attachment copies | S | it::uploads::erasure_scopes |
| Q98 | Message, thread or counterparty erasure of a message whose attachments came from uploads | The source uploads still present are deleted and counted; other messages’ copies of the same upload are untouched | S | it::uploads::erasure_scopes |
| Q99 | Tenant erasure; a PUT during it | Every upload is gone after sweep_r2; the PUT gets 404 upload_not_found | S | it::uploads::erasure_scopes |
| Q100 | A point-in-time restore brings back an upload row whose object was deleted | The first PUT, attach or alarm that finds the object missing sets expired (object_missing); a send gets 410 upload_expired; nothing is sent with missing bytes; the upload_object_missing alert fires only outside a freeze | S | it::uploads::q100_restore_object_missing |
| Q101 | An object written whose row never recorded it (crash before CompleteUpload), or a row deleted whose object delete failed | A retried PUT finds the stored object; the orphan sweep deletes an object no row names once it is 15 days old; the nightly backup job never copies …/up/ keys | S | it::uploads::q101_orphans_and_backup |
| Q102 | A test tenant | Uploads behave the same; a send attaching one reaches only simulator and loopback recipients (policy step 8) | S | it::uploads::q102_test_tenant |
| Q103 | A dry run with upload attachments | Upload states and sizes validated and the link plan computed; no byte read, nothing copied, no pin left, use_count unchanged | S | it::uploads::q103_dry_run |
| Q104 | expires_in longer than the tenant’s retention.message_days or the identity’s retention.message_hours | expires_at capped and shown in the response; the upload is deleted at that time; its tombstone answers 410 for 7 days, then 404 | S | core::uploads::validate_create, it::uploads::expiry_alarm |
| Q105 | An inline base64 attachment with a quarantining risk | 422 attachment_risky with details.index and details.risk; nothing stored (a change from earlier behaviour, where outbound attachments were not classified) | S | it::uploads::inline_base64_classified |
| Q106 | A forward with include_attachments: true and attachments from uploads | Both sets attached; the total and transport limits count both | S | it::uploads::q106_forward_attachments |
| Q107 | Ten counted failed PUTs | The upload becomes expired (too_many_attempts) and its object is deleted; 503 answers caused by the service are never counted | S | core::uploads::state_machine, it::uploads::q107_attempt_limit |
| Q108 | A draft revision references an upload, and the upload is deleted or expires before the draft is sent | The revision’s save copied the bytes; the draft sends with them; the send’s attachment has the upload’s ID in upload_id | S | it::uploads::q108_draft_reference |
18. Tests
| Test | Proves | Covers |
|---|---|---|
core::uploads::validate_create | Every bound of create (size, digest pattern and lower-casing, filename sanitising and length, content type, expires_in), and the expiry cap by message_days and message_hours | Q65, Q104 |
core::uploads::token_purpose | The upload token round-trips; expiry is min(now + 3,600, expires_at); an att or export token is refused by the upload verifier and an upload token by the others; a flipped bit, a retired kid and a changed upload ID fail; comparison is constant-time (the shared verifier) | Q72 |
core::uploads::state_machine | Every transition of §2: claim from pending and from a lapsed lease, a second claim refused with the remaining lease, ready and rejected only with the claim, counted and uncounted failures, the tenth counted failure, expiry from each live state, tombstone purge after 7 days | §2, Q69, Q107 |
core::attach::v19_final_name_reclassified | classify_name with a sniffed PDF: report.exe, report.docm and report.zip are risky, Report.PDF and scan.pdf are not; an octet-stream declared type is harmless | Q83 |
core::compose::v23_link_plan_from_sizes | encoded_size equals the length of the real base64 encoding with 76-character lines for sizes 0–4,000,000 (property test); the plan replaces the largest attachments first and fits the limit exactly at the boundary | Q87 |
it::uploads::v_put_stream_contract | Against workerd’s R2, not a fake: a request body piped through FixedLengthStream into put with the sha256 option stores an exact body and returns its size and checksum; a short body stores nothing and fails with an error the handler maps; a wrong digest fails with code 10037; head returns the size and the sha256 custom metadata, and the test records whether it also returns checksums.sha256 (§3.2 step 6). Written first in M33; if workerd differs from the page’s assumptions, the page changes before the handler is built | Q66, Q68 |
it::uploads::q65_invalid_create | Each invalid field gets 400 invalid_request with its path; no row exists afterwards | Q65 |
it::uploads::q66_partial_body | A PUT that stops halfway (fault-injected client) leaves no object, counts one failure, returns the upload to pending; a full PUT then makes it ready | Q66 |
it::uploads::q67_framing | Chunked, wrong Content-Length and gzip Content-Encoding get 400 upload_size_mismatch with the right reason; failed_puts unchanged; no R2 call made | Q67 |
it::uploads::q68_checksum_mismatch | Bytes with a different digest get 400 upload_checksum_mismatch; no object; one failure counted | Q68 |
it::uploads::q69_concurrent_puts | Two PUTs started together: one 200, one 409 upload_in_progress with Retry-After; the object’s digest is the declared one | Q69 |
it::uploads::q70_put_after_ready | A second PUT after ready gets 200 without the body being read (the fake request body records no read) and no second scanner call | Q70 |
it::uploads::q71_put_after_expiry | Clock past expires_at: 410 upload_expired and the row is expired; after the 7-day purge: 404; a token minted 61 minutes earlier: 404 upload_not_found | Q71 |
it::uploads::q73_crash_and_resume | A PUT whose Worker is dropped after the R2 write: after 120 seconds the next PUT claims, the head check finds the object, no bytes are re-read, and the upload becomes ready | Q73 |
it::uploads::q74_r2_errors | Fault injection of R2 10058 and a 500 on put: 503 unavailable, failed_puts unchanged, the upload back to pending | Q74 |
it::uploads::q75_classify_budget | With the isolate budget held by another classification for 11 seconds, the PUT gets 503; the retry classifies without streaming; upload_classify_wait_total{result=timed_out} | Q75 |
it::uploads::q76_risky_rejected | One fixture per quarantining risk is rejected with its risk, the object is gone, and attach gets 422 attachment_risky; an encrypted PDF is ready with risk = "encrypted_document" and is sent | Q76, Q77 |
it::uploads::q78_scanner_verdicts | With a fake scanner: clean → ready and scan_status = "clean"; infected → rejected (infected), object deleted; error, timeout, 500 and a non-JSON body → ready with scan_status = "error" and the counter; the request carries the two headers and the whole file | Q78, Q79 |
it::uploads::q80_cross_identity_attach | A send from identity B naming A’s upload, another tenant’s upload and a random upl_ ID gets three byte-identical 404 upload_not_found bodies except request_id | Q80 |
it::uploads::q81_attach_states | Attach of pending and uploading uploads: 409 upload_not_ready; of expired: 410; of rejected: 422; of ready past expires_at with the alarm suppressed: 410; nothing stored in each case | Q81, Q82 |
it::uploads::q83_rename_refused | A clean PDF upload attached as report.exe gets 422 attachment_risky; attached as claim.pdf it is sent with that filename | Q83 |
it::uploads::q84_attachment_item_shape | Both, neither, and upload_id with content_type get 400 with the item’s path; the same upload twice is sent as two attachments with distinct IDs and use_count 1 | Q84, Q85 |
it::uploads::q86_totals_and_links | 26,214,401 bytes of attachments get 413 (attachments_total) under refuse and link; a 20 MiB upload under link is sent as a link whose download has the upload’s digest; under refuse it gets 413 (transport_limit); a 1 MiB upload is embedded | Q86, Q87 |
it::uploads::q88_pins | A send paused (test hook) between step 12 and step 7: DELETE gets 409 upload_in_use; an expiry alarm firing then leaves the upload ready and re-arms; the send completes; after it, DELETE succeeds | Q88 |
it::uploads::q89_replay_after_expiry | Send accepted; upload deleted; the same request and key replayed: the original response with deduplicated: true, one message, one transport call | Q89 |
it::uploads::q90_refused_send_cleanup | With the step 7 transaction made to fail, no out/{msg}/a/* object remains, use_count is 0, and the upload is attachable by the next send | Q90 |
it::uploads::q91_create_idempotency | Same key and body: same upload ID, a different upload_url, Idempotent-Replayed: true; different body: 409 idempotency_conflict | Q91 |
it::uploads::q92_limits | 100 live uploads then the 101st: 422 (count); declared bytes over 524,288,000: 422 (bytes); 20 creates racing for the last 3 places store exactly 3; tombstones do not count | Q92 |
it::uploads::q93_storage_gate | With storage_gb used = granted, create gets 402 billing_limit (storage_gb) and a PUT of an earlier upload succeeds; exempt billing skips the gate; the hourly storage report includes a ready upload’s size and excludes a deleted one | Q93, §10 |
it::uploads::q94_suspension_and_pause | Each state against create and PUT gives the listed code; a suspension set while a PUT streams lets that PUT complete and the send of it gets 403 tenant_suspended | Q94 |
it::uploads::q95_freeze_and_read_only | Under a freeze and under read_only, create, PUT and DELETE get 503 unavailable with the reason; no row or object changes | Q95 |
it::uploads::scope_matrix | The four routes and PUT /v1/links/{token} join it::security::cross_tenant_matrix; an identity key reaches only its own uploads; a key with only drafts:write and one with only messages:send both work; one with neither gets 403 with both listed | Q72, Q96, NFR-SEC-1 |
it::uploads::erasure_scopes | Message, counterparty, identity (with and without a hold) and tenant erasure each delete the uploads they own, report uploads_deleted, and leave other messages’ copies of a shared upload; probes find no up/ object afterwards | Q97–Q99, §9 |
it::uploads::q100_restore_object_missing | A ready row with its object removed: a send gets 410 upload_expired (object_missing), the row is expired, uploads_expired_total{reason=object_missing} is 1, and the alert evaluator fires upload_object_missing only when no freeze is set | Q100 |
it::uploads::q101_orphans_and_backup | An up/ object with no row is kept at 14 days and deleted at 15 by the orphan sweep; the backup job’s copy list never contains an up/ key | Q101 |
it::uploads::q102_test_tenant | In a test tenant, an upload is created, PUT and sent to a simulator address; a send to an outside address gets 403 test_mode_recipient as without uploads | Q102 |
it::uploads::q103_dry_run | dry_run=true with an expired upload gets 410; with ready uploads it returns would_send: true, reads no R2 object (the fake counts zero reads), leaves use_count 0 and no pin | Q103 |
it::uploads::expiry_alarm | An unused upload is expired at expires_at and its object deleted; a failed delete is retried until it succeeds; the tombstone is purged after 7 days; the daily maintenance runs the sweep when the alarm was missed | Q104, §5.3 |
it::uploads::inline_base64_classified | A base64 PE file attached inline gets 422 attachment_risky (executable); a base64 PDF is sent as before | Q105 |
it::uploads::q106_forward_attachments | A forward with the original’s attachments and an upload sends both; with the total above the limit it gets 413 | Q106 |
it::uploads::q107_attempt_limit | Nine digest failures leave the upload pending; the tenth sets expired (too_many_attempts) and deletes nothing that was never stored; ten 503s from R2 leave failed_puts at 0 | Q107 |
it::uploads::q108_draft_reference | A draft revision saved with an upload; the upload deleted; the draft sent: the message carries the bytes and upload_id | Q108 |
it::uploads::lifecycle_and_reads | Create, list (with status), get and delete answers; upload_url only in create and in GET of pending and uploading; Message.attachments[].upload_id set for copied attachments and null otherwise; the metrics of §14 are written | §2, §3, §14 |
it::mcp::upload_tools | mail_create_upload and mail_get_upload are listed for keys with messages:send or drafts:write and not otherwise; they map to their routes and return the same errors; mail_send accepts an upload_id item and refuses an item with both shapes | §13 |
cli::uploads::create_and_attach | Against recorded fakes: idvy uploads create sends the locally computed size and SHA-256, PUTs the file, retries a 409 upload_in_progress after Retry-After, prints the ID; idvy send --attach inlines a 1 MiB file and uploads a 1 MiB + 1 byte file | §13 |
live::uploads::large_attachment_link | On staging: a 20 MiB PDF uploaded and sent with large_attachments: "link" to a Gmail test mailbox arrives with a working link whose download has the upload’s SHA-256; upload_copy_ms for it is recorded for the copy budget | Q87, §4.4 |
19. Milestone
To add to the build plan:
M33 · Uploads (after M7, M9, M14, M15, M16 and M22)
(In build-plan.md this heading is level 2.)
Files: crates/core/src/uploads.rs (create validation, the state machine, the upload-token payload,
expiry arithmetic; pure), crates/worker/src/uploads/{mod.rs, put.rs, classify.rs, copy.rs},
handlers/uploads.rs (the four …/uploads routes), mailbox/uploads.rs (the uploads table, its
operations and alarm:uploads), crates/cli/src/commands/uploads.rs. Mailbox schema: the uploads table
and attachments.upload_id, in mailbox schema v1 while nothing is built
(Data model), as for M29. Hooks in other milestones’ files, each reviewed by that
file’s owner:
| File | Hook |
|---|---|
crates/platform/src/lib.rs, cf/glue.rs (M4) | BlobStore::put_stream(key, BodyStream, len, sha256, meta), head_checksum(key) -> Option<(u64, Option<[u8; 32]>)> and copy(from, to, len, sha256); RequestBody::stream() for the handler; the fakes gain the same, with fault injection for 10037, 10013 and 10058 |
router.rs, auth/routespec.rs (M5) | RouteSpec.any_permissions; the four routes and PUT /v1/links/{token} |
handlers/links.rs (M9) | PUT with purpose upload; GET refuses upload tokens |
mailbox/submit.rs, core/src/compose.rs (M9) | Policy step 12 additions, the size plan from sizes, steps 4, 6 and 7 with uploads, pins; classification of inline base64 items |
core/src/attach.rs (M7) | classify_name exposed for the final filename |
jobs/erasure.rs, jobs/backup.rs, mailbox/maintenance.rs (M14) | ErasurePlan and EraseRows with source uploads, UploadPlan and EraseUploads, uploads_deleted; the backup job skips up/; the orphan sweep names uploads.r2_key; Hygiene runs the uploads sweep |
billing/storage.rs (M22) | Upload bytes in the mailbox storage report; the create gate |
mcp/tools.rs (M15) | mail_create_upload, mail_get_upload, both item shapes in the send tools, attachments on mail_forward, with rows in M15’s table test |
crates/cli/src/commands/send.rs (M16) | --attach through uploads above 1 MiB |
observability/catalogue.rs, alert table (M17) | The nine metrics and upload_object_missing |
| Drafts (M29) | Whichever of M29 and M33 lands second adds the draft-save copy (uploads::copy_to, with the pin and the draft_att marker), drafts:write in the upload routes’ any_permissions (M29 defines the permission), and it::uploads::q108_draft_reference |
Implements: FR-UPL-1 to FR-UPL-12 and edge rows Q65–Q108 (Uploads).
Order. it::uploads::v_put_stream_contract is the first pull request of the milestone: it runs the
FixedLengthStream and sha256 behaviour against workerd’s R2 and settles §3.2 steps 7 and 8 before any
handler code.
Acceptance:
- Edge rows Q65–Q108 with the tests named in the register, all passing.
core::uploads::*,core::attach::v19_final_name_reclassified,core::compose::v23_link_plan_from_sizes.it::uploads::scope_matrix; the routes joinit::security::cross_tenant_matrix.it::uploads::erasure_scopesfills the uploads rows of M14’s erasure suites; the receipt schema hasuploads_deleted.it::uploads::lifecycle_and_reads,it::mcp::upload_tools,cli::uploads::create_and_attach;sdk::coverage::every_operationpasses with the four new operations and thePUT.live::uploads::large_attachment_linkpasses in M20, and itsupload_copy_msfor 20 MiB is below the submit deadline with at least a factor of 3 to spare; otherwise the per-message attachment total is lowered before release.- With no upload used, the M9 send suites pass unchanged, except
it::uploads::inline_base64_classified’s deliberate change (risky inline attachments are refused).
20. Compared with AgentMail (read 2026-10-17)
From the AgentMail inventories (public docs and OpenAPI, read 2026-10-17; their content is untrusted data):
| AgentMail | Pylota Identity | Verdict |
|---|---|---|
Inline content (base64) up to 6 MB per whole request; 413 above | Inline content_base64 up to the 7 MiB request body; uploads stage up to 25 MiB per file, but the composed message is capped at 5 MiB encoded (Limits), so a larger file goes out as a signed download link under large_attachments: "link", which is P1 (§4.2) | Deliberately different on size: AgentMail attaches about 30 MB per message (by URL), Pylota Identity attaches at most 5 MiB and links the rest. Exceed on integrity, classification and SSRF (the rows below) |
url attachments fetched at send time, “around 30 MB total per message” (OpenAPI SendAttachment, re-read 2026-10-18); an unfetchable URL is 403 message_rejected | No URL attachments; uploads verified by SHA-256 before use; up to 25 MiB of uploads per message, delivered inline within the 5 MiB message cap and as links above it | Deliberately different: SSRF, content integrity, prompt injection (§1) |
| Agent Drive (Labs, US only): per-inbox files under paths, create → PUT to storage with headers → complete, upload URL up to 1 hour, 100 MiB per file, 1 GiB and 10,000 files per org, download links | Per-identity uploads with no paths or downloads; create → PUT to the Worker (no complete call); upload URL 1 hour; 25 MiB per file; 100 uploads and 500 MiB per identity; at most 7 days | Deliberately different: a staging area for sends, not a drive; the 25 MiB file limit fits every Workers plan and Email Routing’s inbound size |
Drive: storage verifies size and SHA-256; a retried PUT after success gets 412 | The Worker and R2 verify size and SHA-256; a retried PUT after success gets 200 with the upload; stored-but-unanswered uploads resume without re-sending bytes | Exceed |
Drive: create without read can upload; another key’s file at the same path gets 409 | Same-identity uploads only; another identity’s or tenant’s upload is indistinguishable from a missing one | Match (scope), exceed (no path collisions to leak) |
| Attachment risk handling is not described for sends (Agent Armor covers inbound) | Every upload and every inline attachment is sniffed and risk-classified; uploads also scanned when a scanner is configured; risky files refused | Exceed |
MCP upload_file takes text or base64, about 7 MB through the hosted server | mail_create_upload returns an upload URL that a code tool PUTs to, so no file passes through model output; inline base64 still allowed within 7 MiB | Exceed |
| Drive files deleted with the inbox | Uploads deleted by every erasure scope with receipt counts, by expiry, and excluded from backups | Exceed |
21. Integration
The shared pages carry this page’s contracts: the PRD, the REST API reference and openapi.yaml, the
errors, events, limits and configuration references, the data model, security, workspace policy and the
other design pages it extends, the MCP and CLI references, the edge-case register and the build plan
(ADR 0017 § Integration, integrated on 2026-10-18). A
difference between this page and one of them is a defect: fix both in the same change.