From fb5a1060bd93bb94ac696be63ec7547248422ed3 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sun, 1 May 2022 17:59:24 +0000 Subject: [PATCH] Check the length of the bytes passed to `Stub::from_bytes` This will be zero-length when the client uses `-j yubikey`. Closes str4d/age-plugin-yubikey#48. --- src/key.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/key.rs b/src/key.rs index 6883d20..01b5209 100644 --- a/src/key.rs +++ b/src/key.rs @@ -238,6 +238,9 @@ impl Stub { } pub(crate) fn from_bytes(bytes: &[u8], identity_index: usize) -> Option { + if bytes.len() < 9 { + return None; + } let serial = Serial::from(u32::from_le_bytes(bytes[0..4].try_into().unwrap())); let slot: RetiredSlotId = bytes[4].try_into().ok()?; Some(Stub { @@ -572,6 +575,8 @@ mod tests { }; let encoded = stub.to_bytes(); + assert_eq!(Stub::from_bytes(&[], 0), None); assert_eq!(Stub::from_bytes(&encoded, 0), Some(stub)); + assert_eq!(Stub::from_bytes(&encoded[..encoded.len() - 1], 0), None); } }