use crate::detectors::additional_providers::KnownFormatProviderDetector;
use crate::detectors::pattern::{self, PrefixShape};
const REPLICATE_BODY_LEN: usize = 37;
const GROQ_BODY_LEN: usize = 52;
const XAI_BODY_LEN: usize = 80;
const OPENROUTER_BODY_LEN: usize = 64;
const PERPLEXITY_BODY_LEN: usize = 48;
const FIREWORKS_BODY_LENS: &[usize] = &[22, 24];
fn is_token_char(byte: u8) -> bool {
pattern::is_alnum_dash(byte) || byte == b'_'
}
const REPLICATE_SIGNALS: [&str; 2] = ["replicate-documented-prefix", "exact-length-suffix"];
const GROQ_SIGNALS: [&str; 2] = ["groq-tool-corroborated-prefix", "exact-length-suffix"];
const XAI_SIGNALS: [&str; 2] = ["xai-documented-prefix", "exact-length-suffix"];
const OPENROUTER_SIGNALS: [&str; 2] = ["openrouter-documented-prefix", "lowercase-hex-suffix"];
const PERPLEXITY_SIGNALS: [&str; 2] = ["perplexity-documented-prefix", "exact-length-suffix"];
const FIREWORKS_SIGNALS: [&str; 2] = ["fireworks-documented-prefix", "observed-length-suffix"];
pub(super) const REPLICATE: KnownFormatProviderDetector = KnownFormatProviderDetector::new(
"replicate-api-token",
"replicate_api_token",
&[PrefixShape::exact(
"r8_",
REPLICATE_BODY_LEN,
is_token_char,
&REPLICATE_SIGNALS,
)],
is_token_char,
);
pub(super) const GROQ: KnownFormatProviderDetector = KnownFormatProviderDetector::new(
"groq-api-key",
"groq_api_key",
&[PrefixShape::exact(
"gsk_",
GROQ_BODY_LEN,
pattern::is_alnum,
&GROQ_SIGNALS,
)],
is_token_char,
);
pub(super) const XAI: KnownFormatProviderDetector = KnownFormatProviderDetector::new(
"xai-api-key",
"xai_api_key",
&[PrefixShape::exact(
"xai-",
XAI_BODY_LEN,
is_token_char,
&XAI_SIGNALS,
)],
is_token_char,
);
pub(super) const OPENROUTER: KnownFormatProviderDetector = KnownFormatProviderDetector::new(
"openrouter-api-key",
"openrouter_api_key",
&[PrefixShape::exact(
"sk-or-v1-",
OPENROUTER_BODY_LEN,
pattern::is_lower_hex,
&OPENROUTER_SIGNALS,
)],
is_token_char,
);
pub(super) const PERPLEXITY: KnownFormatProviderDetector = KnownFormatProviderDetector::new(
"perplexity-api-key",
"perplexity_api_key",
&[PrefixShape::exact(
"pplx-",
PERPLEXITY_BODY_LEN,
pattern::is_alnum,
&PERPLEXITY_SIGNALS,
)],
is_token_char,
);
pub(super) const FIREWORKS: KnownFormatProviderDetector = KnownFormatProviderDetector::new(
"fireworks-ai-api-key",
"fireworks_ai_api_key",
&[PrefixShape::one_of(
"fw_",
FIREWORKS_BODY_LENS,
pattern::is_alnum,
&FIREWORKS_SIGNALS,
)],
is_token_char,
);
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const REPLICATE_BODY: &str = "SYNTHETIC_REVOKED-REPLICATE-TOKEN-001";
const _: () = assert!(REPLICATE_BODY.len() == REPLICATE_BODY_LEN);
const GROQ_BODY: &str = "SYNTHETICREVOKEDGROQAPIKEYVALUE000000000000000000001";
const _: () = assert!(GROQ_BODY.len() == GROQ_BODY_LEN);
const XAI_BODY: &str =
"SYNTHETIC_REVOKED-XAI-API-KEY-VALUE-00000000000000000000000000000000000000000001";
const _: () = assert!(XAI_BODY.len() == XAI_BODY_LEN);
const OPENROUTER_BODY: &str =
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
const _: () = assert!(OPENROUTER_BODY.len() == OPENROUTER_BODY_LEN);
fn detect(detector: &KnownFormatProviderDetector, input: &str) -> Vec<Candidate> {
detector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
struct Family {
detector: &'static KnownFormatProviderDetector,
id: &'static str,
type_name: &'static str,
prefix: &'static str,
body: &'static str,
}
const FAMILIES: [Family; 4] = [
Family {
detector: &REPLICATE,
id: "replicate-api-token",
type_name: "replicate_api_token",
prefix: "r8_",
body: REPLICATE_BODY,
},
Family {
detector: &GROQ,
id: "groq-api-key",
type_name: "groq_api_key",
prefix: "gsk_",
body: GROQ_BODY,
},
Family {
detector: &XAI,
id: "xai-api-key",
type_name: "xai_api_key",
prefix: "xai-",
body: XAI_BODY,
},
Family {
detector: &OPENROUTER,
id: "openrouter-api-key",
type_name: "openrouter_api_key",
prefix: "sk-or-v1-",
body: OPENROUTER_BODY,
},
];
#[test]
fn every_family_detects_the_exact_frozen_shape_at_provider_specificity() {
for family in &FAMILIES {
let token = format!("{}{}", family.prefix, family.body);
let candidates = detect(family.detector, &token);
assert_eq!(candidates.len(), 1, "{}", family.id);
assert_eq!(family.detector.id(), family.id);
assert_eq!(candidates[0].type_name(), family.type_name);
assert_eq!(candidates[0].confidence(), Confidence::High);
assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, token.len()).unwrap()
);
}
}
#[test]
fn the_range_covers_exactly_the_credential_inside_surrounding_text() {
for family in &FAMILIES {
let token = format!("{}{}", family.prefix, family.body);
let input = format!("KEY=\"{token}\", next");
let candidates = detect(family.detector, &input);
assert_eq!(candidates.len(), 1, "{}", family.id);
assert_eq!(
candidates[0].range(),
ByteRange::new(5, 5 + token.len()).unwrap(),
"{}",
family.id
);
}
}
#[test]
fn a_body_one_byte_short_or_long_is_an_intentional_false_negative() {
for family in &FAMILIES {
let short = format!("{}{}", family.prefix, &family.body[..family.body.len() - 1]);
let long = format!("{}{}0", family.prefix, family.body);
assert!(detect(family.detector, &short).is_empty(), "{short}");
assert!(detect(family.detector, &long).is_empty(), "{long}");
}
}
#[test]
fn a_glued_identifier_boundary_rejects_an_embedded_credential() {
for family in &FAMILIES {
let token = format!("{}{}", family.prefix, family.body);
for input in [
format!("x{token}"),
format!("-{token}"),
format!("_{token}"),
format!("{token}_backup"),
format!("{token}-1"),
] {
assert!(detect(family.detector, &input).is_empty(), "{input}");
}
}
}
#[test]
fn near_miss_prefixes_are_rejected() {
for family in &FAMILIES {
let prefix = family.prefix;
let swapped: String = prefix
.chars()
.map(|c| match c {
'_' => '-',
'-' => '_',
other => other,
})
.collect();
for input in [
format!("{}{}", prefix.to_uppercase(), family.body),
format!("{swapped}{}", family.body),
format!("{}{}", &prefix[1..], family.body),
] {
assert!(detect(family.detector, &input).is_empty(), "{input}");
}
}
}
#[test]
fn replicate_and_xai_admit_dash_and_underscore_inside_the_body() {
for family in [&FAMILIES[0], &FAMILIES[2]] {
let mut body = family.body.to_owned();
body.replace_range(4..6, "-_");
let token = format!("{}{body}", family.prefix);
assert_eq!(detect(family.detector, &token).len(), 1, "{token}");
}
}
#[test]
fn groq_rejects_dash_and_underscore_inside_the_body() {
for byte in ["-", "_", "+", "/", "."] {
let mut body = GROQ_BODY.to_owned();
body.replace_range(4..5, byte);
let token = format!("gsk_{body}");
assert!(detect(&GROQ, &token).is_empty(), "{token}");
}
}
#[test]
fn openrouter_requires_lowercase_hex() {
let upper = format!("sk-or-v1-{}", OPENROUTER_BODY.to_uppercase());
let mut non_hex = OPENROUTER_BODY.to_owned();
non_hex.replace_range(10..11, "g");
let non_hex = format!("sk-or-v1-{non_hex}");
for input in [upper, non_hex] {
assert!(detect(&OPENROUTER, &input).is_empty(), "{input}");
}
}
#[test]
fn openrouter_management_keys_stay_unclaimed() {
let input = format!("sk-or-mgmt-{OPENROUTER_BODY}");
assert!(detect(&OPENROUTER, &input).is_empty());
}
#[test]
fn public_lookalikes_and_placeholders_are_not_claimed() {
for input in [
"docker pull r8.im/owner/model@sha256:0123456789abcdef",
"model: xai-grok-4-fast-reasoning",
"model = \"gsk_model_name\"",
"GROQ_API_KEY=gsk_********************************",
"OPENROUTER_API_KEY=sk-or-v1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"REPLICATE_API_TOKEN=r8_your_token_here",
"XAI_API_KEY=xai-your-api-key-here",
] {
for family in &FAMILIES {
assert!(detect(family.detector, input).is_empty(), "{input}");
}
}
}
#[test]
fn a_repeated_value_is_reported_once_per_occurrence() {
for family in &FAMILIES {
let token = format!("{}{}", family.prefix, family.body);
let input = format!("{token} {token}");
let candidates = detect(family.detector, &input);
assert_eq!(candidates.len(), 2, "{}", family.id);
let second = token.len() + 1;
assert_eq!(
candidates[1].range(),
ByteRange::new(second, second + token.len()).unwrap()
);
}
}
#[test]
fn the_families_do_not_claim_each_others_values() {
for owner in &FAMILIES {
let token = format!("{}{}", owner.prefix, owner.body);
for other in &FAMILIES {
if other.id != owner.id {
assert!(detect(other.detector, &token).is_empty(), "{}", other.id);
}
}
}
}
const PERPLEXITY_BODY: &str = "SyntheticRevokedPerplexityKeyValue00000000000001";
const _: () = assert!(PERPLEXITY_BODY.len() == PERPLEXITY_BODY_LEN);
const FIREWORKS_BODY_22: &str = "SyntheticRevokedFwKey1";
const FIREWORKS_BODY_24: &str = "SyntheticRevokedFwKey123";
const _: () = assert!(FIREWORKS_BODY_22.len() == 22);
const _: () = assert!(FIREWORKS_BODY_24.len() == 24);
#[test]
fn perplexity_detects_the_exact_frozen_shape_and_rejects_neighbouring_widths() {
let token = format!("pplx-{PERPLEXITY_BODY}");
let candidates = detect(&PERPLEXITY, &token);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "perplexity_api_key");
assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, token.len()).unwrap()
);
for input in [
format!("pplx-{}", &PERPLEXITY_BODY[1..]),
format!("pplx-{PERPLEXITY_BODY}0"),
format!("pplx-{}-{}", &PERPLEXITY_BODY[..20], &PERPLEXITY_BODY[21..]),
format!("PPLX-{PERPLEXITY_BODY}"),
format!("x{token}"),
format!("{token}_backup"),
] {
assert!(detect(&PERPLEXITY, &input).is_empty(), "{input}");
}
}
#[test]
fn perplexity_leaves_public_names_and_placeholders_clean() {
for input in [
"model: pplx-embed-v1-0.6b",
"npm install pplx-sdk-node",
"PERPLEXITY_API_KEY=pplx-********************",
"PERPLEXITY_API_KEY=pplx-your-api-key-here",
] {
assert!(detect(&PERPLEXITY, input).is_empty(), "{input}");
}
}
#[test]
fn fireworks_detects_both_observed_widths_and_no_other() {
for body in [FIREWORKS_BODY_22, FIREWORKS_BODY_24] {
let token = format!("fw_{body}");
let candidates = detect(&FIREWORKS, &token);
assert_eq!(candidates.len(), 1, "{token}");
assert_eq!(candidates[0].type_name(), "fireworks_ai_api_key");
assert_eq!(
candidates[0].range(),
ByteRange::new(0, token.len()).unwrap()
);
}
for input in [
format!("fw_{}", &FIREWORKS_BODY_22[1..]),
format!("fw_{FIREWORKS_BODY_22}0"),
format!("fw_{FIREWORKS_BODY_24}0"),
format!("fw_{}-{}", &FIREWORKS_BODY_22[..5], &FIREWORKS_BODY_22[6..]),
format!("xfw_{FIREWORKS_BODY_22}"),
format!("fw_{FIREWORKS_BODY_22}_backup"),
] {
assert!(detect(&FIREWORKS, &input).is_empty(), "{input}");
}
}
#[test]
fn fireworks_leaves_identifiers_keychain_references_and_fpk_keys_clean() {
for input in [
"fw_id: 4f2a",
"fw_version = 12",
"fw_spec_name",
"keychain: fw_api_key_reference",
"FIREWORKS_API_KEY=fw_********************",
"FIREWORKS_API_KEY=fpk_SyntheticRevokedFwKey1",
] {
assert!(detect(&FIREWORKS, input).is_empty(), "{input}");
}
}
}