use crate::detectors::additional_providers::KnownFormatProviderDetector;
use crate::detectors::pattern::{self, Alphabet, PrefixShape};
const PREFIX: &str = "dapi";
const BODY_LEN: usize = 32;
const MAX_ROTATION_DIGITS: usize = 3;
fn is_hex_or_dash(byte: u8) -> bool {
pattern::is_hex(byte) || byte == b'-'
}
const BODY_ALPHABET: Alphabet = is_hex_or_dash;
const SIGNALS: [&str; 2] = [
"databricks-documented-prefix",
"hex-exact-length-or-rotation-suffix",
];
fn databricks_body_shape(bytes: &[u8], start: usize, end: usize) -> bool {
let body = &bytes[start + PREFIX.len()..end];
if body.len() < BODY_LEN || !body[..BODY_LEN].iter().copied().all(pattern::is_hex) {
return false;
}
let suffix = &body[BODY_LEN..];
suffix.is_empty()
|| (suffix[0] == b'-'
&& (2..=MAX_ROTATION_DIGITS + 1).contains(&suffix.len())
&& suffix[1..].iter().all(u8::is_ascii_digit))
}
pub(super) const DATABRICKS: KnownFormatProviderDetector = KnownFormatProviderDetector::new(
"databricks-personal-access-token",
"databricks_personal_access_token",
&[
PrefixShape::at_least(PREFIX, BODY_LEN, BODY_ALPHABET, &SIGNALS)
.with_post_check(databricks_body_shape),
],
pattern::is_alnum_dash,
);
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const BODY: &str = "0123456789abcdef0123456789abcdef";
const _: () = assert!(BODY.len() == BODY_LEN);
fn detect(input: &str) -> Vec<Candidate> {
DATABRICKS
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn bare_token() -> String {
format!("{PREFIX}{BODY}")
}
fn rotated_token() -> String {
format!("{PREFIX}{BODY}-2")
}
#[test]
fn detects_a_bare_token_with_provider_specificity() {
let value = bare_token();
let candidates = detect(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].type_name(),
"databricks_personal_access_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_a_rotated_token_with_the_same_specificity() {
let value = rotated_token();
let candidates = detect(&value);
assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].type_name(),
"databricks_personal_access_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_a_body_one_byte_short_of_the_documented_length() {
let short_body = &BODY[..BODY_LEN - 1];
assert!(detect(&format!("{PREFIX}{short_body}")).is_empty());
}
#[test]
fn rejects_a_body_one_byte_longer_than_the_documented_length_with_no_rotation() {
assert!(detect(&format!("{PREFIX}{BODY}a")).is_empty());
}
#[test]
fn accepts_uppercase_hex_and_rejects_non_hex_bytes_inside_the_body() {
for byte in ['A', 'F'] {
let mut body = BODY.to_string();
body.replace_range(4..5, &byte.to_string());
let value = format!("{PREFIX}{body}");
assert_eq!(detect(&value).len(), 1, "{byte}");
}
let upper = format!("{PREFIX}{}", BODY.to_ascii_uppercase());
assert_eq!(detect(&upper).len(), 1);
for byte in ['g', 'Z'] {
let mut body = BODY.to_string();
body.replace_range(4..5, &byte.to_string());
assert!(detect(&format!("{PREFIX}{body}")).is_empty(), "{byte}");
}
}
#[test]
fn accepts_a_rotation_suffix_of_up_to_three_digits() {
for suffix in ["-22", "-123"] {
let value = format!("{PREFIX}{BODY}{suffix}");
let candidates = detect(&value);
assert_eq!(candidates.len(), 1, "{suffix}");
assert_eq!(
candidates[0].range(),
ByteRange::new(0, value.len()).unwrap()
);
}
}
#[test]
fn rejects_a_rotation_suffix_with_four_digits() {
assert!(detect(&format!("{PREFIX}{BODY}-1234")).is_empty());
}
#[test]
fn rejects_a_rotation_suffix_with_a_non_digit_after_the_dash() {
for suffix in ["-a", "-A", "-#"] {
assert!(
detect(&format!("{PREFIX}{BODY}{suffix}")).is_empty(),
"{suffix}"
);
}
}
#[test]
fn rejects_a_bare_trailing_dash_with_nothing_after_it() {
assert!(detect(&format!("{PREFIX}{BODY}-")).is_empty());
}
#[test]
fn rejects_undocumented_near_miss_prefixes() {
for input in [
format!("dap{BODY}"),
format!("dapii{BODY}"),
format!("Dapi{BODY}"),
format!("dap1{BODY}"),
format!("dapi_{BODY}"),
] {
assert!(detect(&input).is_empty(), "{input}");
}
}
#[test]
fn rejects_the_prefix_embedded_in_a_wider_identifier() {
assert!(detect(&format!("legacy{}", bare_token())).is_empty());
assert!(detect(&format!("{}_backup", bare_token())).is_empty());
assert!(detect(&format!("{}-1extra", rotated_token())).is_empty());
}
#[test]
fn rejects_a_masked_or_doc_style_placeholder_value() {
assert!(detect(&format!("{PREFIX}{}", "*".repeat(BODY_LEN))).is_empty());
assert!(detect(&format!("{PREFIX}${{DATABRICKS_TOKEN}}")).is_empty());
}
#[test]
fn rejects_benign_databricks_context_with_no_token_present() {
for input in [
"adb-1234567890123456.7.azuredatabricks.net",
"host = https://my-workspace.cloud.databricks.com",
"cluster_id: 0921-150000-abcd1234",
] {
assert!(detect(input).is_empty(), "{input}");
}
}
#[test]
fn accepts_punctuation_and_assignment_boundaries() {
for value in [bare_token(), rotated_token()] {
for (input, start) in [
(format!("({value})."), 1),
(format!("\"{value}\""), 1),
(
format!("DATABRICKS_TOKEN={value}"),
"DATABRICKS_TOKEN=".len(),
),
] {
let candidates = detect(&input);
assert_eq!(candidates.len(), 1, "{input}");
assert_eq!(
candidates[0].range(),
ByteRange::new(start, start + value.len()).unwrap(),
"{input}"
);
}
}
}
#[test]
fn reports_a_repeated_identical_value_once_per_occurrence() {
for value in [bare_token(), rotated_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 [bare_token(), rotated_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 finds_deterministic_findings_across_repeated_calls() {
let value = bare_token();
assert_eq!(detect(&value), detect(&value));
}
#[test]
fn stays_bounded_over_a_long_run_of_near_miss_prefixes() {
let short_body = &BODY[..BODY_LEN - 1];
let input = format!("{PREFIX}{short_body} ").repeat(10_000);
assert_eq!(detect(&input).len(), 0);
}
}