use crate::detectors::pattern::{self, RunLength};
use crate::detectors::prefilter::Literals;
use crate::error::DetectorFailure;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const PREFIXES: [(&str, &str); 3] = [
("sk-ant-api03-", "anthropic_api_key"),
("sk-ant-api01-", "anthropic_enterprise_api_key"),
("sk-ant-admin01-", "anthropic_admin_api_key"),
];
pub(super) struct AnthropicTokenDetector;
impl Detector for AnthropicTokenDetector {
fn id(&self) -> &'static str {
"anthropic-token"
}
fn detect(
&self,
input: &str,
_context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure> {
let mut candidates = Vec::new();
let prefixes: Vec<&str> = PREFIXES.iter().map(|(prefix, _)| *prefix).collect();
let ranges = pattern::scan_prefixed_runs(
input,
&prefixes,
RunLength::AtLeast(20),
pattern::is_alnum_dash,
pattern::is_alnum_dash,
);
let bytes = input.as_bytes();
for (start, end) in ranges {
let Some(range) = ByteRange::new(start, end) else {
continue;
};
for (prefix, type_name) in PREFIXES {
if bytes[start..].starts_with(prefix.as_bytes()) {
candidates.push(
Candidate::built_in(type_name, Confidence::High, range)
.with_specificity(Specificity::Provider)
.with_signal_pack(crate::types::signal_pack!(
"anthropic-versioned-prefix",
"opaque-suffix"
)),
);
break;
}
}
}
Ok(candidates)
}
}
pub(super) const REQUIRED_LITERALS: &[Literals] = &[Literals::Prefixes(&PREFIXES)];
#[cfg(test)]
mod tests {
use super::*;
fn detect(input: &str) -> Vec<Candidate> {
AnthropicTokenDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
#[test]
fn detects_the_synthetic_fixture() {
let input = "sk-ant-api03-SYNTHETIC_REVOKED_ANTHROPIC_KEY";
let candidates = detect(input);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "anthropic_api_key");
assert_eq!(
candidates[0].range(),
ByteRange::new(0, input.len()).unwrap()
);
}
#[test]
fn rejects_an_unsupported_version_prefix() {
assert_eq!(
detect("sk-ant-api02-SYNTHETIC_REVOKED_ANTHROPIC_KEY").len(),
0
);
}
#[test]
fn rejects_a_short_suffix() {
assert_eq!(detect("sk-ant-api03-short").len(), 0);
}
#[test]
fn accepts_a_doc_style_placeholder_built_from_valid_alphabet_characters() {
let input = format!("sk-ant-api03-{}", "x".repeat(20));
assert_eq!(detect(&input).len(), 1);
}
#[test]
fn rejects_a_prefix_embedded_in_a_wider_benign_identifier() {
let input = format!("mysk-ant-api03-{}", "SYNTHETIC_REVOKED_ANTHROPIC_KEY");
assert_eq!(detect(&input).len(), 0);
}
#[test]
fn a_trailing_comma_does_not_get_folded_into_or_suppress_the_match() {
let token = format!("sk-ant-api03-{}", "SYNTHETIC_REVOKED_KEY_VALUE");
let input = format!("Rotate {token}, then redeploy.");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
let start = "Rotate ".len();
let end = start + token.len();
assert_eq!(candidates[0].range(), ByteRange::new(start, end).unwrap());
}
#[test]
fn an_unversioned_prefix_is_a_documented_false_negative() {
let input = "sk-ant-SYNTHETIC_REVOKED_LEGACY_ANTHROPIC_KEY_VALUE";
assert_eq!(detect(input).len(), 0);
}
#[test]
fn rejects_a_future_version_marker_not_yet_documented() {
let input = format!("sk-ant-api04-{}", "SYNTHETIC_REVOKED_ANTHROPIC_KEY");
assert_eq!(detect(&input).len(), 0);
}
#[test]
fn rejects_a_near_prefix_variant_using_underscore_instead_of_dash() {
let input = format!("sk_ant_api03_{}", "SYNTHETIC_REVOKED_ANTHROPIC_KEY");
assert_eq!(detect(&input).len(), 0);
}
#[test]
fn reports_each_occurrence_of_a_repeated_value_independently() {
let token = format!("sk-ant-api03-{}", "SYNTHETIC_REVOKED_KEY_VALUE");
let input = format!("{token} {token}");
let candidates = detect(&input);
assert_eq!(candidates.len(), 2);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, token.len()).unwrap()
);
let second_start = token.len() + 1;
assert_eq!(
candidates[1].range(),
ByteRange::new(second_start, second_start + token.len()).unwrap()
);
}
fn synthetic_body() -> String {
const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
let mut state: u32 = 0x5EED_0862;
let mut body = String::new();
for _ in 0..93 {
state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
body.push(ALPHABET[(state >> 24) as usize % ALPHABET.len()] as char);
}
body.push_str("AA");
body
}
const NEW_PREFIXES: [(&str, &str); 2] = [
("sk-ant-api01-", "anthropic_enterprise_api_key"),
("sk-ant-admin01-", "anthropic_admin_api_key"),
];
fn assert_single_exact_typed(input: &str, token: &str, expected_type: &str) {
let candidates = detect(input);
assert_eq!(candidates.len(), 1, "input: {input}");
assert_eq!(candidates[0].type_name(), expected_type);
let start = input.find(token).unwrap();
assert_eq!(
candidates[0].range(),
ByteRange::new(start, start + token.len()).unwrap()
);
}
fn assert_single_exact(input: &str, token: &str) {
assert_single_exact_typed(input, token, "anthropic_api_key");
}
#[test]
fn detects_each_new_prefix_in_every_context_with_exact_spans() {
for (prefix, expected_type) in NEW_PREFIXES {
let token = format!("{prefix}{}", synthetic_body());
let contexts = [
token.clone(),
format!("ANTHROPIC_ADMIN_KEY={token}\n"),
format!("export ANTHROPIC_COMPLIANCE_ACCESS_KEY=\"{token}\"\n"),
format!("anthropic_admin_key: {token}\n"),
format!("{{\"admin_api_key\": \"{token}\"}}"),
format!("client = Anthropic(api_key=\"{token}\")"),
format!("const c = new Anthropic({{ apiKey: '{token}' }});"),
format!("2026-09-27T00:00:00Z INFO org sync key={token} status=200"),
format!(
"{{\"tool\":\"bash\",\"input\":{{\"command\":\"curl -H 'x-api-key: {token}' https://example.invalid\"}}}}"
),
format!("Rotate {token}, then redeploy."),
format!("({token})"),
];
for input in &contexts {
assert_single_exact_typed(input, &token, expected_type);
}
}
}
#[test]
fn new_prefixes_accept_the_same_twenty_byte_floor_as_api03() {
for (prefix, _) in NEW_PREFIXES {
assert_eq!(detect(&format!("{prefix}{}", "A".repeat(20))).len(), 1);
assert_eq!(detect(&format!("{prefix}{}", "A".repeat(19))).len(), 0);
}
}
#[test]
fn new_prefixes_reject_a_short_body_placeholders_and_prose() {
for (prefix, _) in NEW_PREFIXES {
assert_eq!(detect(&format!("{prefix}short")).len(), 0);
assert_eq!(detect(&format!("{prefix}...")).len(), 0);
assert_eq!(detect(&format!("{prefix}<YOUR_KEY>")).len(), 0);
assert_eq!(detect(&format!("the {prefix} prefix")).len(), 0);
}
}
#[test]
fn new_prefixes_reject_wrong_prefix_twins() {
let body = synthetic_body();
for prefix in [
"sk-ant-admin02-",
"sk-ant-admin-",
"sk-ant-api02-",
"sk-ant-oat01-",
"sk-ant-ort01-",
"sk_ant_admin01_",
"sk_ant_api01_",
"SK-ANT-ADMIN01-",
"SK-ANT-API01-",
] {
assert_eq!(detect(&format!("{prefix}{body}")).len(), 0, "{prefix}");
}
}
#[test]
fn new_prefixes_reject_embedding_in_a_longer_token() {
let body = synthetic_body();
for (prefix, _) in NEW_PREFIXES {
assert_eq!(detect(&format!("my{prefix}{body}")).len(), 0);
assert_eq!(detect(&format!("x-{prefix}{body}")).len(), 0);
}
}
#[test]
fn a_trailing_alphabet_run_stays_part_of_the_span_and_a_delimiter_ends_it() {
for (prefix, expected_type) in NEW_PREFIXES {
let token = format!("{prefix}{}", synthetic_body());
let longer = format!("{token}extra");
assert_single_exact_typed(&longer, &longer, expected_type);
let delimited = format!("{token}.tail");
assert_single_exact_typed(&delimited, &token, expected_type);
}
}
#[test]
fn a_body_with_a_character_outside_the_alphabet_ends_the_span_there() {
for (prefix, expected_type) in NEW_PREFIXES {
let head = "A".repeat(25);
let input = format!("{prefix}{head}!{}", "B".repeat(25));
assert_single_exact_typed(&input, &format!("{prefix}{head}"), expected_type);
}
}
#[test]
fn the_three_prefixes_are_reported_independently_in_one_input() {
let body = synthetic_body();
let a = format!("sk-ant-api03-{body}");
let b = format!("sk-ant-api01-{body}");
let c = format!("sk-ant-admin01-{body}");
let input = format!("{a} {b} {c}");
let candidates = detect(&input);
assert_eq!(candidates.len(), 3);
let mut start = 0;
let expected_types = [
"anthropic_api_key",
"anthropic_enterprise_api_key",
"anthropic_admin_api_key",
];
for ((candidate, token), expected_type) in
candidates.iter().zip([&a, &b, &c]).zip(expected_types)
{
assert_eq!(candidate.type_name(), expected_type);
assert_eq!(
candidate.range(),
ByteRange::new(start, start + token.len()).unwrap()
);
start += token.len() + 1;
}
}
#[test]
fn api03_behavior_is_unchanged_for_the_full_scanner_shape() {
let token = format!("sk-ant-api03-{}", synthetic_body());
assert_single_exact(&token, &token);
assert_eq!(detect(&format!("sk-ant-api03-{}", "A".repeat(19))).len(), 0);
assert_eq!(detect(&format!("sk-ant-api03-{}", "A".repeat(20))).len(), 1);
}
}