Spaces:
Running
Running
Create pii_masking.py
Browse files- pii_masking.py +46 -0
pii_masking.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from typing import Dict
|
| 3 |
+
|
| 4 |
+
# PII regex patterns
|
| 5 |
+
PII_PATTERNS: Dict[str, str] = {
|
| 6 |
+
"EMAIL": r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+",
|
| 7 |
+
"PHONE": r"\b\d{10}\b",
|
| 8 |
+
"CREDIT_CARD": r"\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b",
|
| 9 |
+
"AADHAAR": r"\b\d{4}\s?\d{4}\s?\d{4}\b",
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def contains_pii_regex(text: str) -> bool:
|
| 14 |
+
"""Return True if any configured PII regex pattern matches the text."""
|
| 15 |
+
for pattern in PII_PATTERNS.values():
|
| 16 |
+
if re.search(pattern, text):
|
| 17 |
+
return True
|
| 18 |
+
return False
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Masking function
|
| 22 |
+
def mask_pii(
|
| 23 |
+
text: str,
|
| 24 |
+
user_tier: str = "free"
|
| 25 |
+
) -> str:
|
| 26 |
+
"""
|
| 27 |
+
Masks PII in the given text based on user tier.
|
| 28 |
+
|
| 29 |
+
free -> full masking
|
| 30 |
+
enterprise -> partial masking (future)
|
| 31 |
+
internal -> no masking (future)
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
if user_tier == "internal":
|
| 35 |
+
return text # no masking
|
| 36 |
+
|
| 37 |
+
masked_text = text
|
| 38 |
+
|
| 39 |
+
for pii_type, pattern in PII_PATTERNS.items():
|
| 40 |
+
masked_text = re.sub(
|
| 41 |
+
pattern,
|
| 42 |
+
f"[MASKED_{pii_type}]",
|
| 43 |
+
masked_text
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
return masked_text
|