# F4E1 chunk-size bound (decrypt memory safety)

> Status: **hardening for a decrypt-time OOM found by adversarial fuzzing.**
> Applies to every runtime that restores an F4E1-encrypted archive — the legacy
> `Sandbox` path served by `develop` and the Sandbox 1.0 (`v1-sandbox`) track.

## The issue

`src/crypto/backup-encryption.ts` decrypts chunk by chunk:

```
header (17B) | AES-256-GCM chunk | tag | AES-256-GCM chunk | tag | …
```

`decryptStream` buffers a full `chunkSize + TAG_LENGTH` before it can
authenticate and emit that chunk, and `parseHeader` used to accept any chunk size
up to **256 MiB**. A corrupt or maliciously written object could therefore make
a restore try to buffer ~256 MiB in a single ArrayBuffer — far above the Workers
isolate memory limit — and the Worker is OOM-killed mid-restore, exactly when a
recovery archive must be readable. The legacy restore path reaches this code on
every encrypted recovery point:

- `src/crypto/r2-backup.ts#decryptBackupArchive` → `chunkSizeFromHeader` +
  `decryptStream` on `backups/<id>/data.sqsh.enc`.
- `src/persistence.ts` uses `decryptBytes` for config versions (one chunk).

Fuzz finding: a 17-byte header advertising a 256 MiB chunk is enough to trigger
it — no valid ciphertext body is needed.

## The change

- New `MAX_CHUNK_SIZE` (16 MiB) is enforced by `parseHeader`, so
  `chunkSizeFromHeader` / `decryptStream` reject an out-of-range header from the
  header bytes alone, before any chunk is buffered. 16 MiB is four times the
  4 MiB writer default and far below the isolate limit.
- `encryptBytes` clamps its single-chunk header to `MAX_CHUNK_SIZE`, so the
  single-shot writer (config versions, secrets) can never emit a header a reader
  would reject.

Existing objects are unaffected: the writer default (4 MiB) and every
`encryptBytes` payload small enough to be one chunk are inside the bound. An
object written with a header above 16 MiB is already unreadable in practice
(that is the OOM case) and now fails fast instead.

## Verification

- `src/crypto/backup-encryption.test.ts` — default and maximum accepted; `0` and
  `MAX_CHUNK_SIZE + 1` rejected; the historic 256 MiB header rejected from the
  header alone; `encryptBytes` clamps and still round-trips a
  `MAX_CHUNK_SIZE + 1` payload.
- Seeded fuzz harness (`bun run --cwd apps/fouria test:fuzz:sandbox-v1
  [iterations] [seed]`): round-trip, length invariants, single-byte corruption,
  wrong key, mid-chunk truncation, and the header bound across thousands of
  random shapes.

## Known limitation (unchanged)

F4E1 authenticates each chunk but does not commit to a total plaintext length, so
truncating a multi-chunk ciphertext exactly on a chunk boundary decrypts to a
shorter plaintext without an authentication error. On the legacy path the
squashfs restore and its size/integrity validation are the only guard; the
Sandbox 1.0 `DirectoryBackup` path additionally checks `record.size` before the
SDK restore. A future format version should authenticate a total length/EOF
marker.
