use crate::detectors::pattern::{self, RunCursor, is_alnum};
use crate::detectors::prefilter::Literals;
use crate::error::DetectorFailure;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const ANCHOR: &str = "AZDO";
const PREFIX_LEN: usize = 76;
const SUFFIX_LEN: usize = 4;
pub(super) struct AzureDevOpsPersonalAccessTokenDetector;
impl Detector for AzureDevOpsPersonalAccessTokenDetector {
fn id(&self) -> &'static str {
"azure-devops-personal-access-token"
}
fn detect(
&self,
input: &str,
_context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure> {
let mut candidates = Vec::new();
for (start, end) in scan(input) {
let Some(range) = ByteRange::new(start, end) else {
continue;
};
candidates.push(
Candidate::built_in(
"azure_devops_personal_access_token",
Confidence::High,
range,
)
.with_specificity(Specificity::Provider)
.with_signal_pack(crate::types::signal_pack!(
"azdo-fixed-signature",
"azdo-84-byte-length"
)),
);
}
Ok(candidates)
}
}
fn scan(input: &str) -> Vec<(usize, usize)> {
if input.len() < PREFIX_LEN + ANCHOR.len() + SUFFIX_LEN || !input.contains(ANCHOR) {
return Vec::new();
}
let bytes = input.as_bytes();
let mut prefix_ends = RunCursor::new(bytes, is_alnum);
let mut suffix_ends = RunCursor::new(bytes, is_alnum);
let mut matches = Vec::new();
let mut anchor = 0;
while anchor < bytes.len() {
if !bytes[anchor..].starts_with(ANCHOR.as_bytes()) {
anchor += 1;
continue;
}
let Some((start, end)) = match_at(bytes, &mut prefix_ends, &mut suffix_ends, anchor) else {
anchor += 1;
continue;
};
matches.push((start, end));
anchor = end;
}
matches
}
fn match_at(
bytes: &[u8],
prefix_ends: &mut RunCursor<'_>,
suffix_ends: &mut RunCursor<'_>,
anchor: usize,
) -> Option<(usize, usize)> {
let start = anchor.checked_sub(PREFIX_LEN)?;
if prefix_ends.end(start) < anchor {
return None;
}
let suffix_start = anchor + ANCHOR.len();
let end = suffix_start.checked_add(SUFFIX_LEN)?;
if end > bytes.len() || suffix_ends.end(suffix_start) < end {
return None;
}
pattern::boundary_ok(bytes, start, end, is_alnum).then_some((start, end))
}
pub(super) const REQUIRED_LITERALS: &[Literals] = &[Literals::Strs(&[ANCHOR])];
#[cfg(test)]
mod tests {
use super::*;
const PREFIX_76: &str =
"SYNTHETICREVOKEDAZUREDEVOPSPATVALUEFORCONFORMANCETESTINGPADDINGFIXTUREDATAZZ";
const SUFFIX_4: &str = "abcd";
fn detect(input: &str) -> Vec<Candidate> {
AzureDevOpsPersonalAccessTokenDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn token() -> String {
format!("{PREFIX_76}AZDO{SUFFIX_4}")
}
#[test]
fn detects_the_synthetic_fixture() {
let input = token();
assert_eq!(input.len(), 84);
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].type_name(),
"azure_devops_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, input.len()).unwrap()
);
}
#[test]
fn rejects_a_prefix_one_byte_below_the_required_length() {
let short_prefix = &PREFIX_76[1..];
assert_eq!(short_prefix.len(), 75);
assert_eq!(detect(&format!("{short_prefix}AZDO{SUFFIX_4}")).len(), 0);
}
#[test]
fn rejects_a_prefix_one_byte_above_the_required_length_rather_than_truncating() {
let long_prefix = format!("x{PREFIX_76}");
assert_eq!(long_prefix.len(), 77);
assert_eq!(detect(&format!("{long_prefix}AZDO{SUFFIX_4}")).len(), 0);
}
#[test]
fn rejects_a_suffix_one_byte_below_the_required_length() {
assert_eq!(detect(&format!("{PREFIX_76}AZDOabc")).len(), 0);
}
#[test]
fn rejects_a_suffix_one_byte_above_the_required_length_rather_than_truncating() {
assert_eq!(detect(&format!("{PREFIX_76}AZDOabcde")).len(), 0);
}
#[test]
fn rejects_a_prefix_broken_by_an_out_of_alphabet_byte() {
let broken = format!("{} {}", &PREFIX_76[..10], &PREFIX_76[11..]);
assert_eq!(detect(&format!("{broken}AZDO{SUFFIX_4}")).len(), 0);
}
#[test]
fn rejects_an_anchor_too_close_to_the_start_of_input() {
assert_eq!(detect(&format!("AZDO{SUFFIX_4}")).len(), 0);
}
#[test]
fn rejects_a_bare_legacy_length_value_with_no_marker() {
let legacy = "a".repeat(52);
assert_eq!(detect(&legacy).len(), 0);
}
#[test]
fn rejects_the_literal_azdo_with_no_qualifying_prefix_or_suffix() {
assert_eq!(detect("our AZDO service tier covers this").len(), 0);
}
#[test]
fn finds_a_qualified_match_inside_surrounding_context() {
let input = format!("PAT={}", token());
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
let start = input.find(PREFIX_76).unwrap();
assert_eq!(
candidates[0].range(),
ByteRange::new(start, input.len()).unwrap()
);
}
#[test]
fn finds_deterministic_findings_across_repeated_calls() {
let input = token();
assert_eq!(detect(&input), detect(&input));
}
#[test]
fn stays_bounded_over_a_long_run_of_rejected_anchors() {
let input = format!("{}!{}", token(), "AZDO".repeat(10_000));
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].range(), ByteRange::new(0, 84).unwrap());
}
}