Skip to content

Commit 78de011

Browse files
tirkarthiserhiy-storchaka
authored andcommitted
bpo-35603: Escape table header of make_table output that can cause potential XSS. (GH-11341)
1 parent 1f511e1 commit 78de011

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Lib/difflib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,6 +2036,10 @@ def make_table(self,fromlines,tolines,fromdesc='',todesc='',context=False,
20362036
s.append( fmt % (next_id[i],next_href[i],fromlist[i],
20372037
next_href[i],tolist[i]))
20382038
if fromdesc or todesc:
2039+
fromdesc = fromdesc.replace("&", "&").replace(">", ">") \
2040+
.replace("<", "&lt;")
2041+
todesc = todesc.replace("&", "&amp;").replace(">", "&gt;") \
2042+
.replace("<", "&lt;")
20392043
header_row = '<thead><tr>%s%s%s%s</tr></thead>' % (
20402044
'<th class="diff_next"><br /></th>',
20412045
'<th colspan="2" class="diff_header">%s</th>' % fromdesc,

Lib/test/test_difflib.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ def test_html_diff(self):
238238
with open(findfile('test_difflib_expect.html')) as fp:
239239
self.assertEqual(actual, fp.read())
240240

241+
def test_make_table_escape_table_header(self):
242+
html_diff = difflib.HtmlDiff()
243+
output = html_diff.make_table(patch914575_from1.splitlines(),
244+
patch914575_to1.splitlines(),
245+
fromdesc='<from>',
246+
todesc='<to>')
247+
self.assertIn('&lt;from&gt;', output)
248+
self.assertIn('&lt;to&gt;', output)
249+
241250
def test_recursion_limit(self):
242251
# Check if the problem described in patch #1413711 exists.
243252
limit = sys.getrecursionlimit()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Escape table header output of :meth:`difflib.HtmlDiff.make_table`.
2+
Patch by Karthikeyan Singaravelan.

0 commit comments

Comments
 (0)