files
Status: implemented (cmd/files, tests). Runs in the apps profile of docker-compose.yml on port 8084
(S3 = Garage, presigned URLs signed for http://localhost:3900) and is reached through the gateway thanks to the
compose GATEWAY_ROUTES entry /v1/files; the gateway built-in default routes do not include it.
Role: generic attachment storage for every module. A file belongs to an owner resource ARN (e.g.
atl:inventory:paris:item/01J…); the service sniffs, converts or sanitises the upload, stores its variants in
S3, keeps the metadata in MongoDB and hands out short-lived presigned download URLs.
Limits: no business meaning of attachments (owners reference file IDs themselves) and no identity documents (borrowing stores those encrypted in its own bucket, see borrowing). No antivirus scan. Files are not encrypted at application level.
Access control
Permissions are derived from the owner ARN, then checked through authz (deny by default, see authorization):
| Operation | Permission on the owner |
|---|---|
| list, metadata, download URL | <service>.<type>.read (projects: projects.file.read) |
| upload, delete | <service>.<type>.update (projects: projects.file.write) |
<service> and <type> come from the owner ARN service and leaf type. A permission absent from the catalogue
answers 403 (files cannot be attached to this resource). Owner rules: valid ARN, not a pattern, not a files
ARN, ID segments [A-Za-z0-9._-]{1,128}. The owner site atl:settings:<site>:site/<site> is always added as a
parent; up to 4 extra parents are accepted, only that site or a project of the same site
(atl:projects:<site>:project/<id>). Parents are stored with the file and reused for later checks.
Delegated authorization
Some owners carry business rules authz cannot express (a borrower on their own sheet, the reporter of an
inventory issue). When the derived check is denied (403; authz errors and anonymous callers are never
delegated) and the owner service has a delegate in FILES_DELEGATES, files calls
POST <delegate>/internal/v1/attachments/authorize with {"owner": "<ARN>", "parents": [...], "action": "read" | "write"} and the caller X-Atl-Principal token (timeout FILES_DELEGATE_TIMEOUT, no caching):
| Delegate answer | Files answer |
|---|---|
| 204 | allowed |
| 401, 403, 404 | 403 (the original problem) |
| anything else, timeout | 503 |
| Delegate | Owner | Rules |
|---|---|---|
| borrowing | atl:borrowing:<site>:sheet/<id> | read: sheet read rule; write: member borrower / creator while the sheet is not returned or cancelled, else borrowing.sheet.update |
| inventory | atl:inventory:<site>:issue/<id> | read: reporter or inventory.item.read on the item; write: reporter, inventory.issue.handle, or inventory.issue.report while open |
/internal/... routes require a principal token and are never routed by the gateway (it only proxies /v1/...).
Upload pipeline
-
Body limits (in memory, no temporary files): body up to
FILES_MAX_BYTES+ 64 KiB, at most 16 parts, form fields up to 1 KiB, exactly onefilepart; unknown fields answer 422. -
Validation: owner and parents (above),
purposematches^[a-z][a-z0-9_]{0,31}$, permission check, non-empty file withinFILES_MAX_BYTES. -
Detection from the bytes (the client content type is ignored); the extension is only used for formats without a reliable magic number:
Format Detection Stored as jpeg, png, gif, webp, heic magic number / ftypbrandwebp original+ webpthumbsvg XML whose root element is svgsanitised svg ( thumbserves the original)pdf %PDF-as is csv, txt extension + valid UTF-8 plain text (no NUL, sniffed text/plain)as is pes, dst extension + #PES/LA:prefixas is jef, exp extension + opaque binary as is Anything else answers 415.
-
Conversion (
media.Process, at mostFILES_CONVERT_WORKERSat once): raster images are rejected aboveFILES_MAX_PIXELSbefore decoding (413), decoded (decoder panics become 422), scaled so the longest side is at mostFILES_MAX_IMAGE_DIMENSION(thumbnail:FILES_THUMB_DIMENSION), JPEG EXIF orientation applied, then re-encoded as lossy webp (quality 82), which drops EXIF and every other metadata. GIF keeps its first frame only. -
SVG sanitising: re-serialised with an element and attribute allow-list;
script,styleelements,foreignObject,a, animations, event handlers, comments, DOCTYPE / entities are dropped; values withjavascript:,expression(,@import... orurl()pointing outside the document are removed;imageonly keeps inlinedata:image/(png|jpeg|gif|webp);base64hrefs, otherhrefs must be#fragments. Depth is capped at 128. -
Storage: every variant is put in S3 with its content type and
Content-Disposition(images inline, everything else attachment, sanitised name). Then one Mongo transaction inserts the metadata, the audit record and thefiles.file.uploadedevent in the outbox. If S3 or Mongo fails, the uploaded objects are removed (leftovers are logged).
The stored name is sanitised (no path, control or reserved characters, max 120 runes) and its extension is the
stored format (.webp for converted images). size, content_type and sha256 describe the stored original.
Downloads, deletion and purge
GET /v1/files/{id}/url?variant=original|thumbpresigns a GET forFILES_URL_TTLagainstS3_PUBLIC_ENDPOINT, forcing content type, disposition andCache-Control: private, max-age=300; the JSON response isno-store. Non-image files have no thumbnail (404variant_not_found).DELETE /v1/files/{id}soft-deletes (deleted_at,purge_at = now + FILES_PURGE_DELAY); deleted files answer 404.- Owner erasure: on
members.member.erased, every live file owned byatl:members:*:member/<id>is soft-deleted (batches of 200, service principal, no permission check). - Purge job: every
FILES_PURGE_INTERVAL, a single replica (leasefiles-purgeinlocks) deletes the S3 objects of files whosepurge_athas passed, then their document, with afiles.file.purgeaudit record.
Dependencies
- Services: authz (
Checkthroughauthzclient); delegates fromFILES_DELEGATES(borrowing, inventory). - Infrastructure: MongoDB (replica set, transactions), Kafka, S3 (Garage locally).
- Libraries:
authzclient,httpx,kafkax,outbox,lease,mongox,s3x(config only),audit,cloudevent,catalog,arn,ids,pagination,principal,problem,config,logging,telemetry(see libraries).
Container builds
The shared services/Dockerfile and make build use CGO_ENABLED=0 go build -tags nodynamic.
This selects the bundled HEIC (WASM) and WebP (transpiled Go) codecs. Without nodynamic, their
optional purego bindings introduce a dynamic loader dependency even with CGO disabled. An Alpine-built
binary then fails in the distroless static image with exec /app: no such file or directory because
/lib/ld-musl-…so.1 is missing, although /app exists.
After updating the build configuration, rebuild and recreate the service:
docker compose build files
docker compose up -d --no-deps --force-recreate files
docker compose logs --tail=50 files
Inputs and outputs
| Kind | Name | Notes |
|---|---|---|
| HTTP | POST/GET /v1/files, GET/DELETE /v1/files/{id}, GET /v1/files/{id}/url | API reference; authenticated principals only; list requires owner (optional parents, max 4), cursor paginated |
| RPC out | authz.v1.AuthzService/Check | permission derived from the owner |
| HTTP out | POST <delegate>/internal/v1/attachments/authorize | delegated authorization (see above) |
| Kafka out | files.file.v1 | files.file.uploaded (id, owner, site, content type, size, purpose, variant names; no file name), files.file.deleted (id, owner, site, purge time); key = file ID, CloudEvents site set except for global owners |
| Kafka out | audit.records.v1 | files.file.upload, files.file.delete, files.file.purge (resource atl:files:<site>:file/<id>, ref owner) |
| Kafka in | members.member.v1 | members.member.erased only, other types ignored (group files) |
| Kafka out | members.member.v1.dlq | erasure events still failing after 5 attempts |
Data
MongoDB database files:
| Collection | Content | Indexes |
|---|---|---|
files | metadata: owner, parents, site, name, content type, size, SHA-256, variants (name, key, type, size, width, height), purpose, uploaded by, created / deleted / purge times | (owner, deleted_at, _id); purge_at (partial, dates only) |
outbox | pending events and audit records | see Kafka |
locks | purge job lease |
S3 bucket FILES_BUCKET (default atelier-files, private), object keys
<service>/<site>/<type>/<owner id>/<file id>/<variant>, where <site> is global for owners with a * site
and <variant> is original or thumb.
Not implemented yet
- Erasure / deletion events of other owners (only
members.member.erasedis consumed); deleting an owner resource in another service leaves its files in place. - No antivirus scan, no application-level encryption (see the limits above).
- No web screen uploads or displays files yet.
Configuration
| Variable | Default | Description |
|---|---|---|
HTTP_ADDR, METRICS_ADDR | :8084, :9184 | Listen addresses |
PRINCIPAL_PUBLIC_KEY | required | Base64 Ed25519 public key verifying X-Atl-Principal |
AUTHZ_URL | http://localhost:8081 | authz ConnectRPC endpoint |
KAFKA_BROKERS | localhost:19092 | Outbox relay and consumer |
MONGO_URI, MONGO_DATABASE | mongodb://localhost:27017/?directConnection=true, files | MongoDB |
S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY | required | S3 API |
S3_REGION | garage | S3 region |
S3_PUBLIC_ENDPOINT | S3_ENDPOINT | Host used to sign URLs handed to browsers |
FILES_BUCKET | atelier-files | Bucket |
FILES_MAX_BYTES | 20971520 | Max upload size (20 MiB) |
FILES_MAX_IMAGE_DIMENSION, FILES_THUMB_DIMENSION | 2560, 320 | Longest side of stored image / thumbnail (thumb not larger than max) |
FILES_MAX_PIXELS | 50000000 | Decompression bomb guard |
FILES_CONVERT_WORKERS | 2 | Concurrent conversions |
FILES_URL_TTL | 5m | Presigned URL lifetime (1 s to 7 days) |
FILES_PURGE_DELAY | 24h | Soft delete to purge delay |
FILES_PURGE_INTERVAL | 15m | Purge job period |
FILES_DELEGATES | empty | service=url,... delegates of attachment authorization (compose: borrowing=http://borrowing:8087,inventory=http://inventory:8085) |
FILES_DELEGATE_TIMEOUT | 3s | Delegate call timeout (up to 1 m) |
Plus the common APP_ENV, LOG_LEVEL, OTEL_EXPORTER_OTLP_ENDPOINT
(see environment variables).