-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue #2: tests and fix for sanitize
- Loading branch information
Showing
5 changed files
with
38 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,6 @@ | ||
import re | ||
|
||
|
||
def sanitize(input, pattern): | ||
"""Mitigates log injection risks.""" | ||
return re.sub(pattern, "", input) |
This file contains 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,20 @@ | ||
import unittest | ||
from app.utils import sanitize | ||
|
||
|
||
class TestSanitize(unittest.TestCase): | ||
def setUp(self): | ||
self.invalid_chars = ["\n", "\r", "\t", "<", ">", "%s", ";", "/", "(", ")", "\u202e", "\x00"] | ||
self.base_string = "Hello{}World" | ||
self.pattern = "[^\w \d\"#\$%&'\(\)\*\+,-\.\/:;?@\^_`{\|}~]+|\%\w+|;|/|\(|\)" | ||
|
||
def test_sanitize_invalid_characters(self): | ||
for char in self.invalid_chars: | ||
test_string = self.base_string.format(char) | ||
with self.subTest(char=char): | ||
sanitized = sanitize(test_string, self.pattern) | ||
self.assertNotIn(char, sanitized, f"Invalid character '{char}' was not removed") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |