use crate::detectors::additional_providers::KnownFormatProviderDetector;
use crate::detectors::pattern::{self, Alphabet, PrefixShape};
const PREFIX: &str = "PMAK-";
const HEX_SEGMENT_1_LEN: usize = 24;
const HEX_SEGMENT_2_LEN: usize = 34;
const SEPARATOR_LEN: usize = 1;
const BODY_LEN: usize = HEX_SEGMENT_1_LEN + SEPARATOR_LEN + HEX_SEGMENT_2_LEN;
const DASH_OFFSET: usize = HEX_SEGMENT_1_LEN;
const SIGNALS: [&str; 2] = ["postman-documented-prefix", "hex-hex-structured-body"];
const BOUNDARY: Alphabet = pattern::is_alnum_dash;
fn body_is_hex_dash_hex(bytes: &[u8], _start: usize, end: usize) -> bool {
let body_start = end - BODY_LEN;
let dash_index = body_start + DASH_OFFSET;
bytes[dash_index] == b'-'
&& bytes[body_start..dash_index]
.iter()
.copied()
.all(pattern::is_hex)
&& bytes[dash_index + 1..end]
.iter()
.copied()
.all(pattern::is_hex)
}
pub(super) const POSTMAN: KnownFormatProviderDetector = KnownFormatProviderDetector::new(
"postman-api-key",
"postman_api_key",
&[
PrefixShape::exact(PREFIX, BODY_LEN, pattern::is_hex_or_dash, &SIGNALS)
.with_post_check(body_is_hex_dash_hex),
],
BOUNDARY,
);
const COLLECTION_ACCESS_KEY_PREFIX: &str = "PMAT-";
const COLLECTION_ACCESS_KEY_BODY_LEN: usize = 26;
const COLLECTION_ACCESS_KEY_SIGNALS: [&str; 2] = [
"postman-collection-access-key-prefix",
"tool-corroborated-length",
];
fn collection_access_key_is_not_filler(bytes: &[u8], start: usize, end: usize) -> bool {
let body = &bytes[start + COLLECTION_ACCESS_KEY_PREFIX.len()..end];
body.iter().any(|&byte| byte != body[0])
}
pub(super) const POSTMAN_COLLECTION_ACCESS_KEY: KnownFormatProviderDetector =
KnownFormatProviderDetector::new(
"postman-collection-access-key",
"postman_collection_access_key",
&[PrefixShape::exact(
COLLECTION_ACCESS_KEY_PREFIX,
COLLECTION_ACCESS_KEY_BODY_LEN,
pattern::is_alnum,
&COLLECTION_ACCESS_KEY_SIGNALS,
)
.with_post_check(collection_access_key_is_not_filler)],
BOUNDARY,
);
#[cfg(test)]
mod collection_access_key_tests {
use super::*;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const BODY: &str = "SynthRevoked0Postman0Pmat1";
const _: () = assert!(BODY.len() == COLLECTION_ACCESS_KEY_BODY_LEN);
fn key() -> String {
format!("{COLLECTION_ACCESS_KEY_PREFIX}{BODY}")
}
fn detect(input: &str) -> Vec<Candidate> {
POSTMAN_COLLECTION_ACCESS_KEY
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
#[test]
fn detects_a_key_bare_and_in_a_share_url() {
let key = key();
for input in [
key.clone(),
format!(
"https://api.getpostman.com/collections/12345678-0000-0000-0000-000000000000?access_key={key}"
),
format!("POSTMAN_COLLECTION_ACCESS_KEY={key}"),
] {
let candidates = detect(&input);
assert_eq!(candidates.len(), 1, "{input}");
assert_eq!(candidates[0].type_name(), "postman_collection_access_key");
assert_eq!(candidates[0].confidence(), Confidence::High);
assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
let start = input.find(&key).unwrap();
assert_eq!(
candidates[0].range(),
ByteRange::new(start, start + key.len()).unwrap()
);
}
}
#[test]
fn accepts_a_lowercase_body() {
assert_eq!(
detect(&format!("PMAT-{}", BODY.to_ascii_lowercase())).len(),
1
);
}
#[test]
fn rejects_the_wrong_length_filler_and_embedding() {
for input in [
format!("PMAT-{}", &BODY[..25]),
format!("PMAT-{BODY}X"),
format!("PMAT-{}", "*".repeat(26)),
format!("PMAT-{}", "X".repeat(26)),
format!("xPMAT-{BODY}"),
format!("PMAT-{BODY}-x"),
"PMAT-<your-collection-access-key>".to_owned(),
] {
assert!(detect(&input).is_empty(), "{input}");
}
}
#[test]
fn the_api_key_prefix_is_not_a_collection_access_key() {
assert!(detect(&format!("PMAK-{BODY}")).is_empty());
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const HEX_SEGMENT_1: &str = "0123456789abcdef01234567";
const _: () = assert!(HEX_SEGMENT_1.len() == HEX_SEGMENT_1_LEN);
const HEX_SEGMENT_2: &str = "0123456789abcdef0123456789abcdef01";
const _: () = assert!(HEX_SEGMENT_2.len() == HEX_SEGMENT_2_LEN);
fn detect(input: &str) -> Vec<Candidate> {
POSTMAN
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn token() -> String {
format!("{PREFIX}{HEX_SEGMENT_1}-{HEX_SEGMENT_2}")
}
#[test]
fn detects_the_api_key_with_provider_specificity() {
let value = token();
let candidates = detect(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "postman_api_key");
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 accepts_uppercase_hex_segments() {
let value = format!(
"{PREFIX}{}-{}",
HEX_SEGMENT_1.to_ascii_uppercase(),
HEX_SEGMENT_2.to_ascii_uppercase()
);
let candidates = detect(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, value.len()).unwrap()
);
}
#[test]
fn accepts_mixed_case_hex_segments() {
let value = format!("{PREFIX}0123456789ABCdef01234567-{HEX_SEGMENT_2}");
assert_eq!(detect(&value).len(), 1);
}
#[test]
fn rejects_a_body_with_no_internal_dash() {
let bare = format!("{HEX_SEGMENT_1}0{HEX_SEGMENT_2}");
assert_eq!(bare.len(), BODY_LEN);
assert!(detect(&format!("{PREFIX}{bare}")).is_empty());
}
#[test]
fn rejects_a_dash_at_the_wrong_offset() {
let value = format!(
"{PREFIX}{}-{}0",
&HEX_SEGMENT_1[..HEX_SEGMENT_1_LEN - 1],
HEX_SEGMENT_2
);
assert!(detect(&value).is_empty());
}
#[test]
fn rejects_a_second_dash_in_place_of_a_hex_byte() {
let mut segment_2 = HEX_SEGMENT_2.to_string();
segment_2.replace_range(5..6, "-");
assert!(detect(&format!("{PREFIX}{HEX_SEGMENT_1}-{segment_2}")).is_empty());
}
#[test]
fn rejects_a_body_one_byte_short_of_the_documented_length() {
let short = &HEX_SEGMENT_2[..HEX_SEGMENT_2_LEN - 1];
assert!(detect(&format!("{PREFIX}{HEX_SEGMENT_1}-{short}")).is_empty());
}
#[test]
fn rejects_a_body_one_byte_longer_than_the_documented_length() {
assert!(detect(&format!("{}0", token())).is_empty());
}
#[test]
fn rejects_a_non_hex_letter_in_a_hex_segment() {
let mut segment_1 = HEX_SEGMENT_1.to_string();
segment_1.replace_range(0..1, "g");
assert!(detect(&format!("{PREFIX}{segment_1}-{HEX_SEGMENT_2}")).is_empty());
}
#[test]
fn rejects_a_lowercase_prefix() {
assert!(detect(&format!("pmak-{HEX_SEGMENT_1}-{HEX_SEGMENT_2}")).is_empty());
}
#[test]
fn rejects_the_prefix_embedded_in_a_wider_identifier() {
assert!(detect(&format!("legacy{}", token())).is_empty());
}
#[test]
fn rejects_the_value_embedded_in_a_wider_identifier_leading_trailing_or_dash_joined() {
let value = token();
assert!(detect(&format!("legacy{value}")).is_empty());
assert!(detect(&format!("{value}_backup")).is_empty());
assert!(detect(&format!("{value}-1")).is_empty());
}
#[test]
fn rejects_a_percent_encoded_delimiter_lookalike() {
assert!(detect(&format!("PMAK%2D{HEX_SEGMENT_1}-{HEX_SEGMENT_2}")).is_empty());
}
#[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 reports_a_repeated_identical_value_once_per_occurrence() {
let value = token();
let input = format!("{value} {value}");
assert_eq!(detect(&input).len(), 2);
}
#[test]
fn finds_a_match_across_crlf_and_a_unicode_prefix() {
let value = token();
let input = format!("# \u{1F511} caf\u{e9}\r\n{value}\r\n");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
let start = input.find(&value).unwrap();
assert_eq!(
candidates[0].range(),
ByteRange::new(start, start + value.len()).unwrap()
);
}
#[test]
fn rejects_a_masked_value() {
assert!(detect(&format!("{PREFIX}{}", "*".repeat(BODY_LEN))).is_empty());
assert!(detect(&format!("{PREFIX}${{POSTMAN_API_KEY}}")).is_empty());
}
#[test]
fn rejects_a_doc_style_x_filled_placeholder() {
let placeholder = format!(
"{}-{}",
"x".repeat(HEX_SEGMENT_1_LEN),
"x".repeat(HEX_SEGMENT_2_LEN)
);
assert!(detect(&format!("{PREFIX}{placeholder}")).is_empty());
}
#[test]
fn finds_deterministic_findings_across_repeated_calls() {
let value = token();
assert_eq!(detect(&value), detect(&value));
}
#[test]
fn stays_bounded_over_a_long_run_of_near_miss_prefixes() {
let short = &HEX_SEGMENT_2[..HEX_SEGMENT_2_LEN - 1];
let input = format!("{PREFIX}{HEX_SEGMENT_1}-{short} ").repeat(10_000);
assert_eq!(detect(&input).len(), 0);
}
}