Attachments backend: upload, content-type detection, limits, dated paths (ADR 0028) #539

Closed
opened 2026-10-17 16:44:24 +00:00 by kayg · 5 comments
Owner

Decided by kayg on 2026-10-13 (B7, "agreed. Attachments go to Documents/ or Photos/ by type") and 2026-10-14 (G12: Photos/YYYY/YYYY-MM-DD/, everything else Documents/YYYY/YYYY-MM-DD/, no Others/). ADR 0028 is accepted.

What is missing: #322 only builds the editor side. dev has no attachment upload route, no content-signature detector and no size/bomb limits (only the backdrop upload in crates/backdrops does this).

Scope:

  • upload action through the one action pipeline;
  • type from content signature, extension only for text, never rename, HTML stored as text/plain;
  • size limits and decoders that resist hostile media (image bombs; kayg's "heifheist", unconfirmed meaning);
  • per-user "Already uploaded" check (#409);
  • the dated paths above.

Blocks #322.

Found by the 2026-10-17 decided-but-not-started audit (nothing had an issue, PR or branch). Listed in #174. Grill/ADR rules still apply before foundational code.

Decided by kayg on 2026-10-13 (B7, "agreed. Attachments go to Documents/ or Photos/ by type") and 2026-10-14 (G12: `Photos/YYYY/YYYY-MM-DD/`, everything else `Documents/YYYY/YYYY-MM-DD/`, no `Others/`). ADR 0028 is accepted. What is missing: #322 only builds the editor side. dev has no attachment upload route, no content-signature detector and no size/bomb limits (only the backdrop upload in `crates/backdrops` does this). Scope: - upload action through the one action pipeline; - type from content signature, extension only for text, never rename, HTML stored as text/plain; - size limits and decoders that resist hostile media (image bombs; kayg's "heifheist", unconfirmed meaning); - per-user "Already uploaded" check (#409); - the dated paths above. Blocks #322. Found by the 2026-10-17 decided-but-not-started audit (nothing had an issue, PR or branch). Listed in #174. Grill/ADR rules still apply before foundational code.
Author
Owner

Progress: PR #563 (issue/539-attachments, into dev, not merged).

Built:

  • A new attachments plugin.
  • A streaming upload that goes through the one action pipeline: the bytes are staged, then attachments.add runs as an idempotent, journaled action. There is also an attachments.check action.
  • Content-signature typing with infer 0.22, plus HEIF brands and CR3. The extension only picks between text types, and HTML is stored as text/plain.
  • G12 dated paths: Photos/YYYY/YYYY-MM-DD/ and Documents/YYYY/YYYY-MM-DD/.
  • A 2 GiB default limit (CALTERNAL_ATTACHMENT_MAX_MB), enforced while the bytes stream in.
  • Per-person "Already uploaded" from a blake3 index (ADR 0027).
  • Traversal-proof file access with openat2(RESOLVE_BENEATH) in the new calternal_fsio::beneath module.
  • Unsafe types are served as downloads with nosniff and a sandboxing CSP.
  • Temp file → fsync → rename → fsync of the directory, plus crash markers that the next start sweeps.

Gates:
Commit a2a20c4, on this host (debug build):

== cargo fmt --all --check
fmt ok
== cargo clippy --workspace --all-targets --locked -- -D warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 21.93s   (no warnings)
== cargo test --workspace --locked --no-fail-fast
test exit: 0 — 57 test binaries, every one "test result: ok. … 0 failed"
  calternal (bin, incl. isolation + contract): 12 passed
  attachments_crash: 2 passed
  calternal-attachments: 19 passed
  calternal-fsio: 12 passed
  calternal-notes: 202 passed, 1 ignored (unchanged)
upload 1024 MiB in 2.86 s = 358 MiB/s; RSS before 27528 KiB, peak during 35796 KiB, after 33076 KiB, high-water 35140 KiB

Throughput benchmarks on tmpfs and disk (debug and release) are waiting for a build slot; I'll post them next.

**Progress:** PR #563 (`issue/539-attachments`, into dev, not merged). Built: - A new `attachments` plugin. - A streaming upload that goes through the one action pipeline: the bytes are staged, then `attachments.add` runs as an idempotent, journaled action. There is also an `attachments.check` action. - Content-signature typing with `infer` 0.22, plus HEIF brands and CR3. The extension only picks between text types, and HTML is stored as `text/plain`. - G12 dated paths: `Photos/YYYY/YYYY-MM-DD/` and `Documents/YYYY/YYYY-MM-DD/`. - A 2 GiB default limit (`CALTERNAL_ATTACHMENT_MAX_MB`), enforced while the bytes stream in. - Per-person "Already uploaded" from a blake3 index (ADR 0027). - Traversal-proof file access with `openat2(RESOLVE_BENEATH)` in the new `calternal_fsio::beneath` module. - Unsafe types are served as downloads with `nosniff` and a sandboxing CSP. - Temp file → fsync → rename → fsync of the directory, plus crash markers that the next start sweeps. Gates: Commit a2a20c4, on this host (debug build): ``` == cargo fmt --all --check fmt ok == cargo clippy --workspace --all-targets --locked -- -D warnings Finished `dev` profile [unoptimized + debuginfo] target(s) in 21.93s (no warnings) == cargo test --workspace --locked --no-fail-fast test exit: 0 — 57 test binaries, every one "test result: ok. … 0 failed" calternal (bin, incl. isolation + contract): 12 passed attachments_crash: 2 passed calternal-attachments: 19 passed calternal-fsio: 12 passed calternal-notes: 202 passed, 1 ignored (unchanged) upload 1024 MiB in 2.86 s = 358 MiB/s; RSS before 27528 KiB, peak during 35796 KiB, after 33076 KiB, high-water 35140 KiB ``` Throughput benchmarks on tmpfs and disk (debug and release) are waiting for a build slot; I'll post them next.
Author
Owner

Benchmarks (tests/attachments_crash.rs::a_big_upload_streams_in_bounded_memory; real binary, raw HTTP/1.1 client on loopback, 1 MiB writes; on this dev host, under load from trains and other builds):

build target size time throughput peak RSS above idle (that upload / a 64 MiB one)
debug tmpfs 1 GiB 2.86–5.41 s 189–358 MiB/s 6.4–10.8 MiB / 5.8–9.4 MiB
debug disk (sda, ext4) 1 GiB 34.6 s 30 MiB/s 6.4 / 9.4 MiB
debug disk 2 GiB 62.3 s 33 MiB/s 9.8 / 5.8 MiB
release tmpfs 1 GiB 3.12 s 328 MiB/s 7.8 / 8.8 MiB
release disk 1 GiB 21.5 s 48 MiB/s 7.2 / 8.1 MiB
release disk 2 GiB 43.1 s 47 MiB/s 10.0 / 6.4 MiB
  • Memory is bounded and flat with size. A 64 MiB upload and a 2 GiB one peak the same, 6–11 MiB above idle. That is the 1 MiB fsync'd disk step, up to 4 queued network chunks, and allocator slack. Steady RSS after an upload is +3–4 MiB, and malloc_trim takes it back after a quiet spell. The test asserts an absolute bound of < 16 MiB; the relative check was noise (±4 MiB).
  • Throughput: on tmpfs, ~330 MiB/s in release. On disk, the host's shared sda while trains ran: ~48 MiB/s. That is bound by the deliberate fsync per 1 MiB step from #298 (paced), so note saves never wait behind an upload. The perf VM is the place for real numbers (zvol layouts).
  • Kill -9: kill_nine_mid_upload_leaves_nothing_half_written passes 3/3 runs, three kills each.

Commit 3d3dfea swaps the flaky relative memory assertion for the absolute bound. Re-verified there: fmt clean, clippy clean on the workspace, attachments_crash 3/3.

**Benchmarks** (`tests/attachments_crash.rs::a_big_upload_streams_in_bounded_memory`; real binary, raw HTTP/1.1 client on loopback, 1 MiB writes; on this dev host, under load from trains and other builds): | build | target | size | time | throughput | peak RSS above idle (that upload / a 64 MiB one) | |---|---|---|---|---|---| | debug | tmpfs | 1 GiB | 2.86–5.41 s | 189–358 MiB/s | 6.4–10.8 MiB / 5.8–9.4 MiB | | debug | disk (sda, ext4) | 1 GiB | 34.6 s | 30 MiB/s | 6.4 / 9.4 MiB | | debug | disk | 2 GiB | 62.3 s | 33 MiB/s | 9.8 / 5.8 MiB | | release | tmpfs | 1 GiB | 3.12 s | 328 MiB/s | 7.8 / 8.8 MiB | | release | disk | 1 GiB | 21.5 s | 48 MiB/s | 7.2 / 8.1 MiB | | release | disk | 2 GiB | 43.1 s | 47 MiB/s | 10.0 / 6.4 MiB | - **Memory is bounded and flat with size.** A 64 MiB upload and a 2 GiB one peak the same, 6–11 MiB above idle. That is the 1 MiB fsync'd disk step, up to 4 queued network chunks, and allocator slack. Steady RSS after an upload is +3–4 MiB, and `malloc_trim` takes it back after a quiet spell. The test asserts an absolute bound of < 16 MiB; the relative check was noise (±4 MiB). - **Throughput:** on tmpfs, ~330 MiB/s in release. On disk, the host's shared sda while trains ran: ~48 MiB/s. That is bound by the deliberate fsync per 1 MiB step from #298 (`paced`), so note saves never wait behind an upload. The perf VM is the place for real numbers (zvol layouts). - **Kill -9:** `kill_nine_mid_upload_leaves_nothing_half_written` passes 3/3 runs, three kills each. Commit 3d3dfea swaps the flaky relative memory assertion for the absolute bound. Re-verified there: fmt clean, clippy clean on the workspace, `attachments_crash` 3/3.
Author
Owner

Open questions for kayg. Each one shipped with the default shown; nothing below blocks the PR.

  1. Size limit. The default is 2 GiB per plain upload (CALTERNAL_ATTACHMENT_MAX_MB). Bigger files would wait for tus and the IETF draft (F4, LQ3). Is that the right number, and is there a per-person quota for the public cloud?
  2. Videos go to Photos. G12 says "Photos by type"; I put every video/* there, as Immich and iOS do. Audio, SVG, icons, PSD and CAD go to Documents.
  3. Which day files an upload. The client's date decides; without it, UTC today. It is not the EXIF capture date. PH26 has Photos in YYYY/YYYY-MM-DD folders: by upload day, or by capture day once EXIF is read?
  4. "Already uploaded" keeps no second copy. The same bytes return the existing path with already: true, even under a different name. Should a person be able to keep a deliberate copy anyway, for example from a "Keep both" button?
  5. PDF CSP. PDFs are served inline with default-src 'none' and without sandbox, because browser PDF viewers refuse to run in a sandbox. Everything else gets sandbox. Is that acceptable, or should PDFs be downloads too?
  6. Symlinked Photos or Documents. Opening with RESOLVE_BENEATH refuses a symlink that leaves the Home, for example Photos -> /mnt/hdd. Tiering (ADR 0027) would need a bind mount instead of a symlink. OK?
  7. Leading dots are removed from uploaded names (.env becomes env), so nothing becomes hidden. "Never rename" is otherwise kept.
  8. "heifheist" is still unconfirmed. This PR decodes nothing, so a malicious HEIC can't reach a parser. The pixel size comes from the header only (imagesize). Thumbnails wait for the PH22 sandbox.
  9. No upload timeout or concurrency cap yet. A slow client holds one hidden temporary file and a few MiB until it finishes or drops. Add a per-person limit on uploads in flight?
**Open questions for kayg.** Each one shipped with the default shown; nothing below blocks the PR. 1. **Size limit.** The default is 2 GiB per plain upload (`CALTERNAL_ATTACHMENT_MAX_MB`). Bigger files would wait for tus and the IETF draft (F4, LQ3). Is that the right number, and is there a per-person quota for the public cloud? 2. **Videos go to Photos.** G12 says "Photos by type"; I put every `video/*` there, as Immich and iOS do. Audio, SVG, icons, PSD and CAD go to Documents. 3. **Which day files an upload.** The client's `date` decides; without it, UTC today. It is not the EXIF capture date. PH26 has Photos in `YYYY/YYYY-MM-DD` folders: by upload day, or by capture day once EXIF is read? 4. **"Already uploaded" keeps no second copy.** The same bytes return the existing path with `already: true`, even under a different name. Should a person be able to keep a deliberate copy anyway, for example from a "Keep both" button? 5. **PDF CSP.** PDFs are served inline with `default-src 'none'` and without `sandbox`, because browser PDF viewers refuse to run in a sandbox. Everything else gets `sandbox`. Is that acceptable, or should PDFs be downloads too? 6. **Symlinked Photos or Documents.** Opening with `RESOLVE_BENEATH` refuses a symlink that leaves the Home, for example `Photos -> /mnt/hdd`. Tiering (ADR 0027) would need a bind mount instead of a symlink. OK? 7. **Leading dots are removed** from uploaded names (`.env` becomes `env`), so nothing becomes hidden. "Never rename" is otherwise kept. 8. **"heifheist"** is still unconfirmed. This PR decodes nothing, so a malicious HEIC can't reach a parser. The pixel size comes from the header only (`imagesize`). Thumbnails wait for the PH22 sandbox. 9. **No upload timeout or concurrency cap yet.** A slow client holds one hidden temporary file and a few MiB until it finishes or drops. Add a per-person limit on uploads in flight?
Author
Owner

Update (baec5b1, pushed to #563): the memory test now uploads 128 MiB by default, so workspace runs and trains stay fast: the attachments_crash suite takes about 1.7 s, down from 6–9 s. The 16 MiB bound still means something at that size: a buffered 128 MiB upload would peak eight times over it. The 1 GiB run moved to the nightly job (~/codex-jobs/_lib/nightly-matrix.sh, replaced with mv): CALTERNAL_TEST_UPLOAD_MB=1024 cargo test -p calternal --test attachments_crash a_big_upload_streams_in_bounded_memory, through the build slot, with a failure reported on #445 like the colour matrix.

Re-verified on baec5b1:

== cargo fmt --all --check
fmt ok
== cargo clippy --workspace --all-targets --locked -- -D warnings
clippy rc: 0
== cargo test -p calternal-attachments -p calternal-fsio
test result: ok. 19 passed; 0 failed
test result: ok. 12 passed; 0 failed
== cargo test -p calternal --bin calternal (isolation, contract)
test result: ok. 12 passed; 0 failed
== cargo test -p calternal --test attachments_crash (default 128 MiB), 3 runs
peak above idle: 32 MiB upload 6680 KiB, 128 MiB upload 7920 KiB — ok. 2 passed (1.61s)
peak above idle: 32 MiB upload 6444 KiB, 128 MiB upload 6352 KiB — ok. 2 passed (1.89s)
peak above idle: 32 MiB upload 6968 KiB, 128 MiB upload 7000 KiB — ok. 2 passed (1.67s)
**Update (`baec5b1`, pushed to #563):** the memory test now uploads 128 MiB by default, so workspace runs and trains stay fast: the `attachments_crash` suite takes about 1.7 s, down from 6–9 s. The 16 MiB bound still means something at that size: a buffered 128 MiB upload would peak eight times over it. The 1 GiB run moved to the nightly job (`~/codex-jobs/_lib/nightly-matrix.sh`, replaced with `mv`): `CALTERNAL_TEST_UPLOAD_MB=1024 cargo test -p calternal --test attachments_crash a_big_upload_streams_in_bounded_memory`, through the build slot, with a failure reported on #445 like the colour matrix. Re-verified on `baec5b1`: ``` == cargo fmt --all --check fmt ok == cargo clippy --workspace --all-targets --locked -- -D warnings clippy rc: 0 == cargo test -p calternal-attachments -p calternal-fsio test result: ok. 19 passed; 0 failed test result: ok. 12 passed; 0 failed == cargo test -p calternal --bin calternal (isolation, contract) test result: ok. 12 passed; 0 failed == cargo test -p calternal --test attachments_crash (default 128 MiB), 3 runs peak above idle: 32 MiB upload 6680 KiB, 128 MiB upload 7920 KiB — ok. 2 passed (1.61s) peak above idle: 32 MiB upload 6444 KiB, 128 MiB upload 6352 KiB — ok. 2 passed (1.89s) peak above idle: 32 MiB upload 6968 KiB, 128 MiB upload 7000 KiB — ok. 2 passed (1.67s) ```
Author
Owner

Closing during the 26 Oct handover cleanup: The attachments backend landed in dev (PR #563).

Closing during the 26 Oct handover cleanup: The attachments backend landed in dev (PR #563).
kayg closed this issue 2026-10-26 08:46:39 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
kayg/calternal#539
No description provided.