Skip to content

Commit 491c25b

Browse files
fix(nfc): bounds-check slicing flagged by clippy::indexing_slicing
The NFC backend (gated behind the `nfc`/`pcsc`/`libnfc` features) was not exercised by the default `cargo build`, so the lint enabled in the previous commits did not surface there. CI's `cargo clippy --all-targets --all-features` flags 5 sites: - `channel::NfcChannel::handle`: replace `&buf[..len]` (returned by `handle_in_ctx` into a fixed 1024-byte buffer) with `buf.get(..len)` and surface `HandleError::NotEnoughBuffer` if a backend overruns. - `channel::NfcChannel::cbor_send`: replace the `&rest[..250]` / `&rest[250..]` chunking with `rest.split_at(250)`; the `rest.len() > 250` loop predicate keeps this panic-safe. - `libnfc::Channel::connect_to_target`: replace `&modulations[modulations.len() - 1]` with `.last()`, returning `TransportUnavailable` if the device reports no supported baud rates.
1 parent e84a8db commit 491c25b

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

libwebauthn/src/transport/nfc/channel.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ where
186186
let mut rapdu = Vec::new();
187187

188188
let len = self.handle_in_ctx(ctx, &command_buf, &mut buf)?;
189-
let mut resp = Response::from(&buf[..len]);
189+
let resp_bytes = buf.get(..len).ok_or(HandleError::NotEnoughBuffer(len))?;
190+
let mut resp = Response::from(resp_bytes);
190191

191192
let (mut sw1, mut sw2) = resp.trailer;
192193
rapdu.extend_from_slice(resp.payload);
@@ -195,7 +196,8 @@ where
195196
let get_response_cmd = command_get_response(0x00, 0x00, sw2);
196197
let get_response_buf = Vec::from(get_response_cmd);
197198
let len = self.handle_in_ctx(ctx, &get_response_buf, &mut buf)?;
198-
resp = Response::from(&buf[..len]);
199+
let resp_bytes = buf.get(..len).ok_or(HandleError::NotEnoughBuffer(len))?;
200+
resp = Response::from(resp_bytes);
199201
(sw1, sw2) = resp.trailer;
200202
rapdu.extend_from_slice(resp.payload);
201203
}
@@ -262,8 +264,8 @@ where
262264
let mut rest: &[u8] = data;
263265

264266
while rest.len() > 250 {
265-
let to_send = &rest[..250];
266-
rest = &rest[250..];
267+
let (to_send, remaining) = rest.split_at(250);
268+
rest = remaining;
267269
let ctap_msg = command_ctap_msg(true, to_send);
268270
let resp = self.handle(self.ctx, ctap_msg)?;
269271
trace!("cbor_send has_more {:?} {:?}", to_send, resp);

libwebauthn/src/transport/nfc/libnfc/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ impl Channel {
150150
baud_rate: *baud_rate,
151151
})
152152
.collect::<Vec<nfc1::Modulation>>();
153-
let modulation = &modulations[modulations.len() - 1];
153+
let modulation = modulations
154+
.last()
155+
.ok_or(Error::Transport(TransportError::TransportUnavailable))?;
154156
let is_one_rate = modulations.len() == 1;
155157
for i in 0..2 {
156158
if i > 0 {

0 commit comments

Comments
 (0)