Skip to content

Commit 00b7b74

Browse files
refactor(psl): deny indexing_slicing and remove slice indexing
Add a module-scoped deny(clippy::indexing_slicing) for non-test code, mirroring the crate-wide lint planned in #207, as an interim guard. Replace the remaining panic-capable slice indexing in parse_header, is_public_suffix and public_suffix with split_at_checked, starts_with, last, .get() and split_once. Behaviour is unchanged (verified by the existing unit tests and the gated system-file test).
1 parent d262f04 commit 00b7b74

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

libwebauthn/src/ops/webauthn/psl/dafsa.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ impl DafsaFilePublicSuffixList {
7979
if let Some(flags) = lookup(&self.graph, domain.as_bytes()) {
8080
return (flags & FLAG_EXCEPTION) == 0;
8181
}
82-
if let Some(parent_start) = domain.find('.').map(|i| i + 1) {
83-
let parent = &domain[parent_start..];
82+
if let Some((_, parent)) = domain.split_once('.') {
8483
if let Some(flags) = lookup(&self.graph, parent.as_bytes()) {
8584
return (flags & FLAG_WILDCARD) != 0;
8685
}
@@ -96,40 +95,43 @@ impl PublicSuffixList for DafsaFilePublicSuffixList {
9695
if self.is_public_suffix(current) {
9796
return Some(current.to_string());
9897
}
99-
match current.find('.') {
100-
Some(i) => current = &current[i + 1..],
98+
match current.split_once('.') {
99+
Some((_, rest)) => current = rest,
101100
None => return None,
102101
}
103102
}
104103
}
105104
}
106105

107106
fn parse_header(bytes: &[u8]) -> Result<Vec<u8>, DafsaFileLoadError> {
108-
if bytes.len() < HEADER_LEN {
109-
return Err(DafsaFileLoadError::Truncated);
110-
}
111-
if &bytes[..MAGIC.len()] != MAGIC {
107+
let (header, graph) = bytes
108+
.split_at_checked(HEADER_LEN)
109+
.ok_or(DafsaFileLoadError::Truncated)?;
110+
if !header.starts_with(MAGIC) {
112111
return Err(DafsaFileLoadError::BadMagic);
113112
}
114-
if bytes[HEADER_LEN - 1] != b'\n' {
113+
if header.last() != Some(&b'\n') {
115114
return Err(DafsaFileLoadError::BadMagic);
116115
}
117-
let version_field = &bytes[MAGIC.len()..HEADER_LEN - 1];
116+
let version_field = header
117+
.get(MAGIC.len()..HEADER_LEN - 1)
118+
.ok_or(DafsaFileLoadError::BadMagic)?;
118119
let digit_count = version_field
119120
.iter()
120121
.take_while(|b| b.is_ascii_digit())
121122
.count();
122-
if digit_count == 0 {
123-
return Err(DafsaFileLoadError::BadMagic);
124-
}
125-
let version: u32 = std::str::from_utf8(&version_field[..digit_count])
123+
let version_digits = version_field
124+
.get(..digit_count)
125+
.filter(|digits| !digits.is_empty())
126+
.ok_or(DafsaFileLoadError::BadMagic)?;
127+
let version: u32 = std::str::from_utf8(version_digits)
126128
.map_err(|_| DafsaFileLoadError::BadMagic)?
127129
.parse()
128130
.map_err(|_| DafsaFileLoadError::BadMagic)?;
129131
if version != 0 {
130132
return Err(DafsaFileLoadError::UnsupportedVersion(version));
131133
}
132-
Ok(bytes[HEADER_LEN..].to_vec())
134+
Ok(graph.to_vec())
133135
}
134136

135137
/// Port of `LookupStringInFixedSet` from libpsl's `lookup_string_in_fixed_set.c`.

libwebauthn/src/ops/webauthn/psl/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
//! Most callers should use [`SystemPublicSuffixList::auto`], which probes
2121
//! the standard system paths for whichever format is available.
2222
23+
// Module-scoped until the crate-wide indexing_slicing deny lands.
24+
#![cfg_attr(not(any(test, feature = "virt")), deny(clippy::indexing_slicing))]
25+
2326
pub mod dafsa;
2427
pub mod dat;
2528
mod system;

0 commit comments

Comments
 (0)