use crate::detectors::additional_providers::KnownFormatProviderDetector;
use crate::detectors::pattern::{self, Alphabet, PrefixShape};
const API_PREFIX: &str = "lin_api_";
const API_BODY_LEN: usize = 40;
const API_ALPHABET: Alphabet = pattern::is_alnum;
const API_SIGNALS: [&str; 2] = ["linear-documented-prefix", "base62-exact-length"];
const OAUTH_PREFIX: &str = "lin_oauth_";
const OAUTH_MIN_LEN: usize = 20;
const OAUTH_ALPHABET: Alphabet = pattern::is_alnum_dash;
const OAUTH_SIGNALS: [&str; 2] = ["linear-scannable-prefix", "opaque-suffix"];
const BOUNDARY: Alphabet = pattern::is_alnum_dash;
pub(super) const LINEAR: KnownFormatProviderDetector = KnownFormatProviderDetector::new(
"linear-token",
"linear_token",
&[
PrefixShape::exact(API_PREFIX, API_BODY_LEN, API_ALPHABET, &API_SIGNALS),
PrefixShape::open_floor(OAUTH_PREFIX, OAUTH_MIN_LEN, OAUTH_ALPHABET, &OAUTH_SIGNALS),
],
BOUNDARY,
);
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const API_BODY: &str = "SYNTHETICREVOKEDLINEARAPITOKENVALUE01234";
const _: () = assert!(API_BODY.len() == API_BODY_LEN);
const OAUTH_BODY: &str = "SYNTHETICREVOKED0001";
const _: () = assert!(OAUTH_BODY.len() == OAUTH_MIN_LEN);
fn detect(input: &str) -> Vec<Candidate> {
LINEAR
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn api_token() -> String {
format!("{API_PREFIX}{API_BODY}")
}
fn oauth_token() -> String {
format!("{OAUTH_PREFIX}{OAUTH_BODY}")
}
#[test]
fn detects_the_api_key_with_provider_specificity() {
let value = api_token();
let candidates = detect(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "linear_token");
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_oauth_interim_guard_with_provider_specificity() {
let value = oauth_token();
let candidates = detect(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "linear_token");
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 rejects_an_api_body_one_byte_short_of_the_documented_length() {
let short_body = &API_BODY[..API_BODY_LEN - 1];
assert!(detect(&format!("{API_PREFIX}{short_body}")).is_empty());
}
#[test]
fn rejects_an_api_body_one_byte_longer_than_the_documented_length() {
assert!(detect(&format!("{API_PREFIX}{API_BODY}a")).is_empty());
}
#[test]
fn rejects_an_api_body_containing_underscore_or_dash() {
for byte in ['_', '-'] {
let mut body = API_BODY.to_string();
body.replace_range(4..5, &byte.to_string());
assert!(detect(&format!("{API_PREFIX}{body}")).is_empty());
}
}
#[test]
fn oauth_keeps_the_beta4_minimum_length_rule_unchanged() {
assert!(detect(&format!("{OAUTH_PREFIX}{}", "x".repeat(19))).is_empty());
for body in [
"x".repeat(20),
"x".repeat(200),
"x_y-z".repeat(5),
API_BODY.to_string(),
] {
let value = format!("{OAUTH_PREFIX}{body}");
let candidates = detect(&value);
assert_eq!(candidates.len(), 1, "{body}");
assert_eq!(
candidates[0].range(),
ByteRange::new(0, value.len()).unwrap(),
"{body}"
);
}
}
#[test]
fn oauth_matches_a_body_longer_than_the_twenty_byte_floor() {
let value = format!("{OAUTH_PREFIX}{}", "x".repeat(45));
let candidates = detect(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, value.len()).unwrap()
);
}
#[test]
fn detects_every_variant_in_the_same_input_without_one_suppressing_another() {
let api = api_token();
let oauth = oauth_token();
let input = format!("{api}\n{oauth}\n");
let candidates = detect(&input);
assert_eq!(candidates.len(), 2);
assert_eq!(candidates[0].range(), ByteRange::new(0, api.len()).unwrap());
let oauth_start = api.len() + 1;
assert_eq!(
candidates[1].range(),
ByteRange::new(oauth_start, oauth_start + oauth.len()).unwrap()
);
}
#[test]
fn rejects_an_undocumented_segment_name() {
assert!(detect(&format!("lin_scim_{API_BODY}")).is_empty());
}
#[test]
fn rejects_the_prefix_embedded_in_a_wider_identifier() {
assert!(detect(&format!("legacy{}", api_token())).is_empty());
assert!(detect(&format!("legacy{}", oauth_token())).is_empty());
}
#[test]
fn rejects_every_shape_embedded_in_a_wider_identifier_leading_trailing_or_dash_joined() {
for value in [api_token(), oauth_token()] {
assert!(detect(&format!("legacy{value}")).is_empty(), "{value}");
assert!(detect(&format!("{value}_backup")).is_empty(), "{value}");
assert!(detect(&format!("{value}-1")).is_empty(), "{value}");
}
}
#[test]
fn rejects_a_percent_encoded_delimiter_lookalike() {
assert!(detect(&format!("lin%5Fapi_{API_BODY}")).is_empty());
}
#[test]
fn accepts_punctuation_boundaries() {
for value in [api_token(), oauth_token()] {
let input = format!("({value}).");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1, "{value}");
assert_eq!(
candidates[0].range(),
ByteRange::new(1, value.len() + 1).unwrap(),
"{value}"
);
}
}
#[test]
fn reports_a_repeated_identical_value_once_per_occurrence() {
for value in [api_token(), oauth_token()] {
let input = format!("{value} {value}");
assert_eq!(detect(&input).len(), 2, "{value}");
}
}
#[test]
fn finds_a_match_across_crlf_and_a_unicode_prefix() {
for value in [api_token(), oauth_token()] {
let input = format!("# \u{1F511} caf\u{e9}\r\n{value}\r\n");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1, "{value}");
let start = input.find(&value).unwrap();
assert_eq!(
candidates[0].range(),
ByteRange::new(start, start + value.len()).unwrap(),
"{value}"
);
}
}
#[test]
fn rejects_a_masked_value() {
assert!(detect(&format!("{API_PREFIX}{}", "*".repeat(API_BODY_LEN))).is_empty());
assert!(detect(&format!("{OAUTH_PREFIX}${{LINEAR_OAUTH_TOKEN}}")).is_empty());
}
#[test]
fn finds_deterministic_findings_across_repeated_calls() {
let value = api_token();
assert_eq!(detect(&value), detect(&value));
}
#[test]
fn stays_bounded_over_a_long_run_of_near_miss_prefixes() {
let short_body = &API_BODY[..API_BODY_LEN - 1];
let input = format!("{API_PREFIX}{short_body} ").repeat(10_000);
assert_eq!(detect(&input).len(), 0);
}
}