Spaces:
Runtime error
Runtime error
GitHub Actions commited on
Commit ·
d91b95f
1
Parent(s): a09abbb
Deploy 644cb98
Browse files- app/security/sanitizer.py +8 -1
- tests/test_redact_pii.py +10 -0
app/security/sanitizer.py
CHANGED
|
@@ -51,6 +51,9 @@ _PII_PATTERNS: tuple[re.Pattern, ...] = (
|
|
| 51 |
re.compile(r"\b(?:\d[ \-]?){13,19}\b"),
|
| 52 |
)
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
def sanitize_input(text: str) -> str:
|
| 56 |
"""
|
|
@@ -83,11 +86,15 @@ def redact_pii(text: str) -> str:
|
|
| 83 |
|
| 84 |
PERSON entities are intentionally not redacted: visitors are expected to
|
| 85 |
name Darshan Chheda in their queries. Redacting that breaks retrieval.
|
|
|
|
| 86 |
"""
|
| 87 |
if not text:
|
| 88 |
return text
|
| 89 |
|
|
|
|
|
|
|
|
|
|
| 90 |
for pattern in _PII_PATTERNS:
|
| 91 |
-
text = pattern.sub(
|
| 92 |
return text
|
| 93 |
|
|
|
|
| 51 |
re.compile(r"\b(?:\d[ \-]?){13,19}\b"),
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# Published on darshanchheda.com and in its llms.txt as the way to reach him.
|
| 55 |
+
_PUBLIC_CONTACTS: frozenset[str] = frozenset({"me@darshanchheda.com"})
|
| 56 |
+
|
| 57 |
|
| 58 |
def sanitize_input(text: str) -> str:
|
| 59 |
"""
|
|
|
|
| 86 |
|
| 87 |
PERSON entities are intentionally not redacted: visitors are expected to
|
| 88 |
name Darshan Chheda in their queries. Redacting that breaks retrieval.
|
| 89 |
+
Nor is the contact address his site publishes, so answers can give it.
|
| 90 |
"""
|
| 91 |
if not text:
|
| 92 |
return text
|
| 93 |
|
| 94 |
+
def _redact(match: re.Match[str]) -> str:
|
| 95 |
+
return match.group(0) if match.group(0).lower() in _PUBLIC_CONTACTS else "[REDACTED]"
|
| 96 |
+
|
| 97 |
for pattern in _PII_PATTERNS:
|
| 98 |
+
text = pattern.sub(_redact, text)
|
| 99 |
return text
|
| 100 |
|
tests/test_redact_pii.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from app.security.sanitizer import redact_pii
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def test_the_published_contact_address_is_kept_and_other_contact_details_are_not() -> None:
|
| 5 |
+
redacted = redact_pii("Email me@darshanchheda.com or Me@DarshanChheda.com, not dchheda111@gmail.com or +44 7818 975908.")
|
| 6 |
+
|
| 7 |
+
assert redacted.startswith("Email me@darshanchheda.com or Me@DarshanChheda.com, not [REDACTED] or ")
|
| 8 |
+
assert "gmail" not in redacted
|
| 9 |
+
assert "7818" not in redacted
|
| 10 |
+
assert "975908" not in redacted
|