pub(super) type Alphabet = fn(u8) -> bool;
pub(super) fn is_alnum(byte: u8) -> bool {
byte.is_ascii_alphanumeric()
}
pub(super) fn is_upper_alnum(byte: u8) -> bool {
byte.is_ascii_uppercase() || byte.is_ascii_digit()
}
pub(super) fn is_alnum_underscore(byte: u8) -> bool {
is_alnum(byte) || byte == b'_'
}
pub(super) fn is_alnum_dash(byte: u8) -> bool {
is_alnum(byte) || byte == b'_' || byte == b'-'
}
pub(super) fn is_digit(byte: u8) -> bool {
byte.is_ascii_digit()
}
pub(super) fn is_alnum_dash_dot(byte: u8) -> bool {
is_alnum_dash(byte) || byte == b'.'
}
pub(super) fn is_lower_hex(byte: u8) -> bool {
byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)
}
pub(super) fn is_hex(byte: u8) -> bool {
byte.is_ascii_hexdigit()
}
pub(super) fn is_hex_or_dash(byte: u8) -> bool {
is_hex(byte) || byte == b'-'
}
pub(super) fn is_lower_alnum(byte: u8) -> bool {
byte.is_ascii_digit() || byte.is_ascii_lowercase()
}
pub(super) fn is_base64_body(byte: u8) -> bool {
is_alnum(byte) || byte == b'+' || byte == b'/'
}
#[derive(Clone, Copy, Debug)]
pub(super) enum RunLength {
Exact(usize),
OneOf(&'static [usize]),
AtLeast(usize),
OpenFloor(usize),
}
pub(super) type PostCheck = fn(&[u8], usize, usize) -> bool;
#[derive(Clone, Copy, Debug)]
pub(super) struct PrefixShape<'a> {
pub(super) prefix: &'a str,
pub(super) run: RunLength,
pub(super) alphabet: Alphabet,
pub(super) signals: &'static [&'static str],
pub(super) post_check: Option<PostCheck>,
}
impl<'a> PrefixShape<'a> {
pub(super) const fn exact(
prefix: &'a str,
len: usize,
alphabet: Alphabet,
signals: &'static [&'static str],
) -> Self {
Self {
prefix,
run: RunLength::Exact(len),
alphabet,
signals,
post_check: None,
}
}
pub(super) const fn one_of(
prefix: &'a str,
lens: &'static [usize],
alphabet: Alphabet,
signals: &'static [&'static str],
) -> Self {
Self {
prefix,
run: RunLength::OneOf(lens),
alphabet,
signals,
post_check: None,
}
}
pub(super) const fn at_least(
prefix: &'a str,
len: usize,
alphabet: Alphabet,
signals: &'static [&'static str],
) -> Self {
Self {
prefix,
run: RunLength::AtLeast(len),
alphabet,
signals,
post_check: None,
}
}
pub(super) const fn open_floor(
prefix: &'a str,
len: usize,
alphabet: Alphabet,
signals: &'static [&'static str],
) -> Self {
Self {
prefix,
run: RunLength::OpenFloor(len),
alphabet,
signals,
post_check: None,
}
}
pub(super) const fn with_post_check(mut self, post_check: PostCheck) -> Self {
self.post_check = Some(post_check);
self
}
}
#[cfg(test)]
pub(super) fn run_ends(bytes: &[u8], alphabet: Alphabet) -> Vec<usize> {
probe::record_table();
let mut ends = vec![0usize; bytes.len() + 1];
ends[bytes.len()] = bytes.len();
for index in (0..bytes.len()).rev() {
ends[index] = if alphabet(bytes[index]) {
ends[index + 1]
} else {
index
};
}
ends
}
pub(super) fn run_end(bytes: &[u8], from: usize, alphabet: Alphabet) -> usize {
from + bytes[from..]
.iter()
.position(|&byte| !alphabet(byte))
.unwrap_or(bytes.len() - from)
}
pub(super) struct RunCursor<'a> {
bytes: &'a [u8],
alphabet: Alphabet,
start: usize,
end: usize,
#[cfg(test)]
scanned: usize,
}
impl<'a> RunCursor<'a> {
pub(super) fn new(bytes: &'a [u8], alphabet: Alphabet) -> Self {
Self {
bytes,
alphabet,
start: bytes.len(),
end: bytes.len(),
#[cfg(test)]
scanned: 0,
}
}
pub(super) fn end(&mut self, at: usize) -> usize {
let end = self.measure(at);
#[cfg(test)]
probe::check_answer(self.bytes, self.alphabet, at, end);
end
}
fn measure(&mut self, at: usize) -> usize {
if self.start <= at && at <= self.end {
return self.end;
}
let rest = &self.bytes[at..];
let mut end = at;
for &byte in rest {
if end == self.start || !(self.alphabet)(byte) {
break;
}
end += 1;
}
#[cfg(test)]
{
self.scanned += end - at;
probe::record_scanned(end - at);
}
if end == self.start {
end = self.end;
}
self.start = at;
self.end = end;
end
}
#[cfg(test)]
pub(super) fn scanned(&self) -> usize {
self.scanned
}
}
#[cfg(test)]
pub(super) mod probe {
use super::{Alphabet, run_ends};
use std::cell::Cell;
thread_local! {
static SCANNED: Cell<usize> = const { Cell::new(0) };
static CHECKING: Cell<bool> = const { Cell::new(false) };
static CHECKED: Cell<usize> = const { Cell::new(0) };
static TABLES: Cell<usize> = const { Cell::new(0) };
}
pub(in crate::detectors) fn record_scanned(bytes: usize) {
SCANNED.with(|scanned| scanned.set(scanned.get() + bytes));
}
pub(in crate::detectors) fn record_table() {
TABLES.with(|tables| tables.set(tables.get() + 1));
}
pub(in crate::detectors) fn tables_during(body: impl FnOnce()) -> usize {
let before = TABLES.with(Cell::get);
body();
TABLES.with(Cell::get) - before
}
pub(in crate::detectors) fn check_answer(
bytes: &[u8],
alphabet: Alphabet,
at: usize,
end: usize,
) {
if CHECKING.with(Cell::get) {
assert_eq!(end, run_ends(bytes, alphabet)[at], "{bytes:?} at {at}");
CHECKED.with(|checked| checked.set(checked.get() + 1));
}
}
pub(in crate::detectors) fn scanned_during(body: impl FnOnce()) -> usize {
let before = SCANNED.with(Cell::get);
body();
SCANNED.with(Cell::get) - before
}
pub(in crate::detectors) fn with_table_check(body: impl FnOnce()) -> usize {
struct Reset;
impl Drop for Reset {
fn drop(&mut self) {
CHECKING.with(|checking| checking.set(false));
}
}
let before = CHECKED.with(Cell::get);
CHECKING.with(|checking| checking.set(true));
let reset = Reset;
body();
drop(reset);
CHECKED.with(Cell::get) - before
}
}
pub(super) fn find_literal(bytes: &[u8], needle: &[u8], from: usize) -> Option<usize> {
find_literal_with(&LeadBytes::of(std::iter::once(needle)), bytes, needle, from)
}
pub(super) fn find_literal_with(
lead: &LeadBytes,
bytes: &[u8],
needle: &[u8],
from: usize,
) -> Option<usize> {
let (&first, rest) = needle.split_first()?;
let last_start = bytes.len().checked_sub(needle.len())?;
let candidates = &bytes[..=last_start];
let mut at = from;
while at <= last_start {
at = lead.find(candidates, at)?;
debug_assert_eq!(bytes[at], first);
if bytes[at + 1..at + needle.len()] == *rest {
return Some(at);
}
at += 1;
}
None
}
pub(super) fn boundary_ok(bytes: &[u8], start: usize, end: usize, boundary: Alphabet) -> bool {
let before_ok = start == 0 || !boundary(bytes[start - 1]);
let after_ok = end >= bytes.len() || !boundary(bytes[end]);
before_ok && after_ok
}
pub(super) fn open_floor_run_end(
bytes: &[u8],
cursor: usize,
floor: usize,
available: usize,
) -> usize {
let floor_end = cursor + floor;
if available > floor && matches!(bytes.get(floor_end), Some(b'-' | b'_')) {
floor_end
} else {
cursor + available
}
}
const WORD_SEARCH_LEADS: usize = 4;
const LOW_BITS: u64 = u64::from_le_bytes([0x01; 8]);
const HIGH_BITS: u64 = u64::from_le_bytes([0x80; 8]);
pub(super) struct LeadBytes {
table: [bool; 256],
leads: [u8; WORD_SEARCH_LEADS],
lead_count: Option<usize>,
}
impl LeadBytes {
pub(super) fn of<'a>(prefixes: impl Iterator<Item = &'a [u8]>) -> Self {
let mut lead_bytes = Self {
table: [false; 256],
leads: [0; WORD_SEARCH_LEADS],
lead_count: Some(0),
};
for prefix in prefixes {
let Some(&byte) = prefix.first() else {
lead_bytes.table = [true; 256];
lead_bytes.lead_count = None;
return lead_bytes;
};
if lead_bytes.table[usize::from(byte)] {
continue;
}
lead_bytes.table[usize::from(byte)] = true;
lead_bytes.lead_count = lead_bytes
.lead_count
.filter(|&count| count < WORD_SEARCH_LEADS)
.map(|count| {
lead_bytes.leads[count] = byte;
count + 1
});
}
lead_bytes
}
pub(super) fn find(&self, bytes: &[u8], from: usize) -> Option<usize> {
let mut at = from;
if let Some(count) = self.lead_count {
let leads = &self.leads[..count];
while let Some(word) = bytes.get(at..at + 8) {
let word = u64::from_le_bytes(word.try_into().ok()?);
let mut hits = 0u64;
for &lead in leads {
let difference = word ^ (u64::from(lead) * LOW_BITS);
hits |= difference.wrapping_sub(LOW_BITS) & !difference & HIGH_BITS;
}
if hits != 0 {
return Some(at + (hits.trailing_zeros() / 8) as usize);
}
at += 8;
}
}
bytes
.get(at..)?
.iter()
.position(|&byte| self.table[usize::from(byte)])
.map(|offset| at + offset)
}
}
fn has_prefix_at(bytes: &[u8], at: usize, prefix: &[u8]) -> bool {
bytes.len() - at >= prefix.len() && bytes[at..].iter().zip(prefix).all(|(a, b)| a == b)
}
pub(super) fn scan_prefixed_runs(
input: &str,
prefixes: &[&str],
run: RunLength,
alphabet: Alphabet,
boundary: Alphabet,
) -> Vec<(usize, usize)> {
if LeadBytes::of(prefixes.iter().map(|prefix| prefix.as_bytes()))
.find(input.as_bytes(), 0)
.is_none()
{
return Vec::new();
}
let shapes: Vec<PrefixShape<'_>> = prefixes
.iter()
.map(|prefix| PrefixShape {
prefix,
run,
alphabet,
signals: &[],
post_check: None,
})
.collect();
scan_prefixed_shapes(input, &shapes, boundary)
.into_iter()
.map(|(start, end, _signals)| (start, end))
.collect()
}
pub(super) fn scan_prefixed_shapes(
input: &str,
shapes: &[PrefixShape<'_>],
boundary: Alphabet,
) -> Vec<(usize, usize, &'static [&'static str])> {
let bytes = input.as_bytes();
let lead_bytes = LeadBytes::of(shapes.iter().map(|shape| shape.prefix.as_bytes()));
let Some(mut start) = lead_bytes.find(bytes, 0) else {
return Vec::new();
};
let mut cursors: Vec<RunCursor<'_>> = Vec::new();
let mut matches = Vec::new();
while start < bytes.len() {
let Some(lead) = lead_bytes.find(bytes, start) else {
break;
};
start = lead;
let Some(shape) = shapes
.iter()
.filter(|shape| has_prefix_at(bytes, start, shape.prefix.as_bytes()))
.max_by_key(|shape| shape.prefix.len())
else {
start += 1;
continue;
};
let cursor_index = if let Some(index) = cursors
.iter()
.position(|cursor| std::ptr::fn_addr_eq(cursor.alphabet, shape.alphabet))
{
index
} else {
cursors.push(RunCursor::new(bytes, shape.alphabet));
cursors.len() - 1
};
let suffix_start = start + shape.prefix.len();
let available = cursors[cursor_index].end(suffix_start) - suffix_start;
let matched_len = match shape.run {
RunLength::Exact(len) if available >= len => len,
RunLength::OneOf(lens) => {
let accepted = |len: usize| {
let end = suffix_start + len;
available >= len
&& shape
.post_check
.is_none_or(|check| check(bytes, start, end))
&& boundary_ok(bytes, start, end, boundary)
};
let Some(len) = lens.iter().copied().filter(|&len| accepted(len)).max() else {
start += 1;
continue;
};
len
}
RunLength::AtLeast(len) if available >= len => available,
RunLength::OpenFloor(len) if available >= len => {
open_floor_run_end(bytes, suffix_start, len, available) - suffix_start
}
_ => {
start += 1;
continue;
}
};
let end = suffix_start + matched_len;
let post_check_ok = match shape.post_check {
Some(check) => check(bytes, start, end),
None => true,
};
if post_check_ok && boundary_ok(bytes, start, end, boundary) {
matches.push((start, end, shape.signals));
}
start = end;
}
matches
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lead_bytes_find_the_same_offset_as_a_byte_at_a_time_scan() {
let mut haystack: Vec<u8> = Vec::new();
for round in 0u8..40 {
haystack.extend_from_slice(&[0x00, 0x01, 0x7f, 0x80, 0xff, b'a', b's', b'k', b'_']);
haystack.extend(std::iter::repeat_n(
round.wrapping_mul(37),
usize::from(round % 11),
));
}
let prefix_sets: [&[&[u8]]; 7] = [
&[b"s"],
&[b"sk_", b"sk-", b"rk_"],
&[b"\x00", b"\x01"],
&[b"\x80\x81", b"\xff"],
&[b"a", b"k", b"s", b"_"],
&[b"a", b"k", b"s", b"_", b"\x7f"],
&[b"x", b""],
];
for prefixes in prefix_sets {
let lead_bytes = LeadBytes::of(prefixes.iter().copied());
for end in [0, 1, 7, 8, 9, 63, haystack.len()] {
let bytes = &haystack[..end];
for from in 0..=end + 1 {
let expected = (from..end).find(|&at| {
prefixes
.iter()
.any(|prefix| prefix.first().is_none_or(|&lead| lead == bytes[at]))
});
assert_eq!(
lead_bytes.find(bytes, from),
expected,
"{prefixes:?} end {end} from {from}"
);
}
}
}
}
const ALPHABETS: [Alphabet; 12] = [
is_alnum,
is_upper_alnum,
is_alnum_underscore,
is_alnum_dash,
is_digit,
is_alnum_dash_dot,
is_lower_hex,
is_hex,
is_hex_or_dash,
is_lower_alnum,
is_base64_body,
|byte| byte == b'0',
];
struct XorShift(u64);
impl XorShift {
fn next(&mut self) -> u64 {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
self.0
}
fn below(&mut self, bound: usize) -> usize {
usize::try_from(self.next() % bound as u64).unwrap()
}
}
fn random_inputs(rng: &mut XorShift) -> Vec<Vec<u8>> {
const BYTES: &[u8] = b"0aF9zZ_-.+/= \n\x80";
let mut inputs = vec![Vec::new(), b"0".to_vec(), b"-".to_vec()];
for _ in 0..200 {
let len = rng.below(96);
let mut input = Vec::with_capacity(len);
while input.len() < len {
let byte = BYTES[rng.below(BYTES.len())];
let repeat = if rng.below(4) == 0 { rng.below(24) } else { 1 };
input.extend(std::iter::repeat_n(byte, repeat));
}
input.truncate(len);
inputs.push(input);
}
inputs
}
#[test]
fn run_end_matches_the_run_end_table_at_every_offset() {
let mut rng = XorShift(0x0982_0001_d1ce_f00d);
for input in random_inputs(&mut rng) {
for alphabet in ALPHABETS {
let table = run_ends(&input, alphabet);
for (from, &expected) in table.iter().enumerate() {
assert_eq!(
run_end(&input, from, alphabet),
expected,
"{input:?} {from}"
);
}
}
}
}
#[test]
fn run_cursor_matches_the_run_end_table_for_arbitrary_query_sequences() {
let mut rng = XorShift(0x0982_0002_5eed_cafe);
for input in random_inputs(&mut rng) {
for alphabet in ALPHABETS {
let table = run_ends(&input, alphabet);
for _ in 0..8 {
let mut cursor = RunCursor::new(&input, alphabet);
let mut at = rng.below(table.len());
for _ in 0..64 {
assert_eq!(cursor.end(at), table[at], "{input:?} {at}");
at = match rng.below(5) {
0 => rng.below(table.len()),
1 => at.saturating_sub(rng.below(30)),
2 => at,
_ => (at + rng.below(8)).min(input.len()),
};
}
}
}
}
}
#[test]
fn run_cursor_tests_each_byte_once_for_forward_queries() {
let input = "sk-".repeat(40_000);
let bytes = input.as_bytes();
let mut cursor = RunCursor::new(bytes, is_alnum_dash);
for at in 0..=bytes.len() {
assert_eq!(cursor.end(at), bytes.len());
}
assert!(cursor.scanned() <= bytes.len(), "{}", cursor.scanned());
}
#[test]
fn run_cursor_rescans_only_the_backward_step() {
let input = "a".repeat(100_000);
let bytes = input.as_bytes();
let mut cursor = RunCursor::new(bytes, is_alnum);
assert_eq!(cursor.end(50_000), bytes.len());
let first = cursor.scanned();
for at in (0..50_000).rev() {
assert_eq!(cursor.end(at), bytes.len());
}
assert_eq!(cursor.scanned() - first, 50_000);
}
#[test]
fn find_literal_returns_the_first_occurrence_at_or_after_the_offset() {
let haystacks: [&[u8]; 6] = [
b"",
b"x",
b"xoxb-xoxb",
b"aaaa",
b"xoxxoxbxoxb-",
b"://:/://",
];
let needles: [&[u8]; 5] = [b"x", b"xoxb-", b"aa", b"://", b"aaaaa"];
for haystack in haystacks {
for needle in needles {
for from in 0..=haystack.len() + 1 {
let expected =
(from..haystack.len()).find(|&at| haystack[at..].starts_with(needle));
assert_eq!(
find_literal(haystack, needle, from),
expected,
"{haystack:?} {needle:?} {from}"
);
}
}
}
assert_eq!(find_literal(b"abc", b"", 0), None);
}
#[test]
fn alphabets_match_documented_classes() {
assert!(is_alnum(b'a') && is_alnum(b'9') && !is_alnum(b'_'));
assert!(is_upper_alnum(b'A') && is_upper_alnum(b'0') && !is_upper_alnum(b'a'));
assert!(is_alnum_underscore(b'_') && !is_alnum_underscore(b'-'));
assert!(is_alnum_dash(b'-') && is_alnum_dash(b'_') && !is_alnum_dash(b'.'));
assert!(is_alnum_dash_dot(b'.') && is_alnum_dash_dot(b'-'));
assert!(is_digit(b'0') && is_digit(b'9') && !is_digit(b'a') && !is_digit(b'-'));
assert!(
is_lower_alnum(b'0')
&& is_lower_alnum(b'a')
&& is_lower_alnum(b'z')
&& !is_lower_alnum(b'A')
&& !is_lower_alnum(b'_')
&& !is_lower_alnum(b'-')
);
assert!(
is_lower_hex(b'0')
&& is_lower_hex(b'f')
&& !is_lower_hex(b'g')
&& !is_lower_hex(b'F')
&& !is_lower_hex(b'_')
);
assert!(
is_hex(b'0')
&& is_hex(b'f')
&& is_hex(b'F')
&& !is_hex(b'g')
&& !is_hex(b'G')
&& !is_hex(b'-')
);
assert!(is_hex_or_dash(b'-') && is_hex_or_dash(b'a') && !is_hex_or_dash(b'g'));
assert!(
is_base64_body(b'+')
&& is_base64_body(b'/')
&& !is_base64_body(b'=')
&& !is_base64_body(b'_')
);
}
#[test]
fn exact_run_rejects_a_longer_alphabet_span() {
let matches = scan_prefixed_runs(
"AKIA0123456789ABCDEFG",
&["AKIA"],
RunLength::Exact(16),
is_upper_alnum,
is_alnum,
);
assert_eq!(matches, Vec::new());
}
#[test]
fn at_least_run_takes_the_maximal_span() {
let input = "hf_0123456789012345678901234";
let matches = scan_prefixed_runs(
input,
&["hf_"],
RunLength::AtLeast(20),
is_alnum_dash,
is_alnum_dash,
);
assert_eq!(matches, vec![(0, input.len())]);
}
#[test]
fn open_floor_run_extends_past_the_floor_when_not_delimited() {
let input = "hf_0123456789012345678901234";
let matches = scan_prefixed_runs(
input,
&["hf_"],
RunLength::OpenFloor(20),
is_alnum_dash,
is_alnum_dash,
);
assert_eq!(matches, vec![(0, input.len())]);
}
#[test]
fn open_floor_run_is_capped_and_rejected_when_delimited_at_the_floor() {
for suffix in ["_backup", "-1"] {
let input = format!("hf_{}{suffix}", "0".repeat(20));
let matches = scan_prefixed_runs(
&input,
&["hf_"],
RunLength::OpenFloor(20),
is_alnum_dash,
is_alnum_dash,
);
assert_eq!(matches, Vec::new(), "{suffix}");
}
}
#[test]
fn open_floor_run_end_extends_past_a_plain_alphanumeric_continuation() {
let bytes = b"01234567890123456789extra";
assert_eq!(open_floor_run_end(bytes, 0, 20, bytes.len()), bytes.len());
}
#[test]
fn open_floor_run_end_caps_at_the_floor_when_delimited() {
let bytes = b"01234567890123456789_backup";
assert_eq!(open_floor_run_end(bytes, 0, 20, bytes.len()), 20);
let bytes = b"01234567890123456789-1";
assert_eq!(open_floor_run_end(bytes, 0, 20, bytes.len()), 20);
}
#[test]
fn longest_matching_prefix_wins_at_a_shared_position() {
let input = "glrtr-0123456789012345678901234";
let matches = scan_prefixed_runs(
input,
&["glrt-", "glrtr-"],
RunLength::AtLeast(20),
is_alnum_dash,
is_alnum_dash,
);
assert_eq!(matches, vec![(0, input.len())]);
}
#[test]
fn a_run_that_never_reaches_the_minimum_finds_nothing() {
let input = "hf_!".repeat(8);
let matches = scan_prefixed_runs(
&input,
&["hf_"],
RunLength::AtLeast(20),
is_alnum_dash,
is_alnum_dash,
);
assert_eq!(matches, Vec::new());
}
#[test]
fn repeated_embedded_prefixes_in_one_run_stay_linear() {
let input = "AKIA".repeat(20_000);
let matches = scan_prefixed_runs(
&input,
&["AKIA", "ASIA"],
RunLength::Exact(16),
is_upper_alnum,
is_alnum,
);
assert_eq!(matches, Vec::new());
assert!(input.len() > 79_000);
}
const SWITCHED: [&str; 14] = [
"azure-devops-personal-access-token",
"firebase-server-key",
"gitlab-runner-authentication-token",
"grafana-service-account-token",
"microsoft-entra-client-secret",
"notion-token",
"openai-token",
"sendgrid-token",
"sentry-org-auth-token",
"slack-token",
"stripe-token",
"terraform-cloud-token",
"github-token",
"docker-token",
];
const DIGITS: &[u8] = b"0123456789";
const ALNUM: &[u8] = b"SYNTHETICrevoked0123456789";
const ALNUM_DASH: &[u8] = b"SYNTHETICrevoked0123456789-_";
const HEX: &[u8] = b"0123456789abcdef";
const BASE64: &[u8] = b"SYNTHETICrevoked0123456789+/";
#[derive(Clone, Copy)]
enum Piece {
Lit(&'static str),
Run(&'static [u8], usize),
}
use Piece::{Lit, Run};
const TEMPLATES: &[&[Piece]] = &[
&[Run(ALNUM, 76), Lit("AZDO"), Run(ALNUM, 4)],
&[
Lit("AAAA"),
Run(ALNUM_DASH, 7),
Lit(":"),
Run(ALNUM_DASH, 140),
],
&[Lit("glrt-"), Run(ALNUM_DASH, 20)],
&[
Lit("glrt-"),
Run(ALNUM_DASH, 27),
Lit("."),
Run(ALNUM, 2),
Lit("."),
Run(ALNUM, 9),
],
&[Lit("glsa_"), Run(ALNUM, 32), Lit("_"), Run(HEX, 8)],
&[Run(ALNUM, 3), Run(DIGITS, 1), Lit("Q~"), Run(ALNUM, 32)],
&[Lit("ntn_"), Run(DIGITS, 11), Run(ALNUM, 35)],
&[Lit("sk-"), Run(ALNUM, 20), Lit("T3BlbkFJ"), Run(ALNUM, 20)],
&[
Lit("sk-proj-"),
Run(ALNUM_DASH, 74),
Lit("T3BlbkFJ"),
Run(ALNUM_DASH, 74),
],
&[
Lit("sk-svcacct-"),
Run(ALNUM_DASH, 58),
Lit("T3BlbkFJ"),
Run(ALNUM_DASH, 58),
],
&[
Lit("SG."),
Run(ALNUM_DASH, 22),
Lit("."),
Run(ALNUM_DASH, 43),
],
&[
Lit("sntrys_eyJ"),
Run(BASE64, 30),
Lit("=_"),
Run(BASE64, 43),
],
&[Lit("whsec_"), Run(BASE64, 32)],
&[
Lit("xoxb-"),
Run(DIGITS, 11),
Lit("-"),
Run(DIGITS, 12),
Lit("-"),
Run(ALNUM, 24),
],
&[
Lit("xoxp-"),
Run(DIGITS, 6),
Lit("-"),
Run(DIGITS, 7),
Lit("-"),
Run(DIGITS, 8),
Lit("-"),
Run(ALNUM, 28),
],
&[
Lit("xapp-"),
Run(DIGITS, 1),
Lit("-"),
Run(ALNUM, 11),
Lit("-"),
Run(DIGITS, 13),
Lit("-"),
Run(ALNUM, 64),
],
&[
Lit("xoxe.xoxb-"),
Run(DIGITS, 1),
Lit("-"),
Run(ALNUM_DASH, 30),
],
&[Run(ALNUM, 14), Lit(".atlasv1."), Run(ALNUM, 67)],
&[Lit("ghp_"), Run(ALNUM, 36)],
&[Lit("dckr_pat_"), Run(ALNUM_DASH, 27)],
];
const FILLER: &[&str] = &[
" ",
"\n",
"-",
"_",
".",
":",
"=",
"/",
"é",
"\u{200B}",
"\u{FEFF}",
"한",
"sk-",
"xoxb-",
"xapp-",
"xoxe.",
"AZDO",
"Q~",
"glrt-",
"glsa_",
"eyJ",
"T3BlbkFJ",
"SG.",
"AAAA",
"ntn_",
"whsec_",
".atlasv1.",
"sntrys_",
"ghp_",
];
fn render(rng: &mut XorShift, template: &[Piece], perturb: bool) -> String {
let mut value = String::new();
for piece in template {
match *piece {
Lit(literal) => value.push_str(literal),
Run(pool, len) => {
let len = match rng.below(if perturb { 6 } else { 1 }) {
1 => len.saturating_sub(1),
2 => len + 1,
_ => len,
};
value.extend((0..len).map(|_| char::from(pool[rng.below(pool.len())])));
}
}
}
value
}
fn generated_detector_inputs(rng: &mut XorShift) -> Vec<String> {
let mut inputs = vec![String::new(), "xoxb-".to_owned(), "sk-".to_owned()];
for template in TEMPLATES {
inputs.push(render(rng, template, false));
}
for _ in 0..300 {
let mut input = String::new();
for _ in 0..=rng.below(10) {
match rng.below(3) {
0 => {
let template = TEMPLATES[rng.below(TEMPLATES.len())];
input.push_str(&render(rng, template, true));
}
1 => input.push_str(FILLER[rng.below(FILLER.len())]),
_ => {
let len = rng.below(40);
input.push_str(&render(rng, &[Run(ALNUM_DASH, len)], false));
}
}
}
if rng.below(4) == 0 {
let mut cut = rng.below(input.len() + 1);
while !input.is_char_boundary(cut) {
cut -= 1;
}
input.truncate(cut);
}
inputs.push(input);
}
inputs
}
#[test]
fn every_detector_run_query_equals_the_whole_input_table() {
use crate::types::{Detector, DetectorContext};
let mut rng = XorShift(0x1056_0001_c0de_5eed);
let mut queries = std::collections::HashMap::<&str, usize>::new();
let mut found = std::collections::HashMap::<&str, usize>::new();
for input in generated_detector_inputs(&mut rng) {
for row in crate::detectors::built_in_detectors() {
let detector: &dyn Detector = &**row;
let mut candidates = 0;
let checked = probe::with_table_check(|| {
candidates = detector
.detect(&input, &DetectorContext::new(input.len()))
.map_or(0, |candidates| candidates.len());
});
*queries.entry(row.id).or_default() += checked;
*found.entry(row.id).or_default() += candidates;
}
}
for id in SWITCHED {
assert!(
queries.get(id).copied().unwrap_or(0) > 0,
"{id} made no query"
);
assert!(
found.get(id).copied().unwrap_or(0) > 0,
"{id} found nothing"
);
}
}
#[test]
fn switched_detectors_measure_only_the_queried_runs() {
use crate::types::{Detector, DetectorContext};
let mut rng = XorShift(0x1056_0002_0bad_cafe);
let filler = "plain words, spaces; nothing else. ";
for template in TEMPLATES {
let value = render(&mut rng, template, false);
let mut measured = Vec::new();
for filler_len in [4 << 10, 256 << 10] {
let input = format!("{value} {}", filler.repeat(filler_len / filler.len()));
let mut scanned = 0;
let mut candidates = 0;
let tables = probe::tables_during(|| {
scanned = probe::scanned_during(|| {
for row in crate::detectors::built_in_detectors() {
if !SWITCHED.contains(&row.id) {
continue;
}
let detector: &dyn Detector = &**row;
candidates += detector
.detect(&input, &DetectorContext::new(input.len()))
.map_or(0, |candidates| candidates.len());
}
});
});
assert_eq!(tables, 0, "{value}");
measured.push((scanned, candidates));
}
assert_eq!(measured[0], measured[1], "{value}");
assert!(measured[1].0 <= 8 * value.len(), "{value} {measured:?}");
}
}
#[test]
fn scan_prefixed_shapes_stays_exact_and_linear_when_queries_step_back() {
const LONGEST: usize = 8;
let shapes = [
PrefixShape::exact("ab-cdefg", 12, is_alnum, &[]),
PrefixShape::exact("b", 12, is_alnum, &[]),
PrefixShape::one_of("cd", &[3, 5], is_alnum, &[]),
PrefixShape::at_least("g-", 4, is_alnum_dash, &[]),
PrefixShape::open_floor("e", 2, is_alnum_dash, &[]),
];
let pieces = ["ab-cdefg", "b", "cd", "g-", "e", "-", " ", "é", "\u{200B}"];
let mut rng = XorShift(0x1056_0003_feed_f00d);
for _ in 0..300 {
let mut input = String::new();
for _ in 0..rng.below(40) {
if rng.below(3) == 0 {
let len = rng.below(30);
input.push_str(&render(&mut rng, &[Run(ALNUM, len)], false));
} else {
input.push_str(pieces[rng.below(pieces.len())]);
}
}
let scanned = probe::scanned_during(|| {
probe::with_table_check(|| {
scan_prefixed_shapes(&input, &shapes, is_alnum_dash);
});
});
assert!(
scanned <= (LONGEST + 1) * (input.len() + 1),
"{input:?} {scanned}"
);
}
}
}