use crate::detectors::additional_providers::KnownFormatProviderDetector;
use crate::detectors::pattern::{self, PrefixShape};
const ID_LEN: usize = 26;
const SECRET_LEN: usize = 22;
const SUFFIX_LEN: usize = 3;
const BODY_LEN: usize = ID_LEN + 1 + SECRET_LEN + 1 + SUFFIX_LEN;
const SIGNALS: [&str; 1] = ["paddle-documented-regex"];
fn has_paddle_layout(bytes: &[u8], _start: usize, end: usize) -> bool {
let body = &bytes[end - BODY_LEN..end];
let (id, rest) = body.split_at(ID_LEN);
let (secret, suffix) = rest.split_at(1 + SECRET_LEN);
id.iter().copied().all(pattern::is_lower_alnum)
&& secret[0] == b'_'
&& secret[1..].iter().all(u8::is_ascii_alphanumeric)
&& suffix[0] == b'_'
&& suffix[1..].iter().all(u8::is_ascii_alphanumeric)
}
pub(super) const PADDLE_API_KEY: KnownFormatProviderDetector = KnownFormatProviderDetector::new(
"paddle-api-key",
"paddle_api_key",
&[
PrefixShape::exact(
"pdl_live_apikey_",
BODY_LEN,
pattern::is_alnum_underscore,
&SIGNALS,
)
.with_post_check(has_paddle_layout),
PrefixShape::exact(
"pdl_sdbx_apikey_",
BODY_LEN,
pattern::is_alnum_underscore,
&SIGNALS,
)
.with_post_check(has_paddle_layout),
],
pattern::is_alnum_dash,
);
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const ALNUM: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const LOWER_ALNUM: &[u8] = b"abcdefghijklmnopqrstuvwxyz0123456789";
fn filler(alphabet: &[u8], len: usize, seed: usize) -> String {
(0..len)
.map(|i| char::from(alphabet[(i * 7 + seed * 13 + i / 5) % alphabet.len()]))
.collect()
}
fn key_with(env: &str, id: usize, secret: usize, suffix: usize) -> String {
format!(
"pdl_{env}_apikey_{}_{}_{}",
filler(LOWER_ALNUM, id, 1),
filler(ALNUM, secret, 2),
filler(ALNUM, suffix, 3)
)
}
fn key(env: &str) -> String {
key_with(env, ID_LEN, SECRET_LEN, SUFFIX_LEN)
}
fn detect(input: &str) -> Vec<Candidate> {
PADDLE_API_KEY
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn assert_single(input: &str, key: &str) {
let candidates = detect(input);
assert_eq!(candidates.len(), 1, "{input}");
let start = input.find(key).unwrap();
assert_eq!(candidates[0].type_name(), "paddle_api_key");
assert_eq!(candidates[0].confidence(), Confidence::High);
assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
assert_eq!(
candidates[0].range(),
ByteRange::new(start, start + key.len()).unwrap(),
"{input}"
);
}
fn contexts(key: &str) -> Vec<String> {
vec![
key.to_owned(),
format!("PADDLE_API_KEY={key}"),
format!("export PADDLE_API_KEY=\"{key}\""),
format!("Authorization: Bearer {key}"),
format!("{{\"token\": \"{key}\"}}"),
format!("const paddle = new Paddle('{key}');"),
format!("Here is my key {key} can you debug this?"),
format!("The key is {key}."),
]
}
#[test]
fn live_and_sandbox_keys_are_detected_in_every_context() {
for env in ["live", "sdbx"] {
let key = key(env);
assert_eq!(key.len(), 69);
assert_eq!(key.matches('_').count(), 5);
for input in contexts(&key) {
assert_single(&input, &key);
}
}
assert_eq!(PADDLE_API_KEY.id(), "paddle-api-key");
}
#[test]
fn near_miss_twins_are_rejected() {
let good = key("sdbx");
let mut upper_in_id = good.clone();
upper_in_id.replace_range(20..21, "Q");
for input in [
key_with("sdbx", ID_LEN - 1, SECRET_LEN, SUFFIX_LEN),
key_with("sdbx", ID_LEN + 1, SECRET_LEN, SUFFIX_LEN),
key_with("sdbx", ID_LEN, SECRET_LEN - 1, SUFFIX_LEN),
key_with("sdbx", ID_LEN, SECRET_LEN + 1, SUFFIX_LEN),
key_with("sdbx", ID_LEN, SECRET_LEN, SUFFIX_LEN - 1),
key_with("sdbx", ID_LEN, SECRET_LEN, SUFFIX_LEN + 1),
key("test"),
good.replacen("apikey_", "", 1),
good.replacen("pdl_", "PDL_", 1),
format!("{}-{}", &good[..42], &good[43..]),
upper_in_id,
format!("x{good}"),
format!("_{good}"),
format!("{good}x"),
format!("{good}_x"),
] {
assert!(detect(&input).is_empty(), "{input}");
}
}
#[test]
fn benign_paddle_text_is_not_claimed() {
for input in [
format!("{{\"id\": \"apikey_{}\"}}", filler(LOWER_ALNUM, ID_LEN, 1)),
filler(LOWER_ALNUM, 50, 4),
"PADDLE_API_KEY=${PADDLE_API_KEY}".to_owned(),
r"^pdl_(live|sdbx)_apikey_[a-z\d]{26}_[a-zA-Z\d]{22}_[a-zA-Z\d]{3}$".to_owned(),
"PADDLE_API_KEY=pdl_sdbx_apikey_...".to_owned(),
] {
assert!(detect(&input).is_empty(), "{input}");
}
}
#[test]
fn repeated_keys_are_reported_once_each_and_a_glued_run_never() {
let key = key("live");
assert_eq!(detect(&format!("{key} {key}")).len(), 2);
assert!(detect(&key.repeat(100)).is_empty());
assert!(detect(&"pdl_live_apikey_".repeat(2_000)).is_empty());
}
}