Skip to content

Commit e266576

Browse files
Merge branch 'master' into fix-xss-601
2 parents 4278f3e + ded5e74 commit e266576

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [pull #591] Add Alerts extra
77
- [pull #595] Fix img alt text being processed as markdown (#594)
88
- [pull #602] Fix XSS issue in safe mode (#601)
9+
- [pull #604] Fix XSS injection in image URLs (#603)
910

1011

1112
## python-markdown2 2.5.0

lib/markdown2.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,9 +1359,23 @@ def _is_comment(token):
13591359
is_html_markup = not is_html_markup
13601360
return ''.join(tokens)
13611361

1362-
def _unhash_html_spans(self, text: str) -> str:
1363-
for key, sanitized in list(self.html_spans.items()):
1364-
text = text.replace(key, sanitized)
1362+
def _unhash_html_spans(self, text: str, spans=True, code=False) -> str:
1363+
'''
1364+
Recursively unhash a block of text
1365+
1366+
Args:
1367+
spans: unhash anything from `self.html_spans`
1368+
code: unhash code blocks
1369+
'''
1370+
orig = ''
1371+
while text != orig:
1372+
if spans:
1373+
for key, sanitized in list(self.html_spans.items()):
1374+
text = text.replace(key, sanitized)
1375+
if code:
1376+
for code, key in list(self._code_table.items()):
1377+
text = text.replace(key, code)
1378+
orig = text
13651379
return text
13661380

13671381
def _sanitize_html(self, s: str) -> str:
@@ -1587,8 +1601,9 @@ def _do_links(self, text: str) -> str:
15871601

15881602
# We've got to encode these to avoid conflicting
15891603
# with italics/bold.
1590-
url = url.replace('*', self._escape_table['*']) \
1591-
.replace('_', self._escape_table['_'])
1604+
url = self._unhash_html_spans(url, code=True) \
1605+
.replace('*', self._escape_table['*']) \
1606+
.replace('_', self._escape_table['_'])
15921607
if title:
15931608
title_str = ' title="%s"' % (
15941609
_xml_escape_attr(title)

test/tm-cases/issue603_xss.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<p><img src="code&gt;&quot; onerror=alert()//&lt;/code" alt="" /></p>
2+
3+
<p><img src="&quot; onerror=alert()//" alt="" />
4+
<a href="#"></a>
5+
<img src="`&quot; onerror=alert()//`" alt="" />
6+
<img src="&lt;code&gt;&quot; onerror=alert()//&lt;code&gt;" alt="" /></p>

test/tm-cases/issue603_xss.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"safe_mode": "escape"}

test/tm-cases/issue603_xss.text

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
![](`" onerror=alert()//`)
2+
3+
4+
![][XSS]
5+
[][XSS]
6+
![][XSS2]
7+
![][XSS3]
8+
9+
10+
[XSS]: " onerror=alert()//
11+
[XSS2]: `" onerror=alert()//`
12+
[XSS3]: <code>" onerror=alert()//<code>

0 commit comments

Comments
 (0)