]> granicus.if.org Git - python/commitdiff
bpo-35603: Escape table header of make_table output that can cause potential XSS...
authorXtreak <tir.karthi@gmail.com>
Sat, 29 Dec 2018 08:53:14 +0000 (14:23 +0530)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 29 Dec 2018 08:53:14 +0000 (10:53 +0200)
Lib/difflib.py
Lib/test/test_difflib.py
Misc/NEWS.d/next/Library/2018-12-28-14-53-22.bpo-35603.rVCZAE.rst [new file with mode: 0644]

index 887c3c26cae4588cdfcfb922e3c2af90cb4a64b3..4571817b9823b03c2b041ba961f0418b5da9205e 100644 (file)
@@ -2036,6 +2036,10 @@ class HtmlDiff(object):
                 s.append( fmt % (next_id[i],next_href[i],fromlist[i],
                                            next_href[i],tolist[i]))
         if fromdesc or todesc:
+            fromdesc = fromdesc.replace("&", "&amp;").replace(">", "&gt;") \
+                                                     .replace("<", "&lt;")
+            todesc = todesc.replace("&", "&amp;").replace(">", "&gt;") \
+                                                 .replace("<", "&lt;")
             header_row = '<thead><tr>%s%s%s%s</tr></thead>' % (
                 '<th class="diff_next"><br /></th>',
                 '<th colspan="2" class="diff_header">%s</th>' % fromdesc,
index 745ccbd6659ed5a3df435daa5b3b2857a85fdbf4..63ebdb0dc83b17da3d30f025d6e9f5143e878541 100644 (file)
@@ -238,6 +238,15 @@ class TestSFpatches(unittest.TestCase):
         with open(findfile('test_difflib_expect.html')) as fp:
             self.assertEqual(actual, fp.read())
 
+    def test_make_table_escape_table_header(self):
+        html_diff = difflib.HtmlDiff()
+        output = html_diff.make_table(patch914575_from1.splitlines(),
+                                      patch914575_to1.splitlines(),
+                                      fromdesc='<from>',
+                                      todesc='<to>')
+        self.assertIn('&lt;from&gt;', output)
+        self.assertIn('&lt;to&gt;', output)
+
     def test_recursion_limit(self):
         # Check if the problem described in patch #1413711 exists.
         limit = sys.getrecursionlimit()
diff --git a/Misc/NEWS.d/next/Library/2018-12-28-14-53-22.bpo-35603.rVCZAE.rst b/Misc/NEWS.d/next/Library/2018-12-28-14-53-22.bpo-35603.rVCZAE.rst
new file mode 100644 (file)
index 0000000..03150c3
--- /dev/null
@@ -0,0 +1,2 @@
+Escape table header output of :meth:`difflib.HtmlDiff.make_table`.
+Patch by Karthikeyan Singaravelan.