Skip to content

Commit c91007d

Browse files
Merge pull request #626 from Crozzers/fix-xss-issue625
Fix XSS when encoding incomplete tags (#625)
2 parents 1e00258 + 03941d1 commit c91007d

5 files changed

+13
-9
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [pull #617] Add MarkdownFileLinks extra (#528)
66
- [pull #622] Add missing block tags to regex (#620)
77
- [pull #623] Don't escape plus signs in URLs (#621)
8+
- [pull #626] Fix XSS when encoding incomplete tags (#625)
89

910

1011
## python-markdown2 2.5.3

lib/markdown2.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1319,17 +1319,17 @@ def _escape_special_chars(self, text: str) -> str:
13191319
is_html_markup = not is_html_markup
13201320
return ''.join(escaped)
13211321

1322+
def _is_auto_link(self, text):
1323+
if ':' in text and self._auto_link_re.match(text):
1324+
return True
1325+
elif '@' in text and self._auto_email_link_re.match(text):
1326+
return True
1327+
return False
1328+
13221329
@mark_stage(Stage.HASH_HTML)
13231330
def _hash_html_spans(self, text: str) -> str:
13241331
# Used for safe_mode.
13251332

1326-
def _is_auto_link(s):
1327-
if ':' in s and self._auto_link_re.match(s):
1328-
return True
1329-
elif '@' in s and self._auto_email_link_re.match(s):
1330-
return True
1331-
return False
1332-
13331333
def _is_code_span(index, token):
13341334
try:
13351335
if token == '<code>':
@@ -1353,7 +1353,7 @@ def _is_comment(token):
13531353
split_tokens = self._sorta_html_tokenize_re.split(text)
13541354
is_html_markup = False
13551355
for index, token in enumerate(split_tokens):
1356-
if is_html_markup and not _is_auto_link(token) and not _is_code_span(index, token):
1356+
if is_html_markup and not self._is_auto_link(token) and not _is_code_span(index, token):
13571357
is_comment = _is_comment(token)
13581358
if is_comment:
13591359
tokens.append(self._hash_span(self._sanitize_html(is_comment.group(1))))
@@ -2165,7 +2165,7 @@ def _encode_incomplete_tags(self, text: str) -> str:
21652165
if self.safe_mode not in ("replace", "escape"):
21662166
return text
21672167

2168-
if text.endswith(">"):
2168+
if self._is_auto_link(text):
21692169
return text # this is not an incomplete tag, this is a link in the form <http://x.y.z>
21702170

21712171
def incomplete_tags_sub(match):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>&lt;x&gt;&lt;img src=x onerror=alert("xss")//>&lt;x&gt;</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{'safe_mode': 'escape'}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<x><img src=x onerror=alert("xss")//><x>

0 commit comments

Comments
 (0)