From 0193f07642db752c3e14e02064c02b0fd1cc060b Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sat, 19 Aug 2023 22:01:31 +0200 Subject: refactor(tvix/nix-compat/nixhash): validate digest lengths There was a NixHash::new() before, which didn't perform any validation of the digest length. We had some length validation when parsing nix hashes or SRI hashes, but some places didn't perform validation and/or constructed the struct directly. Replace NixHash::new() with a `impl TryFrom<(HashAlgo, Vec)> for NixHash`, which does do this validation, and update constructing code to use that, rather than populating structs directly. In some rare cases where we're sure the digest length is correct we still populate the struct manually. Fixes b/291. Change-Id: I7a323c5b18d94de0ec15e391b3e7586df42f4229 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9109 Reviewed-by: raitobezarius Autosubmit: flokli Tested-by: BuildkiteCI --- tvix/nix-compat/src/nixhash/mod.rs | 24 ++++++++++---- tvix/nix-compat/src/nixhash/with_mode.rs | 56 +++++++++++++++++++++----------- 2 files changed, 54 insertions(+), 26 deletions(-) (limited to 'tvix/nix-compat/src/nixhash') diff --git a/tvix/nix-compat/src/nixhash/mod.rs b/tvix/nix-compat/src/nixhash/mod.rs index 77a9035901d9..2bd24d4ed3db 100644 --- a/tvix/nix-compat/src/nixhash/mod.rs +++ b/tvix/nix-compat/src/nixhash/mod.rs @@ -1,6 +1,6 @@ use crate::nixbase32; use data_encoding::{BASE64, BASE64_NOPAD, HEXLOWER}; -use thiserror::Error; +use thiserror; mod algos; mod with_mode; @@ -18,11 +18,6 @@ pub struct NixHash { } impl NixHash { - /// Constructs a new [NixHash] by specifying [HashAlgo] and digest. - pub fn new(algo: HashAlgo, digest: Vec) -> Self { - Self { algo, digest } - } - /// Formats a [NixHash] in the Nix default hash format, /// which is the algo, followed by a colon, then the lower hex encoded digest. pub fn to_nix_hash_string(&self) -> String { @@ -30,8 +25,23 @@ impl NixHash { } } +impl TryFrom<(HashAlgo, Vec)> for NixHash { + type Error = Error; + + /// Constructs a new [NixHash] by specifying [HashAlgo] and digest. + // It can fail if the passed digest length doesn't match what's expected for + // the passed algo. + fn try_from(value: (HashAlgo, Vec)) -> Result { + let (algo, digest) = value; + if digest.len() != hash_algo_length(&algo) { + return Err(Error::InvalidEncodedDigestLength(digest.len(), algo)); + } + Ok(Self { algo, digest }) + } +} + /// Errors related to NixHash construction. -#[derive(Debug, Error)] +#[derive(Debug, thiserror::Error)] pub enum Error { #[error("invalid hash algo: {0}")] InvalidAlgo(String), diff --git a/tvix/nix-compat/src/nixhash/with_mode.rs b/tvix/nix-compat/src/nixhash/with_mode.rs index 344322046614..caf14331426a 100644 --- a/tvix/nix-compat/src/nixhash/with_mode.rs +++ b/tvix/nix-compat/src/nixhash/with_mode.rs @@ -1,5 +1,5 @@ use crate::nixbase32; -use crate::nixhash::{HashAlgo, NixHash}; +use crate::nixhash::{self, HashAlgo, NixHash}; use serde::de::Unexpected; use serde::ser::SerializeMap; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -102,24 +102,42 @@ impl NixHashWithMode { if let Some(v) = map.get("hashAlgo") { if let Some(s) = v.as_str() { match s.strip_prefix("r:") { - Some(rest) => Ok(Some(Self::Recursive(NixHash::new( - HashAlgo::try_from(rest).map_err(|e| { - serde::de::Error::invalid_value( - Unexpected::Other(&e.to_string()), - &format!("one of {}", SUPPORTED_ALGOS.join(",")).as_str(), - ) - })?, - digest, - )))), - None => Ok(Some(Self::Flat(NixHash::new( - HashAlgo::try_from(s).map_err(|e| { - serde::de::Error::invalid_value( - Unexpected::Other(&e.to_string()), - &format!("one of {}", SUPPORTED_ALGOS.join(",")).as_str(), - ) - })?, - digest, - )))), + Some(rest) => Ok(Some(Self::Recursive( + ( + HashAlgo::try_from(rest).map_err(|e| { + serde::de::Error::invalid_value( + Unexpected::Other(&e.to_string()), + &format!("one of {}", SUPPORTED_ALGOS.join(",")).as_str(), + ) + })?, + digest, + ) + .try_into() + .map_err(|e: nixhash::Error| { + serde::de::Error::invalid_value( + Unexpected::Other(&e.to_string()), + &"a digest with right length", + ) + })?, + ))), + None => Ok(Some(Self::Flat( + ( + HashAlgo::try_from(s).map_err(|e| { + serde::de::Error::invalid_value( + Unexpected::Other(&e.to_string()), + &format!("one of {}", SUPPORTED_ALGOS.join(",")).as_str(), + ) + })?, + digest, + ) + .try_into() + .map_err(|e: nixhash::Error| { + serde::de::Error::invalid_value( + Unexpected::Other(&e.to_string()), + &"a digest with right length", + ) + })?, + ))), } } else { Err(serde::de::Error::invalid_type( -- cgit 1.4.1