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/store_path/utils.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'tvix/nix-compat/src/store_path') diff --git a/tvix/nix-compat/src/store_path/utils.rs b/tvix/nix-compat/src/store_path/utils.rs index 26f6e0085c9d..201d98cce460 100644 --- a/tvix/nix-compat/src/store_path/utils.rs +++ b/tvix/nix-compat/src/store_path/utils.rs @@ -54,7 +54,13 @@ pub fn build_text_path, I: IntoIterator, C: AsRef<[u8]>> let hasher = Sha256::new_with_prefix(content); hasher.finalize() }; - NixHash::new(crate::nixhash::HashAlgo::Sha256, content_digest.to_vec()) + + // We populate the struct directly, as we know the sha256 digest has the + // right size. + NixHash { + algo: crate::nixhash::HashAlgo::Sha256, + digest: content_digest.to_vec(), + } }, name, ) @@ -100,7 +106,13 @@ pub fn build_regular_ca_path, I: IntoIterator>( hasher.update(":"); hasher.finalize() }; - NixHash::new(crate::nixhash::HashAlgo::Sha256, content_digest.to_vec()) + + // We don't use [NixHash::from_algo_and_digest], as we know [Sha256] has + // the right digest size. + NixHash { + algo: crate::nixhash::HashAlgo::Sha256, + digest: content_digest.to_vec(), + } }, name, ) -- cgit 1.4.1