Skip to content

Commit a8d058f

Browse files
fix: handle invalid input length when parsing a node id (#3155)
This avoids a panic when calling `decode_mut` with an invalid input length. Closes #3153
1 parent f220cc3 commit a8d058f

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

iroh-base/src/key.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,12 @@ fn decode_base32_hex(s: &str) -> Result<[u8; 32], KeyParsingError> {
330330
// hex
331331
data_encoding::HEXLOWER.decode_mut(s.as_bytes(), &mut bytes)
332332
} else {
333-
data_encoding::BASE32_NOPAD.decode_mut(s.to_ascii_uppercase().as_bytes(), &mut bytes)
333+
let input = s.to_ascii_uppercase();
334+
let input = input.as_bytes();
335+
if data_encoding::BASE32_NOPAD.decode_len(input.len())? != bytes.len() {
336+
return Err(KeyParsingError::DecodeInvalidLength);
337+
}
338+
data_encoding::BASE32_NOPAD.decode_mut(input, &mut bytes)
334339
};
335340
match res {
336341
Ok(len) => {
@@ -390,4 +395,10 @@ mod tests {
390395
key.public()
391396
);
392397
}
398+
399+
#[test]
400+
fn test_regression_parse_node_id_panic() {
401+
let not_a_node_id = "foobarbaz";
402+
assert!(PublicKey::from_str(not_a_node_id).is_err());
403+
}
393404
}

0 commit comments

Comments
 (0)