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; 2] = ["shpat_", "shppa_"];
pub(super) struct ShopifyTokenDetector;
impl Detector for ShopifyTokenDetector {
fn id(&self) -> &'static str {
"shopify-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(20),
pattern::is_alnum_dash,
pattern::is_alnum_dash,
) {
let Some(range) = ByteRange::new(start, end) else {
continue;
};
candidates.push(
Candidate::built_in("shopify_access_token", Confidence::High, range)
.with_specificity(Specificity::Provider)
.with_signal_pack(crate::types::signal_pack!(
"shopify-documented-prefix",
"opaque-suffix"
)),
);
}
Ok(candidates)
}
}
pub(super) const REQUIRED_LITERALS: &[Literals] = &[Literals::Strs(&PREFIXES)];
#[cfg(test)]
mod tests {
use super::*;
fn detect(input: &str) -> Vec<Candidate> {
ShopifyTokenDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
#[test]
fn detects_the_admin_api_prefix() {
let input = "shpat_SYNTHETIC_REVOKED_SHOPIFY_TOKEN";
let candidates = detect(input);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "shopify_access_token");
assert_eq!(
candidates[0].range(),
ByteRange::new(0, input.len()).unwrap()
);
}
#[test]
fn detects_the_delegate_access_prefix() {
let input = "shppa_SYNTHETIC_REVOKED_SHOPIFY_TOKEN";
assert_eq!(detect(input).len(), 1);
}
#[test]
fn rejects_a_short_suffix() {
assert_eq!(detect("shpat_SYNTHETIC_SHORT").len(), 0);
}
#[test]
fn rejects_the_public_storefront_prefix() {
assert_eq!(detect("shpca_SYNTHETIC_REVOKED_SHOPIFY_TOKEN").len(), 0);
}
#[test]
fn rejects_wrong_prefix_masked_interpolated_and_contextualized_near_misses() {
for input in [
"shpad_SYNTHETIC_REVOKED_SHOPIFY_TOKEN",
"(shpca_SYNTHETIC_REVOKED_SHOPIFY_TOKEN).",
"# \u{1F511} caf\u{e9}\r\nshpca_SYNTHETIC_REVOKED_SHOPIFY_TOKEN\r\n",
"shpat_********************",
"shpat_${SHOPIFY_ADMIN_API_TOKEN}",
] {
assert_eq!(detect(input).len(), 0, "{input}");
}
}
#[test]
fn detects_only_the_secret_half_of_a_mixed_public_and_secret_input() {
let input = "shpca_SYNTHETIC_REVOKED_SHOPIFY_TOKEN\nshpat_SYNTHETIC_REVOKED_SHOPIFY_TOKEN";
let candidates = detect(input);
assert_eq!(candidates.len(), 1);
let expected_start = input.find("shpat_").unwrap();
assert_eq!(
candidates[0].range(),
ByteRange::new(expected_start, input.len()).unwrap()
);
}
}