Skip to content

Commit c9db199

Browse files
committed
* BanSubstrings scanner to escape special characters
1 parent fb795f9 commit c9db199

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

docs/changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
-
1212

1313
### Fixed
14-
-
14+
- `BanSubstrings` scanner to handle substrings with special characters.
1515

1616
### Changed
1717
-

llm_guard/input_scanners/ban_substrings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def match(self, text: str, substring: str) -> bool:
2626
return substring in text
2727

2828
if self == MatchType.WORD:
29-
return re.search(r"\b" + substring + r"\b", text) is not None
29+
return re.search(r"\b" + re.escape(substring) + r"\b", text) is not None
3030

3131

3232
class BanSubstrings(Scanner):

tests/input_scanners/test_ban_substrings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
False,
2424
1.0,
2525
),
26+
(
27+
"My special characters are: !@#$%^&*()_+",
28+
False,
29+
"My special characters are: !@#$%^&*()_+",
30+
True,
31+
0.0,
32+
)
2633
],
2734
)
2835
def test_scan(prompt, redact, expected_prompt, expected_valid, expected_score):

0 commit comments

Comments
 (0)