Files
token-circles/docs/plans/field-encryption.md
Komediruzecki e12c78962c docs(encryption): record the second review's fixes, and the receipt orphan
The dev changelog gains four entries: the keyless lookup, the
damaged-key log line, the stored-form comparisons and the non-empty
default guard. It also now gives the numeric-cell precision, and names
exactly which omitted fields no longer fail with a 500.

The plan doc says how JS comparisons agree with D1 on text that UTF-8
cannot carry. It also lists the one orphan case that delete-first
leaves: a D1 delete that commits but whose answer is lost. The sweep
that would collect such objects is not built; it deletes user files,
so it gets its own review.
2026-09-11 11:10:13 +02:00

20 KiB
Raw Permalink Blame History

Field encryption at rest

Status: implemented, shipping dark. Nothing changes on a deployment until it is given a master key (DATA_KEK_1). Decided 2026-09-11; supersedes the end-to-end encryption track in ../e2ee-research.md and Part B of bank-connectivity-and-encryption.md.

What this is — and what it is not

Server-side, application-level encryption. The Worker holds the keys and decrypts inside itself to compute, so every feature keeps working: reports, budgets, reminders with real figures, search, the MCP server and the /api/v1 API, imports, backup and export.

It protects against someone who gets the data without the Workers Secrets:

  • a leaked d1 export — the deploy pipeline takes one before every migration and keeps it as a CI artifact for 30 days;
  • a leaked or misconfigured R2 bucket;
  • SQL injection or any bug that reads rows it should not;
  • anyone with database access who does not also hold the deployment's secrets.

It does not protect against the operator, a compromised Worker, or someone holding both the database and the secrets. It is not end-to-end or zero-knowledge encryption, and that is not planned: bank sync, reminders with amounts, server-side reports and the API all need the server to read the data, which is exactly why no mainstream budgeting app offers E2EE either.

GDPR Art. 34(3)(a) waives the duty to notify users of a breach of properly encrypted data — a leaked export without the master key is that case.

Decisions (2026-09-11)

Question Decision
E2EE or server-side? Server-side. The server keeps the ability to compute; E2EE is dropped.
Tier Not a plan feature at all. No PlanFeatures flag, no gate, on every tier.
What is sealed Transaction text and receipt files (below).
Amounts Stay readable. 62 SUM(amount) queries across 14 files keep running in the database.
Search Decrypt, then filter, in the Worker. Substring search behaves as before.
Keys One data key per user, wrapped by a master key held in Workers Secrets.
Existing data A background backfill seals it in batches; users notice nothing.
Rollout Local, then dev, then prod — and no prod data is ever used for testing.

What is sealed

Table / store Sealed Why these
transactions description, beneficiary, payor, notes Who you paid, who paid you, and what for — the identifying part of a ledger.
recurring_transactions description, notes Copied verbatim into the transactions they generate; left plaintext they would reveal exactly what those rows say.
bills name, notes Same reason: paying a bill writes its name into a transaction.
accounts, savings_goals, retirement_goals, housings, portfolio_holdings notes What a person writes beside the numbers: an account number, who shares an account, what a goal or holding is for. Migration 0033.
loan_prepayments note The same. It has no profile of its own; its owner is found through its loan.
category_mappings pattern A normalised piece of a description the categoriser learned — the text the transaction itself seals.
tag_rules criteria A saved filter's JSON, which quotes description and counterparty text.
R2 receipts the object bytes Photos of receipts — the most sensitive artefact in the product.

Readable on purpose: amounts, dates, types, currencies, category, account and profile ids — what SQL filters, sorts and sums on.

Still plaintext: account, category and tag names, which SQL sorts, joins and looks rows up by (lower(name) = lower(?), ORDER BY a.name, c.name = 'Other'); the other names — profiles, goals, loans, housing, tag rules, saved reports, import sources, API tokens — which are only labels and could be sealed the way notes are; receipt file names (receipts.original_name); import-log details and auth-log reasons. Names are an open decision (Follow-ups).

What a leaked database still reveals about a sealed value: whether it is empty (NULL and '' are stored unsealed, below), and its length — AES-GCM ciphertext is exactly as long as the plaintext plus 28 bytes. Length padding was not judged worth its cost here.

Key hierarchy

DATA_KEK_<n>        Workers Secret: base64 of 32 random bytes. One set per environment —
    |               local, dev and prod never share a key. The highest <n> wraps new keys.
    | wraps
    v
users.dek_wrapped   `dk1.<n>.<iv>.<ct>`: this user's 256-bit data key, AES-256-GCM-wrapped,
    |               bound to the user id by additional data (a key copied to another user's
    | encrypts      row does not unwrap).
    v
that user's sealed columns and receipt objects
  • Independent of JWT_SECRET. 2FA derives its key from JWT_SECRET (twofa.ts), so rotating the auth secret orphans every TOTP secret — survivable there because recovery codes are hashed. The same coupling here would orphan everyone's history.

  • One key per user, because profiles are never shared between users (every access check is id = ? AND user_id = ?), so a user's whole multi-profile household is under one key. A household shared between users would need multi-recipient wrapping; it is not in v1.

  • Rotation retires a master key; it does not change a single data key.

    1. Add DATA_KEK_2, keeping 1. New data keys wrap under 2 at once, and the backfill cron re-wraps existing ones from 1 to 2, up to 1 000 per run, by compare-and-set.
    2. Wait for staleKeys=0 in the [backfill] log line. rewrapFailed counts keys that could not be re-wrapped: under a master key already removed, or damaged (those log a [data-keys] line naming the user).
    3. Remove DATA_KEK_1 from the Worker. Removing it too early shows at once: /api/health says misconfigured while any key is under a master key that is not configured.
    4. Keep DATA_KEK_1 itself in Proton Pass. Backups taken before step 2 finished — the deploy's d1 export artifacts and D1 Time Travel, 30 days each — still hold keys wrapped under it.

    This protects against the old master key leaking later. If it has already leaked together with a copy of the database, the data keys in that copy are exposed and re-wrapping them changes nothing: that takes new data keys and every row re-sealed, which is not built.

  • Deleting an account deletes the only live copy of its key. It does not make old backups unreadable on its own: the deploy's d1 export includes users, wrapped keys and all, so a deleted user's data in a backup still opens with the master key until the 30-day retention expires — exactly as plaintext does today. Immediate crypto-shredding would need the keys kept outside the data backups; that is a follow-up, not a v1 claim.

Losing every DATA_KEK_<n> a user's key was wrapped under loses that user's sealed data. The keys live in Proton Pass, per environment.

Formats

What Format Additional data
Text value tc1.<b64url iv>.<b64url ct+tag>, fresh 96-bit IV tc1|<table>|<column>|u<userId>
Data key dk1.<n>.<b64url iv>.<b64url ct+tag> dk1|u<userId>|k<n>
Receipt object TCE1 · chunk size u32 · 8-byte nonce prefix, then 1 MiB chunks; IV = prefix · u32(index) tce1|receipt|u<userId>|<header>|<index>|<final>

Receipts are chunked because one can be 50 MB and a Worker has 128 MB: an upload streams through a 1 MiB window rather than sitting in memory beside its own ciphertext. The header, index and final flag in every chunk's additional data make a reordered, truncated or re-headed object fail to open rather than open wrong. The additional data does not bind the row id, which the INSERT storing the value assigns.

The invariants the code enforces

  1. A user's rows are sealed only once the user has a key, and from then on a missing or wrong master key is a hard error — 503 on reads and writes alike. Nothing falls back to writing plaintext for a user whose data is sealed.
  2. text_enc is authoritative per row (0 plaintext, 1 sealed; receipts.enc likewise). An existing row changes form in exactly one place, the backfill, by compare-and-set.
  3. Edits never read the marker and then write. sealedUpdate sends one batch with both forms — … AND text_enc = 1 sealed, … AND text_enc = 0 plain — and D1 applies whichever matches the row as it stands. This closes the race where the backfill converts a row between an edit's read and its write, which would leave plaintext inside a sealed row for good.
  4. '' and NULL are stored unsealed. Hides nothing worth hiding, and keeps a column DEFAULT '' — which an INSERT that omits the column gets unsealed from the database — valid inside a sealed row.
  5. No SQL touches a sealed column on the encrypted path. Filtering, sorting, grouping, DISTINCT, COALESCE and dedup move into the Worker after opening. A deployment with no master key keeps the original SQL exactly, so its behaviour is byte-for-byte what it was.

What moved out of SQL, and what it costs

On a deployment with a key: transaction search and text sorts (with pagination and totals), the summary totals under a search, MCP search, merchant grouping and the counterparty list, counterparty totals, bill ordering, import dedup, tag-rule matching, category suggestions, and finding a learned category pattern again to count another use of it.

Semantics on that path differ from SQLite's LIKE in two ways, both closer to what someone typing into a search box means: case folding covers all of Unicode (SQLite folds ASCII only), and % and _ are literal characters rather than wildcards. Where they must agree, they do: a query is compared in the form D1 stores text in (asStored — UTF-8 cannot carry a lone surrogate, so D1 and sealing both store U+FFFD, and D1 converts its parameters the same way).

Measured in workerd: ~9 µs to open a value, ~2.5 µs to seal one. The worst case — a 20,000-row ledger, four columns, 80,000 opens — is about 0.7 s of CPU; a typical 2,000–5,000-row ledger is 70–175 ms. Tag-rule matching, a hot path while a rule is edited, opens only the fields the rule's criteria reference.

The backfill

worker/src/backfill.ts, on its own trigger */20 * * * * (dev and prod). Each run is bounded — 60 s, 5,000 rows, 50 receipts — and the next run resumes. It seals by compare-and-set against the exact values it read, so an edit landing in between makes the write miss and the row is retried later rather than overwritten with the old text. Receipts are resealed to a new object key, the row is swapped to it by compare-and-set, and only then is the plaintext original deleted.

Rows on a profile nobody owns (profiles.user_id NULL — legacy data) have no key to be sealed under and stay plaintext. A user whose key cannot be produced is skipped and logged; everyone else still gets sealed, and the skipped rows do not count against a run's budget — every run reads them again from the start, and counted they would use it up before reaching anyone else.

Rollout — no prod data in testing

  1. Local. Put a local-only key in worker/.dev.vars: DATA_KEK_1=$(node -e "console.log(require('crypto').randomBytes(32).toString('base64'))"). cd worker && pnpm test (keyless) must fail fast with a clear message while that line is there — remove it for keyless runs; pnpm test:sealed runs the whole suite keyed. For the backfill: wrangler dev --test-scheduled, then curl "http://localhost:8787/__scheduled?cron=*/20+*+*+*+*".
  2. Dev. wrangler secret put DATA_KEK_1 --env dev with a dev-only key. GET /api/health must report "encryption": "on"; watch the [backfill] log lines until complete=true; then use the app end to end.
  3. Prod. Only after dev sign-off, with a separate prod-only key.

Merging deploys to dev and applies migrations 0031–0033 there. Tagging a release applies them to prod: schema only — users.dek_wrapped, a text_enc marker on eleven tables and receipts.enc, a partial index over unsealed transactions and an index on users.dek_wrapped, with every existing row at 0 — and with no prod key, behaviour is unchanged. The rehearsal for them uses synthetic data, not a prod export.

Failure modes

Symptom Cause Effect
encryption: "misconfigured" a DATA_KEK_<n> that is not base64 of 32 bytes writes for users needing a new key fail with 503
encryption: "misconfigured" no DATA_KEK_<n> at all while some user holds a data key — a deleted or dropped secret every read and write of sealed data fails with 503; a search on the keyless SQL path can silently miss sealed rows
encryption: "misconfigured" a data key wrapped under a DATA_KEK_<n> that is no longer configured — the old key removed before the re-wrap finished that user's sealed data fails with 503 until the old key is put back
staleKeys above 0 run after run keys the re-wrap cannot open (rewrapFailed): under a master key already removed, or damaged the older master key cannot be retired yet
encryption: "unknown" D1 did not answer the health check's query nothing by itself; ask again
503 "Encrypted data is temporarily unavailable" master key missing or wrong for a user who has a key that user's sealed data is unreadable until the key is restored
500 naming a table and column a stored value failed authentication — tampered, or truncated the request fails; nothing is shown in its place

Follow-ups

Done since the first version of this plan: master-key rotation (the re-wrap job, above); sealing the notes on accounts, goals, housing, holdings and loan prepayments, learned patterns and tag-rule criteria (migration 0033); security.txt; a Content-Length on sealed receipt downloads (the plaintext size is stored with the object); a receipt deleted mid-reseal no longer orphans its sealed copy (the row is deleted first); and a keyed text sort opens only the sort column before it cuts the page.

Open:

  • Immediate crypto-shredding — a decision, not a task. D1 Time Travel keeps 30 days of the database whatever the app does, so a deleted account's wrapped key stays recoverable for that long. Holding keys outside D1 (in R2, say) would make deletion immediate, but a lost or corrupted key store would then take everyone's data with it, and restoring the database from a backup would no longer restore its keys. Recommendation: state the 30-day erasure window in the privacy policy rather than build this.
  • Names. Account, category and tag names stay plaintext by design: SQL sorts, joins and looks rows up by them. Goal, loan, housing, tag-rule, report and import-source names are only labels and could be sealed the way notes are; so could receipts.original_name. Undecided.
  • Bank and OAuth tokens. Nothing stores them yet — bank sync is not built. When it is, they are sealed under this key hierarchy before the first one is written (bank-connectivity plan, decision 1).
  • Receipt objects no row names. Deleting a receipt deletes its row first, then the object that row named. If D1 commits the delete but its answer is lost, the Worker never learns which object to delete, and it stays in R2 with nothing pointing at it — on a keyless deployment, a plaintext file its owner believes gone. Rare (a D1 failure after commit), and the only cure is a sweep: from the backfill cron, delete objects under a profile's prefix that no receipts row names and that are older than a day. Not built: it deletes user files, so it gets its own review.
  • Keyed scans have no cap. The counterparty, merchant and search scans open every candidate row. Opening concurrently was measured and does not help — 20,000 opens take ~178 ms one at a time and ~171 ms 64 at a time in workerd, because the work is CPU, not I/O — so openRows stays sequential. If a large ledger nears the CPU limit, precomputed counterparty aggregates are next.