Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ fn base32(byte: u8) -> bool {
byte.is_ascii_uppercase() || matches!(byte, b'2'..=b'7')
}

fn has_azure_storage_key(text: &str) -> bool {
const PREFIX: &str = "AccountKey=";
const KEY_LENGTH: usize = 88;

text.match_indices(PREFIX).any(|(start, prefix)| {
text.get(start + prefix.len()..)
.and_then(|suffix| suffix.as_bytes().get(..KEY_LENGTH))
.is_some_and(|key| {
key[..86]
.iter()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'+' | b'/'))
&& &key[86..] == b"=="
})
})
}

fn has_token(text: &str, prefixes: &[&str], minimum: usize, body: fn(u8) -> bool) -> bool {
prefixes.iter().any(|prefix| {
text.match_indices(*prefix).any(|(start, prefix)| {
Expand All @@ -211,6 +227,30 @@ struct TokenShape {
/// Distinctive credential prefixes. When two rows match, the first names
/// the kind.
const TOKEN_SHAPES: &[TokenShape] = &[
TokenShape {
kind: "a GitLab token",
prefixes: &["glpat-"],
minimum: 20,
body: token_byte,
},
TokenShape {
kind: "an npm access token",
prefixes: &["npm_"],
minimum: 36,
body: token_byte,
},
TokenShape {
kind: "a Hugging Face token",
prefixes: &["hf_"],
minimum: 34,
body: token_byte,
},
TokenShape {
kind: "a PyPI API token",
prefixes: &["pypi-AgEIcHlwaS5vcmc"],
minimum: 50,
body: token_byte,
},
TokenShape {
kind: "an AWS access-key ID",
prefixes: &["AKIA", "ASIA"],
Expand Down Expand Up @@ -283,6 +323,11 @@ fn secret_shape(text: &str) -> Option<&'static str> {
{
return Some("a private-key marker");
}

if has_azure_storage_key(text) {
return Some("an Azure Storage account key");
}

TOKEN_SHAPES
.iter()
.find(|shape| has_token(text, shape.prefixes, shape.minimum, shape.body))
Expand All @@ -296,6 +341,16 @@ mod tests {
#[test]
fn credential_families_refuse_minimum_bodies_but_accept_short_or_invalid_ones() {
let cases = [
(&["glpat-"][..], 20, "Az09_.-", '!', "a GitLab token"),
(&["npm_"][..], 36, "Az09_.-", '!', "an npm access token"),
(&["hf_"][..], 34, "Az09_.-", '!', "a Hugging Face token"),
(
&["pypi-AgEIcHlwaS5vcmc"][..],
50,
"Az09_.-",
'!',
"a PyPI API token",
),
(
&["AKIA", "ASIA"][..],
16,
Expand Down Expand Up @@ -368,6 +423,21 @@ mod tests {
}
}

#[test]
fn azure_storage_keys_require_valid_base64_padding() {
let valid = format!("AccountKey={}==", "A".repeat(86));
assert_eq!(secret_shape(&valid), Some("an Azure Storage account key"));

let middle_padding = format!("AccountKey={}={}A", "A".repeat(85), "A");
assert_eq!(secret_shape(&middle_padding), None);

let missing_padding = format!("AccountKey={}", "A".repeat(88));
assert_eq!(secret_shape(&missing_padding), None);

let short = format!("AccountKey={}==", "A".repeat(85));
assert_eq!(secret_shape(&short), None);
}

#[test]
fn private_key_markers_refuse_both_edges_and_suffixes_but_accept_certificates() {
for edge in ["BEGIN", "END"] {
Expand Down