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<u8>)> 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 <tvl@lahfa.xyz>
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
This commit is contained in:
Florian Klink 2023-08-19 22:01:31 +02:00 committed by flokli
parent 4017039595
commit 0193f07642
6 changed files with 99 additions and 43 deletions

View file

@ -185,7 +185,13 @@ impl Derivation {
hasher.finalize().to_vec()
});
NixHash::new(crate::nixhash::HashAlgo::Sha256, 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: digest.to_vec(),
}
}
/// This calculates all output paths of a Derivation and updates the struct.