use super::generic_token::is_vendor_prefixed_placeholder;
use super::text::{
ascii_run_len, ends_with_ci, find_ci, is_instructional_token_placeholder, is_js_whitespace,
is_repeated_character_filler, matches_placeholder_vocabulary, prev_char, rskip_while_chars,
starts_with_ci,
};
use crate::error::DetectorFailure;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const MIN_TOKEN_LEN: usize = 16;
const MIN_HEADER_TOKEN_LEN: usize = 12;
const MAX_TRAILING_EQUALS: usize = 2;
const PLACEHOLDER_WORDS: &[&str] = &[
"example",
"sample",
"placeholder",
"redacted",
"changeme",
"password",
"secret",
"replaceme",
];
const DIGIT_SUFFIX_PLACEHOLDER_WORDS: &[&str] = &[
"example",
"sample",
"placeholder",
"redacted",
"changeme",
"replaceme",
];
fn is_non_secret_bearer_value(value: &str) -> bool {
is_repeated_character_filler(value)
|| matches_placeholder_vocabulary(value, PLACEHOLDER_WORDS, DIGIT_SUFFIX_PLACEHOLDER_WORDS)
|| is_instructional_token_placeholder(value)
|| is_vendor_prefixed_placeholder(value)
}
fn is_token_char(byte: u8) -> bool {
byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'~' | b'+' | b'/' | b'-')
}
fn token_run_len(bytes: &[u8], start: usize, percent: bool) -> usize {
let mut at = start;
while let Some(&byte) = bytes.get(at) {
if is_token_char(byte) {
at += 1;
} else if percent
&& byte == b'%'
&& bytes.get(at + 1).is_some_and(u8::is_ascii_hexdigit)
&& bytes.get(at + 2).is_some_and(u8::is_ascii_hexdigit)
{
at += 3;
} else {
break;
}
}
at - start
}
fn is_boundary_identifier_char(byte: u8) -> bool {
byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-')
}
fn is_value_join(byte: u8) -> bool {
byte == b':' || byte == b'|'
}
fn trailing_equals_at(bytes: &[u8], at: usize) -> usize {
let count = (0..MAX_TRAILING_EQUALS)
.take_while(|&offset| bytes.get(at + offset) == Some(&b'='))
.count();
if count > 0 && opens_field_value(bytes, at + count) {
0
} else {
count
}
}
fn opens_field_value(bytes: &[u8], at: usize) -> bool {
let opens = |byte: u8| is_token_char(byte) || matches!(byte, b'@' | b'$' | b'<' | b'{' | b'[');
match bytes.get(at) {
Some(&byte) if opens(byte) => true,
Some(b'"' | b'\'') => bytes.get(at + 1).copied().is_some_and(opens),
_ => false,
}
}
fn is_field_key(bytes: &[u8], run_end: usize) -> bool {
if bytes.get(run_end) != Some(&b'=') {
return false;
}
let equals = bytes[run_end..]
.iter()
.take_while(|&&byte| byte == b'=')
.count();
opens_field_value(bytes, run_end + equals)
}
fn is_host_char(byte: u8) -> bool {
byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-')
}
fn host_tail_end(bytes: &[u8], at: usize) -> Option<usize> {
if bytes.get(at) != Some(&b'@') || !bytes.get(at + 1).is_some_and(u8::is_ascii_alphanumeric) {
return None;
}
let mut end = at + 1 + ascii_run_len(bytes, at + 1, is_host_char);
while bytes[end - 1] == b'.' {
end -= 1;
}
Some(end)
}
fn joined_value_end(
bytes: &[u8],
value_start: usize,
first_run_end: usize,
percent: bool,
) -> (usize, Vec<(usize, usize)>) {
let mut runs = vec![(value_start, first_run_end)];
let mut end = first_run_end + trailing_equals_at(bytes, first_run_end);
while bytes.get(end).copied().is_some_and(is_value_join) {
let run_start = end + 1;
let run_len = token_run_len(bytes, run_start, percent);
if run_len == 0 || bytes[run_start..].starts_with(b"//") {
break;
}
let run_end = run_start + run_len;
if is_field_key(bytes, run_end) {
break;
}
runs.push((run_start, run_end));
end = run_end + trailing_equals_at(bytes, run_end);
}
let last_run_end = runs.last().map_or(first_run_end, |&(_, run_end)| run_end);
if end == last_run_end
&& let Some(host_end) = host_tail_end(bytes, end)
{
end = host_end;
}
(end, runs)
}
fn is_glued_to_placeholder(bytes: &[u8], value_end: usize) -> bool {
let mut at = value_end;
if bytes.get(at).copied().is_some_and(is_value_join) {
at += 1;
}
match bytes.get(at) {
Some(b'$') => bytes
.get(at + 1)
.is_some_and(|&next| next == b'{' || next == b'_' || next.is_ascii_alphabetic()),
Some(b'{') => bytes.get(at + 1) == Some(&b'{'),
Some(b'<') => {
let inner_len = bytes[at + 1..]
.iter()
.take(64)
.take_while(|&&byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
.count();
let inner = &bytes[at + 1..at + 1 + inner_len];
inner_len > 0
&& bytes.get(at + 1 + inner_len) == Some(&b'>')
&& inner
.iter()
.any(|&byte| byte.is_ascii_uppercase() || matches!(byte, b'_' | b'-'))
}
_ => false,
}
}
fn is_space_or_tab(byte: u8) -> bool {
byte == b' ' || byte == b'\t'
}
fn match_scheme_at(input: &str, pos: usize) -> Option<(usize, bool)> {
if starts_with_ci(input, pos, "authorization") {
let mut cursor = super::text::skip_while_chars(
input,
pos + "authorization".len(),
super::text::is_js_whitespace,
);
if super::text::char_at(input, cursor) != Some(':') {
return None;
}
cursor += 1;
cursor = super::text::skip_while_chars(input, cursor, super::text::is_js_whitespace);
return starts_with_ci(input, cursor, "bearer").then(|| (cursor + "bearer".len(), true));
}
starts_with_ci(input, pos, "bearer").then(|| (pos + "bearer".len(), false))
}
fn preceded_by_proxy_prefix(bytes: &[u8], pos: usize) -> bool {
const PROXY: &[u8] = b"proxy-";
pos >= PROXY.len()
&& bytes[pos - PROXY.len()..pos].eq_ignore_ascii_case(PROXY)
&& (pos == PROXY.len() || !is_boundary_identifier_char(bytes[pos - PROXY.len() - 1]))
}
fn trim_end_js_whitespace(input: &str) -> &str {
&input[..rskip_while_chars(input, input.len(), is_js_whitespace)]
}
fn is_identifier_boundary_char(ch: char) -> bool {
ch.is_ascii_alphanumeric() || matches!(ch, '_' | '-')
}
fn ends_with_authorization_boundary(s: &str) -> bool {
if !ends_with_ci(s, "authorization") {
return false;
}
let prefix_len = s.len() - "authorization".len();
prev_char(s, prefix_len).is_none_or(|ch| !is_identifier_boundary_char(ch))
|| preceded_by_proxy_prefix(s.as_bytes(), prefix_len)
}
pub(crate) fn has_open_bearer_authorization(input: &str) -> bool {
let trimmed = trim_end_js_whitespace(input);
match trimmed.strip_suffix(':') {
Some(before_colon) => {
ends_with_authorization_boundary(trim_end_js_whitespace(before_colon))
}
None => ends_with_authorization_boundary(trimmed),
}
}
const SECRET_TOKEN_SCHEME: &[u8] = b"secret-token:";
const MIN_SECRET_TOKEN_BODY_LEN: usize = 8;
fn is_secret_token_body_byte(byte: u8) -> bool {
byte.is_ascii_alphanumeric()
|| matches!(
byte,
b'-' | b'.'
| b'_'
| b'~'
| b'%'
| b'!'
| b'$'
| b'&'
| b'*'
| b'+'
| b'='
| b':'
| b'@'
)
}
fn secret_token_uri_candidates(input: &str) -> Vec<Candidate> {
secret_token_uri_candidates_from(input, |bytes, from| {
find_ci(bytes, from, SECRET_TOKEN_SCHEME)
})
}
fn secret_token_uri_candidates_from(
input: &str,
mut next: impl FnMut(&[u8], usize) -> Option<usize>,
) -> Vec<Candidate> {
let bytes = input.as_bytes();
let mut candidates = Vec::new();
let mut cursor = 0usize;
while cursor + SECRET_TOKEN_SCHEME.len() <= bytes.len() {
let Some(at) = next(bytes, cursor) else { break };
cursor = at;
if cursor + SECRET_TOKEN_SCHEME.len() > bytes.len() {
break;
}
if !bytes[cursor..cursor + SECRET_TOKEN_SCHEME.len()]
.eq_ignore_ascii_case(SECRET_TOKEN_SCHEME)
|| (cursor > 0 && is_boundary_identifier_char(bytes[cursor - 1]))
{
cursor += 1;
continue;
}
let body_start = cursor + SECRET_TOKEN_SCHEME.len();
let mut end = body_start;
while end < bytes.len() && is_secret_token_body_byte(bytes[end]) {
if bytes[end] == b'%'
&& !(end + 2 < bytes.len()
&& bytes[end + 1].is_ascii_hexdigit()
&& bytes[end + 2].is_ascii_hexdigit())
{
break;
}
end += if bytes[end] == b'%' { 3 } else { 1 };
}
while end > body_start && matches!(bytes[end - 1], b'.' | b':') {
end -= 1;
}
let body = &input[body_start..end];
if body.len() >= MIN_SECRET_TOKEN_BODY_LEN
&& !is_non_secret_bearer_value(body)
&& let Some(range) = ByteRange::new(cursor, end)
{
candidates.push(
Candidate::built_in("bearer_token", Confidence::High, range)
.with_specificity(Specificity::Structural)
.with_signal_pack(crate::types::signal_pack!("secret-token-uri")),
);
}
cursor = end.max(body_start);
}
candidates
}
pub(super) struct BearerTokenDetector;
impl Detector for BearerTokenDetector {
fn id(&self) -> &'static str {
"bearer-token"
}
fn detect(
&self,
input: &str,
_context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure> {
let mut next_authorization: Option<Option<usize>> = None;
let mut next_bearer: Option<Option<usize>> = None;
let mut candidates = bearer_scheme_candidates(input, |bytes, from| {
for (slot, keyword) in [
(&mut next_authorization, b"authorization".as_slice()),
(&mut next_bearer, b"bearer".as_slice()),
] {
if slot.is_none_or(|found| found.is_some_and(|at| at < from)) {
*slot = Some(find_ci(bytes, from, keyword));
}
}
match (next_authorization.flatten(), next_bearer.flatten()) {
(Some(a), Some(b)) => Some(a.min(b)),
(a, b) => a.or(b),
}
});
candidates.extend(secret_token_uri_candidates(input));
Ok(candidates)
}
}
fn bearer_scheme_candidates(
input: &str,
mut next: impl FnMut(&[u8], usize) -> Option<usize>,
) -> Vec<Candidate> {
let bytes = input.as_bytes();
let mut candidates = Vec::new();
let mut cursor = 0usize;
while cursor < bytes.len() {
let Some(at) = next(bytes, cursor) else { break };
cursor = at;
let Some((scheme_end, header)) = match_scheme_at(input, cursor) else {
cursor += super::text::char_at(input, cursor).map_or(1, char::len_utf8);
continue;
};
let ws_len = ascii_run_len(bytes, scheme_end, is_space_or_tab);
if ws_len == 0 {
cursor += super::text::char_at(input, cursor).map_or(1, char::len_utf8);
continue;
}
let value_start = scheme_end + ws_len;
let token_len = token_run_len(bytes, value_start, header);
if token_len == 0 {
cursor += super::text::char_at(input, cursor).map_or(1, char::len_utf8);
continue;
}
let (value_end, runs) =
joined_value_end(bytes, value_start, value_start + token_len, header);
let last_run_end = runs.last().map_or(value_start + token_len, |&(_, end)| end);
if last_run_end - value_start
< if header {
MIN_HEADER_TOKEN_LEN
} else {
MIN_TOKEN_LEN
}
{
cursor += super::text::char_at(input, cursor).map_or(1, char::len_utf8);
continue;
}
let boundary_blocked = cursor > 0
&& is_boundary_identifier_char(bytes[cursor - 1])
&& !(header && preceded_by_proxy_prefix(bytes, cursor));
if boundary_blocked && header {
cursor += 1;
continue;
}
let non_secret = runs
.iter()
.all(|&(start, end)| is_non_secret_bearer_value(&input[start..end]));
if !boundary_blocked
&& !non_secret
&& !is_glued_to_placeholder(bytes, value_end)
&& let Some(range) = ByteRange::new(value_start, value_end)
{
candidates.push(
Candidate::built_in("bearer_token", Confidence::High, range)
.with_specificity(Specificity::Structural)
.with_signal_pack(crate::types::signal_pack!("bearer-scheme")),
);
}
cursor = value_end.max(cursor + 1);
}
candidates
}
#[cfg(test)]
mod tests {
use super::*;
fn every_offset(bytes: &[u8], from: usize) -> Option<usize> {
(from < bytes.len()).then_some(from)
}
#[test]
fn keyword_jumps_equal_the_per_offset_scan() {
let pieces = [
"Bearer",
"bearer",
"BEARER",
"Authorization",
"authorization",
"Proxy-Authorization",
"X-Authorization",
"secret-token:",
"Secret-Token:",
" ",
" ",
"\t",
":",
"\n",
"\r\n",
"\u{e9}",
"\u{2003}",
"-",
"_",
"a",
"b",
"%",
"%4A",
"Bearer U1lOVEhFVElDX1JFVk9LRUQ=",
"Authorization: Bearer SYNTHETICREVOKED0123",
"secret-token:SYNTHETICREVOKED0123",
"SYNTHETICREVOKED0123",
".",
];
let mut state: u64 = 0x0F1E_2D3C_4B5A_6978;
let mut next = move || {
state = state
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
(state >> 33) as usize
};
let mut matched = 0usize;
for _ in 0..6000 {
let mut input = String::new();
for _ in 0..next() % 24 {
input.push_str(pieces[next() % pieces.len()]);
}
let fast = detect(&input);
matched += fast.len();
let mut oracle = bearer_scheme_candidates(&input, every_offset);
oracle.extend(secret_token_uri_candidates_from(&input, every_offset));
assert_eq!(fast, oracle, "{input:?}");
}
assert!(
matched > 100,
"the generator must exercise real matches: {matched}"
);
}
fn detect(input: &str) -> Vec<Candidate> {
BearerTokenDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn only_range(candidates: &[Candidate]) -> (usize, usize) {
assert_eq!(candidates.len(), 1);
(candidates[0].range().start(), candidates[0].range().end())
}
#[test]
fn explicit_bearer_scheme_is_detected() {
let input = "Bearer SYNTHETIC_REVOKED_BEARER_VALUE";
let candidates = detect(input);
assert_eq!(only_range(&candidates), (7, input.len()));
assert_eq!(candidates[0].confidence(), Confidence::High);
assert_eq!(candidates[0].specificity(), Some(Specificity::Structural));
}
#[test]
fn bearer_embedded_in_an_identifier_is_excluded() {
assert!(detect("notbearer SYNTHETIC_REVOKED_IDENTIFIER").is_empty());
}
#[test]
fn short_development_token_is_ignored() {
assert!(detect("Bearer short-token").is_empty());
}
#[test]
fn a_secret_token_uri_is_reported_whole_with_its_scheme() {
const URI: &str = "secret-token:SYNTHETIC-7F3A-4C2B%20fixture";
for (input, value) in [
(URI.to_owned(), URI),
(format!("token = {URI}"), URI),
(
"token = SECRET-TOKEN:SYNTHETIC-7F3A-4C2B%20fixture".to_owned(),
"SECRET-TOKEN:SYNTHETIC-7F3A-4C2B%20fixture",
),
(format!("Authorization: Bearer\n {URI}"), URI),
(format!("Use the URI {URI}."), URI),
(
"url = \"secret-token:SYNTHETIC-7F3A-4C2B%2\"".to_owned(),
"secret-token:SYNTHETIC-7F3A-4C2B",
),
] {
let candidates = detect(&input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], value, "{input}");
assert_eq!(candidates[0].type_name(), "bearer_token");
}
for input in [
"token_uri_prefix = \"secret-token:\"",
"secret-token:abc",
"secret-token:placeholder",
"mysecret-token:SYNTHETIC-7F3A-4C2B%20fixture",
"x-secret-token:SYNTHETIC-7F3A-4C2B%20fixture",
] {
assert!(detect(input).is_empty(), "{input}");
}
}
#[test]
fn an_explicit_authorization_header_lowers_the_floor_to_twelve() {
for (input, value) in [
("Authorization: Bearer SYNTH.q8v-N3xR7", "SYNTH.q8v-N3xR7"),
("Proxy-Authorization: Bearer SYNTHq8vN3xR", "SYNTHq8vN3xR"),
(
"curl -H 'authorization: bearer SYNTH.q8v-N3xR7'",
"SYNTH.q8v-N3xR7",
),
] {
let candidates = detect(input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], value, "{input}");
}
for input in [
"Bearer SYNTH.q8v-N3xR7",
"the Bearer SYNTH.q8v-N3xR7 value",
"Authorization: Bearer SYNTHq8vN3x",
"Authorization: Bearer YOUR_TOKEN_HERE",
"reverse_proxy-authorization: Bearer SYNTH.q8v-N3xR7",
] {
assert!(detect(input).is_empty(), "{input}");
}
}
#[test]
fn authorization_header_prefix_is_supported() {
let input = "authorization: Bearer SYNTHETIC_REVOKED_HEADER_VALUE";
let candidates = detect(input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], "SYNTHETIC_REVOKED_HEADER_VALUE");
}
#[test]
fn long_invalid_alphabet_terminates_without_a_finding() {
let input = format!("Bearer {}", "!".repeat(100_000));
assert!(detect(&input).is_empty());
}
#[test]
fn open_bearer_authorization_hint_recognizes_a_pending_header_name() {
for open in [
"authorization",
"Authorization",
"AUTHORIZATION",
"authorization:",
"authorization: ",
"authorization \t: ",
"line one\nauthorization:",
"Proxy-Authorization:",
"proxy-authorization \n",
"x: Proxy-Authorization\n:\n",
] {
assert!(has_open_bearer_authorization(open), "{open:?}");
}
}
#[test]
fn open_bearer_authorization_hint_rejects_resolved_or_unrelated_text() {
for closed in [
"",
"authorization: bearer",
"authorizationx",
"notauthorization",
"x-authorization",
"xproxy-authorization:",
"authorization: Bearer SYNTHETIC_REVOKED_VALUE",
"plain text",
] {
assert!(!has_open_bearer_authorization(closed), "{closed:?}");
}
}
#[test]
fn a_bearer_value_after_a_wider_authorization_header_name_is_a_bare_match() {
for input in [
"X-Authorization: Bearer SYNTHETIC_REVOKED_WIDER_HEADER",
"X-Authorization:\nBearer SYNTHETIC_REVOKED_WIDER_HEADER",
"HTTP_AUTHORIZATION: bearer SYNTHETIC_REVOKED_WIDER_HEADER",
] {
let candidates = detect(input);
let (start, end) = only_range(&candidates);
assert_eq!(
&input[start..end],
"SYNTHETIC_REVOKED_WIDER_HEADER",
"{input:?}"
);
}
assert!(detect("X-Authorization: Bearer SYNTHrevoked7").is_empty());
assert_eq!(detect("Proxy-Authorization: Bearer SYNTHrevoked7").len(), 1);
}
#[test]
fn missing_value_after_scheme_is_ignored() {
assert!(detect("Authorization: Bearer").is_empty());
}
#[test]
fn newline_separator_is_not_accepted() {
assert!(detect("Bearer\nSYNTHETIC_REVOKED_BEARER_NEWLINE_VALUE").is_empty());
}
#[test]
fn case_insensitive_scheme_and_tab_separator_are_accepted() {
let input = "BEARER\tSYNTHETIC_REVOKED_BEARER_TAB_VALUE";
let candidates = detect(input);
assert_eq!(only_range(&candidates), (7, input.len()));
}
#[test]
fn extra_whitespace_and_case_around_the_authorization_header_are_accepted() {
let input = "AUTHORIZATION : BEARER SYNTHETIC_REVOKED_BEARER_CASE_WS_VALUE";
let candidates = detect(input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], "SYNTHETIC_REVOKED_BEARER_CASE_WS_VALUE");
}
#[test]
fn ordinary_prose_usage_of_bearer_is_safe_but_a_long_incidental_word_is_an_accepted_tradeoff() {
assert!(
detect("The bearer of this letter is authorized to collect the package.").is_empty()
);
let input =
"Please note the bearer identification-verification-procedure must be followed.";
let candidates = detect(input);
assert_eq!(
only_range(&candidates),
(23, 23 + "identification-verification-procedure".len())
);
}
#[test]
fn a_mid_value_alphabet_break_or_an_embedded_narrower_provider_shape_still_clears_the_length_floor()
{
const SENDGRID_ID: &str = "SYNTHETIC_REVOKED_0000";
const SENDGRID_SECRET: &str = "SYNTHETIC_REVOKED_SENDGRID_SECRET_000000000";
let left = "SYNTHETIC_REVOKED_AB"; let right = "SYNTHETIC_TAIL_WXYZ"; assert_eq!(left.len(), 20);
assert_eq!(right.len(), 19);
let input = format!("Authorization: Bearer {left}!{right}");
let candidates = detect(&input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], left);
let sendgrid_shaped = format!(
"SG.{SENDGRID_ID}.{}",
&SENDGRID_SECRET[..SENDGRID_SECRET.len() - 1]
);
let input = format!("Authorization: Bearer {sendgrid_shaped}");
let candidates = detect(&input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], sendgrid_shaped);
}
fn joined_values() -> Vec<(String, usize)> {
let hex = |len: usize| -> String {
(0..len)
.map(|i| char::from(b"0123456789abcdef"[(i * 7 + i / 3) % 16]))
.collect()
};
let uuid = "5f0c2a9e-1b3d-4c7e-9a8f-0d6e4b2c1a7f";
vec![
(format!("{uuid}:{}", hex(32)), uuid.len()),
(
format!("convex-self-hosted|01{}", hex(74)),
"convex-self-hosted".len(),
),
(format!("prod:happy-otter-123|01{}", hex(76)), "prod".len()),
(format!("{uuid}:{}==", hex(31)), uuid.len()),
(
"SYNTHID:SYNTHETIC_REVOKED_SECRET_A|SYNTHETIC_TAIL_B".to_owned(),
7,
),
]
}
#[test]
fn a_colon_or_pipe_joined_value_is_selected_whole() {
for (value, _) in joined_values() {
for input in [
format!("Authorization: Bearer {value}"),
format!("Authorization: Bearer {value}\r\n"),
format!("curl -H \"Authorization: Bearer {value}\" https://example.invalid"),
format!("curl -H 'authorization: bearer {value}'"),
format!("{{\"Authorization\": \"Bearer {value}\"}}"),
format!("Bearer {value}"),
format!("use Bearer {value}, then retry"),
] {
let candidates = detect(&input);
assert_eq!(candidates.len(), 1, "{input}");
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], value, "{input}");
}
}
}
#[test]
fn a_short_left_half_no_longer_hides_a_long_joined_value() {
let input = "Bearer dev:SYNTHETIC_REVOKED_TAIL";
let candidates = detect(input);
assert_eq!(only_range(&candidates), (7, input.len()));
assert!(detect("Bearer ab:cd|ef").is_empty());
assert!(detect("Authorization: Bearer ab:cdef:gh").is_empty());
}
#[test]
fn a_dangling_join_url_or_prose_colon_keeps_the_single_run_span() {
for (input, value) in [
(
"Authorization: Bearer SYNTHETIC_REVOKED_VALUE:",
"SYNTHETIC_REVOKED_VALUE",
),
(
"Authorization: Bearer SYNTHETIC_REVOKED_VALUE: see docs",
"SYNTHETIC_REVOKED_VALUE",
),
(
"| Bearer SYNTHETIC_REVOKED_VALUE | header |",
"SYNTHETIC_REVOKED_VALUE",
),
(
"Authorization: Bearer SYNTHETIC_REVOKED_VALUE|",
"SYNTHETIC_REVOKED_VALUE",
),
(
"Bearer SYNTHETIC_REVOKED_VALUE://example.invalid/x",
"SYNTHETIC_REVOKED_VALUE",
),
] {
let candidates = detect(input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], value, "{input}");
}
assert!(detect("an OAuth bearer https://example.invalid/rfc6750").is_empty());
assert!(detect("The bearer of: this letter").is_empty());
assert!(detect("The bearer 10:30 train").is_empty());
}
#[test]
fn a_join_never_absorbs_the_next_field_of_a_delimited_record() {
const TOKEN: &str = "SYNTHETIC_REVOKED_BEARER_VALUE";
for tail in [
"|email=fixture@example.test",
"|x=1",
"|x==1",
"|note=\"see docs\"",
"|v=${NEXT}",
":scope=read",
] {
for input in [
format!("Authorization: Bearer {TOKEN}{tail}"),
format!("level=info auth=\"Bearer {TOKEN}{tail}\""),
] {
let candidates = detect(&input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], TOKEN, "{input}");
}
}
for value in [
"SYNTHETICq8vN3xR7tLm2Qw==",
"SYNTHETICq8vN3xR7tLm2Qw=",
"SYNTHID|SYNTHETICq8vN3xR7tLm2Qw==",
"SYNTHETICq8vN3xR7==:SYNTHETICtLm2Qw",
] {
for input in [
format!("Authorization: Bearer {value}"),
format!("{{\"Authorization\": \"Bearer {value}\"}}"),
format!("Authorization: Bearer {value} next"),
] {
let candidates = detect(&input);
assert_eq!(candidates.len(), 1, "{input}");
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], value, "{input}");
}
}
let input = format!("Bearer {TOKEN}=1");
let candidates = detect(&input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], TOKEN);
}
#[test]
fn a_colon_label_after_a_joined_secret_never_drops_the_secret() {
let input = "Authorization: Bearer SYNTHID:SYNTHETIC_REVOKED_SECRET: see docs";
let candidates = detect(input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], "SYNTHID:SYNTHETIC_REVOKED_SECRET");
let input = "Authorization: Bearer SYNTHETIC_REVOKED_BEARER_VALUE|user: alice";
let candidates = detect(input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], "SYNTHETIC_REVOKED_BEARER_VALUE|user");
}
#[test]
fn a_name_at_host_value_is_selected_whole() {
for (input, value) in [
(
"Authorization: Bearer svc-deploy-bot@example.test",
"svc-deploy-bot@example.test",
),
(
"Authorization: Bearer svc-deploy-bot@example.test.",
"svc-deploy-bot@example.test",
),
(
"curl -H 'Authorization: Bearer svc-deploy-bot@ci.example.test' https://x.invalid",
"svc-deploy-bot@ci.example.test",
),
(
"Bearer SYNTHETIC_REVOKED_VALUE@example.test",
"SYNTHETIC_REVOKED_VALUE@example.test",
),
] {
let candidates = detect(input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], value, "{input}");
}
for input in [
"Authorization: Bearer ops@example.test",
"Bearer alice@example.test",
] {
assert!(detect(input).is_empty(), "{input}");
}
let input = "Authorization: Bearer SYNTHETIC_REVOKED_VALUE@ trailing";
let candidates = detect(input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], "SYNTHETIC_REVOKED_VALUE");
}
#[test]
fn a_joined_value_is_excluded_only_when_every_run_is_a_placeholder() {
for input in [
"Authorization: Bearer YOUR_ACCESS_TOKEN:xxxxxxxxxxxx",
"Authorization: Bearer xxxxxxxxxxxxxxxx|0000000000000000",
"Authorization: Bearer PASSWORD_SECRET_EXAMPLE:YOUR_ACCESS_TOKEN",
] {
assert!(detect(input).is_empty(), "{input}");
}
for input in [
"Authorization: Bearer YOUR_ACCESS_TOKEN:SYNTHETIC_REVOKED_SECRET",
"Authorization: Bearer SYNTHETIC_REVOKED_ID|xxxxxxxxxxxxxxxx",
] {
let candidates = detect(input);
assert_eq!(only_range(&candidates), (22, input.len()), "{input}");
}
}
#[test]
fn a_lead_glued_to_a_placeholder_or_reference_is_not_a_partial_span() {
for input in [
"Authorization: Bearer signkey-prod-<YOUR-SIGNING-KEY>",
"Authorization: Bearer signkey-prod-<your-signing-key>",
"Authorization: Bearer prod:happy-otter-123|${CONVEX_BODY}",
"Authorization: Bearer convex-self-hosted|$ADMIN_KEY",
"Authorization: Bearer SYNTHETIC_REVOKED_LEAD_{{ token }}",
"Authorization: Bearer 5e7c0ded-feed-4bad-9ace-0ddba11c0de5:<FAL_KEY_SECRET>",
] {
assert!(detect(input).is_empty(), "{input}");
}
for (input, value) in [
(
"<td>Bearer SYNTHETIC_REVOKED_BEARER_VALUE</td>",
"SYNTHETIC_REVOKED_BEARER_VALUE",
),
(
"Bearer SYNTHETIC_REVOKED_BEARER_VALUE<br>",
"SYNTHETIC_REVOKED_BEARER_VALUE",
),
(
"Bearer SYNTHETIC_REVOKED_BEARER_VALUE<3",
"SYNTHETIC_REVOKED_BEARER_VALUE",
),
(
"Bearer SYNTHETIC_REVOKED_BEARER_VALUE$5",
"SYNTHETIC_REVOKED_BEARER_VALUE",
),
] {
let candidates = detect(input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], value, "{input}");
}
}
#[test]
fn a_long_join_chain_stays_linear() {
let input = format!("Bearer {}", "ab:".repeat(50_000));
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(only_range(&candidates), (7, input.len() - 1));
}
#[test]
fn trailing_padding_equals_are_included() {
let input = "Bearer SYNTHETIC_REVOKED_BEARER_VALUE==";
let candidates = detect(input);
let (start, end) = only_range(&candidates);
assert_eq!(&input[start..end], "SYNTHETIC_REVOKED_BEARER_VALUE==");
}
#[test]
fn repeated_character_filler_values_are_excluded() {
for filler in [
"xxxxxxxxxxxxxxxxxxxx",
"00000000000000000000",
"aaaaaaaaaaaaaaaaaaaa",
"--------------------",
] {
let input = format!("Authorization: Bearer {filler}");
assert!(detect(&input).is_empty(), "{input:?}");
}
}
#[test]
fn a_near_miss_of_repeated_character_filler_is_still_detected() {
assert!(!detect("Bearer xxxxxxxxxxxxxxxxxxxxy").is_empty());
}
#[test]
fn whole_value_placeholder_vocabulary_is_excluded() {
assert!(detect("Authorization: Bearer PASSWORD_SECRET_EXAMPLE").is_empty());
}
#[test]
fn compound_placeholder_forms_are_not_excluded() {
assert!(!detect("Bearer EXAMPLE_TOKEN_VALUE").is_empty());
}
#[test]
fn a_real_looking_value_is_still_detected_at_high_confidence() {
let candidates = detect("Bearer SYNTHETIC_REVOKED_BEARER_VALUE");
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].confidence(), Confidence::High);
}
#[test]
fn instructional_placeholders_of_sixteen_or_more_bytes_are_excluded() {
for value in [
"YOUR_ACCESS_TOKEN",
"YOUR_OAUTH_TOKEN",
"YOUR_SECRET_TOKEN",
"INSERT_ACCESS_TOKEN",
"your_access_token",
"your-oauth-token-here",
"ENTER_YOUR_ACCESS_TOKEN_HERE",
"YOUR_PERSONAL_ACCESS_TOKEN",
"PASTE_API_KEY_HERE",
] {
let input = format!("Authorization: Bearer {value}");
assert!(detect(&input).is_empty(), "{input}");
}
}
#[test]
fn values_that_only_resemble_an_instructional_placeholder_stay_detected() {
for value in [
"YOUR_ACCESS_TOKEN9f2c",
"YOUR_ACCESS_TOKEN_Zx81Qp",
"YOUR_PERSONAL_ACCESS_VALUE",
"TOKEN_YOUR_ACCESS_TOKEN",
"yourAccessTokenAbcdef",
"YOUR_PERSONAL_ACCESS",
"SYNTHETIC_REVOKED_BEARER_VALUE",
] {
let input = format!("Authorization: Bearer {value}");
let candidates = detect(&input);
assert_eq!(only_range(&candidates), (22, input.len()), "{input}");
}
}
}