mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
Seals transaction text (description, beneficiary, payor, notes), recurring rules (description, notes), bills (name, notes) and receipt files with AES-256-GCM under a per-user data key, itself wrapped by a master key from Workers Secrets (DATA_KEK_<n>). The Worker decrypts to compute, so every feature keeps working; this protects a leaked D1 export, backup artifact or bucket, not the data from the operator. It is not end-to-end encryption, which is dropped. Ships dark: with no DATA_KEK_<n> every route keeps its original SQL and nothing is sealed. With a key, SQL work over sealed text (LIKE, text ORDER BY, GROUP BY) moves to JS over opened rows and reproduces SQLite's answers exactly. Edits use a two-statement batch so they land in whichever form the row is in when the backfill races them; the backfill (*/20 cron) is the only place a row changes form, by compare-and-set. Migration 0031 adds users.dek_wrapped, text_enc on the three tables, receipts.enc and a partial index. Schema only. CI runs the worker suite keyless and keyed (test:sealed). Design and rollout: docs/plans/field-encryption.md.
26 lines
1.7 KiB
SQL
26 lines
1.7 KiB
SQL
-- Field encryption at rest (migration 0031). Server-side: the Worker holds the keys and decrypts
|
|
-- to compute, so this protects a leaked database export, not the data from the operator.
|
|
-- Design, threat model and rollout: docs/plans/field-encryption.md.
|
|
--
|
|
-- Schema only. Every existing row starts at 0 (plaintext) and nothing here reads or rewrites a
|
|
-- value: with no DATA_KEK_<n> secret configured, behaviour is exactly what it was before.
|
|
|
|
-- This user's data key, AES-256-GCM-wrapped under a master key from Workers Secrets
|
|
-- (`dk1.<n>.<iv>.<ct>`). NULL: the user has no key, so every row they own is plaintext. It lives
|
|
-- on the users row on purpose — deleting the account deletes the only live copy of the key.
|
|
ALTER TABLE users ADD COLUMN dek_wrapped TEXT;
|
|
|
|
-- 0: this row's sealed text columns are plaintext. 1: they are ciphertext under the owner's key.
|
|
-- New rows are written at 1 when the owner has a key. An existing row goes 0 -> 1 in exactly one
|
|
-- place, the backfill, and only by compare-and-set against the values it read.
|
|
ALTER TABLE transactions ADD COLUMN text_enc INTEGER NOT NULL DEFAULT 0;
|
|
ALTER TABLE recurring_transactions ADD COLUMN text_enc INTEGER NOT NULL DEFAULT 0;
|
|
ALTER TABLE bills ADD COLUMN text_enc INTEGER NOT NULL DEFAULT 0;
|
|
|
|
-- 0: the R2 object is the raw upload. 1: it is the chunked AES-GCM format (field-crypto.ts, TCE1).
|
|
ALTER TABLE receipts ADD COLUMN enc INTEGER NOT NULL DEFAULT 0;
|
|
|
|
-- How the backfill finds work in the one table that can be large. Partial, so it shrinks to
|
|
-- nothing once the backfill is done instead of indexing a column that is 1 on every row.
|
|
CREATE INDEX IF NOT EXISTS idx_transactions_text_plain ON transactions(id) WHERE text_enc = 0;
|