fix(pii): detect encrypted PKCS#8 and OpenPGP private key headers

The private_key pattern required PRIVATE KEY----- after an optional
prefix, so the ENCRYPTED PRIVATE KEY label (RFC 7468, section 11) and
the PGP PRIVATE KEY BLOCK armor header (RFC 4880, section 6.2) were
not reported. The PGP alternative could never match real OpenPGP armor.
This commit is contained in:
JR committed 2026-11-22 20:43:46 +01:00
1 parent dbbdf739e3
commit 43e4aa9fa7
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()