use crate::detectors::pattern::{self, RunLength};
use crate::detectors::prefilter::Literals;
use crate::detectors::text;
use crate::error::DetectorFailure;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const PREFIX: &str = "nfp_";
const BODY_LEN: usize = 36;
fn boundary(byte: u8) -> bool {
pattern::is_alnum_dash(byte) || byte == b'_'
}
pub(super) struct NetlifyPersonalAccessTokenDetector;
impl Detector for NetlifyPersonalAccessTokenDetector {
fn id(&self) -> &'static str {
"netlify-token"
}
fn detect(
&self,
input: &str,
_context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure> {
let mut candidates = Vec::new();
for (start, end) in pattern::scan_prefixed_runs(
input,
&[PREFIX],
RunLength::Exact(BODY_LEN),
pattern::is_alnum_underscore,
boundary,
) {
if text::is_repeated_character_filler(&input[start + PREFIX.len()..end]) {
continue;
}
let Some(range) = ByteRange::new(start, end) else {
continue;
};
candidates.push(
Candidate::built_in("netlify_personal_access_token", Confidence::High, range)
.with_specificity(Specificity::Provider)
.with_signals([
"netlify-documented-prefix",
"exact-length-alnum-underscore-body",
]),
);
}
Ok(candidates)
}
}
pub(super) const REQUIRED_LITERALS: &[Literals] = &[Literals::Strs(&[PREFIX])];
#[cfg(test)]
mod tests {
use super::*;
const BODY: &str = "SYNTHETIC_REVOKED_NETLIFY_PAT_BODY01";
const _: () = assert!(BODY.len() == BODY_LEN);
fn detect(input: &str) -> Vec<Candidate> {
NetlifyPersonalAccessTokenDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn token() -> String {
format!("{PREFIX}{BODY}")
}
#[test]
fn detects_the_token_with_provider_specificity() {
let value = token();
let candidates = detect(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "netlify_personal_access_token");
assert_eq!(candidates[0].confidence(), Confidence::High);
assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, value.len()).unwrap()
);
}
#[test]
fn rejects_a_body_one_byte_short_of_the_documented_length() {
let short_body = &BODY[..BODY_LEN - 1];
assert!(detect(&format!("{PREFIX}{short_body}")).is_empty());
}
#[test]
fn rejects_a_body_one_byte_longer_than_the_documented_length() {
assert!(detect(&format!("{PREFIX}{BODY}a")).is_empty());
}
#[test]
fn rejects_a_body_containing_a_byte_outside_the_documented_alphabet() {
for byte in ['-', '.', '+', '/'] {
let mut body = BODY.to_string();
body.replace_range(4..5, &byte.to_string());
assert!(detect(&format!("{PREFIX}{body}")).is_empty());
}
}
#[test]
fn rejects_an_undocumented_prefix() {
assert!(detect(&format!("nfc_{BODY}")).is_empty());
assert!(detect(&format!("nfo_{BODY}")).is_empty());
assert!(detect(&format!("nfu_{BODY}")).is_empty());
assert!(detect(&format!("nfb_{BODY}")).is_empty());
assert!(detect(&format!("nf_{BODY}")).is_empty());
}
#[test]
fn rejects_a_bare_unprefixed_legacy_shaped_value() {
assert!(detect(&"a".repeat(43)).is_empty());
}
#[test]
fn rejects_the_prefix_embedded_in_a_wider_identifier() {
assert!(detect(&format!("legacy{}", token())).is_empty());
}
#[test]
fn rejects_the_value_embedded_in_a_wider_identifier_leading_trailing_or_dash_joined() {
let value = token();
assert!(detect(&format!("legacy{value}")).is_empty());
assert!(detect(&format!("{value}_backup")).is_empty());
assert!(detect(&format!("{value}-1")).is_empty());
}
#[test]
fn rejects_a_percent_encoded_delimiter_lookalike() {
assert!(detect(&format!("nfp%5F{BODY}")).is_empty());
}
#[test]
fn accepts_punctuation_boundaries() {
let value = token();
let input = format!("({value}).");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].range(),
ByteRange::new(1, value.len() + 1).unwrap()
);
}
#[test]
fn rejects_a_masked_value() {
assert!(detect(&format!("{PREFIX}{}", "X".repeat(BODY_LEN))).is_empty());
assert!(detect(&format!("{PREFIX}${{NETLIFY_AUTH_TOKEN}}")).is_empty());
}
#[test]
fn reports_a_repeated_identical_value_once_per_occurrence() {
let value = token();
let input = format!("{value} {value}");
assert_eq!(detect(&input).len(), 2);
}
#[test]
fn finds_a_match_across_crlf_and_a_unicode_prefix() {
let value = token();
let input = format!("# \u{1F511} caf\u{e9}\r\n{value}\r\n");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
let start = input.find(&value).unwrap();
assert_eq!(
candidates[0].range(),
ByteRange::new(start, start + value.len()).unwrap()
);
}
#[test]
fn finds_deterministic_findings_across_repeated_calls() {
let value = token();
assert_eq!(detect(&value), detect(&value));
}
#[test]
fn stays_bounded_over_a_long_run_of_near_miss_prefixes() {
let short_body = &BODY[..BODY_LEN - 1];
let input = format!("{PREFIX}{short_body} ").repeat(10_000);
assert_eq!(detect(&input).len(), 0);
}
}