-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add safe configurable sensitive word detection #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| """Tests for sensitive word detection.""" | ||
|
|
||
| from fastapi.testclient import TestClient | ||
|
|
||
| from app.core.config import settings | ||
|
|
||
|
|
||
| class TestSensitiveWords: | ||
| """Tests for SENSITIVE_WORD entity detection and masking.""" | ||
|
|
||
| def test_detect_sensitive_word(self, client: TestClient, monkeypatch): | ||
| """Should detect configured sensitive words.""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "secret,confidential") | ||
| response = client.post("/api/v1/detect", json={"text": "This is a secret document"}) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| sw = [e for e in data["entities"] if e["type"] == "SENSITIVE_WORD"] | ||
| assert len(sw) == 1 | ||
| assert sw[0]["value"] == "secret" | ||
|
|
||
| def test_detect_multiple_sensitive_words(self, client: TestClient, monkeypatch): | ||
| """Should detect multiple different sensitive words.""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "secret,confidential") | ||
| response = client.post("/api/v1/detect", json={"text": "This is secret and confidential"}) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| sw = [e for e in data["entities"] if e["type"] == "SENSITIVE_WORD"] | ||
| assert len(sw) == 2 | ||
| values = {e["value"].lower() for e in sw} | ||
| assert "secret" in values | ||
| assert "confidential" in values | ||
|
|
||
| def test_case_insensitive_detection(self, client: TestClient, monkeypatch): | ||
| """Should detect words regardless of case.""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "secret") | ||
| response = client.post("/api/v1/detect", json={"text": "This is SECRET and Secret"}) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| sw = [e for e in data["entities"] if e["type"] == "SENSITIVE_WORD"] | ||
| assert len(sw) == 2 | ||
|
|
||
| def test_word_boundary_prevents_substring_match(self, client: TestClient, monkeypatch): | ||
| """Should not match sensitive words inside other words.""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "secret") | ||
| response = client.post("/api/v1/detect", json={"text": "This is a secretory gland"}) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| sw = [e for e in data["entities"] if e["type"] == "SENSITIVE_WORD"] | ||
| assert len(sw) == 0 | ||
|
|
||
| def test_empty_config_no_detection(self, client: TestClient, monkeypatch): | ||
| """Should not detect sensitive words when config is empty.""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "") | ||
| response = client.post("/api/v1/detect", json={"text": "This is a secret document"}) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| sw = [e for e in data["entities"] if e["type"] == "SENSITIVE_WORD"] | ||
| assert len(sw) == 0 | ||
|
|
||
| def test_mask_sensitive_word(self, client: TestClient, monkeypatch): | ||
| """Should mask sensitive words with asterisks.""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "secret") | ||
| response = client.post("/api/v1/mask", json={"text": "This is a secret document"}) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| assert "***" in data["text"] | ||
| assert "secret" not in data["text"] | ||
|
|
||
| def test_redact_sensitive_word(self, client: TestClient, monkeypatch): | ||
| """Should redact sensitive words with [REDACTED].""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "secret") | ||
| response = client.post("/api/v1/redact", json={"text": "This is a secret document"}) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| assert "[REDACTED]" in data["text"] | ||
| assert "secret" not in data["text"] | ||
|
|
||
| def test_overlap_existing_entity_wins(self, client: TestClient, monkeypatch): | ||
| """Should prefer EMAIL over SENSITIVE_WORD on overlap.""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "test") | ||
| response = client.post("/api/v1/detect", json={"text": "Contact test@example.com"}) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| types = [e["type"] for e in data["entities"]] | ||
| assert "EMAIL" in types | ||
| assert "SENSITIVE_WORD" not in types | ||
|
|
||
| def test_earlier_sensitive_phrase_does_not_override_email( | ||
| self, client: TestClient, monkeypatch | ||
| ): | ||
| """Should prefer the full EMAIL even when a sensitive phrase starts earlier.""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "Contact test") | ||
| response = client.post("/api/v1/mask", json={"text": "Contact test@example.com"}) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| assert data["text"] == "Contact ***" | ||
| assert "@example.com" not in data["text"] | ||
| types = [entity["type"] for entity in data["entities"]] | ||
| assert "EMAIL" in types | ||
| assert "SENSITIVE_WORD" not in types | ||
|
|
||
| def test_sensitive_term_can_end_with_punctuation(self, client: TestClient, monkeypatch): | ||
| """Should detect configured terms that end with non-word characters.""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "C++") | ||
| response = client.post("/api/v1/detect", json={"text": "Use C++ safely"}) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| sw = [e for e in data["entities"] if e["type"] == "SENSITIVE_WORD"] | ||
| assert len(sw) == 1 | ||
| assert sw[0]["value"] == "C++" | ||
|
|
||
| def test_cyrillic_word_boundary(self, client: TestClient, monkeypatch): | ||
| """Should detect Cyrillic sensitive words.""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "пароль,секрет") | ||
| response = client.post( | ||
| "/api/v1/detect", | ||
| json={"text": "Введите пароль и секрет.", "language": "ru"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| sw = [e for e in data["entities"] if e["type"] == "SENSITIVE_WORD"] | ||
| assert {entity["value"] for entity in sw} == {"пароль", "секрет"} | ||
|
|
||
| def test_sensitive_word_at_text_boundaries(self, client: TestClient, monkeypatch): | ||
| """Should detect words at start and end of text.""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "secret") | ||
| response = client.post("/api/v1/detect", json={"text": "secret is the secret"}) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| sw = [e for e in data["entities"] if e["type"] == "SENSITIVE_WORD"] | ||
| assert len(sw) == 2 | ||
| assert sw[0]["start"] == 0 | ||
|
|
||
| def test_sensitive_word_entity_positions(self, client: TestClient, monkeypatch): | ||
| """Should return correct positions for sensitive words.""" | ||
| monkeypatch.setattr(settings, "sensitive_words", "secret") | ||
| text = "The secret is here" | ||
| response = client.post("/api/v1/detect", json={"text": text}) | ||
|
|
||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| sw = [e for e in data["entities"] if e["type"] == "SENSITIVE_WORD"] | ||
| assert len(sw) == 1 | ||
| assert text[sw[0]["start"] : sw[0]["end"]] == "secret" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a caller requests only
SENSITIVE_WORD, overlap resolution still considers unrequested PII and always gives it precedence. For example, withMASKER_SENSITIVE_WORDS=secret, maskingsecret@example.comwithentities: ["SENSITIVE_WORD"]keeps theEMAILmatch, discards the requested sensitive-word match, and then filters the email out, returning the input unchanged. Filter candidates before resolving overlaps so an unrequested entity cannot suppress an explicitly selected redaction.Useful? React with 👍 / 👎.