nix_compat/nixhash/
ca_hash.rs

1use crate::nixbase32;
2use crate::nixhash::{HashAlgo, NixHash};
3use serde::de::Unexpected;
4use serde::ser::SerializeMap;
5use serde::{Deserialize, Deserializer, Serialize, Serializer};
6use serde_json::{Map, Value};
7use std::borrow::Cow;
8
9use super::algos::SUPPORTED_ALGOS;
10use super::decode_digest;
11
12/// A Nix CAHash describes a content-addressed hash of a path.
13///
14/// The way Nix prints it as a string is a bit confusing, but there's essentially
15/// three modes, `Flat`, `Nar` and `Text`.
16/// `Flat` and `Nar` support all 4 algos that [NixHash] supports
17/// (sha1, md5, sha256, sha512), `Text` only supports sha256.
18#[derive(Clone, Debug, Eq, PartialEq)]
19pub enum CAHash {
20    Flat(NixHash),  // "fixed flat"
21    Nar(NixHash),   // "fixed recursive"
22    Text([u8; 32]), // "text", only supports sha256
23}
24
25/// Representation for the supported hash modes.
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum HashMode {
28    Flat,
29    Nar,
30    Text,
31}
32
33impl CAHash {
34    pub fn hash(&self) -> Cow<NixHash> {
35        match *self {
36            CAHash::Flat(ref digest) => Cow::Borrowed(digest),
37            CAHash::Nar(ref digest) => Cow::Borrowed(digest),
38            CAHash::Text(digest) => Cow::Owned(NixHash::Sha256(digest)),
39        }
40    }
41
42    pub fn mode(&self) -> HashMode {
43        match self {
44            CAHash::Flat(_) => HashMode::Flat,
45            CAHash::Nar(_) => HashMode::Nar,
46            CAHash::Text(_) => HashMode::Text,
47        }
48    }
49
50    /// Returns a colon-separated string consisting of mode, recursiveness and
51    /// hash algo. Used as a prefix in various string representations.
52    pub fn algo_str(&self) -> &'static str {
53        match self.mode() {
54            HashMode::Flat => match self.hash().as_ref() {
55                NixHash::Md5(_) => "fixed:md5",
56                NixHash::Sha1(_) => "fixed:sha1",
57                NixHash::Sha256(_) => "fixed:sha256",
58                NixHash::Sha512(_) => "fixed:sha512",
59            },
60            HashMode::Nar => match self.hash().as_ref() {
61                NixHash::Md5(_) => "fixed:r:md5",
62                NixHash::Sha1(_) => "fixed:r:sha1",
63                NixHash::Sha256(_) => "fixed:r:sha256",
64                NixHash::Sha512(_) => "fixed:r:sha512",
65            },
66            HashMode::Text => "text:sha256",
67        }
68    }
69
70    /// Constructs a [CAHash] from the textual representation,
71    /// which is one of the three:
72    /// - `text:sha256:$nixbase32sha256digest`
73    /// - `fixed:r:$algo:$nixbase32digest`
74    /// - `fixed:$algo:$nixbase32digest`
75    ///
76    /// These formats are used in NARInfo, for example.
77    pub fn from_nix_hex_str(s: &str) -> Option<Self> {
78        let (tag, s) = s.split_once(':')?;
79
80        match tag {
81            "text" => {
82                let digest = s.strip_prefix("sha256:")?;
83                let digest = nixbase32::decode_fixed(digest).ok()?;
84                Some(CAHash::Text(digest))
85            }
86            "fixed" => {
87                if let Some(s) = s.strip_prefix("r:") {
88                    NixHash::from_nix_hex_str(s).map(CAHash::Nar)
89                } else {
90                    NixHash::from_nix_hex_str(s).map(CAHash::Flat)
91                }
92            }
93            _ => None,
94        }
95    }
96
97    /// Formats a [CAHash] in the Nix default hash format, which is the format
98    /// that's used in NARInfos for example.
99    pub fn to_nix_nixbase32_string(&self) -> String {
100        format!(
101            "{}:{}",
102            self.algo_str(),
103            nixbase32::encode(self.hash().digest_as_bytes())
104        )
105    }
106
107    /// This takes a serde_json::Map and turns it into this structure. This is necessary to do such
108    /// shenigans because we have external consumers, like the Derivation parser, who would like to
109    /// know whether we have a invalid or a missing NixHashWithMode structure in another structure,
110    /// e.g. Output.
111    /// This means we have this combinatorial situation:
112    ///
113    /// - no hash, no hashAlgo: no [CAHash] so we return Ok(None).
114    /// - present hash, missing hashAlgo: invalid, we will return missing_field
115    /// - missing hash, present hashAlgo: same
116    /// - present hash, present hashAlgo: either we return ourselves or a type/value validation
117    ///   error.
118    ///
119    /// This function is for internal consumption regarding those needs until we have a better
120    /// solution. Now this is said, let's explain how this works.
121    ///
122    /// We want to map the serde data model into a [CAHash].
123    ///
124    /// The serde data model has a `hash` field (containing a digest in nixbase32),
125    /// and a `hashAlgo` field, containing the stringified hash algo.
126    /// In case the hash is recursive, hashAlgo also has a `r:` prefix.
127    ///
128    /// This is to match how `nix show-derivation` command shows them in JSON
129    /// representation.
130    pub(crate) fn from_map<'de, D>(map: &Map<String, Value>) -> Result<Option<Self>, D::Error>
131    where
132        D: Deserializer<'de>,
133    {
134        // If we don't have hash neither hashAlgo, let's just return None.
135        if !map.contains_key("hash") && !map.contains_key("hashAlgo") {
136            return Ok(None);
137        }
138
139        let hash_algo_v = map.get("hashAlgo").ok_or_else(|| {
140            serde::de::Error::missing_field(
141                "couldn't extract `hashAlgo` key, but `hash` key present",
142            )
143        })?;
144        let hash_algo = hash_algo_v.as_str().ok_or_else(|| {
145            serde::de::Error::invalid_type(Unexpected::Other(&hash_algo_v.to_string()), &"a string")
146        })?;
147        let (mode_is_nar, hash_algo) = if let Some(s) = hash_algo.strip_prefix("r:") {
148            (true, s)
149        } else {
150            (false, hash_algo)
151        };
152        let hash_algo = HashAlgo::try_from(hash_algo).map_err(|e| {
153            serde::de::Error::invalid_value(
154                Unexpected::Other(&e.to_string()),
155                &format!("one of {}", SUPPORTED_ALGOS.join(",")).as_str(),
156            )
157        })?;
158
159        let hash_v = map.get("hash").ok_or_else(|| {
160            serde::de::Error::missing_field(
161                "couldn't extract `hash` key but `hashAlgo` key present",
162            )
163        })?;
164        let hash = hash_v.as_str().ok_or_else(|| {
165            serde::de::Error::invalid_type(Unexpected::Other(&hash_v.to_string()), &"a string")
166        })?;
167        let hash = decode_digest(hash.as_bytes(), hash_algo)
168            .map_err(|e| serde::de::Error::custom(e.to_string()))?;
169        if mode_is_nar {
170            Ok(Some(Self::Nar(hash)))
171        } else {
172            Ok(Some(Self::Flat(hash)))
173        }
174    }
175}
176
177impl Serialize for CAHash {
178    /// map a CAHash into the serde data model.
179    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
180    where
181        S: Serializer,
182    {
183        let mut map = serializer.serialize_map(Some(2))?;
184        match self {
185            CAHash::Flat(h) => {
186                map.serialize_entry("hash", &nixbase32::encode(h.digest_as_bytes()))?;
187                map.serialize_entry("hashAlgo", &h.algo())?;
188            }
189            CAHash::Nar(h) => {
190                map.serialize_entry("hash", &nixbase32::encode(h.digest_as_bytes()))?;
191                map.serialize_entry("hashAlgo", &format!("r:{}", &h.algo()))?;
192            }
193            // It is not legal for derivations to use this (which is where
194            // we're currently exercising [Serialize] mostly,
195            // but it's still good to be able to serialize other CA hashes too.
196            CAHash::Text(h) => {
197                map.serialize_entry("hash", &nixbase32::encode(h.as_ref()))?;
198                map.serialize_entry("hashAlgo", "text")?;
199            }
200        };
201        map.end()
202    }
203}
204
205impl<'de> Deserialize<'de> for CAHash {
206    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
207    where
208        D: Deserializer<'de>,
209    {
210        let value = Self::from_map::<D>(&Map::deserialize(deserializer)?)?;
211
212        match value {
213            None => Err(serde::de::Error::custom("couldn't parse as map")),
214            Some(v) => Ok(v),
215        }
216    }
217}
218
219#[cfg(test)]
220mod tests {
221    use crate::{derivation::CAHash, nixhash};
222
223    #[test]
224    fn serialize_flat() {
225        let json_bytes = r#"{
226  "hash": "1fnf2m46ya7r7afkcb8ba2j0sc4a85m749sh9jz64g4hx6z3r088",
227  "hashAlgo": "sha256"
228}"#;
229        let hash = CAHash::Flat(
230            nixhash::from_nix_str(
231                "sha256:08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba",
232            )
233            .unwrap(),
234        );
235        let serialized = serde_json::to_string_pretty(&hash).unwrap();
236        assert_eq!(serialized, json_bytes);
237    }
238
239    #[test]
240    fn serialize_nar() {
241        let json_bytes = r#"{
242  "hash": "1fnf2m46ya7r7afkcb8ba2j0sc4a85m749sh9jz64g4hx6z3r088",
243  "hashAlgo": "r:sha256"
244}"#;
245        let hash = CAHash::Nar(
246            nixhash::from_nix_str(
247                "sha256:08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba",
248            )
249            .unwrap(),
250        );
251        let serialized = serde_json::to_string_pretty(&hash).unwrap();
252        assert_eq!(serialized, json_bytes);
253    }
254
255    #[test]
256    fn deserialize_flat() {
257        let json_bytes = r#"
258        {
259            "hash": "08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba",
260            "hashAlgo": "sha256"
261        }"#;
262        let hash: CAHash = serde_json::from_str(json_bytes).expect("must parse");
263
264        assert_eq!(
265            hash,
266            CAHash::Flat(
267                nixhash::from_nix_str(
268                    "sha256:08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba"
269                )
270                .unwrap()
271            )
272        );
273    }
274
275    #[test]
276    fn deserialize_hex() {
277        let json_bytes = r#"
278        {
279            "hash": "08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba",
280            "hashAlgo": "r:sha256"
281        }"#;
282        let hash: CAHash = serde_json::from_str(json_bytes).expect("must parse");
283
284        assert_eq!(
285            hash,
286            CAHash::Nar(
287                nixhash::from_nix_str(
288                    "sha256:08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba"
289                )
290                .unwrap()
291            )
292        );
293    }
294
295    #[test]
296    fn deserialize_nixbase32() {
297        let json_bytes = r#"
298        {
299            "hash": "1fnf2m46ya7r7afkcb8ba2j0sc4a85m749sh9jz64g4hx6z3r088",
300            "hashAlgo": "r:sha256"
301        }"#;
302        let hash: CAHash = serde_json::from_str(json_bytes).expect("must parse");
303
304        assert_eq!(
305            hash,
306            CAHash::Nar(
307                nixhash::from_nix_str(
308                    "sha256:08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba"
309                )
310                .unwrap()
311            )
312        );
313    }
314
315    #[test]
316    fn deserialize_base64() {
317        let json_bytes = r#"
318        {
319            "hash": "CIE8vumQPGK+TFAncmpBijANpFALLTadOvkob0gVzro=",
320            "hashAlgo": "r:sha256"
321        }"#;
322        let hash: CAHash = serde_json::from_str(json_bytes).expect("must parse");
323
324        assert_eq!(
325            hash,
326            CAHash::Nar(
327                nixhash::from_nix_str(
328                    "sha256:08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba"
329                )
330                .unwrap()
331            )
332        );
333    }
334
335    #[test]
336    fn serialize_deserialize_nar() {
337        let json_bytes = r#"
338        {
339            "hash": "08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba",
340            "hashAlgo": "r:sha256"
341        }"#;
342        let hash: CAHash = serde_json::from_str(json_bytes).expect("must parse");
343
344        let serialized = serde_json::to_string(&hash).expect("Serialize");
345        let hash2: CAHash = serde_json::from_str(&serialized).expect("must parse again");
346
347        assert_eq!(hash, hash2);
348    }
349
350    #[test]
351    fn serialize_deserialize_flat() {
352        let json_bytes = r#"
353        {
354            "hash": "08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba",
355            "hashAlgo": "sha256"
356        }"#;
357        let hash: CAHash = serde_json::from_str(json_bytes).expect("must parse");
358
359        let serialized = serde_json::to_string(&hash).expect("Serialize");
360        let hash2: CAHash = serde_json::from_str(&serialized).expect("must parse again");
361
362        assert_eq!(hash, hash2);
363    }
364}