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; 3] = ["hvs.", "hvb.", "hvr."];
pub(super) struct VaultTokenDetector;
impl Detector for VaultTokenDetector {
fn id(&self) -> &'static str {
"vault-token"
}
fn detect(
&self,
input: &str,
_context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure> {
let mut candidates = Vec::new();
for (start, end) in pattern::scan_prefixed_runs(
input,
&PREFIXES,
RunLength::AtLeast(24),
pattern::is_alnum_dash,
pattern::is_alnum_dash_dot,
) {
let Some(range) = ByteRange::new(start, end) else {
continue;
};
candidates.push(
Candidate::built_in("vault_token", Confidence::High, range)
.with_specificity(Specificity::Provider)
.with_signal_pack(crate::types::signal_pack!(
"vault-modern-prefix",
"documented-minimum-length"
)),
);
}
Ok(candidates)
}
}
pub(super) const REQUIRED_LITERALS: &[Literals] = &[Literals::Strs(&PREFIXES)];
#[cfg(test)]
mod tests {
use super::*;
fn detect(input: &str) -> Vec<Candidate> {
VaultTokenDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
#[test]
fn detects_the_service_token_prefix() {
let input = "hvs.SYNTHETIC_REVOKED_VAULT_TOKEN";
let candidates = detect(input);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "vault_token");
assert_eq!(
candidates[0].range(),
ByteRange::new(0, input.len()).unwrap()
);
}
#[test]
fn detects_the_batch_and_recovery_prefixes() {
assert_eq!(detect("hvb.SYNTHETIC_REVOKED_VAULT_TOKEN").len(), 1);
assert_eq!(detect("hvr.SYNTHETIC_REVOKED_VAULT_TOKEN").len(), 1);
}
#[test]
fn rejects_the_legacy_one_letter_prefix() {
assert_eq!(detect("s.SYNTHETIC_REVOKED_LEGACY_VAULT_TOKEN").len(), 0);
}
#[test]
fn rejects_a_short_suffix() {
assert_eq!(detect("hvs.SYNTHETIC_SHORT").len(), 0);
}
#[test]
fn a_dot_widens_the_boundary_but_not_the_matched_alphabet() {
let input = "hvs.SYNTHETIC_REVOKED_VAULT_TOKEN.example";
assert_eq!(detect(input).len(), 0);
}
#[test]
fn accepts_a_doc_style_placeholder_built_from_valid_alphabet_characters() {
for prefix in PREFIXES {
let input = format!("{prefix}{}", "x".repeat(24));
assert_eq!(detect(&input).len(), 1, "{prefix}");
}
}
#[test]
fn rejects_a_prefix_embedded_in_a_wider_benign_identifier() {
for prefix in PREFIXES {
let input = format!("legacy{prefix}SYNTHETIC_REVOKED_VAULT_TOKEN");
assert_eq!(detect(&input).len(), 0, "{prefix}");
}
}
#[test]
fn rejects_case_changed_wrong_prefixes() {
for prefix in PREFIXES {
let input = format!(
"{}SYNTHETIC_REVOKED_VAULT_TOKEN",
prefix.to_ascii_uppercase()
);
assert_eq!(detect(&input).len(), 0, "{prefix}");
}
}
#[test]
fn rejects_masked_and_interpolated_near_misses() {
for prefix in PREFIXES {
for input in [
format!("{prefix}{}", "*".repeat(24)),
format!("{prefix}${{ENV_VAR}}"),
] {
assert_eq!(detect(&input).len(), 0, "{input}");
}
}
}
#[test]
fn a_trailing_comma_does_not_get_folded_into_or_suppress_the_match() {
for prefix in PREFIXES {
let token = format!("{prefix}SYNTHETIC_REVOKED_VAULT_TOKEN");
let input = format!("Rotate {token}, then redeploy.");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1, "{prefix}");
let start = "Rotate ".len();
let end = start + token.len();
assert_eq!(
candidates[0].range(),
ByteRange::new(start, end).unwrap(),
"{prefix}"
);
}
}
#[test]
fn rejects_an_ordinary_vault_path_and_dotted_version_string() {
let input = "vault kv get -namespace=admin secret/data/myapp/config on server v1.15.4";
assert_eq!(detect(input).len(), 0);
}
#[test]
fn reports_a_repeated_identical_value_once_per_occurrence() {
let value = "hvs.SYNTHETIC_REVOKED_VAULT_TOKEN";
let input = format!("{value} {value}");
assert_eq!(detect(&input).len(), 2);
}
}