Skip to content

Commit

Permalink
fix: handle invalid input length when parsing a node id (#3155)
Browse files Browse the repository at this point in the history
This avoids a panic when calling `decode_mut` with an invalid input
length.

Closes #3153
  • Loading branch information
dignifiedquire authored Jan 28, 2025
1 parent f220cc3 commit a8d058f
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion iroh-base/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,12 @@ fn decode_base32_hex(s: &str) -> Result<[u8; 32], KeyParsingError> {
// hex
data_encoding::HEXLOWER.decode_mut(s.as_bytes(), &mut bytes)
} else {
data_encoding::BASE32_NOPAD.decode_mut(s.to_ascii_uppercase().as_bytes(), &mut bytes)
let input = s.to_ascii_uppercase();
let input = input.as_bytes();
if data_encoding::BASE32_NOPAD.decode_len(input.len())? != bytes.len() {
return Err(KeyParsingError::DecodeInvalidLength);
}
data_encoding::BASE32_NOPAD.decode_mut(input, &mut bytes)
};
match res {
Ok(len) => {
Expand Down Expand Up @@ -390,4 +395,10 @@ mod tests {
key.public()
);
}

#[test]
fn test_regression_parse_node_id_panic() {
let not_a_node_id = "foobarbaz";
assert!(PublicKey::from_str(not_a_node_id).is_err());
}
}

0 comments on commit a8d058f

Please sign in to comment.