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 CLASSIC_FAMILY_PREFIXES: [(&str, &str); 4] = [
("ghp_", "github_token"),
("gho_", "github_oauth_token"),
("ghu_", "github_app_user_to_server_token"),
("ghr_", "github_app_refresh_token"),
];
const INSTALLATION_PREFIX: &str = "ghs_";
const INSTALLATION_TYPE: &str = "github_app_installation_token";
const FINE_GRAINED_PREFIX: &str = "github_pat_";
const FINE_GRAINED_TYPE: &str = "github_fine_grained_personal_access_token";
const FINE_GRAINED_FIRST_LEN: usize = 22;
const FINE_GRAINED_SECOND_LEN: usize = 59;
pub(super) struct GitHubTokenDetector;
impl Detector for GitHubTokenDetector {
fn id(&self) -> &'static str {
"github-token"
}
fn detect(
&self,
input: &str,
_context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure> {
let mut candidates = Vec::new();
push_classic_family(&mut candidates, input);
push(
&mut candidates,
pattern::scan_prefixed_runs(
input,
&[INSTALLATION_PREFIX],
RunLength::AtLeast(36),
pattern::is_alnum_dash_dot,
pattern::is_alnum_dash_dot,
),
INSTALLATION_TYPE,
);
push(&mut candidates, scan_fine_grained(input), FINE_GRAINED_TYPE);
Ok(candidates)
}
}
fn push_classic_family(candidates: &mut Vec<Candidate>, input: &str) {
let prefixes: Vec<&str> = CLASSIC_FAMILY_PREFIXES.iter().map(|(p, _)| *p).collect();
let ranges = pattern::scan_prefixed_runs(
input,
&prefixes,
RunLength::Exact(36),
pattern::is_alnum,
pattern::is_alnum_underscore,
);
let bytes = input.as_bytes();
for (start, end) in ranges {
let Some(range) = ByteRange::new(start, end) else {
continue;
};
for (prefix, type_name) in CLASSIC_FAMILY_PREFIXES {
if bytes[start..].starts_with(prefix.as_bytes()) {
candidates.push(
Candidate::built_in(type_name, Confidence::High, range)
.with_specificity(Specificity::Provider),
);
break;
}
}
}
}
fn push(candidates: &mut Vec<Candidate>, ranges: Vec<(usize, usize)>, type_name: &'static str) {
for (start, end) in ranges {
let Some(range) = ByteRange::new(start, end) else {
continue;
};
candidates.push(
Candidate::built_in(type_name, Confidence::High, range)
.with_specificity(Specificity::Provider),
);
}
}
fn scan_fine_grained(input: &str) -> Vec<(usize, usize)> {
let bytes = input.as_bytes();
let mut matches = Vec::new();
let mut start = 0;
while start < bytes.len() {
if !bytes[start..].starts_with(FINE_GRAINED_PREFIX.as_bytes()) {
start += 1;
continue;
}
let Some(end) = fine_grained_end(bytes, start + FINE_GRAINED_PREFIX.len()) else {
start += 1;
continue;
};
if pattern::boundary_ok(bytes, start, end, pattern::is_alnum_underscore) {
matches.push((start, end));
}
start = end;
}
matches
}
fn fine_grained_end(bytes: &[u8], first_start: usize) -> Option<usize> {
let separator = first_start + FINE_GRAINED_FIRST_LEN;
if !exact_run(bytes, first_start, FINE_GRAINED_FIRST_LEN) || bytes.get(separator) != Some(&b'_')
{
return None;
}
let second_start = separator + 1;
if !exact_run(bytes, second_start, FINE_GRAINED_SECOND_LEN) {
return None;
}
Some(second_start + FINE_GRAINED_SECOND_LEN)
}
fn exact_run(bytes: &[u8], start: usize, len: usize) -> bool {
bytes
.get(start..start.saturating_add(len))
.is_some_and(|run| run.iter().all(|&byte| pattern::is_alnum(byte)))
}
pub(super) const REQUIRED_LITERALS: &[Literals] = &[
Literals::Prefixes(&CLASSIC_FAMILY_PREFIXES),
Literals::Strs(&[INSTALLATION_PREFIX, FINE_GRAINED_PREFIX]),
];
#[cfg(test)]
mod tests {
use super::*;
fn detect(input: &str) -> Vec<Candidate> {
GitHubTokenDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
#[test]
fn detects_a_classic_token() {
let input = format!("ghp_{}", "S".repeat(36));
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "github_token");
assert_eq!(
candidates[0].range(),
ByteRange::new(0, input.len()).unwrap()
);
}
#[test]
fn detects_a_fine_grained_token() {
let first = "S".repeat(FINE_GRAINED_FIRST_LEN);
let second = "T".repeat(FINE_GRAINED_SECOND_LEN);
let input = format!("github_pat_{first}_{second}");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].type_name(),
"github_fine_grained_personal_access_token"
);
assert_eq!(
candidates[0].range(),
ByteRange::new(0, input.len()).unwrap()
);
}
#[test]
fn detects_a_stateful_installation_token() {
let input = format!("ghs_{}", "S".repeat(36));
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "github_app_installation_token");
}
#[test]
fn selects_the_complete_stateless_installation_token() {
let token =
"ghs_SYNTHETIC_APP_ID.eyJTWU5USEVUSUNfUkVWT0tFRF9IRUFERVI.SYNTHETIC_REVOKED_SIGNATURE";
let input = format!("before {token} after");
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "github_app_installation_token");
let start = input.find(token).unwrap();
assert_eq!(
candidates[0].range(),
ByteRange::new(start, start + token.len()).unwrap()
);
}
#[test]
fn detects_an_oauth_access_token() {
let input = format!("gho_{}", "S".repeat(36));
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "github_oauth_token");
}
#[test]
fn detects_a_user_to_server_token() {
let input = format!("ghu_{}", "S".repeat(36));
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "github_app_user_to_server_token");
}
#[test]
fn detects_a_refresh_token() {
let input = format!("ghr_{}", "S".repeat(36));
let candidates = detect(&input);
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].type_name(), "github_app_refresh_token");
}
#[test]
fn each_classic_family_prefix_keeps_its_own_type_at_a_shared_position() {
let input = format!(
"{} {} {} {}",
format_args!("ghp_{}", "S".repeat(36)),
format_args!("gho_{}", "S".repeat(36)),
format_args!("ghu_{}", "S".repeat(36)),
format_args!("ghr_{}", "S".repeat(36)),
);
let candidates = detect(&input);
let types: Vec<&str> = candidates.iter().map(Candidate::type_name).collect();
assert_eq!(
types,
vec![
"github_token",
"github_oauth_token",
"github_app_user_to_server_token",
"github_app_refresh_token",
]
);
}
#[test]
fn rejects_short_lookalikes() {
assert_eq!(detect("ghp_SYNTHETICSHORT").len(), 0);
assert_eq!(detect("gho_SYNTHETICSHORT").len(), 0);
assert_eq!(detect("ghu_SYNTHETICSHORT").len(), 0);
assert_eq!(detect("ghr_SYNTHETICSHORT").len(), 0);
assert_eq!(detect("ghs_SYNTHETICSHORT").len(), 0);
let short_fine_grained = format!(
"github_pat_{}_{}",
"S".repeat(FINE_GRAINED_FIRST_LEN),
"T".repeat(FINE_GRAINED_SECOND_LEN - 1)
);
assert_eq!(detect(&short_fine_grained).len(), 0);
}
#[test]
fn rejects_an_undocumented_prefix_that_only_resembles_the_classic_family() {
assert_eq!(detect(&format!("ghq_{}", "S".repeat(36))).len(), 0);
}
#[test]
fn rejects_a_fine_grained_token_with_the_wrong_separator() {
let first = "S".repeat(FINE_GRAINED_FIRST_LEN);
let second = "T".repeat(FINE_GRAINED_SECOND_LEN);
let input = format!("github_pat_{first}-{second}");
assert_eq!(detect(&input).len(), 0);
}
#[test]
fn finds_deterministic_findings_across_repeated_calls() {
let input = format!("ghp_{}", "S".repeat(36));
assert_eq!(detect(&input), detect(&input));
}
}