summaryrefslogtreecommitdiffstats
path: root/src/hash.rs
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-18 14:47:59 +0000
committeraur-build <aur-build@localhost>2026-11-18 14:47:59 +0000
commit7946e9ffc48748992f051ada15bec772b16a585d (patch)
tree96b6674efb30774647687a117759723ff3207965 /src/hash.rs
parent3370a487d6e1100623ebfd982967f1223d251607 (diff)
downloadaur-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/hash.rs')
-rw-r--r--src/hash.rs19
1 files changed, 13 insertions, 6 deletions
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)?)
}