Skip to main content

snix_store/proto/
mod.rs

1#![allow(clippy::derive_partial_eq_without_eq, non_snake_case)]
2use bstr::ByteSlice;
3use bytes::Bytes;
4use data_encoding::BASE64;
5// https://github.com/hyperium/tonic/issues/1056
6use nix_compat::{
7    narinfo::{Signature, SignatureError},
8    nixhash::{CAHash, NixHash},
9    store_path::{self, StorePathRef},
10};
11use snix_castore::DirectoryError;
12use thiserror::Error;
13
14mod grpc_pathinfoservice_wrapper;
15
16pub use grpc_pathinfoservice_wrapper::GRPCPathInfoServiceWrapper;
17
18tonic::include_proto!("snix.store.v1");
19
20use snix_castore::proto as castorepb;
21
22/// Compiled file descriptors for implementing [gRPC
23/// reflection](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md) with e.g.
24/// [`tonic_reflection`](https://docs.rs/tonic-reflection).
25pub const FILE_DESCRIPTOR_SET: &[u8] = tonic::include_file_descriptor_set!("snix.store.v1");
26
27#[cfg(test)]
28mod tests;
29
30/// Errors that can occur during the validation of PathInfo messages.
31#[derive(Debug, Error, PartialEq)]
32pub enum ValidatePathInfoError {
33    /// Invalid length of a reference
34    #[error("Invalid length of digest at position {}, expected {}, got {}", .0, store_path::DIGEST_SIZE, .1)]
35    InvalidReferenceDigestLen(usize, usize),
36
37    /// No entry present
38    #[error("No entry present")]
39    NoEntryPresent,
40
41    /// Node fails validation
42    #[error("Invalid root node: {:?}", .0.to_string())]
43    InvalidRootNode(DirectoryError),
44
45    /// Invalid node name encountered. Root nodes in PathInfos have more strict name requirements
46    #[error("Failed to parse {} as StorePath: {}", .0.to_str_lossy(), .1)]
47    InvalidNodeName(Vec<u8>, store_path::Error),
48
49    /// The digest in narinfo.nar_sha256 has an invalid len.
50    #[error("Invalid narinfo.nar_sha256 length: expected {}, got {}", 32, .0)]
51    InvalidNarSha256DigestLen(usize),
52
53    /// The number of references in the narinfo.reference_names field does not match
54    /// the number of references in the .references field.
55    #[error("Inconsistent Number of References: {0} (references) vs {1} (narinfo)")]
56    InconsistentNumberOfReferences(usize, usize),
57
58    /// A string in narinfo.reference_names does not parse to a [store_path::StorePath].
59    #[error("Invalid reference_name at position {0}: {1}")]
60    InvalidNarinfoReferenceName(usize, String),
61
62    /// The digest in the parsed `.narinfo.reference_names[i]` does not match
63    /// the one in `.references[i]`.`
64    #[error("digest in reference_name at position {} does not match digest in PathInfo, expected {}, got {}", .0, BASE64.encode(.1), BASE64.encode(.2))]
65    InconsistentNarinfoReferenceNameDigest(
66        usize,
67        [u8; store_path::DIGEST_SIZE],
68        [u8; store_path::DIGEST_SIZE],
69    ),
70
71    /// The deriver field is invalid.
72    #[error("deriver field is invalid: {0}")]
73    InvalidDeriverField(store_path::Error),
74
75    /// The narinfo field is missing
76    #[error("The narinfo field is missing")]
77    NarInfoFieldMissing,
78
79    /// The ca field is invalid
80    #[error("The ca field is invalid: {0}")]
81    InvalidCaField(ConvertCAError),
82
83    /// The signature at position is invalid
84    #[error("The signature at position {0} is invalid: {1}")]
85    InvalidSignature(usize, SignatureError),
86}
87
88/// Errors that can occur when converting from a [nar_info::Ca] to a (stricter)
89/// [nix_compat::nixhash::CAHash].
90#[derive(Debug, Error, PartialEq)]
91pub enum ConvertCAError {
92    /// Invalid length of a reference
93    #[error("Invalid digest length '{0}' for type {1}")]
94    InvalidReferenceDigestLen(usize, &'static str),
95    /// Unknown Hash type
96    #[error("Unknown hash type: {0}")]
97    UnknownHashType(i32),
98}
99
100impl TryFrom<&nar_info::Ca> for nix_compat::nixhash::CAHash {
101    type Error = ConvertCAError;
102
103    fn try_from(value: &nar_info::Ca) -> Result<Self, Self::Error> {
104        Ok(match value.r#type {
105            typ if typ == nar_info::ca::Hash::NarSha256 as i32 => {
106                Self::Nar(NixHash::Sha256(value.digest[..].try_into().map_err(
107                    |_| ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "NarSha256"),
108                )?))
109            }
110            typ if typ == nar_info::ca::Hash::NarSha1 as i32 => {
111                Self::Nar(NixHash::Sha1(value.digest[..].try_into().map_err(
112                    |_| ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "NarSha1"),
113                )?))
114            }
115            typ if typ == nar_info::ca::Hash::NarSha512 as i32 => Self::Nar(NixHash::Sha512(
116                Box::new(value.digest[..].try_into().map_err(|_| {
117                    ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "NarSha512")
118                })?),
119            )),
120            typ if typ == nar_info::ca::Hash::NarMd5 as i32 => {
121                Self::Nar(NixHash::Md5(value.digest[..].try_into().map_err(|_| {
122                    ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "NarMd5")
123                })?))
124            }
125            typ if typ == nar_info::ca::Hash::TextSha256 as i32 => {
126                Self::Text(value.digest[..].try_into().map_err(|_| {
127                    ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "TextSha256")
128                })?)
129            }
130            typ if typ == nar_info::ca::Hash::FlatSha1 as i32 => {
131                Self::Flat(NixHash::Sha1(value.digest[..].try_into().map_err(
132                    |_| ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "FlatSha1"),
133                )?))
134            }
135            typ if typ == nar_info::ca::Hash::FlatMd5 as i32 => {
136                Self::Flat(NixHash::Md5(value.digest[..].try_into().map_err(|_| {
137                    ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "FlatMd5")
138                })?))
139            }
140            typ if typ == nar_info::ca::Hash::FlatSha256 as i32 => {
141                Self::Flat(NixHash::Sha256(value.digest[..].try_into().map_err(
142                    |_| ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "FlatSha256"),
143                )?))
144            }
145            typ if typ == nar_info::ca::Hash::FlatSha512 as i32 => Self::Flat(NixHash::Sha512(
146                Box::new(value.digest[..].try_into().map_err(|_| {
147                    ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "FlatSha512")
148                })?),
149            )),
150            typ => return Err(ConvertCAError::UnknownHashType(typ)),
151        })
152    }
153}
154
155impl From<&nix_compat::nixhash::CAHash> for nar_info::ca::Hash {
156    fn from(value: &nix_compat::nixhash::CAHash) -> Self {
157        match value {
158            CAHash::Flat(NixHash::Md5(_)) => nar_info::ca::Hash::FlatMd5,
159            CAHash::Flat(NixHash::Sha1(_)) => nar_info::ca::Hash::FlatSha1,
160            CAHash::Flat(NixHash::Sha256(_)) => nar_info::ca::Hash::FlatSha256,
161            CAHash::Flat(NixHash::Sha512(_)) => nar_info::ca::Hash::FlatSha512,
162            CAHash::Nar(NixHash::Md5(_)) => nar_info::ca::Hash::NarMd5,
163            CAHash::Nar(NixHash::Sha1(_)) => nar_info::ca::Hash::NarSha1,
164            CAHash::Nar(NixHash::Sha256(_)) => nar_info::ca::Hash::NarSha256,
165            CAHash::Nar(NixHash::Sha512(_)) => nar_info::ca::Hash::NarSha512,
166            CAHash::Text(_) => nar_info::ca::Hash::TextSha256,
167        }
168    }
169}
170
171impl From<&nix_compat::nixhash::CAHash> for nar_info::Ca {
172    fn from(value: &nix_compat::nixhash::CAHash) -> Self {
173        nar_info::Ca {
174            r#type: Into::<nar_info::ca::Hash>::into(value) as i32,
175            digest: value.hash().digest_as_bytes().to_vec().into(),
176        }
177    }
178}
179
180impl From<crate::pathinfoservice::PathInfo> for PathInfo {
181    fn from(value: crate::pathinfoservice::PathInfo) -> Self {
182        Self {
183            entry: Some(castorepb::Entry::from_name_and_node(
184                value.store_path.to_string().into_bytes().into(),
185                value.node,
186            )),
187            references: value
188                .references
189                .iter()
190                .map(|reference| Bytes::copy_from_slice(reference.digest()))
191                .collect(),
192            narinfo: Some(NarInfo {
193                nar_size: value.nar_size,
194                nar_sha256: Bytes::copy_from_slice(&value.nar_sha256),
195                signatures: value
196                    .signatures
197                    .iter()
198                    .map(|sig| nar_info::Signature {
199                        name: sig.name().to_string(),
200                        data: Bytes::copy_from_slice(sig.bytes()),
201                    })
202                    .collect(),
203                reference_names: value.references.iter().map(|r| r.to_string()).collect(),
204                deriver: value.deriver.as_ref().map(|sp| StorePath {
205                    name: (*sp.name()).to_owned(),
206                    digest: Bytes::copy_from_slice(sp.digest()),
207                }),
208                ca: value.ca.as_ref().map(|ca| ca.into()),
209            }),
210        }
211    }
212}
213
214impl TryFrom<PathInfo> for crate::pathinfoservice::PathInfo {
215    type Error = ValidatePathInfoError;
216    fn try_from(value: PathInfo) -> Result<Self, Self::Error> {
217        let narinfo = value
218            .narinfo
219            .ok_or_else(|| ValidatePathInfoError::NarInfoFieldMissing)?;
220
221        // ensure the references have the right number of bytes.
222        for (i, reference) in value.references.iter().enumerate() {
223            if reference.len() != store_path::DIGEST_SIZE {
224                return Err(ValidatePathInfoError::InvalidReferenceDigestLen(
225                    i,
226                    reference.len(),
227                ));
228            }
229        }
230
231        // ensure the number of references there matches PathInfo.references count.
232        if narinfo.reference_names.len() != value.references.len() {
233            return Err(ValidatePathInfoError::InconsistentNumberOfReferences(
234                value.references.len(),
235                narinfo.reference_names.len(),
236            ));
237        }
238
239        // parse references in reference_names.
240        let mut references = vec![];
241        for (i, reference_name_str) in narinfo.reference_names.iter().enumerate() {
242            // ensure thy parse as (non-absolute) store path
243            let reference_names_store_path =
244                StorePathRef::from_bytes(reference_name_str.as_bytes()).map_err(|_| {
245                    ValidatePathInfoError::InvalidNarinfoReferenceName(
246                        i,
247                        reference_name_str.to_owned(),
248                    )
249                })?;
250
251            // ensure their digest matches the one at self.references[i].
252            {
253                // This is safe, because we ensured the proper length earlier already.
254                let reference_digest = value.references[i].to_vec().try_into().unwrap();
255
256                if reference_names_store_path.digest() != &reference_digest {
257                    return Err(
258                        ValidatePathInfoError::InconsistentNarinfoReferenceNameDigest(
259                            i,
260                            reference_digest,
261                            *reference_names_store_path.digest(),
262                        ),
263                    );
264                } else {
265                    references.push(reference_names_store_path.to_owned());
266                }
267            }
268        }
269
270        let nar_sha256_length = narinfo.nar_sha256.len();
271
272        // split value.entry into the name and node components
273        let (name, node) = value
274            .entry
275            .ok_or_else(|| ValidatePathInfoError::NoEntryPresent)?
276            .try_into_name_and_node()
277            .map_err(ValidatePathInfoError::InvalidRootNode)?;
278
279        Ok(Self {
280            // value.node has a valid name according to the castore model but might not parse to a
281            // [StorePath]
282            store_path: nix_compat::store_path::StorePath::from_bytes(name.as_ref()).map_err(
283                |err| ValidatePathInfoError::InvalidNodeName(name.as_ref().to_vec(), err),
284            )?,
285            node,
286            references,
287            nar_size: narinfo.nar_size,
288            nar_sha256: narinfo.nar_sha256.to_vec()[..]
289                .try_into()
290                .map_err(|_| ValidatePathInfoError::InvalidNarSha256DigestLen(nar_sha256_length))?,
291            // If the Deriver field is populated, ensure it parses to a
292            // [StorePath].
293            // We can't check for it to *not* end with .drv, as the .drv files produced by
294            // recursive Nix end with multiple .drv suffixes, and only one is popped when
295            // converting to this field.
296            deriver: narinfo
297                .deriver
298                .map(|deriver| {
299                    nix_compat::store_path::StorePath::from_name_and_digest(
300                        &deriver.name,
301                        &deriver.digest,
302                    )
303                    .map_err(ValidatePathInfoError::InvalidDeriverField)
304                })
305                .transpose()?,
306            signatures: narinfo
307                .signatures
308                .into_iter()
309                .enumerate()
310                .map(|(i, signature)| {
311                    signature.data.to_vec()[..]
312                        .try_into()
313                        .map_err(|_| {
314                            ValidatePathInfoError::InvalidSignature(
315                                i,
316                                SignatureError::InvalidSignatureLen(signature.data.len()),
317                            )
318                        })
319                        .map(|signature_data| Signature::new(signature.name, signature_data))
320                })
321                .collect::<Result<Vec<_>, ValidatePathInfoError>>()?,
322            ca: narinfo
323                .ca
324                .as_ref()
325                .map(TryFrom::try_from)
326                .transpose()
327                .map_err(ValidatePathInfoError::InvalidCaField)?,
328        })
329    }
330}