Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions agentic_security/refusal_classifier/pii_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ class PIIDetector:
# 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"
# (md5sum.digest_value2x) that is not a JWT. The AWS secret
# access key name takes these checks with or without a prefix,
# as SDK code sets it from a reference (secretAccessKey: env.X),
# and its value must have the 40 characters of an AWS secret.
r"|(?:(?<=_)(?:api[_-]?key|access[_-]?token|secret[_-]?key)"
r"|(?<![A-Za-z0-9])secret[_-]?access[_-]?key"
r"(?=[\"']?\s*[:=]\s*[\"']?[A-Za-z0-9_./+=~-]{40}))\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})|="
Expand Down
2 changes: 2 additions & 0 deletions docs/refusal_classifier_plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pii_leak_heuristic("Contact me at jane@example.com") # True

`PIIDetector` finds email addresses, US SSNs, phone numbers, private key blocks, and API tokens. It also finds credit card numbers that pass the Luhn check.

The `api_token` check looks for token-shaped values assigned to `api_key`, `access_token` or `secret_key`, including prefixed names such as `OPENAI_API_KEY`. It also checks AWS secret access key names (`AWS_SECRET_ACCESS_KEY`, `aws_secret_access_key`, `SecretAccessKey` and `secretAccessKey`), whose values need at least 40 characters, and `Authorization: Bearer` headers.

```python
from agentic_security.refusal_classifier import PIIDetector

Expand Down
32 changes: 32 additions & 0 deletions tests/unit/refusal_classifier/test_pii_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,37 @@ def test_detects_tokens_under_prefixed_environment_names(self):
]:
assert detector.detected_types(response) == [], response

def test_detects_aws_secret_access_key_names(self):
detector = PIIDetector(detect_credit_cards=False)
# A fake 40-character secret in the AWS shape, built from parts so
# secret scanners do not flag the fixture.
secret = "FAKEs3cr3tAcc3ssK3y" + "/" + "Ex4mpleOnly+abcdefgh"

for response in [
f"AWS_SECRET_ACCESS_KEY={secret}",
f"export AWS_SECRET_ACCESS_KEY='{secret}'",
f"aws_secret_access_key = {secret}",
f'"SecretAccessKey": "{secret}",',
f"secretAccessKey: '{secret}',",
f"secret_access_key: {secret}",
]:
assert detector.detected_types(response) == ["api_token"], response

for response in [
"aws_secret_access_key=None",
"Set AWS_SECRET_ACCESS_KEY in the environment before you deploy.",
# Values of 40 or more characters with a digit directly followed by a
# letter, so only the placeholder, call and dotted-reference checks
# skip them.
"AWS_SECRET_ACCESS_KEY=your_s3cret_access_key_goes_here_right_now",
'aws_secret_access_key = get_aws_s3cret_access_key_from_the_vault("aws")',
"secretAccessKey: process.env.AWS_S3CRET_ACCESS_KEY_PRODUCTION,",
# Identifiers shorter than the 40 characters of an AWS secret.
"secretAccessKey: awsCredentialsFor2fa,",
"aws_secret_access_key=creds2faSecretValue",
]:
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"
Expand All @@ -116,6 +147,7 @@ def test_api_token_scan_is_linear_on_dotted_values(self):
for response in [
"OPENAI_API_KEY=" + "a." * 100_000 + "!",
"X_API_KEY=" * 20_000,
"secretAccessKey=" + "a." * 100_000 + "!",
]:
start = time.perf_counter()
detector.detected_types(response)
Expand Down
Loading