Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.

Commit 7c4867c

Browse files
committed
fix: xss bypass in allowed protocol test using unicode invisible characters
This fixes sanitize_uri_values where it wouldn't correctly filter uris with disallowed protocols if the scheme had a unicode invisible character in it. Now the code strips all non-ASCII characters from the uri before parsing and testing the scheme. GHSA-8rfp-98v4-mmr6
1 parent 913ab75 commit 7c4867c

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎CHANGES‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,23 @@ Version 6.4.0 (TBD)
1010

1111
**Security fixes**
1212

13-
None
13+
* Fix bug 2023812 / GHSA-8rfp-98v4-mmr6.
14+
15+
Fix XSS issue with sanitize_uri_value where disallowed schemes with
16+
Unicode invisible characters wouldn't be rejected.
17+
18+
For example::
19+
20+
import bleach
21+
payload1 = '<a href="javascript\u200b:alert(document.cookie)">Click</a>'
22+
result1 = bleach.clean(payload1)
23+
print(repr(result1))
24+
25+
outputs::
26+
27+
'<a href="javascript\u200b:alert(document.cookie)">Click</a>'
28+
29+
See the advisory for details.
1430

1531
**Bug fixes**
1632

‎bleach/sanitizer.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,14 +488,15 @@ def sanitize_uri_value(self, value, allowed_protocols):
488488
# Convert all character entities in the value
489489
normalized_uri = html5lib_shim.convert_entities(value)
490490

491-
# Nix backtick, space characters, and control characters
491+
# Strip backtick, whitespace, and control characters
492492
normalized_uri = re.sub(r"[`\000-\040\177-\240\s]+", "", normalized_uri)
493493

494-
# Remove REPLACEMENT characters
495-
normalized_uri = normalized_uri.replace("\ufffd", "")
494+
# Strip non-ASCII characters so that urlparse can parse the url into
495+
# components correctly. This drops invisible and whitespace unicode
496+
# characters among other things.
497+
normalized_uri = re.sub(r"[^\x00-\x7f]", "", normalized_uri)
496498

497-
# Lowercase it--this breaks the value, but makes it easier to match
498-
# against
499+
# Lowercase value to make matching easier
499500
normalized_uri = normalized_uri.lower()
500501

501502
try:

‎tests/test_clean.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,22 @@ def test_attributes_list():
618618
# Disallowed protocols with sneaky character entities
619619
('<a href="javas&#x09;cript:alert(1)">alert</a>', {}, "<a>alert</a>"),
620620
('<a href="&#14;javascript:alert(1)">alert</a>', {}, "<a>alert</a>"),
621+
# Disallowed protocols with Unicode characters injected
622+
(
623+
'<a href="javascript\u200b:alert(1)">alert</a>',
624+
{"protocols": ALLOWED_PROTOCOLS},
625+
"<a>alert</a>",
626+
),
627+
(
628+
'<a href="\ufeffjavascript:alert(1)">alert</a>',
629+
{"protocols": ALLOWED_PROTOCOLS},
630+
"<a>alert</a>",
631+
),
632+
(
633+
'<a href="javascript\u00ad:alert(1)">alert</a>',
634+
{"protocols": ALLOWED_PROTOCOLS},
635+
"<a>alert</a>",
636+
),
621637
# Checking the uri should change it at all
622638
(
623639
'<a href="http://example.com/?foo&nbsp;bar">foo</a>',

0 commit comments

Comments
 (0)