diff --git a/server/migrations/0015_key_events_day_only.sql b/server/migrations/0015_key_events_day_only.sql new file mode 100644 index 0000000..4499841 --- /dev/null +++ b/server/migrations/0015_key_events_day_only.sql @@ -0,0 +1,51 @@ +-- Keep only the DAY of key events. The key directory and the KT log stored the +-- exact second of every registration, signed-prekey rotation, recovery and "log +-- out everywhere" - a record of when an account was created or recovered and, +-- since rotations run as the device connects, of when its device was online. +-- Nothing needs more than the day. +-- +-- Enforced HERE, at the shared write boundary, rather than in app code: every +-- writer stores only the day from the moment this runs - any server version +-- (including the old binary still serving while a deploy runs), the KT repair +-- after an unclean shutdown, and any future code path. + +CREATE FUNCTION kt_log_day_only() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + NEW.timestamp := NEW.timestamp - (NEW.timestamp % 86400); + RETURN NEW; +END +$$; + +CREATE TRIGGER kt_log_day_only + BEFORE INSERT OR UPDATE OF timestamp ON kt_log + FOR EACH ROW EXECUTE FUNCTION kt_log_day_only(); + +CREATE FUNCTION key_directory_day_only() RETURNS trigger LANGUAGE plpgsql AS $$ +BEGIN + NEW.created_at := NEW.created_at - (NEW.created_at % 86400); + NEW.spk_created_at := NEW.spk_created_at - (NEW.spk_created_at % 86400); + RETURN NEW; +END +$$; + +CREATE TRIGGER key_directory_day_only + BEFORE INSERT OR UPDATE OF created_at, spk_created_at ON key_directory + FOR EACH ROW EXECUTE FUNCTION key_directory_day_only(); + +-- Round what was stored before, once. key_directory's times aren't hashed or +-- served. kt_log's time is inside each leaf hash, so this rewrites the log's +-- history and root once. That's safe now: clients pin no roots (each proof is +-- checked against the root signed with it), roots aren't yet published anywhere +-- outside, and prev_hash chains bundle hashes, not times. A server process +-- already running keeps serving proofs from its in-memory tree until it's +-- replaced - self-consistent, and only times it was already serving; a process +-- started after this builds its tree from the rounded rows. Irreversible by +-- design: the exact seconds are gone. +UPDATE key_directory + SET created_at = created_at - (created_at % 86400), + spk_created_at = spk_created_at - (spk_created_at % 86400) + WHERE created_at % 86400 <> 0 OR spk_created_at % 86400 <> 0; + +UPDATE kt_log + SET timestamp = timestamp - (timestamp % 86400) + WHERE timestamp % 86400 <> 0; diff --git a/server/src/routes/keys.rs b/server/src/routes/keys.rs index 27d64c2..ec422c5 100644 --- a/server/src/routes/keys.rs +++ b/server/src/routes/keys.rs @@ -339,6 +339,7 @@ pub async fn spk_rotate( return Err(ApiError::bad_request()); } + // The DB keeps only the day of this and the KT entry below (migration 0015). let now = now_unix(); kd::update_spk(&st.db, &user_id, &spk, &sig_ed, &sig_dil, now as i32) .await diff --git a/server/src/routes/register.rs b/server/src/routes/register.rs index 68d00c7..f9a70e2 100644 --- a/server/src/routes/register.rs +++ b/server/src/routes/register.rs @@ -111,6 +111,7 @@ pub async fn register( spk_sig_ed, spk_sig_dil, kyber1024_pub: kyber, + // The DB keeps only the day of these (migration 0015). spk_created_at: now as i32, created_at: now as i32, }; diff --git a/server/tests/integration.rs b/server/tests/integration.rs index f82b0a8..846170c 100644 --- a/server/tests/integration.rs +++ b/server/tests/integration.rs @@ -1396,6 +1396,49 @@ async fn server_end_to_end() { "rotated bundle proof must verify" ); + // Only the DAY of each key event is stored (migration 0015's triggers): the + // register and rotate entries, and the directory's times. The proof above + // verifies with it. + let times: Vec = sqlx::query_scalar("SELECT timestamp FROM kt_log WHERE user_id = $1") + .bind(&bob.user_id) + .fetch_all(&pool) + .await + .unwrap(); + assert!(times.iter().all(|t| t % 86_400 == 0), "KT times are whole days: {times:?}"); + let (created, spk_created): (i32, i32) = + sqlx::query_as("SELECT created_at, spk_created_at FROM key_directory WHERE user_id = $1") + .bind(&bob.user_id) + .fetch_one(&pool) + .await + .unwrap(); + assert_eq!((created % 86_400, spk_created % 86_400), (0, 0), "directory times are whole days"); + + // The rule holds at the DB, whoever writes: exact seconds written straight to + // the tables (as an older server version would) are stored as the day. + sqlx::query("UPDATE kt_log SET timestamp = timestamp + 1234 WHERE user_id = $1") + .bind(&bob.user_id) + .execute(&pool) + .await + .unwrap(); + sqlx::query("UPDATE key_directory SET created_at = created_at + 5, spk_created_at = spk_created_at + 7 WHERE user_id = $1") + .bind(&bob.user_id) + .execute(&pool) + .await + .unwrap(); + let swept: Vec = sqlx::query_scalar("SELECT timestamp FROM kt_log WHERE user_id = $1") + .bind(&bob.user_id) + .fetch_all(&pool) + .await + .unwrap(); + assert_eq!(swept, times, "exact KT times stored as the day"); + let swept_kd: (i32, i32) = + sqlx::query_as("SELECT created_at, spk_created_at FROM key_directory WHERE user_id = $1") + .bind(&bob.user_id) + .fetch_one(&pool) + .await + .unwrap(); + assert_eq!(swept_kd, (created, spk_created), "exact directory times stored as the day"); + // The owner's WS-ticket reply now carries the NEW signed prekey, signatures // included, so a device that didn't publish it can tell (docs 8.2). let tkt: serde_json::Value = http