From 8982471e479725ab651d33e2b0d25dc5b5cdc41b Mon Sep 17 00:00:00 2001 From: Reema-Csb Date: Thu, 3 Dec 2026 06:11:44 +0000 Subject: [PATCH] fix: detect additional credential types --- src/preflight.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/preflight.rs b/src/preflight.rs index 592ec54..d5c33a5 100644 --- a/src/preflight.rs +++ b/src/preflight.rs @@ -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)| { @@ -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"], @@ -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)) @@ -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, @@ -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"] {