use crate::detectors::pattern::{self, RunCursor, is_alnum_dash};
use crate::detectors::prefilter::Literals;
use crate::error::DetectorFailure;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const PREFIX: &[u8] = b"SG.";
const ID_LEN: usize = 22;
const SECRET_LEN: usize = 43;
pub(super) struct SendgridTokenDetector;
impl Detector for SendgridTokenDetector {
fn id(&self) -> &'static str {
"sendgrid-token"
}
fn detect(
&self,
input: &str,
_context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure> {
let bytes = input.as_bytes();
let Some(first) = pattern::find_literal(bytes, PREFIX, 0) else {
return Ok(Vec::new());
};
let mut id_ends = RunCursor::new(bytes, is_alnum_dash);
let mut secret_ends = RunCursor::new(bytes, is_alnum_dash);
let mut candidates = Vec::new();
let mut start = first;
while start < bytes.len() {
if !bytes[start..].starts_with(PREFIX) {
start += 1;
continue;
}
let Some(end) = match_at(bytes, &mut id_ends, &mut secret_ends, start) else {
start += 1;
continue;
};
if pattern::boundary_ok(bytes, start, end, is_alnum_dash)
&& let Some(range) = ByteRange::new(start, end)
{
candidates.push(
Candidate::built_in("sendgrid_api_key", Confidence::High, range)
.with_specificity(Specificity::Provider)
.with_signal_pack(crate::types::signal_pack!(
"sendgrid-documented-shape",
"two-segment-exact-length"
)),
);
}
start = end;
}
Ok(candidates)
}
}
fn match_at(
bytes: &[u8],
id_ends: &mut RunCursor<'_>,
secret_ends: &mut RunCursor<'_>,
start: usize,
) -> Option<usize> {
let id_start = start + PREFIX.len();
if id_ends.end(id_start) < id_start + ID_LEN {
return None;
}
let dot = id_start + ID_LEN;
if bytes.get(dot) != Some(&b'.') {
return None;
}
let secret_start = dot + 1;
if secret_ends.end(secret_start) < secret_start + SECRET_LEN {
return None;
}
Some(secret_start + SECRET_LEN)
}
pub(super) const REQUIRED_LITERALS: &[Literals] = &[Literals::Bytes(&[PREFIX])];
#[cfg(test)]
mod tests {
use super::*;
const ID: &str = "SYNTHETIC_REVOKED_0000";
const SECRET: &str = "SYNTHETIC_REVOKED_SENDGRID_SECRET_000000000";
fn token() -> String {
format!("SG.{ID}.{SECRET}")
}
fn detect(input: &str) -> Vec<Candidate> {
SendgridTokenDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
#[test]
fn fixture_segments_are_exactly_documented_length() {
assert_eq!(ID.len(), ID_LEN);
assert_eq!(SECRET.len(), SECRET_LEN);
}
#[test]
fn detects_a_synthetic_key_with_provider_specificity() {
let value = token();
let candidates = detect(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].confidence(), Confidence::High);
assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, value.len()).unwrap()
);
}
#[test]
fn detects_the_key_bare_in_env_and_control_contexts() {
let value = token();
for input in [
value.clone(),
format!("SENDGRID_TOKEN={value}"),
format!("api_key={value}"),
format!("{{\"apiKey\": \"{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() {
let value = token();
let input = format!("({value}).");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].range(),
ByteRange::new(1, value.len() + 1).unwrap()
);
}
#[test]
fn rejects_a_truncated_id_segment() {
let short_id = &ID[..ID_LEN - 1];
assert!(detect(&format!("SG.{short_id}.{SECRET}")).is_empty());
}
#[test]
fn rejects_a_truncated_secret_segment() {
let short_secret = &SECRET[..SECRET_LEN - 1];
assert!(detect(&format!("SG.{ID}.{short_secret}")).is_empty());
}
#[test]
fn rejects_an_id_segment_longer_than_documented() {
assert!(detect(&format!("SG.{ID}A.{SECRET}")).is_empty());
}
#[test]
fn rejects_a_secret_segment_longer_than_documented() {
assert!(detect(&format!("SG.{ID}.{SECRET}A")).is_empty());
}
#[test]
fn rejects_a_malformed_separator() {
assert!(detect(&format!("SG_{ID}.{SECRET}")).is_empty());
assert!(detect(&format!("SG.{ID}_{SECRET}")).is_empty());
}
#[test]
fn rejects_the_prefix_alone() {
assert!(detect("SG.").is_empty());
assert!(detect("SG.SYNTHETIC").is_empty());
}
#[test]
fn rejects_the_id_or_secret_embedded_in_a_wider_identifier() {
let value = token();
assert!(detect(&format!("x{value}")).is_empty());
assert!(detect(&format!("{value}x")).is_empty());
}
#[test]
fn accepts_an_all_valid_alphabet_documentation_placeholder() {
let value = format!("SG.{}.{}", "x".repeat(ID_LEN), "x".repeat(SECRET_LEN));
let candidates = detect(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].confidence(), Confidence::High);
}
#[test]
fn rejects_a_percent_encoded_separator_lookalike() {
let value = format!("SG%2E{ID}.{SECRET}");
assert!(detect(&value).is_empty());
}
#[test]
fn reports_a_repeated_identical_value_once_per_occurrence() {
let value = token();
let input = format!("{value} {value}");
let candidates = detect(&input);
assert_eq!(candidates.len(), 2);
}
#[test]
fn accepts_segments_with_trailing_url_safe_characters() {
let id = format!("{}-", &ID[..ID_LEN - 1]);
let secret = format!("{}_", &SECRET[..SECRET_LEN - 1]);
assert_eq!(id.len(), ID_LEN);
assert_eq!(secret.len(), SECRET_LEN);
let value = format!("SG.{id}.{secret}");
let candidates = detect(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, value.len()).unwrap()
);
}
}