Appearance
0009. Files door: uploads as bus commands, downloads by single-use tokens
Status: proposed (awaiting the security reviewer and the architecture owner). Applies to crates/storage, crates/doors, crates/api-graphql (the HTTP routes), crates/orbit-bin (wiring) and every module that takes or hands out files: first hello (the reference and test harness), core (employee documents) and payroll (payslip PDFs).
Context
Phase 3 modules take files (CVs, receipts, resignation letters, contracts, policy PDFs) and hand them out again. crates/storage already stores them (Blobs: content-addressed, encrypted per tenant, type sniffed, size capped, signed download tokens) but has no HTTP door, and payroll's payslip token (payroll.payslip.pdf) has nowhere to be redeemed. Contract rule 2.2 says every action goes through the bus and Cedar, with no exceptions. A file upload is an action; a download reveals a document. Both must be authorised and audited like any other request.
Decision
- Uploads are bus commands.
POST /files/<module>/<purpose>(web role, sidecar token, doorweb) is a thin adapter: it authenticates the caller, reads the request body (the file's raw bytes, at mostMAX_FILE_BYTES= 10 MiB; the declared content type and any file name are ignored), checks the quota (point 3) and dispatches the bus command<module>.file.upload { purpose, content }(contentbase64). Cedar decides on the resource kind<module>.upload.<purpose>, owned by the uploader, so a module's policy allows each purpose separately; the bus audits it like any command (actor, door, module, and an after state naming the upload id, purpose, blob id, type and size; never the bytes). The multipart format was considered and dropped: one file per request, so the raw body is simpler and needs no parser. - Only the files door uploads. The shared handler (
orbit_storage::uploads::store) refuses any door butweb, and each module's policy forbids<module>.file.uploadunlesscontext.door == "web". GraphQL generates a mutation for the command (every command gets one) but it is always refused; MCP never lists it and an MCP token is never accepted on the files door (MCP tokens work on the MCP door only). - Quota. Per person per tenant, across modules: 50 uploads and 100 MiB per rolling hour (
MAX_UPLOADS_PER_HOUR,MAX_UPLOAD_BYTES_PER_HOUR), counted indoors.file_uploadsby theSECURITY DEFINERfunctiondoors.reserve_uploadunder a per-person lock, before the body is read: the request must declare its size (Content-Length, else 411; overMAX_FILE_BYTES, 413) and that size is charged. A refused upload answers HTTP 429 with codeRATE_LIMITED; an attempt counts even if the command then fails (conservative). Each upload holds the file and its base64 copies in memory, so at most 2 per person and 8 per process are in flight (MAX_UPLOADS_IN_FLIGHT_PER_ACTOR,MAX_UPLOADS_IN_FLIGHT; beyond that 429 before reading). A download takes one of the caller's in-flight request slots. - Types and sizes. Bytes are sniffed: PDF, PNG, JPEG and WEBP only (D9; DOCX, HEIC and anything else is
disallowed_type). Each module declares its purposes (orbit_storage::uploads::Purpose { name, max_bytes, types }):hello.attachment1 MiB, any of the four;core.document10 MiB, any of the four. Over the cap istoo_large. - The module's
uploadstable. Each module that takes files has<module>.uploads (id, tenant_id, person_id, purpose, blob_id, content_type, size, created_at, consumed_at), row-level security on, granted to the module's role only.blob_idis set while the upload is unconsumed and cleared when it is consumed (CHECK ((consumed_at IS NULL) = (blob_id IS NOT NULL))), so a consumed row no longer names the blob. - Consuming. A later command of the same module (
core.document.add,hello.attachment.add) consumes the upload id once (orbit_storage::uploads::consume): only the person who uploaded it, only for the purpose it was uploaded for, only within 24 hours. Another person's id, an unknown id or an expired one isNOT_FOUND; a second consume isCONFLICT(upload_consumed). - Downloads. A module query that authorised the caller on the row and called
ctx.audit_read("<blob column>")issues a token withBlobs::sign(tenant, person, blob, now, ttl):<tenant>.<module>.<person>.<blob>.<expiry>.<nonce>.<hmac>, at most 5 minutes (MAX_TOKEN_TTL).GET /files/<token>(web role, sidecar token, doorweb) verifies it (Blobs::verify_grant) and serves the file only when:- the signature holds and it has not expired;
- its tenant and person are the caller's (a token is useless to anyone else, even in the same tenant; a mismatch is audited as
deniedin the caller's tenant); - its nonce was never redeemed before (
doors.redeem_file_token, kept until the token's expiry; the function also refuses a token expired by the database clock, so a node whose clock runs behind cannot redeem one whose nonce was pruned): a token works once. The response carries the sniffed MIME type,Content-Disposition: attachment,X-Content-Type-Options: nosniff,Content-Security-Policy: sandboxandCache-Control: no-store. The redemption is audited (doors.file.download, outcomeread, fieldblob_id) in the token's tenant. Every refusal isNOT_FOUND, so a probe learns nothing. Refusals of another person's token count against the caller's denial allowance (MAX_DENIALS), so a leaked token cannot fill the audit log, and the request log writes/files/<redacted>in place of the token.
- Deletion. Blob ids are content addresses: two rows can name one blob. A module deletes a blob only when no row of that module (its own tables and unconsumed
uploads) names it, checked underorbit_storage::lock_blob(a transaction-level advisory lock per tenant and blob) in the deleting transaction. Storing takes the same lock, so an upload of the same content either commits first (and the check sees it) or writes the blob again after the delete. Consuming an upload takes it too (held until the consuming row commits), so a deleter that has already checked the module's tables still sees the upload pending. - Sweep. A daily job per module (
<module>.upload.sweep; for core, part of the auditedcore.document.purge.runthe daily job dispatches as the system) deletesuploadsrows unconsumed after 24 hours and then each blob only if unreferenced. Idempotent.
Allow/deny matrix (what the implementation tests)
| Action | Person (member) | Other person, same tenant | Admin | MCP door | API client / agent | Other tenant |
|---|---|---|---|---|---|---|
<module>.file.upload | yes, if the module's policy allows the purpose | n/a | as module policy | never | no | no |
| consume an upload id | own, once | no (NOT_FOUND) | no | no | no | no |
| download token issue | only through a module query that authorised the row | per that query | per that query | refused | no | no |
GET /files/<token> | valid, unexpired, unredeemed own token | refused (token bound to its person) | same rule | no | no | refused |
Consequences
- One mechanism for every module; a module adds a purpose, an
uploadstable, afile.uploadcommand and its own consuming command, and registers itsBlobswith the door (Doors::with_files). - Downloads need the caller's bearer token, so the web app fetches the file and saves it (no plain links). A link pasted elsewhere does nothing.
- The upload body is carried base64 through the bus (a third larger in memory); the audit digest is over that input, never stored.
doors.file_uploadsanddoors.file_redemptionsgrow with traffic; both prune their own expired rows on every call.
Residual risks
- Deleting a blob is not transactional with the database: if the transaction fails after the blob was deleted, a row can name a missing blob (the download is
NOT_FOUND). Rows that can do so are ones being deleted or purged anyway. - The quota counts per person; a tenant with many people can still store a lot. Disk quotas per tenant are an operator concern for now.
- Files are not scanned for malware. They are served as attachments in a sandbox, never inline, and only the four sniffed types are accepted.
- A redemption does not check that the token's module is still enabled for the tenant: a token issued just before the module was disabled still serves its file until it expires (at most 5 minutes, 120 seconds as issued).
Alternatives considered
- A ticket endpoint outside the bus (upload first, then a command takes the ticket): rejected, an action outside Cedar and audit.
- A shared files module owning every blob: rejected, it crosses module data ownership (rule 2.3) and every module would need a published query into it.
- Unbound bearer tokens (tenant only, any holder): rejected, a leaked link would work for anyone with a session in the tenant, and replay would be possible for 5 minutes.