use crate::detectors::additional_providers::KnownFormatProviderDetector;
use crate::detectors::pattern::{PrefixShape, is_alnum, is_alnum_dash, is_lower_hex};
const USER_PREFIX: &str = "cfut_";
const ACCOUNT_PREFIX: &str = "cfat_";
const BODY_LEN: usize = 40;
const CHECKSUM_LEN: usize = 8;
const SUFFIX_LEN: usize = BODY_LEN + CHECKSUM_LEN;
const USER_SIGNALS: [&str; 2] = ["cloudflare-scannable-prefix", "checksum-shaped-suffix"];
const ACCOUNT_SIGNALS: [&str; 2] = [
"cloudflare-account-scannable-prefix",
"checksum-shaped-suffix",
];
fn checksum_tail_is_lower_hex(bytes: &[u8], _start: usize, end: usize) -> bool {
let checksum_start = end - CHECKSUM_LEN;
bytes[checksum_start..end].iter().copied().all(is_lower_hex)
}
pub(super) const CLOUDFLARE: KnownFormatProviderDetector = KnownFormatProviderDetector::new(
"cloudflare-token",
"cloudflare_api_token",
&[
PrefixShape::exact(USER_PREFIX, SUFFIX_LEN, is_alnum, &USER_SIGNALS)
.with_post_check(checksum_tail_is_lower_hex),
PrefixShape::exact(ACCOUNT_PREFIX, SUFFIX_LEN, is_alnum, &ACCOUNT_SIGNALS)
.with_post_check(checksum_tail_is_lower_hex),
],
is_alnum_dash,
);
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const BODY: &str = "SYNTHETICREVOKEDCLOUDFLAREAPITOKENVALUE1";
const CHECKSUM: &str = "deadbeef";
const _: () = assert!(BODY.len() == BODY_LEN);
const _: () = assert!(CHECKSUM.len() == CHECKSUM_LEN);
fn user_token() -> String {
format!("{USER_PREFIX}{BODY}{CHECKSUM}")
}
fn account_token() -> String {
format!("{ACCOUNT_PREFIX}{BODY}{CHECKSUM}")
}
fn detect(input: &str) -> Vec<Candidate> {
CLOUDFLARE
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
#[test]
fn detects_a_synthetic_credential_with_provider_specificity() {
for value in [user_token(), account_token()] {
let candidates = detect(&value);
assert_eq!(candidates.len(), 1, "{value}");
assert_eq!(candidates[0].type_name(), "cloudflare_api_token", "{value}");
assert_eq!(candidates[0].confidence(), Confidence::High, "{value}");
assert_eq!(
candidates[0].effective_specificity(),
Specificity::Provider,
"{value}"
);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, value.len()).unwrap(),
"{value}"
);
}
}
#[test]
fn detects_the_token_bare_in_env_and_control_contexts() {
for value in [user_token(), account_token()] {
for input in [
value.clone(),
format!("CLOUDFLARE_API_TOKEN={value}"),
format!("cloudflare_api_token: {value}"),
format!("{{\"token\": \"{value}\"}}"),
] {
let candidates = detect(&input);
assert_eq!(candidates.len(), 1, "{input}");
let (start, end) = (candidates[0].range().start(), candidates[0].range().end());
assert_eq!(&input[start..end], value, "{input}");
}
}
}
#[test]
fn accepts_punctuation_boundaries() {
for value in [user_token(), account_token()] {
let input = format!("({value}).");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1, "{value}");
assert_eq!(
candidates[0].range(),
ByteRange::new(1, value.len() + 1).unwrap(),
"{value}"
);
}
}
#[test]
fn rejects_a_body_one_byte_short_of_the_documented_length() {
let short_body = &BODY[..BODY_LEN - 1];
for prefix in [USER_PREFIX, ACCOUNT_PREFIX] {
assert!(
detect(&format!("{prefix}{short_body}{CHECKSUM}")).is_empty(),
"{prefix}"
);
}
}
#[test]
fn rejects_a_checksum_one_byte_short_of_the_documented_length() {
let short_checksum = &CHECKSUM[..CHECKSUM_LEN - 1];
for prefix in [USER_PREFIX, ACCOUNT_PREFIX] {
assert!(
detect(&format!("{prefix}{BODY}{short_checksum} ")).is_empty(),
"{prefix}"
);
}
}
#[test]
fn rejects_a_run_one_byte_longer_than_the_documented_length() {
for prefix in [USER_PREFIX, ACCOUNT_PREFIX] {
assert!(
detect(&format!("{prefix}{BODY}{CHECKSUM}a")).is_empty(),
"{prefix}"
);
}
}
#[test]
fn rejects_a_non_hex_checksum() {
for prefix in [USER_PREFIX, ACCOUNT_PREFIX] {
assert!(
detect(&format!("{prefix}{BODY}ghijklmn")).is_empty(),
"{prefix}"
);
}
}
#[test]
fn rejects_an_uppercase_hex_checksum() {
for prefix in [USER_PREFIX, ACCOUNT_PREFIX] {
assert!(
detect(&format!("{prefix}{BODY}DEADBEEF")).is_empty(),
"{prefix}"
);
}
}
#[test]
fn rejects_a_body_containing_underscore_or_dash() {
for prefix in [USER_PREFIX, ACCOUNT_PREFIX] {
for byte in ['_', '-'] {
let mut body = BODY.to_string();
body.replace_range(4..5, &byte.to_string());
assert!(
detect(&format!("{prefix}{body}{CHECKSUM}")).is_empty(),
"{prefix}"
);
}
}
}
#[test]
fn rejects_a_truncated_prefix_with_a_realistic_length_body() {
for prefix in ["cfu_", "cfa_"] {
assert!(
detect(&format!("{prefix}{BODY}{CHECKSUM}")).is_empty(),
"{prefix}"
);
}
}
#[test]
fn rejects_the_prefix_alone() {
for prefix in [USER_PREFIX, ACCOUNT_PREFIX] {
assert!(detect(prefix).is_empty(), "{prefix}");
assert!(detect(&format!("{prefix}{BODY}")).is_empty(), "{prefix}");
}
}
#[test]
fn rejects_the_body_or_checksum_embedded_in_a_wider_identifier() {
for value in [user_token(), account_token()] {
assert!(detect(&format!("x{value}")).is_empty(), "{value}");
assert!(detect(&format!("{value}x")).is_empty(), "{value}");
}
}
#[test]
fn rejects_every_shape_embedded_in_a_wider_identifier_leading_trailing_or_dash_joined() {
for value in [user_token(), account_token()] {
assert!(detect(&format!("legacy{value}")).is_empty(), "{value}");
assert!(detect(&format!("{value}_backup")).is_empty(), "{value}");
assert!(detect(&format!("{value}-1")).is_empty(), "{value}");
}
}
#[test]
fn rejects_a_masked_value() {
for prefix in [USER_PREFIX, ACCOUNT_PREFIX] {
assert!(
detect(&format!("{prefix}{}", "*".repeat(BODY_LEN + CHECKSUM_LEN))).is_empty(),
"{prefix}"
);
}
}
#[test]
fn rejects_an_environment_variable_reference() {
assert!(detect("${CLOUDFLARE_API_TOKEN}").is_empty());
}
#[test]
fn rejects_an_ordinary_zone_resource_id() {
assert!(detect("023e105f4ecef8ad9ca31a8372d0c353").is_empty());
}
#[test]
fn finds_a_match_across_crlf_and_a_unicode_prefix() {
for value in [user_token(), account_token()] {
let input = format!("# \u{1F511} caf\u{e9}\r\n{value}\r\n");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1, "{value}");
let start = input.find(&value).unwrap();
assert_eq!(
candidates[0].range(),
ByteRange::new(start, start + value.len()).unwrap(),
"{value}"
);
}
}
#[test]
fn reports_a_repeated_identical_value_once_per_occurrence() {
for value in [user_token(), account_token()] {
let input = format!("{value} {value}");
assert_eq!(detect(&input).len(), 2, "{value}");
}
}
#[test]
fn finds_deterministic_findings_across_repeated_calls() {
for value in [user_token(), account_token()] {
assert_eq!(detect(&value), detect(&value), "{value}");
}
}
#[test]
fn detects_both_prefixes_in_the_same_input_without_one_suppressing_another() {
let user = user_token();
let account = account_token();
let input = format!("{user}\n{account}\n");
let candidates = detect(&input);
assert_eq!(candidates.len(), 2);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, user.len()).unwrap()
);
let account_start = user.len() + 1;
assert_eq!(
candidates[1].range(),
ByteRange::new(account_start, account_start + account.len()).unwrap()
);
}
#[test]
fn stays_bounded_over_a_long_run_of_near_miss_prefixes() {
let short_body = &BODY[..BODY_LEN - 1];
let input = format!("{USER_PREFIX}{short_body}{CHECKSUM} ").repeat(10_000);
assert_eq!(detect(&input).len(), 0);
}
}