diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 14:47:59 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 14:47:59 +0000 |
| commit | 7946e9ffc48748992f051ada15bec772b16a585d (patch) | |
| tree | 96b6674efb30774647687a117759723ff3207965 /src | |
| parent | 3370a487d6e1100623ebfd982967f1223d251607 (diff) | |
| download | aur-build-7946e9ffc48748992f051ada15bec772b16a585d.tar.gz aur-build-7946e9ffc48748992f051ada15bec772b16a585d.zip | |
hash: canonical_json is canonical now, and a serialization failure is an error
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/db/model.rs | 9 | ||||
| -rw-r--r-- | src/db/queries.rs | 2 | ||||
| -rw-r--r-- | src/hash.rs | 19 | ||||
| -rw-r--r-- | src/pipeline/mod.rs | 4 | ||||
| -rw-r--r-- | src/pipeline/steps.rs | 2 | ||||
| -rw-r--r-- | src/watch/detect.rs | 2 |
6 files changed, 24 insertions, 14 deletions
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<String> { + 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<i6 let mut sorted = members.to_vec(); sorted.sort(); sorted.dedup(); - let hash = crate::hash::canonical_json(&sorted); + let hash = crate::hash::canonical_json(&sorted)?; let json = serde_json::to_string(&sorted)?; let at = time::now(); writer.tx(move |tx| { diff --git a/src/hash.rs b/src/hash.rs index 5243520..0321437 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -69,10 +69,17 @@ pub fn recipe_hash(entries: &[(String, String)]) -> 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<T: serde::Serialize>(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<T: serde::Serialize>(v: &T) -> Result<String, serde_json::Error> { + 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<St rel.aur_commit, rel.recipe_hash )); 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(pkgbase, &identity, &world_hash, &cfg.build_profile); let world_id = db::put_world_snapshot(&db.writer, &world)?; builds::enqueue(db, &id, pb.id, None, "manual_rebuild", Some(world_id), &cfg.build_profile, 1)?; diff --git a/src/pipeline/steps.rs b/src/pipeline/steps.rs index 50e41d3..6bdf12c 100644 --- a/src/pipeline/steps.rs +++ b/src/pipeline/steps.rs @@ -103,7 +103,7 @@ fn plan(ctx: &Ctx) -> Result<StepResult> { 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<CheckRepor upstream_pins: pins.clone(), source_digests: digests.clone(), }; - let identity_hash = identity.hash(); + let identity_hash = identity.hash()?; let snap_id = db::put_snapshot( &db.writer, pb.id, |
