use crate::detectors::pattern::{self, RunCursor, RunLength, is_alnum_underscore};
use crate::detectors::prefilter::Literals;
use crate::error::DetectorFailure;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
fn is_lower_hex(byte: u8) -> bool {
byte.is_ascii_digit() || byte.is_ascii_lowercase() && byte <= b'f'
}
fn is_base64_std(byte: u8) -> bool {
byte.is_ascii_alphanumeric() || byte == b'+' || byte == b'/'
}
const USER_PREFIX: &str = "sntryu_";
const USER_SECRET_LEN: usize = 64;
const ORG_PREFIX: &str = "sntrys_";
const ORG_JSON_MARKER: &str = "eyJ";
const ORG_MIN_PAYLOAD_LEN: usize = 26;
const ORG_MAX_PADDING_LEN: usize = 2;
const ORG_SIGNATURE_LEN: usize = 43;
pub(super) struct SentryUserAuthTokenDetector;
impl Detector for SentryUserAuthTokenDetector {
fn id(&self) -> &'static str {
"sentry-user-auth-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,
&[USER_PREFIX],
RunLength::Exact(USER_SECRET_LEN),
is_lower_hex,
is_alnum_underscore,
) {
let Some(range) = ByteRange::new(start, end) else {
continue;
};
candidates.push(
Candidate::built_in("sentry_user_auth_token", Confidence::High, range)
.with_specificity(Specificity::Provider)
.with_signal_pack(crate::types::signal_pack!(
"sentry-documented-prefix",
"exact-length-hex-secret"
)),
);
}
Ok(candidates)
}
}
pub(super) struct SentryOrgAuthTokenDetector;
impl Detector for SentryOrgAuthTokenDetector {
fn id(&self) -> &'static str {
"sentry-org-auth-token"
}
fn detect(
&self,
input: &str,
_context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure> {
let bytes = input.as_bytes();
let Some(first) = pattern::find_literal(bytes, ORG_PREFIX.as_bytes(), 0) else {
return Ok(Vec::new());
};
let mut payload_ends = RunCursor::new(bytes, is_base64_std);
let mut signature_ends = RunCursor::new(bytes, is_base64_std);
let mut candidates = Vec::new();
let mut start = first;
while start < bytes.len() {
if !bytes[start..].starts_with(ORG_PREFIX.as_bytes()) {
start += 1;
continue;
}
let Some(end) = org_match_at(bytes, &mut payload_ends, &mut signature_ends, start)
else {
start += 1;
continue;
};
if pattern::boundary_ok(bytes, start, end, is_alnum_underscore)
&& let Some(range) = ByteRange::new(start, end)
{
candidates.push(
Candidate::built_in("sentry_org_auth_token", Confidence::High, range)
.with_specificity(Specificity::Provider)
.with_signal_pack(crate::types::signal_pack!(
"sentry-documented-prefix",
"json-payload-signature-shape"
)),
);
}
start = end.max(start + 1);
}
Ok(candidates)
}
}
fn org_match_at(
bytes: &[u8],
payload_ends: &mut RunCursor<'_>,
signature_ends: &mut RunCursor<'_>,
start: usize,
) -> Option<usize> {
let payload_start = start + ORG_PREFIX.len();
if !bytes[payload_start..].starts_with(ORG_JSON_MARKER.as_bytes()) {
return None;
}
let core_end = payload_ends.end(payload_start);
if core_end - payload_start < ORG_MIN_PAYLOAD_LEN {
return None;
}
let mut separator = core_end;
while separator < bytes.len()
&& bytes[separator] == b'='
&& separator - core_end < ORG_MAX_PADDING_LEN
{
separator += 1;
}
if bytes.get(separator) != Some(&b'_') {
return None;
}
let signature_start = separator + 1;
let signature_end = signature_ends.end(signature_start);
if signature_end - signature_start != ORG_SIGNATURE_LEN {
return None;
}
Some(signature_end)
}
pub(super) const USER_REQUIRED_LITERALS: &[Literals] = &[Literals::Strs(&[USER_PREFIX])];
pub(super) const ORG_REQUIRED_LITERALS: &[Literals] = &[Literals::Strs(&[ORG_PREFIX])];
#[cfg(test)]
mod tests {
use super::*;
fn user_token() -> String {
format!("{USER_PREFIX}{}", "0123456789abcdef".repeat(4))
}
fn org_payload(len: usize) -> String {
assert!(len >= ORG_JSON_MARKER.len());
let filler = "SYNTHETICREVOKEDSENTRYORGPAYLOADFIXTURE0123456789".repeat(4);
format!(
"{ORG_JSON_MARKER}{}",
&filler[..len - ORG_JSON_MARKER.len()]
)
}
fn org_signature() -> String {
let filler = "SigFixSYNTHETICREVOKED0123456789".repeat(2);
filler[..ORG_SIGNATURE_LEN].to_string()
}
fn org_token(payload_len: usize) -> String {
format!(
"{ORG_PREFIX}{}_{}",
org_payload(payload_len),
org_signature()
)
}
fn detect_user(input: &str) -> Vec<Candidate> {
SentryUserAuthTokenDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn detect_org(input: &str) -> Vec<Candidate> {
SentryOrgAuthTokenDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
#[test]
fn fixtures_carry_their_own_declared_lengths() {
assert_eq!(org_signature().len(), ORG_SIGNATURE_LEN);
assert_eq!(org_payload(ORG_MIN_PAYLOAD_LEN).len(), ORG_MIN_PAYLOAD_LEN);
assert_eq!(user_token().len(), USER_PREFIX.len() + USER_SECRET_LEN);
}
#[test]
fn detects_a_user_auth_token_bare() {
let value = user_token();
let candidates = detect_user(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "sentry_user_auth_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_user_auth_token_in_env_json_and_log_contexts() {
let value = user_token();
for input in [
value.clone(),
format!("SENTRY_AUTH_TOKEN={value}"),
format!("{{\"authToken\": \"{value}\"}}"),
format!("2026-09-16T00:00:00Z INFO uploading source maps token={value}"),
] {
let candidates = detect_user(&input);
assert_eq!(candidates.len(), 1, "{input}");
let (start, end) = (candidates[0].range().start(), candidates[0].range().end());
assert_eq!(&input[start..end], value, "{input}");
}
}
#[test]
fn rejects_a_user_secret_one_byte_below_the_exact_length() {
let short = &user_token()[..USER_PREFIX.len() + USER_SECRET_LEN - 1];
assert!(detect_user(short).is_empty());
}
#[test]
fn rejects_a_user_secret_one_byte_above_the_exact_length() {
assert!(detect_user(&format!("{}0", user_token())).is_empty());
}
#[test]
fn rejects_uppercase_hex_in_the_user_secret() {
let value = format!("{USER_PREFIX}{}", "0123456789ABCDEF".repeat(4));
assert!(detect_user(&value).is_empty());
}
#[test]
fn rejects_the_user_token_embedded_in_a_wider_identifier() {
let value = user_token();
assert!(detect_user(&format!("x{value}")).is_empty());
assert!(detect_user(&format!("{value}x")).is_empty());
}
#[test]
fn rejects_a_masked_user_token() {
assert!(detect_user(&format!("{USER_PREFIX}{}", "*".repeat(USER_SECRET_LEN))).is_empty());
}
#[test]
fn rejects_a_user_token_environment_variable_reference() {
assert!(detect_user(&format!("{USER_PREFIX}${{SENTRY_AUTH_TOKEN}}")).is_empty());
}
#[test]
fn finds_the_user_token_across_crlf_and_a_unicode_prefix() {
let value = user_token();
let input = format!("# \u{1F511} caf\u{e9}\r\n{value}\r\n");
let candidates = detect_user(&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 finds_deterministic_user_token_findings_across_repeated_calls() {
let input = user_token();
assert_eq!(detect_user(&input), detect_user(&input));
}
#[test]
fn stays_bounded_over_a_long_run_of_rejected_user_prefixes() {
let input = format!("{}!{}", user_token(), "sntryu_x!".repeat(10_000));
let candidates = detect_user(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, user_token().len()).unwrap()
);
}
#[test]
fn detects_an_org_auth_token_at_the_minimum_payload_length() {
let value = org_token(ORG_MIN_PAYLOAD_LEN);
let candidates = detect_org(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "sentry_org_auth_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_an_org_auth_token_with_a_longer_payload() {
let value = org_token(ORG_MIN_PAYLOAD_LEN + 130);
let candidates = detect_org(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, value.len()).unwrap()
);
}
#[test]
fn detects_an_org_auth_token_with_base64_padding_before_the_separator() {
let value = format!(
"{ORG_PREFIX}{}=_{}",
org_payload(ORG_MIN_PAYLOAD_LEN),
org_signature()
);
let candidates = detect_org(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, value.len()).unwrap()
);
}
#[test]
fn detects_the_org_auth_token_in_env_json_and_log_contexts() {
let value = org_token(ORG_MIN_PAYLOAD_LEN + 20);
for input in [
value.clone(),
format!("SENTRY_AUTH_TOKEN={value}"),
format!("{{\"authToken\": \"{value}\"}}"),
format!("2026-09-16T00:00:00Z INFO release finalize auth={value}"),
] {
let candidates = detect_org(&input);
assert_eq!(candidates.len(), 1, "{input}");
let (start, end) = (candidates[0].range().start(), candidates[0].range().end());
assert_eq!(&input[start..end], value, "{input}");
}
}
#[test]
fn rejects_a_payload_one_byte_below_the_minimum() {
let value = org_token(ORG_MIN_PAYLOAD_LEN - 1);
assert!(detect_org(&value).is_empty());
}
#[test]
fn rejects_a_missing_json_marker() {
let value = format!(
"{ORG_PREFIX}{}_{}",
"A".repeat(ORG_MIN_PAYLOAD_LEN),
org_signature()
);
assert!(detect_org(&value).is_empty());
}
#[test]
fn rejects_a_missing_separator() {
let value = format!(
"{ORG_PREFIX}{}{}",
org_payload(ORG_MIN_PAYLOAD_LEN),
org_signature()
);
assert!(detect_org(&value).is_empty());
}
#[test]
fn rejects_a_signature_one_byte_below_the_exact_length() {
let short_signature = &org_signature()[..ORG_SIGNATURE_LEN - 1];
let value = format!(
"{ORG_PREFIX}{}_{}",
org_payload(ORG_MIN_PAYLOAD_LEN),
short_signature
);
assert!(detect_org(&value).is_empty());
}
#[test]
fn rejects_a_signature_one_byte_above_the_exact_length() {
let value = format!("{}A", org_token(ORG_MIN_PAYLOAD_LEN));
assert!(detect_org(&value).is_empty());
}
#[test]
fn rejects_the_org_token_embedded_in_a_wider_identifier() {
let value = org_token(ORG_MIN_PAYLOAD_LEN);
assert!(detect_org(&format!("x{value}")).is_empty());
assert!(detect_org(&format!("{value}x")).is_empty());
}
#[test]
fn rejects_a_masked_org_token() {
let value = format!(
"{ORG_PREFIX}{}_{}",
org_payload(ORG_MIN_PAYLOAD_LEN),
"*".repeat(ORG_SIGNATURE_LEN)
);
assert!(detect_org(&value).is_empty());
}
#[test]
fn rejects_an_org_token_environment_variable_reference() {
assert!(detect_org(&format!("{ORG_PREFIX}eyJ${{SENTRY_AUTH_TOKEN}}")).is_empty());
}
#[test]
fn finds_the_org_token_across_crlf_and_a_unicode_prefix() {
let value = org_token(ORG_MIN_PAYLOAD_LEN);
let input = format!("# \u{1F511} caf\u{e9}\r\n{value}\r\n");
let candidates = detect_org(&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 finds_deterministic_org_token_findings_across_repeated_calls() {
let input = org_token(ORG_MIN_PAYLOAD_LEN);
assert_eq!(detect_org(&input), detect_org(&input));
}
#[test]
fn stays_bounded_over_a_long_run_of_rejected_org_prefixes() {
let input = format!(
"{}!{}",
org_token(ORG_MIN_PAYLOAD_LEN),
"sntrys_eyJ!".repeat(10_000)
);
let candidates = detect_org(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, org_token(ORG_MIN_PAYLOAD_LEN).len()).unwrap()
);
}
#[test]
fn rejects_the_legacy_unprefixed_hex_token_with_no_context() {
let legacy = "0123456789abcdef".repeat(4);
assert_eq!(legacy.len(), 64);
assert!(detect_user(&legacy).is_empty());
assert!(detect_org(&legacy).is_empty());
}
#[test]
fn rejects_a_public_dsn() {
let dsn = "https://SYNTHETICPUBLICKEY0000000000000000@o000000.ingest.sentry.io/0000000";
assert!(detect_user(dsn).is_empty());
assert!(detect_org(dsn).is_empty());
}
}