Skip to content

Commit 782afa7

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 2846e2f commit 782afa7

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
@@ -196,7 +196,8 @@ where
196196
let mut rapdu = Vec::new();
197197

198198
let len = self.handle_in_ctx(ctx, &command_buf, &mut buf)?;
199-
let mut resp = Response::from(&buf[..len]);
199+
let resp_bytes = buf.get(..len).ok_or(HandleError::NotEnoughBuffer(len))?;
200+
let mut resp = Response::from(resp_bytes);
200201

201202
let (mut sw1, mut sw2) = resp.trailer;
202203
rapdu.extend_from_slice(resp.payload);
@@ -205,7 +206,8 @@ where
205206
let get_response_cmd = command_get_response(0x00, 0x00, sw2);
206207
let get_response_buf = Vec::from(get_response_cmd);
207208
let len = self.handle_in_ctx(ctx, &get_response_buf, &mut buf)?;
208-
resp = Response::from(&buf[..len]);
209+
let resp_bytes = buf.get(..len).ok_or(HandleError::NotEnoughBuffer(len))?;
210+
resp = Response::from(resp_bytes);
209211
(sw1, sw2) = resp.trailer;
210212
rapdu.extend_from_slice(resp.payload);
211213
}
@@ -272,8 +274,8 @@ where
272274
let mut rest: &[u8] = data;
273275

274276
while rest.len() > 250 {
275-
let to_send = &rest[..250];
276-
rest = &rest[250..];
277+
let (to_send, remaining) = rest.split_at(250);
278+
rest = remaining;
277279
let ctap_msg = command_ctap_msg(true, to_send);
278280
let resp = self.handle(self.ctx, ctap_msg)?;
279281
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)