From 7946e9ffc48748992f051ada15bec772b16a585d Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 14:47:59 +0000 Subject: hash: canonical_json is canonical now, and a serialization failure is an error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The function behind `Identity::hash` — the digest an approval echoes back — did two things its name promised and it did not do. It did not order anything: the doc comment conceded that callers were expected to pass "already-canonical values", which is a convention, not a property, and a struct serializes in whatever order its fields were declared. And on a serialization failure it returned `"{}"`, so every value that failed to serialize hashed to one shared digest. For a system whose whole job is provenance, "two identities that could not be serialized would look identical to the approval echo" is the worst possible failure to have available. Serialization now routes through `serde_json::Value`, whose map is a `BTreeMap`, so object keys come out sorted whatever the input's construction order — the name describes what happens. A failure propagates: `canonical_json` returns `Result`, `Identity::hash` returns `Result`, and the four call sites (detect, put_world_snapshot, the world step, both enqueue paths) pass it on. There is no path left where a digest is invented for a value that could not be rendered. `tests/hash.rs` pins all three properties: an unserializable value is an error naming itself, two same-field-different-declaration-order values render identically (and to the exact compact sorted bytes), and the identity digest changes when any element of the tuple changes. --- src/db/model.rs | 9 ++++++--- src/db/queries.rs | 2 +- src/hash.rs | 19 +++++++++++++------ src/pipeline/mod.rs | 4 ++-- src/pipeline/steps.rs | 2 +- src/watch/detect.rs | 2 +- 6 files changed, 24 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/db/model.rs b/src/db/model.rs index 07065c5..e962d6f 100644 --- a/src/db/model.rs +++ b/src/db/model.rs @@ -58,11 +58,14 @@ impl Identity { /// The hash that makes I3 a fact: two byte-identical inputs never produce /// two candidates, and any change to what becomes a byte in the artifact /// produces a new one. + /// /// A digest of the canonical JSON of the identity tuple. This is the value /// an approval must echo: two candidates share an `identity_hash` only if - /// every pin, digest and version in them is identical. - pub fn hash(&self) -> String { - format!("sha256:{}", hash::hex(hash::canonical_json(self).as_bytes())) + /// every pin, digest and version in them is identical. A value that cannot + /// be serialized is an error rather than a digest — a shared fallback digest + /// would be the one way two identities could collide by accident. + pub fn hash(&self) -> crate::error::Result { + Ok(format!("sha256:{}", hash::hex(hash::canonical_json(self)?.as_bytes()))) } } diff --git a/src/db/queries.rs b/src/db/queries.rs index fa530b4..ca73e5f 100644 --- a/src/db/queries.rs +++ b/src/db/queries.rs @@ -833,7 +833,7 @@ pub fn put_world_snapshot(writer: &Writer, members: &[WorldMember]) -> Result String { hex::encode(h.finalize()) } -/// Canonical JSON used for identity digests. Key order in serde_json maps is -/// not stable across object construction, so we serialize through a -/// `BTreeMap`-preserving path: callers pass already-canonical values (arrays of -/// objects with a fixed field order) and we hash the compact rendering. -pub fn canonical_json(v: &T) -> String { - serde_json::to_string(v).unwrap_or_else(|_| "{}".into()) +/// Serialize a value to canonical JSON: compact, with every object's keys in +/// sorted order, so equal values always produce equal bytes. +/// +/// The ordering is not free in `serde_json` — a struct serializes in field +/// declaration order and a map in whatever order it happens to hold — so the +/// value is routed through `serde_json::Value`, whose map is a `BTreeMap`. +/// That makes the name true rather than aspirational. +/// +/// A serialization failure is an error. It used to be `"{}"`, which meant every +/// value that failed to serialize hashed to the same digest — and this is the +/// function behind `Identity::hash`, the value an approval echoes (§6 M-2). +pub fn canonical_json(v: &T) -> Result { + serde_json::to_string(&serde_json::to_value(v)?) } diff --git a/src/pipeline/mod.rs b/src/pipeline/mod.rs index 459cea6..8f4012e 100644 --- a/src/pipeline/mod.rs +++ b/src/pipeline/mod.rs @@ -279,7 +279,7 @@ pub fn enqueue_for_candidate(cfg: &Config, db: &Db, c: &CandidateRow) -> Result< let pb = db::get_pkgbase_by_id(db, c.pkgbase_id)? .ok_or_else(|| Error::not_found("pkgbase"))?; let world = publish::index_world(db)?; - let world_hash = crate::hash::canonical_json(&world); + let world_hash = crate::hash::canonical_json(&world)?; let id = builds::build_id(&pb.name, &c.identity_hash, &world_hash, &cfg.build_profile); let world_id = db::put_world_snapshot(&db.writer, &world)?; builds::enqueue(db, &id, pb.id, Some(c.id), "new_version", Some(world_id), &cfg.build_profile, 0)?; @@ -298,7 +298,7 @@ pub fn enqueue_manual_rebuild(cfg: &Config, db: &Db, pkgbase: &str) -> Result Result { return Ok(StepResult::Blocked(missing)); } let world = crate::pipeline::publish::index_world(&ctx.db)?; - let hash = crate::hash::canonical_json(&world); + let hash = crate::hash::canonical_json(&world)?; ctx.log_line(&format!( " deps: {} ours/official, {} from official repos, world {}", wanted.len() - official, diff --git a/src/watch/detect.rs b/src/watch/detect.rs index 3134e19..71d1b29 100644 --- a/src/watch/detect.rs +++ b/src/watch/detect.rs @@ -174,7 +174,7 @@ fn analyse(cfg: &Config, db: &Db, pb: &Pkgbase, head: &str) -> Result