Skip to content

Commit 08bcce9

Browse files
committed
fix(audit): normalize created_at inside compute_hash
Truncating at the write path fixes the live break, but leaves the invariant as a convention: a future write path that stamps `created_at` without `to_storage_precision` reintroduces the write/read preimage split. Normalize at the single point that consumes the value instead, so the enforcement is structural. Truncation is idempotent and the write path already truncates, so no digest changes — verified by writing a chain with the previous commit and validating those same rows under this one. The `assert_ne!` test that pinned the trap moved down a level: it now asserts on the RFC-3339 preimage (`.123456789` vs `.123456`), which is the property that actually causes the split, and a new test asserts `compute_hash` yields the same digest either way. Signed-off-by: Shani Singh <[email protected]>
1 parent ebfcb93 commit 08bcce9

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

‎crates/buzz-audit/src/hash.rs‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ pub fn to_storage_precision(created_at: DateTime<Utc>) -> DateTime<Utc> {
2929
/// `community_id` is hashed first so chain identity carries the tenant: an entry
3030
/// cannot be lifted out of one community's chain and re-verified inside another.
3131
///
32-
/// `created_at` must already be at [`to_storage_precision`] — see that function
33-
/// for why sub-microsecond digits break verification.
32+
/// `created_at` is normalized through [`to_storage_precision`] here rather than
33+
/// hashed as given. Write paths truncate before storing so the row matches the
34+
/// in-memory entry, but normalizing again at the single point that consumes the
35+
/// value means no future caller can reintroduce the write/read preimage split
36+
/// by forgetting to. Values already at storage precision are unaffected —
37+
/// truncation is idempotent — so this does not change any digest.
3438
///
3539
/// `detail` is serialized via [`canonical_json`] (sorted keys) so the hash is
3640
/// stable across machines and Rust versions. A serialization failure is a hard
@@ -40,7 +44,11 @@ pub fn compute_hash(entry: &AuditEntry) -> Result<[u8; 32], AuditError> {
4044
// Tenant binding: community_id leads the hash.
4145
hasher.update(entry.community_id.as_bytes());
4246
hasher.update(entry.seq.to_be_bytes());
43-
hasher.update(entry.created_at.to_rfc3339().as_bytes());
47+
hasher.update(
48+
to_storage_precision(entry.created_at)
49+
.to_rfc3339()
50+
.as_bytes(),
51+
);
4452
hasher.update(entry.action.as_str().as_bytes());
4553
match &entry.actor_pubkey {
4654
Some(pk) => {
@@ -157,19 +165,34 @@ mod tests {
157165
}
158166

159167
#[test]
160-
fn nanosecond_timestamps_cannot_survive_a_database_round_trip() {
161-
// The trap `to_storage_precision` exists to close. `compute_hash`
162-
// covers an RFC-3339 string whose sub-second digit count follows the
163-
// value, so hashing a nanosecond `created_at` produces a digest that
164-
// cannot be recomputed once Postgres has truncated it to microseconds:
165-
// every entry would then fail `verify_chain` with `HashMismatch`.
168+
fn rfc3339_sub_second_width_follows_the_value() {
169+
// The underlying trap, pinned on the preimage rather than the digest:
170+
// chrono emits 0/3/6/9 fractional digits depending on the value, so a
171+
// nanosecond timestamp and its microsecond truncation are *different
172+
// strings*. Hashing the untruncated value therefore produces a digest
173+
// that cannot be recomputed from the stored row — which is what made
174+
// every entry fail `verify_chain` with `HashMismatch`.
175+
let ns = nanosecond_instant();
176+
assert_eq!(ns.to_rfc3339(), "2023-11-14T22:13:20.123456789+00:00");
177+
assert_eq!(
178+
after_database_round_trip(ns).to_rfc3339(),
179+
"2023-11-14T22:13:20.123456+00:00"
180+
);
181+
assert_ne!(ns.to_rfc3339(), after_database_round_trip(ns).to_rfc3339());
182+
}
183+
184+
#[test]
185+
fn compute_hash_normalizes_sub_microsecond_timestamps() {
186+
// The enforcement point: even handed an untruncated `created_at`,
187+
// `compute_hash` digests the storage-precision value, so a write path
188+
// that forgot to truncate cannot split the write/read preimage.
166189
let ns = nanosecond_instant();
167190
let mut written = sample_entry();
168191
written.created_at = ns;
169192
let mut read_back = sample_entry();
170193
read_back.created_at = after_database_round_trip(ns);
171194

172-
assert_ne!(
195+
assert_eq!(
173196
compute_hash(&written).unwrap(),
174197
compute_hash(&read_back).unwrap()
175198
);

0 commit comments

Comments
 (0)