use crate::detectors::pattern::{self, PrefixShape};
use crate::error::DetectorFailure;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
pub(super) const TYPED_BODY_LEN: usize = 56;
const FLOOR: usize = 20;
const PREFIX_LEN: usize = 4;
const TYPED_SIGNALS: [&str; 2] = ["vercel-documented-prefix", "corroborated-exact-length"];
const AGGREGATE_SIGNALS: [&str; 2] = ["vercel-documented-prefix", "opaque-suffix"];
const AGGREGATE_TYPE: &str = "vercel_token";
const TYPED: [(&str, &str); 3] = [
("vcp_", "vercel_personal_access_token"),
("vca_", "vercel_app_access_token"),
("vcr_", "vercel_app_refresh_token"),
];
const SHAPES: [PrefixShape<'static>; 5] = [
PrefixShape::at_least("vcp_", FLOOR, pattern::is_alnum_dash, &AGGREGATE_SIGNALS),
PrefixShape::at_least("vci_", FLOOR, pattern::is_alnum_dash, &AGGREGATE_SIGNALS),
PrefixShape::at_least("vca_", FLOOR, pattern::is_alnum_dash, &AGGREGATE_SIGNALS),
PrefixShape::at_least("vcr_", FLOOR, pattern::is_alnum_dash, &AGGREGATE_SIGNALS),
PrefixShape::at_least("vck_", FLOOR, pattern::is_alnum_dash, &AGGREGATE_SIGNALS),
];
pub(super) struct VercelDetector;
pub(super) const VERCEL: VercelDetector = VercelDetector;
#[allow(clippy::unused_self)]
impl VercelDetector {
pub(super) const fn detector_id(&self) -> &'static str {
"vercel-token"
}
pub(super) const fn shapes(&self) -> &'static [PrefixShape<'static>] {
&SHAPES
}
}
fn typed_class(value: &[u8]) -> Option<&'static str> {
let (prefix, type_name) = TYPED
.iter()
.find(|(prefix, _)| value.starts_with(prefix.as_bytes()))?;
let body = &value[prefix.len()..];
(body.len() == TYPED_BODY_LEN && body.iter().copied().all(pattern::is_alnum))
.then_some(*type_name)
}
impl Detector for VercelDetector {
fn id(&self) -> &str {
self.detector_id()
}
fn detect(
&self,
input: &str,
_context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure> {
let bytes = input.as_bytes();
let mut candidates = Vec::new();
for (start, end, signals) in
pattern::scan_prefixed_shapes(input, &SHAPES, pattern::is_alnum_dash)
{
if super::text::is_repeated_character_filler(&input[start + PREFIX_LEN..end]) {
continue;
}
let Some(range) = ByteRange::new(start, end) else {
continue;
};
let candidate = match typed_class(&bytes[start..end]) {
Some(type_name) => Candidate::built_in(type_name, Confidence::High, range)
.with_signals(TYPED_SIGNALS.iter().copied()),
None => Candidate::built_in(AGGREGATE_TYPE, Confidence::High, range)
.with_signals(signals.iter().copied()),
};
candidates.push(candidate.with_specificity(Specificity::Provider));
}
Ok(candidates)
}
}
#[cfg(test)]
mod tests {
use super::*;
const INTERIM: [&str; 2] = ["vci_", "vck_"];
fn body(len: usize) -> String {
const STEM: &[u8] = b"SyntheticRevokedVercelFixture";
(0..len)
.map(|i| char::from(*STEM.get(i).unwrap_or(&b'0')))
.collect()
}
fn detect(input: &str) -> Vec<Candidate> {
VERCEL
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn contexts(token: &str) -> Vec<String> {
vec![
token.to_owned(),
format!("VERCEL_TOKEN={token}"),
format!("export VERCEL_TOKEN=\"{token}\""),
format!("vercel deploy --token {token}"),
format!("curl -H \"Authorization: Bearer {token}\" https://api.vercel.com/v2/user"),
format!("{{\"token\": \"{token}\"}}"),
format!("{{\"access_token\": \"{token}\", \"token_type\": \"Bearer\"}}"),
format!("vercel:\n token: {token}\n"),
format!("client = Vercel(bearer_token=\"{token}\")"),
format!("Here is my token {token} can you debug this?"),
format!("Rotate ({token}), then redeploy."),
format!("# \u{1f511} caf\u{e9}\r\n{token}\r\n"),
]
}
fn assert_sole(input: &str, token: &str, type_name: &str) {
let candidates = detect(input);
assert_eq!(candidates.len(), 1, "{input}");
let start = input.find(token).unwrap();
assert_eq!(candidates[0].type_name(), type_name, "{input}");
assert_eq!(candidates[0].confidence(), Confidence::High);
assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
assert_eq!(
candidates[0].range(),
ByteRange::new(start, start + token.len()).unwrap(),
"{input}"
);
}
#[test]
fn typed_classes_are_detected_at_their_exact_span_in_every_context() {
for (prefix, type_name) in TYPED {
let token = format!("{prefix}{}", body(TYPED_BODY_LEN));
assert_eq!(token.len(), 60);
for input in contexts(&token) {
assert_sole(&input, &token, type_name);
}
}
assert_eq!(VERCEL.id(), "vercel-token");
}
#[test]
fn interim_prefixes_keep_the_aggregate_type_and_the_pre_split_shape() {
for prefix in INTERIM {
for len in [FLOOR, 29, TYPED_BODY_LEN, 200] {
let token = format!("{prefix}{}", body(len));
for input in contexts(&token) {
assert_sole(&input, &token, "vercel_token");
}
}
let token = format!("{prefix}SYNTHETIC_REVOKED-KEY_VALUE");
assert_sole(&token, &token, "vercel_token");
assert!(detect(&format!("{prefix}{}", body(FLOOR - 1))).is_empty());
assert!(detect(&format!("{prefix}SYNTH!ETIC_REVOKED_SYNTHETIC")).is_empty());
}
}
#[test]
fn off_contract_typed_values_fall_back_to_vercel_token_whole() {
let valid = body(TYPED_BODY_LEN);
for (prefix, _) in TYPED {
let mut values = vec![
format!("{prefix}{}", body(TYPED_BODY_LEN - 1)),
format!("{prefix}{}", body(TYPED_BODY_LEN + 1)),
format!("{prefix}{}", body(FLOOR)),
format!("{prefix}{}", body(200)),
format!("{prefix}{}_{}", &valid[..28], &valid[28..]),
format!("{prefix}{}-{}", &valid[..28], &valid[28..]),
format!("{prefix}{valid}_backup"),
format!("{prefix}{valid}-1"),
format!("{prefix}{valid}a"),
];
for separator in ["_", "-"] {
let mut replaced = valid.clone();
replaced.replace_range(30..31, separator);
values.push(format!("{prefix}{replaced}"));
}
for value in values {
for input in contexts(&value) {
assert_sole(&input, &value, "vercel_token");
}
}
let cut = format!("{prefix}{}", &valid[..30]);
for separator in [".", "!", " "] {
let input = format!("{cut}{separator}{}", &valid[31..]);
assert_sole(&input, &cut, "vercel_token");
}
assert!(detect(&format!("{prefix}{}", body(FLOOR - 1))).is_empty());
}
}
#[test]
fn marker_twins_and_leading_glue_are_rejected() {
let valid = body(TYPED_BODY_LEN);
for (prefix, _) in TYPED {
let token = format!("{prefix}{valid}");
for input in [
format!("{}{valid}", prefix.to_ascii_uppercase()),
format!("{}{valid}", prefix.replace('_', "-")),
format!("{}{valid}", &prefix[..3]),
format!("x{token}"),
format!("legacy{token}"),
format!("_{token}"),
format!("-{token}"),
format!("7{token}"),
] {
assert!(detect(&input).is_empty(), "{input}");
}
}
}
#[test]
fn classes_are_isolated_from_each_other() {
let value = body(TYPED_BODY_LEN);
let lines: Vec<String> = TYPED
.iter()
.map(|(prefix, _)| format!("{prefix}{value}"))
.chain(INTERIM.iter().map(|prefix| format!("{prefix}{value}")))
.collect();
let input = lines.join("\n");
let candidates = detect(&input);
let types: Vec<&str> = candidates.iter().map(Candidate::type_name).collect();
assert_eq!(
types,
[
"vercel_personal_access_token",
"vercel_app_access_token",
"vercel_app_refresh_token",
"vercel_token",
"vercel_token",
]
);
let mut offset = 0;
for (candidate, line) in candidates.iter().zip(&lines) {
assert_eq!(
candidate.range(),
ByteRange::new(offset, offset + line.len()).unwrap()
);
offset += line.len() + 1;
}
}
#[test]
fn benign_siblings_are_not_claimed() {
let value = body(TYPED_BODY_LEN);
for input in [
"deployment: dpl_8sFjq2K3nQeR7xYtLmWzAbCdEfGh".to_owned(),
format!("vcx_{value}"),
format!("vcs_{value}"),
format!("vc_{value}"),
"vcp_<YOUR_VERCEL_TOKEN>".to_owned(),
format!("vcp_{}", "*".repeat(TYPED_BODY_LEN)),
"vcp_${VERCEL_TOKEN}".to_owned(),
"vcp_...".to_owned(),
"Personal access tokens begin with the prefix vcp_ and app tokens with vca_."
.to_owned(),
"prj_SyntheticRevokedVercelProjectId000".to_owned(),
"team_SyntheticRevokedVercelTeamId0000".to_owned(),
format!("VERCEL_TOKEN={}", body(24)),
] {
assert!(detect(&input).is_empty(), "{input}");
}
}
#[test]
fn a_one_character_filler_body_is_a_placeholder() {
for prefix in ["vcp_", "vci_", "vca_", "vcr_", "vck_"] {
for len in [FLOOR, 24, TYPED_BODY_LEN] {
assert!(detect(&format!("{prefix}{}", "x".repeat(len))).is_empty());
assert!(detect(&format!("{prefix}{}", "0".repeat(len))).is_empty());
}
let twin = format!("{prefix}{}y", "x".repeat(FLOOR));
assert_eq!(detect(&twin).len(), 1, "{twin}");
}
}
#[test]
fn a_repeated_value_is_reported_once_per_occurrence() {
for (prefix, type_name) in TYPED {
let token = format!("{prefix}{}", body(TYPED_BODY_LEN));
let candidates = detect(&format!("{token} {token}"));
assert_eq!(candidates.len(), 2);
assert!(candidates.iter().all(|c| c.type_name() == type_name));
}
}
#[test]
fn a_long_run_is_one_aggregate_match() {
for input in ["vcp_".repeat(4096), format!("vcp_{}", "AB".repeat(50_000))] {
assert_sole(&input, &input, "vercel_token");
}
}
const PRE_SPLIT: crate::detectors::additional_providers::KnownFormatProviderDetector =
crate::detectors::additional_providers::KnownFormatProviderDetector::new(
"vercel-token",
"vercel_token",
&[
PrefixShape::at_least("vcp_", 20, pattern::is_alnum_dash, &AGGREGATE_SIGNALS),
PrefixShape::at_least("vci_", 20, pattern::is_alnum_dash, &AGGREGATE_SIGNALS),
PrefixShape::at_least("vca_", 20, pattern::is_alnum_dash, &AGGREGATE_SIGNALS),
PrefixShape::at_least("vcr_", 20, pattern::is_alnum_dash, &AGGREGATE_SIGNALS),
PrefixShape::at_least("vck_", 20, pattern::is_alnum_dash, &AGGREGATE_SIGNALS),
],
pattern::is_alnum_dash,
);
#[test]
fn every_span_equals_the_pre_split_detector() {
let valid = body(TYPED_BODY_LEN);
let mut values = Vec::new();
for prefix in ["vcp_", "vci_", "vca_", "vcr_", "vck_", "VCP_", "vcx_"] {
for len in [0, 19, 20, 21, 55, 56, 57, 60, 200] {
values.push(format!("{prefix}{}", body(len)));
}
for glue in ["_", "-", ".", "!", " ", "a", "_backup", "-1"] {
values.push(format!("{prefix}{}{glue}{}", &valid[..28], &valid[28..]));
values.push(format!("{prefix}{valid}{glue}"));
values.push(format!("{glue}{prefix}{valid}"));
}
}
values.push("vcp_".repeat(64));
for value in values {
for input in contexts(&value) {
let old = PRE_SPLIT
.detect(&input, &DetectorContext::new(input.len()))
.unwrap();
let new = detect(&input);
assert_eq!(old.len(), new.len(), "{input}");
for (old, new) in old.iter().zip(&new) {
assert_eq!(old.range(), new.range(), "{input}");
assert_eq!(old.confidence(), new.confidence(), "{input}");
assert_eq!(
old.effective_specificity(),
new.effective_specificity(),
"{input}"
);
let typed =
typed_class(&input.as_bytes()[new.range().start()..new.range().end()]);
assert_eq!(new.type_name(), typed.unwrap_or("vercel_token"), "{input}");
}
}
}
}
}