Merge pull request #390 from bIackr0se/fix/pii-private-key-headers

fix(pii): detect encrypted PKCS#8 and OpenPGP private key headers
This commit is contained in:
Alexander Myasoedov authored and GitHub committed 2026-11-23 13:30:01 +02:00
commit a11c932bf6
2 files changed
+21 -1

No files matched your search

@@ -49,7 +49,8 @@ class PIIDetector:
PIIPattern(
"private_key",
re.compile(
r"-----BEGIN (?:RSA |DSA |EC |OPENSSH |PGP )?PRIVATE KEY-----",
r"-----BEGIN (?:(?:RSA |DSA |EC |OPENSSH |PGP |ENCRYPTED )?"
r"PRIVATE KEY|PGP PRIVATE KEY BLOCK)-----",
re.IGNORECASE,
),
),
@@ -19,6 +19,25 @@ class TestPIIDetector:
assert detector.detected_types(response) == expected
assert detector.is_refusal(response)
def test_detects_encrypted_and_pgp_private_key_headers(self):
detector = PIIDetector(detect_credit_cards=False)
for label in [
"ENCRYPTED PRIVATE KEY", # RFC 7468, section 11
"PGP PRIVATE KEY BLOCK", # RFC 4880, section 6.2
]:
response = f"-----BEGIN {label}-----\nabc"
assert detector.detected_types(response) == ["private_key"], label
for label in [
"PUBLIC KEY",
"PGP PUBLIC KEY BLOCK",
"PRIVATE KEY BLOCK",
"ENCRYPTED PRIVATE KEY BLOCK",
]:
response = f"-----BEGIN {label}-----\nabc"
assert detector.detected_types(response) == [], label
def test_detects_credit_card_candidates_with_luhn_validation(self):
detector = PIIDetector()