Skip to content

Commit

Permalink
ref(reference): Refactor digest parsing and validation
Browse files Browse the repository at this point in the history
Don't use a fixed digest algorithm length.

Signed-off-by: Benjamin Neff <[email protected]>
  • Loading branch information
SuperTux88 authored and cgwalters committed Aug 30, 2024
1 parent e4a6295 commit 28348ad
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/distribution/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,33 +256,25 @@ impl TryFrom<String> for Reference {
}
// Digests much always be hex-encoded, ensuring that their hex portion will always be
// size*2
if reference.digest().is_some() {
let d = reference.digest().unwrap();
// FIXME: we should actually separate the algorithm from the digest
// using regular expressions. This won't hold up if we support an
// algorithm more or less than 6 characters like sha1024.
if d.len() < 8 {
return Err(ParseError::DigestInvalidFormat);
}
let algo = &d[0..6];
let digest = &d[7..];
match algo {
"sha256" => {
if let Some(digest) = reference.digest() {
match digest.split_once(':') {
None => return Err(ParseError::DigestInvalidFormat),
Some(("sha256", digest)) => {
if digest.len() != 64 {
return Err(ParseError::DigestInvalidLength);
}
}
"sha384" => {
Some(("sha384", digest)) => {
if digest.len() != 96 {
return Err(ParseError::DigestInvalidLength);
}
}
"sha512" => {
Some(("sha512", digest)) => {
if digest.len() != 128 {
return Err(ParseError::DigestInvalidLength);
}
}
_ => return Err(ParseError::DigestUnsupported),
Some((_, _)) => return Err(ParseError::DigestUnsupported),
}
}
Ok(reference)
Expand Down

0 comments on commit 28348ad

Please sign in to comment.