Skip to content

Commit 3d13e73

Browse files
committed
Revert "fix: sanitize quotes in malicious branch and bare angle brackets in output"
This reverts commit 6a4006d.
1 parent 6a4006d commit 3d13e73

File tree

3 files changed

+15
-158
lines changed

3 files changed

+15
-158
lines changed

usr/lib/python3/dist-packages/sanitize_string/tests/sanitize_string.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,6 @@ def test_malicious_markup_strings(self) -> None:
9292
sanitize_string_main, self.argv0, pos_args_prefix=["nolimit"]
9393
)
9494

95-
def test_bare_angle_bracket_strings(self) -> None:
96-
"""
97-
Wrapper for _test_bare_angle_bracket_strings (from TestStripMarkup)
98-
specific to TestSanitizeString.
99-
"""
100-
101-
self._test_bare_angle_bracket_strings(
102-
sanitize_string_main, self.argv0, pos_args_prefix=["nolimit"]
103-
)
104-
105-
def test_malicious_markup_quote_strings(self) -> None:
106-
"""
107-
Wrapper for _test_malicious_markup_quote_strings (from
108-
TestStripMarkup) specific to TestSanitizeString.
109-
"""
110-
111-
self._test_malicious_markup_quote_strings(
112-
sanitize_string_main, self.argv0, pos_args_prefix=["nolimit"]
113-
)
114-
11595
def test_simple_escape_cases(self) -> None:
11696
"""
11797
Ensures sanitize_string.py correctly sanitizes escape sequences and
@@ -173,10 +153,10 @@ def test_malicious_cases(self) -> None:
173153
""",
174154
"""\
175155
176-
__blowupWorld() __//__ Won_t blow up world, because it_s commented :) \
156+
__blowupWorld() __//__ Won't blow up world, because it's commented :) \
177157
_[8mor not!_[0m
178158
179-
There really isn_t bold text below, I promise!
159+
There really isn't bold text below, I promise!
180160
_b_Not bold!_/b_
181161
[8mThis text might become invisible.[0m
182162

usr/lib/python3/dist-packages/strip_markup/strip_markup_lib.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,19 @@ def strip_markup(untrusted_string: str) -> str:
5555
markup_stripper = StripMarkupEngine()
5656
markup_stripper.feed(strip_one_string)
5757
strip_two_string: str = markup_stripper.get_data()
58-
if strip_one_string != strip_two_string:
59-
## If we get this far, the second strip attempt further transformed
60-
## the text, indicating an attempt to maliciously circumvent the
61-
## stripper. Sanitize the malicious text by changing all '<', '>',
62-
## '&', '"', and "'" characters to underscores. See
63-
## https://stackoverflow.com/a/10371699/19474638
64-
##
65-
## Note that we sanitize strip_one_string, NOT strip_two_string, so
66-
## that the neutered malicious text is displayed to the user. This
67-
## is so that the user is alerted to something odd happening.
68-
strip_one_string = "".join(
69-
"_" if char in ("<", ">", "&", '"', "'") else char
70-
for char in strip_one_string
71-
)
72-
73-
## Sanitize any remaining '<' and '>' characters that survived both
74-
## strip passes (e.g. bare '<' in "2 < 3" which HTMLParser does not
75-
## treat as a tag). These could be misinterpreted as markup if the
76-
## output is later placed into an HTML context.
58+
if strip_one_string == strip_two_string:
59+
return strip_one_string
60+
61+
## If we get this far, the second strip attempt further transformed the
62+
## text, indicating an attempt to maliciously circumvent the stripper.
63+
## Sanitize the malicious text by changing all '<', '>', and '&'
64+
## characters to underscores. See
65+
## https://stackoverflow.com/a/10371699/19474638
66+
##
67+
## Note that we sanitize strip_one_string, NOT strip_two_string, so that
68+
## the neutered malicious text is displayed to the user. This is so that
69+
## the user is alerted to something odd happening.
7770
sanitized_string: str = "".join(
78-
"_" if char in ("<", ">") else char for char in strip_one_string
71+
"_" if char in ["<", ">", "&"] else char for char in strip_one_string
7972
)
8073
return sanitized_string

usr/lib/python3/dist-packages/strip_markup/tests/strip_markup.py

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -359,104 +359,6 @@ def _test_malicious_markup_strings(
359359
stdin_string=test_case[0],
360360
)
361361

362-
def _test_bare_angle_bracket_strings(
363-
self,
364-
main_func: Callable[[], int],
365-
argv0: str,
366-
pos_args_prefix: list[str] | None = None,
367-
) -> None:
368-
"""
369-
Ensure strip_markup.py sanitizes bare '<' and '>' characters that
370-
are not part of valid tags but could be misinterpreted as markup
371-
in downstream HTML contexts. This function is reused by
372-
sanitize_string's tests.
373-
"""
374-
375-
if pos_args_prefix is None:
376-
pos_args_prefix = []
377-
378-
test_case_list: list[tuple[str, str]] = [
379-
(
380-
"2 < 3",
381-
"2 _ 3",
382-
),
383-
(
384-
"2 > 1",
385-
"2 _ 1",
386-
),
387-
(
388-
"1 < 2 > 0",
389-
"1 _ 2 _ 0",
390-
),
391-
(
392-
"x << y",
393-
"x __ y",
394-
),
395-
]
396-
397-
for test_case in test_case_list:
398-
self._test_args(
399-
main_func=main_func,
400-
argv0=argv0,
401-
stdout_string=test_case[1],
402-
stderr_string="",
403-
exit_code=0,
404-
args=[*pos_args_prefix, test_case[0]],
405-
)
406-
self._test_stdin(
407-
main_func=main_func,
408-
argv0=argv0,
409-
stdout_string=test_case[1],
410-
stderr_string="",
411-
args=[*pos_args_prefix],
412-
stdin_string=test_case[0],
413-
)
414-
415-
def _test_malicious_markup_quote_strings(
416-
self,
417-
main_func: Callable[[], int],
418-
argv0: str,
419-
pos_args_prefix: list[str] | None = None,
420-
) -> None:
421-
"""
422-
Ensure strip_markup.py sanitizes quote characters in strings that
423-
trigger the malicious-input branch, preventing attribute injection
424-
if the output is placed into an HTML attribute context. This
425-
function is reused by sanitize_string's tests.
426-
"""
427-
428-
if pos_args_prefix is None:
429-
pos_args_prefix = []
430-
431-
test_case_list: list[tuple[str, str]] = [
432-
(
433-
'<<b>b "onmouseover="alert(1)<</b>/b>',
434-
'_b _onmouseover=_alert(1)_/b_',
435-
),
436-
(
437-
"<<b>b 'onmouseover='alert(1)<</b>/b>",
438-
"_b _onmouseover=_alert(1)_/b_",
439-
),
440-
]
441-
442-
for test_case in test_case_list:
443-
self._test_args(
444-
main_func=main_func,
445-
argv0=argv0,
446-
stdout_string=test_case[1],
447-
stderr_string="",
448-
exit_code=0,
449-
args=[*pos_args_prefix, test_case[0]],
450-
)
451-
self._test_stdin(
452-
main_func=main_func,
453-
argv0=argv0,
454-
stdout_string=test_case[1],
455-
stderr_string="",
456-
args=[*pos_args_prefix],
457-
stdin_string=test_case[0],
458-
)
459-
460362

461363
class TestStripMarkup(TestStripMarkupBase):
462364
"""
@@ -512,21 +414,3 @@ def test_malicious_markup_strings(self) -> None:
512414
"""
513415

514416
self._test_malicious_markup_strings(strip_markup_main, self.argv0)
515-
516-
def test_bare_angle_bracket_strings(self) -> None:
517-
"""
518-
Wrapper for _test_bare_angle_bracket_strings specific to
519-
TestStripMarkup.
520-
"""
521-
522-
self._test_bare_angle_bracket_strings(strip_markup_main, self.argv0)
523-
524-
def test_malicious_markup_quote_strings(self) -> None:
525-
"""
526-
Wrapper for _test_malicious_markup_quote_strings specific to
527-
TestStripMarkup.
528-
"""
529-
530-
self._test_malicious_markup_quote_strings(
531-
strip_markup_main, self.argv0
532-
)

0 commit comments

Comments
 (0)