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/db | |
| 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/db')
| -rw-r--r-- | src/db/model.rs | 9 | ||||
| -rw-r--r-- | src/db/queries.rs | 2 |
2 files changed, 7 insertions, 4 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| { |
