Skip to content

Commit 7fd290f

Browse files
fix(transport): bounds-check continuation-packet parsing in HID framing
HidMessageParser::update validated continuation packets by indexing packet[4] and slicing packet[..4]. The length guard at the top of the function keeps those in bounds, but the raw indexing trips clippy::indexing_slicing. Use .get() and is_some_and so the accesses are bounded at the call site, with no change in behaviour.
1 parent 8b02aaf commit 7fd290f

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

libwebauthn/src/transport/hid/framing.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ impl HidMessageParser {
141141
if self.packets.is_empty() {
142142
// First packet must be an initialization packet: high bit of
143143
// byte 4 set (CTAP 2.2 §11.2.4).
144-
if packet[4] & PACKET_INITIAL_CMD_MASK == 0 {
144+
let is_initialization = packet
145+
.get(4)
146+
.is_some_and(|&byte| byte & PACKET_INITIAL_CMD_MASK != 0);
147+
if !is_initialization {
145148
error!("First packet is not an initialization packet");
146149
return Err(IOError::new(
147150
IOErrorKind::InvalidData,
@@ -151,15 +154,20 @@ impl HidMessageParser {
151154
} else {
152155
// Continuation packets: same CID as the initial packet, SEQ has
153156
// high bit cleared, SEQ starts at 0 and increments monotonically.
154-
let initial = &self.packets[0];
155-
if packet[..4] != initial[..4] {
157+
let initial_cid = self.packets.first().and_then(|initial| initial.get(..4));
158+
if packet.get(..4) != initial_cid {
156159
error!("Continuation packet CID does not match initial packet");
157160
return Err(IOError::new(
158161
IOErrorKind::InvalidData,
159162
"Continuation packet CID mismatch",
160163
));
161164
}
162-
let seq = packet[4];
165+
let Some(&seq) = packet.get(4) else {
166+
return Err(IOError::new(
167+
IOErrorKind::InvalidData,
168+
"Packet length is invalid",
169+
));
170+
};
163171
if seq & PACKET_INITIAL_CMD_MASK != 0 {
164172
error!(seq, "Unexpected init packet during continuation");
165173
return Err(IOError::new(

0 commit comments

Comments
 (0)