Skip to content
Closed
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
178 changes: 178 additions & 0 deletions mempalace/pii_guard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
"""
pii_guard.py — Regex-based PII detection and sanitization for MemPalace
=======================================================================

Detects and replaces personally identifiable information (PII) in text
with deterministic placeholder tokens. Supports round-trip: sanitized
text can be restored to original using the mapping.

Zero new dependencies. Uses stdlib `re` only.

Detected PII types:
- Email addresses
- Phone numbers (US formats)
- Social Security Numbers (SSN)
- Credit card numbers (Visa, MC, Amex, Discover)
- IP addresses (IPv4)
- Dates of birth (common formats)

Usage:
from mempalace.pii_guard import PIIGuard

guard = PIIGuard()
sanitized, mapping = guard.sanitize("Email me at alice@example.com")
# sanitized: "Email me at [EMAIL_a1b2c3]"
# mapping: {"[EMAIL_a1b2c3]": "alice@example.com"}

restored = guard.restore(sanitized, mapping)
# restored: "Email me at alice@example.com"
"""

import hashlib
import re
from dataclasses import dataclass, field


@dataclass
class PIIMatch:
"""A single PII detection result."""

pii_type: str
original: str
start: int
end: int
token: str = ""


# Patterns ordered from most specific to least specific to avoid
# partial matches (e.g. a phone number inside an SSN pattern).
PII_PATTERNS = {
"SSN": re.compile(r"\b\d{3}-\d{2}-\d{4}\b"),
"CREDIT_CARD": re.compile(
r"\b(?:4\d{3}|5[1-5]\d{2}|3[47]\d{2}|6(?:011|5\d{2}))"
r"[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{1,4}\b"
),
"EMAIL": re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"),
"PHONE": re.compile(
r"(?<!\d)"
r"(?:\+?1[\s.-]?)?"
r"(?:\(?\d{3}\)?[\s.-]?)"
r"\d{3}[\s.-]?\d{4}"
r"(?!\d)"
),
"IP_ADDRESS": re.compile(
r"\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}"
r"(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b"
),
"DATE_OF_BIRTH": re.compile(
r"\b(?:DOB|dob|Date of Birth|date of birth|born|Born)"
r"[\s:]*"
r"(\d{1,2}[/-]\d{1,2}[/-]\d{2,4}|\d{4}[/-]\d{1,2}[/-]\d{1,2})"
),
}


def _make_token(pii_type: str, value: str) -> str:
"""Generate a deterministic placeholder token for a PII value.

Same input always produces the same token, enabling consistent
replacement across multiple occurrences of the same PII value.
"""
digest = hashlib.sha256(value.encode()).hexdigest()[:6]
return f"[{pii_type}_{digest}]"


@dataclass
class PIIGuard:
"""Regex-based PII sanitizer with round-trip restore capability.

Attributes:
enabled_types: Set of PII type names to detect. Defaults to all.
Valid types: SSN, CREDIT_CARD, EMAIL, PHONE, IP_ADDRESS, DATE_OF_BIRTH
custom_patterns: Additional regex patterns to detect.
Dict of {type_name: compiled_regex}.
"""

enabled_types: set = field(default_factory=lambda: set(PII_PATTERNS.keys()))
custom_patterns: dict = field(default_factory=dict)

def detect(self, text: str) -> list:
"""Scan text for PII. Returns list of PIIMatch objects."""
matches = []
all_patterns = {**PII_PATTERNS, **self.custom_patterns}

for pii_type, pattern in all_patterns.items():
if pii_type not in self.enabled_types and pii_type not in self.custom_patterns:
continue
for m in pattern.finditer(text):
# For DATE_OF_BIRTH, the date itself is in group 1 if present,
# but we replace the entire match including the label
original = m.group(0)
token = _make_token(pii_type, original)
matches.append(
PIIMatch(
pii_type=pii_type,
original=original,
start=m.start(),
end=m.end(),
token=token,
)
)

# Sort by position (descending) so replacements don't shift indices
matches.sort(key=lambda m: m.start, reverse=True)

# Remove overlapping matches (keep the first/longest one found)
filtered = []
used_ranges = []
for match in matches:
overlaps = any(
match.start < ur_end and match.end > ur_start for ur_start, ur_end in used_ranges
)
if not overlaps:
filtered.append(match)
used_ranges.append((match.start, match.end))

return filtered

def sanitize(self, text: str) -> tuple:
"""Replace PII in text with deterministic tokens.

Returns:
(sanitized_text, mapping) where mapping is {token: original_value}
"""
if not text:
return text, {}

matches = self.detect(text)
mapping = {}
result = text

# Matches are already sorted descending by position
for match in matches:
mapping[match.token] = match.original
result = result[: match.start] + match.token + result[match.end :]

return result, mapping

def restore(self, sanitized_text: str, mapping: dict) -> str:
"""Restore sanitized text using the token mapping."""
result = sanitized_text
for token, original in mapping.items():
result = result.replace(token, original)
return result

def has_pii(self, text: str) -> bool:
"""Quick check: does this text contain any detectable PII?"""
return len(self.detect(text)) > 0

def summary(self, text: str) -> dict:
"""Return a summary of PII found in text, grouped by type."""
matches = self.detect(text)
by_type = {}
for m in matches:
by_type.setdefault(m.pii_type, []).append(m.original)
return {
"total": len(matches),
"by_type": by_type,
}
214 changes: 214 additions & 0 deletions tests/test_pii_guard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
"""
test_pii_guard.py — Tests for PII detection, sanitization, and restoration.

Covers: email, phone, SSN, credit card, IP address, date of birth detection,
sanitize/restore round-trip, overlap handling, custom patterns, and edge cases.
"""

import re

from mempalace.pii_guard import PIIGuard, _make_token


class TestDetection:
def test_detect_email(self):
guard = PIIGuard()
matches = guard.detect("Contact alice@example.com for details.")
assert len(matches) == 1
assert matches[0].pii_type == "EMAIL"
assert matches[0].original == "alice@example.com"

def test_detect_multiple_emails(self):
guard = PIIGuard()
text = "Send to alice@example.com and bob@company.org"
matches = guard.detect(text)
emails = [m for m in matches if m.pii_type == "EMAIL"]
assert len(emails) == 2

def test_detect_phone_formats(self):
guard = PIIGuard()
phones = [
"555-123-4567",
"(555) 123-4567",
"555.123.4567",
"5551234567",
"+1 555-123-4567",
"1-555-123-4567",
]
for phone in phones:
matches = guard.detect(f"Call me at {phone} please.")
phone_matches = [m for m in matches if m.pii_type == "PHONE"]
assert len(phone_matches) >= 1, f"Failed to detect phone: {phone}"

def test_detect_ssn(self):
guard = PIIGuard()
matches = guard.detect("SSN: 123-45-6789")
ssn = [m for m in matches if m.pii_type == "SSN"]
assert len(ssn) == 1
assert ssn[0].original == "123-45-6789"

def test_detect_credit_card(self):
guard = PIIGuard()
cards = [
"4111 1111 1111 1111", # Visa
"5500-0000-0000-0004", # Mastercard
"6011 1111 1111 1117", # Discover
]
for card in cards:
matches = guard.detect(f"Card: {card}")
cc = [m for m in matches if m.pii_type == "CREDIT_CARD"]
assert len(cc) >= 1, f"Failed to detect card: {card}"

def test_detect_ip_address(self):
guard = PIIGuard()
matches = guard.detect("Server at 192.168.1.100 is down.")
ips = [m for m in matches if m.pii_type == "IP_ADDRESS"]
assert len(ips) == 1
assert ips[0].original == "192.168.1.100"

def test_detect_date_of_birth(self):
guard = PIIGuard()
texts = [
"DOB: 03/15/1990",
"Date of Birth: 1990-03-15",
"born 03/15/1990",
]
for text in texts:
matches = guard.detect(text)
dob = [m for m in matches if m.pii_type == "DATE_OF_BIRTH"]
assert len(dob) >= 1, f"Failed to detect DOB in: {text}"

def test_no_false_positives_on_clean_text(self):
guard = PIIGuard()
clean_texts = [
"The weather is nice today.",
"Let's discuss the project timeline.",
"Python 3.12 was released recently.",
"The meeting is at 2pm.",
]
for text in clean_texts:
assert not guard.has_pii(text), f"False positive in: {text}"


class TestSanitize:
def test_sanitize_single_email(self):
guard = PIIGuard()
sanitized, mapping = guard.sanitize("Email: alice@example.com")
assert "alice@example.com" not in sanitized
assert "[EMAIL_" in sanitized
assert len(mapping) == 1

def test_sanitize_multiple_types(self):
guard = PIIGuard()
text = "Email alice@test.com, SSN 123-45-6789, IP 10.0.0.1"
sanitized, mapping = guard.sanitize(text)
assert "alice@test.com" not in sanitized
assert "123-45-6789" not in sanitized
assert "10.0.0.1" not in sanitized
assert len(mapping) == 3

def test_sanitize_empty_string(self):
guard = PIIGuard()
sanitized, mapping = guard.sanitize("")
assert sanitized == ""
assert mapping == {}

def test_sanitize_no_pii(self):
guard = PIIGuard()
text = "Just a normal sentence."
sanitized, mapping = guard.sanitize(text)
assert sanitized == text
assert mapping == {}

def test_deterministic_tokens(self):
"""Same PII value always produces the same token."""
guard = PIIGuard()
_, map1 = guard.sanitize("Email: alice@example.com")
_, map2 = guard.sanitize("Contact alice@example.com")
tokens1 = set(map1.keys())
tokens2 = set(map2.keys())
assert tokens1 == tokens2


class TestRestore:
def test_round_trip(self):
guard = PIIGuard()
original = "Email alice@test.com, call 555-123-4567, SSN 123-45-6789"
sanitized, mapping = guard.sanitize(original)
restored = guard.restore(sanitized, mapping)
assert restored == original

def test_round_trip_multiple_same_type(self):
guard = PIIGuard()
original = "Contact alice@a.com or bob@b.com"
sanitized, mapping = guard.sanitize(original)
restored = guard.restore(sanitized, mapping)
assert restored == original

def test_restore_with_empty_mapping(self):
guard = PIIGuard()
text = "No PII here."
restored = guard.restore(text, {})
assert restored == text


class TestConfiguration:
def test_enabled_types_filter(self):
"""Only detect specified PII types."""
guard = PIIGuard(enabled_types={"EMAIL"})
text = "Email alice@test.com, SSN 123-45-6789"
matches = guard.detect(text)
types = {m.pii_type for m in matches}
assert "EMAIL" in types
assert "SSN" not in types

def test_custom_patterns(self):
"""User-supplied patterns should work alongside built-in ones."""
guard = PIIGuard(
custom_patterns={
"EMPLOYEE_ID": re.compile(r"\bEMP-\d{6}\b"),
}
)
text = "Employee EMP-123456 emailed alice@test.com"
matches = guard.detect(text)
types = {m.pii_type for m in matches}
assert "EMPLOYEE_ID" in types
assert "EMAIL" in types


class TestEdgeCases:
def test_overlapping_matches_handled(self):
"""Overlapping PII detections should not cause double-replacement."""
guard = PIIGuard()
# An SSN could partially overlap with phone patterns;
# verify we don't corrupt the text
text = "SSN: 123-45-6789"
sanitized, mapping = guard.sanitize(text)
restored = guard.restore(sanitized, mapping)
assert restored == text

def test_pii_at_start_and_end(self):
guard = PIIGuard()
text = "alice@test.com is the contact, reach out to bob@test.com"
sanitized, mapping = guard.sanitize(text)
restored = guard.restore(sanitized, mapping)
assert restored == text

def test_summary(self):
guard = PIIGuard()
text = "Email alice@a.com, bob@b.com. SSN 123-45-6789."
summary = guard.summary(text)
assert summary["total"] == 3
assert "EMAIL" in summary["by_type"]
assert len(summary["by_type"]["EMAIL"]) == 2

def test_has_pii(self):
guard = PIIGuard()
assert guard.has_pii("Email: alice@test.com")
assert not guard.has_pii("No PII here")

def test_token_format(self):
token = _make_token("EMAIL", "alice@test.com")
assert token.startswith("[EMAIL_")
assert token.endswith("]")
assert len(token) == len("[EMAIL_") + 6 + 1 # type + _ + 6 hex + ]