Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions server/migrations/0015_key_events_day_only.sql
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions server/src/routes/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions server/src/routes/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
43 changes: 43 additions & 0 deletions server/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32> = 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<i32> = 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
Expand Down
Loading