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 PREFIX: &str = "ATAT";
const MIN_BODY_LEN: usize = 100;
const TAIL_SEPARATOR: u8 = b'=';
const TAIL_HEX_LEN: usize = 8;
fn is_upper_hex(byte: u8) -> bool {
byte.is_ascii_digit() || (b'A'..=b'F').contains(&byte)
}
fn extend_over_tail(bytes: &[u8], end: usize) -> usize {
let hex_start = end + 1;
let tail_end = hex_start + TAIL_HEX_LEN;
if bytes.get(end) != Some(&TAIL_SEPARATOR) || bytes.len() < tail_end {
return end;
}
if !bytes[hex_start..tail_end].iter().copied().all(is_upper_hex) {
return end;
}
match bytes.get(tail_end) {
Some(&next) if pattern::is_alnum_dash(next) || next == TAIL_SEPARATOR => end,
_ => tail_end,
}
}
pub(super) struct AtlassianApiTokenDetector;
impl Detector for AtlassianApiTokenDetector {
fn id(&self) -> &'static str {
"atlassian-api-token"
}
fn detect(
&self,
input: &str,
_context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure> {
let bytes = input.as_bytes();
let mut candidates = Vec::new();
for (start, end) in pattern::scan_prefixed_runs(
input,
&[PREFIX],
RunLength::AtLeast(MIN_BODY_LEN),
pattern::is_alnum_dash,
pattern::is_alnum_dash,
) {
let end = extend_over_tail(bytes, end);
let Some(range) = ByteRange::new(start, end) else {
continue;
};
candidates.push(
Candidate::built_in("atlassian_api_token", Confidence::High, range)
.with_specificity(Specificity::Provider)
.with_signal_pack(crate::types::signal_pack!(
"atlassian-documented-prefix",
"minimum-length-opaque-body"
)),
);
}
Ok(candidates)
}
}
pub(super) const REQUIRED_LITERALS: &[Literals] = &[Literals::Strs(&[PREFIX])];
#[cfg(test)]
mod tests {
use super::*;
const BODY_100: &str = "SYNTHETIC_REVOKED_ATLASSIAN_API_TOKEN_BODY_SYNTHETIC_REVOKED_ATLASSIAN_API_TOKEN_BODY_SYNTHETIC_REVO";
fn detect(input: &str) -> Vec<Candidate> {
AtlassianApiTokenDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
#[test]
fn body_100_is_exactly_the_documented_minimum() {
assert_eq!(BODY_100.len(), MIN_BODY_LEN);
}
#[test]
fn detects_the_synthetic_fixture_at_the_minimum_length() {
let input = format!("{PREFIX}{BODY_100}");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "atlassian_api_token");
assert_eq!(candidates[0].confidence(), Confidence::High);
assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, input.len()).unwrap()
);
}
#[test]
fn detects_a_body_longer_than_the_minimum() {
let input = format!("{PREFIX}{BODY_100}EXTRA-TAIL-BYTES-PAST-THE-MINIMUM");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, input.len()).unwrap()
);
}
#[test]
fn rejects_a_body_one_byte_below_the_minimum() {
let short = &BODY_100[..BODY_100.len() - 1];
assert_eq!(detect(&format!("{PREFIX}{short}")).len(), 0);
}
#[test]
fn rejects_a_body_broken_by_an_out_of_alphabet_byte_before_the_minimum() {
let broken = format!("{} {}", &BODY_100[..10], &BODY_100[11..]);
assert_eq!(detect(&format!("{PREFIX}{broken}")).len(), 0);
}
#[test]
fn stops_at_an_equals_sign_not_followed_by_the_documented_tail() {
for tail in [
"=CHECKSUMSUFFIX",
"=0A1B2C3",
"=0A1B2C3D4",
"=0a1b2c3d",
"=0A1B2C3D-",
"=0A1B2C3D=",
] {
let input = format!("{PREFIX}{BODY_100}{tail}");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1, "{tail}");
assert_eq!(
candidates[0].range(),
ByteRange::new(0, PREFIX.len() + BODY_100.len()).unwrap(),
"{tail}"
);
}
}
#[test]
fn includes_the_equals_and_eight_uppercase_hex_tail_in_the_span() {
for (before, after) in [
("", ""),
("ATLASSIAN_API_TOKEN=", "\n"),
("{\"apiToken\": \"", "\"}"),
("user@example.test:", " "),
] {
let token = format!("{PREFIX}{BODY_100}=0A1B2C3D");
let input = format!("{before}{token}{after}");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1, "{input}");
assert_eq!(
candidates[0].range(),
ByteRange::new(before.len(), before.len() + token.len()).unwrap(),
"{input}"
);
}
}
#[test]
fn rejects_the_access_token_prefix() {
assert_eq!(detect(&format!("ATCT{BODY_100}")).len(), 0);
}
#[test]
fn rejects_the_app_password_prefix() {
assert_eq!(detect(&format!("ATBB{BODY_100}")).len(), 0);
}
#[test]
fn rejects_the_legacy_unprefixed_twenty_four_byte_token_with_no_context() {
assert_eq!(detect("abcdefghijklmnopqrst1234").len(), 0);
}
#[test]
fn rejects_a_placeholder_immediately_after_the_prefix() {
assert_eq!(detect(&format!("{PREFIX}<YOUR_API_TOKEN_HERE>")).len(), 0);
}
#[test]
fn rejects_a_masked_value() {
assert_eq!(
detect(&format!("{PREFIX}{}", "*".repeat(MIN_BODY_LEN))).len(),
0
);
}
#[test]
fn rejects_an_environment_variable_reference() {
assert_eq!(
detect(&format!("{PREFIX}${{ATLASSIAN_API_TOKEN}}")).len(),
0
);
}
#[test]
fn finds_a_qualified_match_immediately_after_an_assignment_delimiter() {
let input = format!("ATLASSIAN_API_TOKEN={PREFIX}{BODY_100}");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
let start = input.rfind(PREFIX).unwrap();
assert_eq!(
candidates[0].range(),
ByteRange::new(start, input.len()).unwrap()
);
}
#[test]
fn finds_a_match_across_crlf_and_a_unicode_prefix() {
let input = format!("# \u{1F511} caf\u{e9}\r\n{PREFIX}{BODY_100}\r\n");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
let start = input.find(PREFIX).unwrap();
assert_eq!(
candidates[0].range(),
ByteRange::new(start, start + PREFIX.len() + BODY_100.len()).unwrap()
);
}
#[test]
fn finds_deterministic_findings_across_repeated_calls() {
let input = format!("{PREFIX}{BODY_100}");
assert_eq!(detect(&input), detect(&input));
}
#[test]
fn stays_bounded_over_a_long_run_of_rejected_prefixes() {
let input = format!("{PREFIX}{BODY_100}!{}", "ATAT!".repeat(10_000));
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, PREFIX.len() + BODY_100.len()).unwrap()
);
}
}