use crate::detectors::pattern;
use crate::detectors::prefilter::Literals;
use crate::error::DetectorFailure;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const LONG_TERM_PREFIX: &str = "ABSK";
const SHORT_TERM_PREFIX: &str = "bedrock-api-key-";
const LONG_TERM_BODY_MIN: usize = 109;
const LONG_TERM_BODY_MAX: usize = 269;
const SHORT_TERM_HEAD: &str = "YmVkcm9jay5hbWF6b25hd3MuY29tLz9BY3Rpb249Q2FsbFdpdGhCZWFyZXJUb2tlbiZYLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsP";
const SHORT_TERM_TAIL_MIN: usize = 64;
const CONSOLE_ALIAS_HEAD: &str = "QmVkcm9ja0FQSUtleS";
const LONG_TERM_SIGNALS: [&str; 2] = ["aws-bedrock-long-term-prefix", "base64-body"];
const LONG_TERM_CONSOLE_SIGNALS: [&str; 3] = [
"aws-bedrock-long-term-prefix",
"base64-body",
"console-alias-head",
];
const SHORT_TERM_SIGNALS: [&str; 3] = [
"aws-bedrock-short-term-prefix",
"fixed-presigned-url-head",
"base64-body",
];
fn is_left_glue(byte: u8) -> bool {
pattern::is_base64_body(byte) || byte == b'_' || byte == b'-'
}
fn is_right_glue(byte: u8) -> bool {
is_left_glue(byte) || byte == b'='
}
struct Match {
start: usize,
end: usize,
body_start: usize,
}
fn base64_run(bytes: &[u8], from: usize) -> usize {
bytes[from..]
.iter()
.take_while(|&&byte| pattern::is_base64_body(byte))
.count()
}
fn finish(bytes: &[u8], mut end: usize) -> Option<usize> {
let mut padding = 0;
while padding < 2 && bytes.get(end) == Some(&b'=') {
end += 1;
padding += 1;
}
match bytes.get(end) {
Some(&byte) if is_right_glue(byte) => None,
_ => Some(end),
}
}
fn scan(input: &str, prefix: &str, head: &str, min_tail: usize, max_tail: usize) -> Vec<Match> {
let bytes = input.as_bytes();
let prefix = prefix.as_bytes();
let mut matches = Vec::new();
let mut at = 0;
while at < bytes.len() {
let Some(offset) = bytes[at..].iter().position(|&byte| byte == prefix[0]) else {
break;
};
let start = at + offset;
at = start + 1;
if !bytes[start..].starts_with(prefix) {
continue;
}
if start > 0 && is_left_glue(bytes[start - 1]) {
continue;
}
let body_start = start + prefix.len();
let run = base64_run(bytes, body_start);
if run < head.len() + min_tail
|| run > head.len() + max_tail
|| !bytes[body_start..].starts_with(head.as_bytes())
{
continue;
}
let Some(end) = finish(bytes, body_start + run) else {
continue;
};
matches.push(Match {
start,
end,
body_start,
});
at = end;
}
matches
}
fn candidates(
type_name: &'static str,
matches: Vec<Match>,
signals: impl Fn(&Match) -> &'static [&'static str],
) -> Vec<Candidate> {
matches
.into_iter()
.filter_map(|found| {
let range = ByteRange::new(found.start, found.end)?;
Some(
Candidate::built_in(type_name, Confidence::High, range)
.with_specificity(Specificity::Provider)
.with_signals(signals(&found).iter().copied()),
)
})
.collect()
}
pub(super) struct AwsBedrockLongTermApiKeyDetector;
impl Detector for AwsBedrockLongTermApiKeyDetector {
fn id(&self) -> &'static str {
"aws-bedrock-long-term-api-key"
}
fn detect(
&self,
input: &str,
_context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure> {
let matches = scan(
input,
LONG_TERM_PREFIX,
"",
LONG_TERM_BODY_MIN,
LONG_TERM_BODY_MAX,
);
Ok(candidates(
"aws_bedrock_long_term_api_key",
matches,
|found| {
if input[found.body_start..].starts_with(CONSOLE_ALIAS_HEAD) {
&LONG_TERM_CONSOLE_SIGNALS
} else {
&LONG_TERM_SIGNALS
}
},
))
}
}
pub(super) struct AwsBedrockShortTermApiKeyDetector;
impl Detector for AwsBedrockShortTermApiKeyDetector {
fn id(&self) -> &'static str {
"aws-bedrock-short-term-api-key"
}
fn detect(
&self,
input: &str,
_context: &DetectorContext,
) -> Result<Vec<Candidate>, DetectorFailure> {
let matches = scan(
input,
SHORT_TERM_PREFIX,
SHORT_TERM_HEAD,
SHORT_TERM_TAIL_MIN,
usize::MAX - SHORT_TERM_HEAD.len(),
);
Ok(candidates(
"aws_bedrock_short_term_api_key",
matches,
|_| &SHORT_TERM_SIGNALS,
))
}
}
pub(super) const LONG_TERM_REQUIRED_LITERALS: &[Literals] = &[Literals::Strs(&[LONG_TERM_PREFIX])];
pub(super) const SHORT_TERM_REQUIRED_LITERALS: &[Literals] =
&[Literals::Strs(&[SHORT_TERM_PREFIX])];
#[cfg(test)]
mod tests {
use super::*;
const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
fn base64(data: &[u8]) -> String {
let mut out = String::new();
for chunk in data.chunks(3) {
let b = [
chunk[0],
*chunk.get(1).unwrap_or(&0),
*chunk.get(2).unwrap_or(&0),
];
let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]);
for i in 0..4 {
if i <= chunk.len() {
out.push(char::from(ALPHABET[((n >> (18 - 6 * i)) & 63) as usize]));
} else {
out.push('=');
}
}
}
out
}
fn filler(len: usize, seed: usize) -> String {
(0..len)
.map(|i| char::from(ALPHABET[(i * 7 + seed * 13 + i / 5) % 64]))
.collect()
}
fn long_key(body_len: usize) -> String {
format!("{LONG_TERM_PREFIX}{}", filler(body_len, 1))
}
fn short_key(tail_len: usize) -> String {
format!(
"{SHORT_TERM_PREFIX}{SHORT_TERM_HEAD}{}",
filler(tail_len, 2)
)
}
fn detect_long(input: &str) -> Vec<Candidate> {
AwsBedrockLongTermApiKeyDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn detect_short(input: &str) -> Vec<Candidate> {
AwsBedrockShortTermApiKeyDetector
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn assert_one(found: &[Candidate], type_name: &str, input: &str, key: &str) {
assert_eq!(found.len(), 1, "{input}");
assert_eq!(found[0].type_name(), type_name);
assert_eq!(found[0].confidence(), Confidence::High);
assert_eq!(found[0].effective_specificity(), Specificity::Provider);
let start = input.find(key).unwrap();
assert_eq!(
found[0].range(),
ByteRange::new(start, start + key.len()).unwrap()
);
}
#[test]
fn short_term_head_is_the_encoded_presigned_url_head() {
let plain = b"bedrock.amazonaws.com/?Action=CallWithBearerToken\
&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=";
assert_eq!(base64(plain).as_bytes()[..133], *SHORT_TERM_HEAD.as_bytes());
assert_eq!(SHORT_TERM_HEAD.len(), 133);
assert_eq!(&base64(b"BedrockAPIKey-")[..18], CONSOLE_ALIAS_HEAD);
}
#[test]
fn long_term_bare_and_boundary_lengths() {
for len in [LONG_TERM_BODY_MIN, 128, 132, LONG_TERM_BODY_MAX] {
let key = long_key(len);
assert_one(
&detect_long(&key),
"aws_bedrock_long_term_api_key",
&key,
&key,
);
}
}
#[test]
fn long_term_rejects_lengths_outside_the_band() {
assert!(detect_long(&long_key(LONG_TERM_BODY_MIN - 1)).is_empty());
assert!(detect_long(&long_key(LONG_TERM_BODY_MAX + 1)).is_empty());
}
#[test]
fn long_term_includes_padding_in_the_span() {
for pad in ["", "=", "=="] {
let key = format!("{}{pad}", long_key(130));
let input = format!("k={key}\n");
assert_one(
&detect_long(&input),
"aws_bedrock_long_term_api_key",
&input,
&key,
);
}
}
#[test]
fn long_term_contexts_have_exact_spans() {
let key = long_key(128);
let contexts = [
key.clone(),
format!("AWS_BEARER_TOKEN_BEDROCK={key}"),
format!("export AWS_BEARER_TOKEN_BEDROCK={key}"),
format!("export AWS_BEARER_TOKEN_BEDROCK=\"{key}\""),
format!("AWS_BEARER_TOKEN_BEDROCK: {key}"),
format!("AWS_BEARER_TOKEN_BEDROCK: '{key}'"),
format!("{{\"AWS_BEARER_TOKEN_BEDROCK\": \"{key}\"}}"),
format!("Authorization: Bearer {key}"),
format!("curl -H \"Authorization: Bearer {key}\" https://example.invalid/"),
format!("os.environ[\"AWS_BEARER_TOKEN_BEDROCK\"] = \"{key}\""),
format!("client = Client(api_key=\"{key}\")"),
format!("{{\"tool\":\"set_env\",\"arguments\":{{\"value\":\"{key}\"}}}}"),
format!("```\n{key}\n```"),
format!("({key}), [{key}] <{key}>."),
];
for input in contexts {
let found = detect_long(&input);
assert!(!found.is_empty(), "{input}");
for candidate in &found {
let range = candidate.range();
assert_eq!(&input[range.start()..range.end()], key);
}
}
}
#[test]
fn long_term_twins_are_rejected() {
let key = long_key(128);
let body = &key[4..];
assert!(detect_long(&format!("ABSX{body}")).is_empty());
assert!(detect_long(&format!("absk{body}")).is_empty());
assert!(detect_long(&format!("Q{key}")).is_empty());
assert!(detect_long(&format!("-{key}")).is_empty());
assert!(detect_long(&format!("{}Q", long_key(LONG_TERM_BODY_MAX))).is_empty());
assert!(detect_long(&format!("{key}_x")).is_empty());
let mut url_safe = key.clone();
url_safe.replace_range(60..61, "-");
assert!(detect_long(&url_safe).is_empty());
url_safe.replace_range(60..61, "_");
assert!(detect_long(&url_safe).is_empty());
let mut mid = key.clone();
mid.replace_range(60..61, "=");
assert!(detect_long(&mid).is_empty());
assert!(detect_long(&format!("{key}===")).is_empty());
}
#[test]
fn long_term_placeholders_and_lookalikes_are_not_reported() {
assert!(detect_long("ABSK...").is_empty());
assert!(detect_long("AWS_BEARER_TOKEN_BEDROCK=ABSK<your-key>").is_empty());
assert!(detect_long("AWS_BEARER_TOKEN_BEDROCK=").is_empty());
assert!(detect_long("AWS_BEARER_TOKEN_BEDROCK=${AWS_BEARER_TOKEN_BEDROCK}").is_empty());
assert!(detect_long(&format!("Q{}", filler(200, 3))).is_empty());
assert!(detect_long("BedrockAPIKey-abcd-at-000000000000").is_empty());
assert!(detect_long("arn:aws:iam::000000000000:user/BedrockAPIKey-abcd").is_empty());
}
#[test]
fn long_term_console_head_adds_a_signal_only() {
let with_head = format!("{LONG_TERM_PREFIX}{CONSOLE_ALIAS_HEAD}{}", filler(110, 4));
let found = detect_long(&with_head);
assert_eq!(found.len(), 1);
assert!(found[0].signals().iter().any(|s| s == "console-alias-head"));
let without = detect_long(&long_key(128));
assert_eq!(without.len(), 1);
assert!(
!without[0]
.signals()
.iter()
.any(|s| s == "console-alias-head")
);
}
#[test]
fn long_term_two_keys_and_no_akia_collision() {
let key = long_key(128);
let input = format!("{key} {key}");
assert_eq!(detect_long(&input).len(), 2);
let akia = format!("AKIA{}", "SYNTHETICEXAMPLE");
assert!(detect_long(&akia).is_empty());
assert!(detect_short(&akia).is_empty());
let aws = crate::detectors::aws::AwsAccessKeyDetector;
assert!(
aws.detect(&key, &DetectorContext::new(key.len()))
.unwrap()
.is_empty()
);
}
#[test]
fn short_term_detects_across_contexts() {
let key = short_key(400);
let contexts = [
key.clone(),
format!("AWS_BEARER_TOKEN_BEDROCK={key}"),
format!("export AWS_BEARER_TOKEN_BEDROCK={key}"),
format!("AWS_BEARER_TOKEN_BEDROCK: {key}"),
format!("{{\"AWS_BEARER_TOKEN_BEDROCK\": \"{key}\"}}"),
format!("Authorization: Bearer {key}"),
format!("os.environ[\"AWS_BEARER_TOKEN_BEDROCK\"] = \"{key}\""),
format!("{{\"tool\":\"call\",\"arguments\":{{\"api_key\":\"{key}\"}}}}"),
];
for input in contexts {
let found = detect_short(&input);
assert_eq!(found.len(), 1, "{input}");
let range = found[0].range();
assert_eq!(&input[range.start()..range.end()], key);
assert_eq!(found[0].type_name(), "aws_bedrock_short_term_api_key");
}
}
#[test]
fn short_term_padding_and_length_are_unbounded_above() {
for pad in ["", "=", "=="] {
let key = format!("{}{pad}", short_key(1500));
let input = format!("t={key}\n");
assert_one(
&detect_short(&input),
"aws_bedrock_short_term_api_key",
&input,
&key,
);
}
assert_eq!(detect_short(&short_key(SHORT_TERM_TAIL_MIN)).len(), 1);
assert!(detect_short(&short_key(SHORT_TERM_TAIL_MIN - 1)).is_empty());
}
#[test]
fn short_term_twins_are_rejected() {
let tail = filler(400, 2);
for cut in [1, 28, 100, 132] {
let head = &SHORT_TERM_HEAD[..cut];
assert!(detect_short(&format!("{SHORT_TERM_PREFIX}{head}{tail}")).is_empty());
}
let mut bad = SHORT_TERM_HEAD.to_owned();
bad.replace_range(40..41, "Z");
assert!(detect_short(&format!("{SHORT_TERM_PREFIX}{bad}{tail}")).is_empty());
assert!(detect_short(SHORT_TERM_PREFIX).is_empty());
assert!(detect_short(&format!("bedrock-api-token-{SHORT_TERM_HEAD}{tail}")).is_empty());
assert!(detect_short(&format!("Bedrock-Api-Key-{SHORT_TERM_HEAD}{tail}")).is_empty());
assert!(detect_short(&format!("{SHORT_TERM_PREFIX}{SHORT_TERM_HEAD}")).is_empty());
let key = short_key(400);
let mut url_safe = key.clone();
let cut = key.len() - 100;
url_safe.replace_range(cut..=cut, "-");
assert!(detect_short(&url_safe).is_empty());
url_safe.replace_range(cut..=cut, "_");
assert!(detect_short(&url_safe).is_empty());
assert!(detect_short(&format!("{key}===")).is_empty());
assert!(detect_short(&format!("x{key}")).is_empty());
assert!(detect_short(&format!("{key}-more")).is_empty());
}
#[test]
fn short_term_placeholders_are_not_reported() {
assert!(detect_short("bedrock-api-key-YOUR_KEY_HERE").is_empty());
assert!(detect_short("<your-bedrock-api-key>").is_empty());
assert!(detect_short("Authorization: Bearer ${AWS_BEARER_TOKEN_BEDROCK}").is_empty());
}
#[test]
fn the_two_shapes_do_not_claim_each_other() {
assert!(detect_long(&short_key(400)).is_empty());
assert!(detect_short(&long_key(128)).is_empty());
}
#[test]
fn adversarial_repetition_is_linear_and_quiet() {
let dense = "ABSK".repeat(20_000);
assert!(detect_long(&dense).is_empty());
let dense = SHORT_TERM_PREFIX.repeat(5_000);
assert!(detect_short(&dense).is_empty());
}
#[test]
fn multibyte_neighbours_do_not_split_offsets() {
let key = long_key(128);
let input = format!("caf\u{e9}{key}\u{e9}");
assert_one(
&detect_long(&input),
"aws_bedrock_long_term_api_key",
&input,
&key,
);
}
}