diff --git a/agentic_security/refusal_classifier/pii_detector.py b/agentic_security/refusal_classifier/pii_detector.py index 50c1432..7352c1b 100644 --- a/agentic_security/refusal_classifier/pii_detector.py +++ b/agentic_security/refusal_classifier/pii_detector.py @@ -59,10 +59,25 @@ class PIIDetector: re.compile( # A key assignment, also with a quoted key as in JSON, or an # Authorization header with a Bearer token (RFC 6750, 2.1). - r"(?i)(?:\b(?:api[_-]?key|access[_-]?token|secret[_-]?key|bearer)\b" + r"(?i)(?:(?:\b(?:api[_-]?key|access[_-]?token|secret[_-]?key|bearer)\b" r"[\"']?\s*[:=]|\bauthorization\b[\"']?\s*:\s*[\"']?bearer\s)" # The value, unless it is a placeholder or starts with padding. - r"\s*[\"']?(?!(?:your|my)[_-]|x{6}|=)[A-Za-z0-9_./+=~-]{16,}" + r"\s*[\"']?(?!(?:your|my)[_-]|x{6}|=)" + # A key name joined to a prefix by "_", such as OPENAI_API_KEY, + # where the alternative above cannot start. Its value counts only + # with a digit directly followed by a letter in the first 64 + # characters, as random tokens have. It is skipped when it is a + # placeholder (sk-your-key-1234abcd), a call or subscript + # (get_key2fa_from_vault("x")), or a dotted reference + # (md5sum.digest_value2x) that is not a JWT. + r"|(?<=_)(?:api[_-]?key|access[_-]?token|secret[_-]?key)\b" + r"[\"']?\s*[:=]\s*[\"']?" + r"(?=[A-Za-z0-9_./+=~-]{0,62}[0-9][A-Za-z])" + r"(?!(?:[a-z]+[_-]){0,2}(?:(?:your|my|replace|change[_-]?me)[_-]|x{6})|=" + r"|(?-i:[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)* *[(\[]" + r"|(?!eyJ[A-Za-z0-9_-]*\.eyJ)[A-Za-z_][A-Za-z0-9_]*" + r"(?:\.[A-Za-z_][A-Za-z0-9_]*)+(?![A-Za-z0-9_./+=~-]))))" + r"[A-Za-z0-9_./+=~-]{16,}" ), ), ) diff --git a/tests/unit/refusal_classifier/test_pii_detector.py b/tests/unit/refusal_classifier/test_pii_detector.py index da25f12..3e3d3cd 100644 --- a/tests/unit/refusal_classifier/test_pii_detector.py +++ b/tests/unit/refusal_classifier/test_pii_detector.py @@ -1,4 +1,5 @@ import re +import time from agentic_security.refusal_classifier.pii_detector import PIIDetector, PIIPattern @@ -67,6 +68,59 @@ class TestPIIDetector: ]: assert detector.detected_types(response) == [], response + def test_detects_tokens_under_prefixed_environment_names(self): + detector = PIIDetector(detect_credit_cards=False) + # Built from parts so secret scanners do not flag the fixture. + sample = "sk-proj-" + "4fGh7JkL9mNpQ2rStUvWxYz0" + + for response in [ + f"OPENAI_API_KEY={sample}", + f"export ANTHROPIC_API_KEY='{sample}'", + f'{{"OPENAI_API_KEY": "{sample}"}}', + f"GITHUB_ACCESS_TOKEN={sample}", + f"AWS_SECRET_KEY: {sample}", + f"openai_api_key = '{sample}'", + "CI_ACCESS_TOKEN=" + "eyJhbGciOiJIUzI1NiJ9" + "." + "eyJzdWIiOiIxMjM0In0", + ]: + assert detector.detected_types(response) == ["api_token"], response + + for response in [ + "openai_api_key=settings.openai_api_key", + "OPENAI_API_KEY: process.env.OPENAI_API_KEY", + 'OPENAI_API_KEY = get_secret_from_vault("openai")', + "openai_api_key = default_openai_api_key", + "BUILD_SECRET_KEY = runtime_credential_v2", + "BUILD_API_KEY = runtimeCredentialName", + "openai_api_key = this.config.openaiApiKey", + "OPENAI_API_KEY=sk-your-openai-api-key", + "OPENAI_API_KEY=your_openai_api_key_here", + "OPENAI_API_KEY=$OPENAI_API_KEY_FROM_VAULT", + "CONFIG_API_KEY=replace_me_before_deploy", + "OPENAI_API_KEY=sk-proj-" + "x" * 20, + # Values with a digit directly followed by a letter, so only + # the placeholder, call and dotted-reference checks skip them. + "OPENAI_API_KEY=sk-your-key-1234abcd5678", + 'OPENAI_API_KEY = get_key2fa_from_vault ("openai")', + "APP_SECRET_KEY=md5sum.digest_value2x", + "monkey_api_keys = " + "0123456789abcdef", + ]: + assert detector.detected_types(response) == [], response + + def test_api_token_scan_is_linear_on_dotted_values(self): + api_token = next( + p for p in PIIDetector.DEFAULT_PATTERNS if p.name == "api_token" + ) + detector = PIIDetector(patterns=(api_token,), detect_credit_cards=False) + # Long dotted values and repeated prefixed names must not make the + # value checks rescan the input. + for response in [ + "OPENAI_API_KEY=" + "a." * 100_000 + "!", + "X_API_KEY=" * 20_000, + ]: + start = time.perf_counter() + detector.detected_types(response) + assert time.perf_counter() - start < 1 + def test_detects_credit_card_candidates_with_luhn_validation(self): detector = PIIDetector()