Skip to contentSkip to conversation

Don't render submitted data as HTML

PATCH New Tim Orling 1 participant 3 messages Oct 18

lore
Share & tools
lore

Tim Orling

author
#0 lore raw

cover Don't render submitted data as HTML

Fix some security loopholes in the old Bootstrap implementation and remove deprecated/unsupported bleach.

Tim Orling (2):
Don't let Bootstrap insert submitted data as HTML. Remove bleach and store submitted data as sent.

 Post/parser.py               | 10 --------
 Post/static/js/main.js       | 14 ++++++++---
 Post/test.py                 | 45 ++++++++++++++++++++++++++++++++++++
 requirements.txt             |  1 -
 templates/latest-errors.html |  7 +-----
 5 files changed, 57 insertions(+), 20 deletions(-)

base-commit: 752b7188d04c2fd0e2f32f0cd2dd437c43058d82
prerequisite-patch-id: 9c9d012e1163dd1d335531d0451657823f17eb64
prerequisite-patch-id: ddb4df3e54736ed25846f91d4918299e601f4071
prerequisite-patch-id: ab69f4aa494291611cbe0e916f18b07f4aeebe57
prerequisite-patch-id: ac2b802ea065536df3854c48e265f5508d201cff
prerequisite-patch-id: 81a7de0e9568848efc8c66d397abb5f56218db95
prerequisite-patch-id: cf32dcaef20903890a020211a35f46b2384207da
prerequisite-patch-id: 6920bc5c70071bbd42b55b08bfc2bff04133c3ab
prerequisite-patch-id: a72bee4356db80ccb45321967d18081961f60860
prerequisite-patch-id: a2285b682329ba20d838085588b7c6681a86d273
prerequisite-patch-id: c3980e46d49b00b419cf4a7c2bc32c346478a453
prerequisite-patch-id: 627f58daa68852066f56558f08b07ad4af7bef9f
prerequisite-patch-id: 2d1b78495c909de035dbed74af06db5683701d1b
prerequisite-patch-id: 028165edb39ba03ad70f83ae9e2f36dff8c7e912
prerequisite-patch-id: f882f3967c3decfef434c86f2bb019b228275bac
prerequisite-patch-id: 6d9b0b9eba49c93ca0bcaf6cd25bc079efccb7cd
prerequisite-patch-id: 460c40f8c35ebd0f31d5d52f06c32cd7c1232fb7
prerequisite-patch-id: c7d6ab550cb907d3cc7e3a3b777695f45649c46f

Signature · 1 line
2.56.0
author
#1 lore raw

PATCH 1/2 Don't let Bootstrap insert submitted data as HTML.

The filter tooltips used html: true, and their titles hold the submitted recipe and task, which bleach doesn't clean, so a report could inject HTML (the CSP stopped scripts). Drop html: true, and build the commit popover from DOM nodes so the commit is inserted as text.

AI-Generated: Claude Opus 5.5

---
 Post/static/js/main.js       | 14 +++++++++++---
 Post/test.py                 | 35 +++++++++++++++++++++++++++++++++++
 templates/latest-errors.html |  7 +------
 3 files changed, 47 insertions(+), 9 deletions(-)
Post/static/js/main.js +11 -3
index 5c07ad5..546aabe 100644
@@ -219,9 +219,16 @@ $(document).ready(function(){
 
   $("#errors-table").show();
 
+  /* Insert the submitted commit as text, not HTML. */
   $('.commit > div').popover({
       placement:'left',
-      html: true
+      html: true,
+      content: function () {
+        var commit = $(this).attr('data-commit');
+        var link = $('<a href="#" class="filter visible" data-type="commit">Filter by commit</a>')
+          .attr('data-filter', commit);
+        return $('<div/>').append($('<span/>').text(commit), $('<p/>').append(link));
+      }
   });
 
   $(".recipe_version a.tooltip-me").tooltip();
@@ -267,8 +274,9 @@ $(document).ready(function(){
     e.stopPropagation();
   });
 
-  $(".icon-filter").tooltip({ container: 'body', html: true });
+  /* Titles hold submitted data: keep them text. */
+  $(".icon-filter").tooltip({ container: 'body' });
   // initialise the remove filter tooltips
-  $("th .btn-mini").tooltip({ container: 'body', html: true, title:'Clear filter' });
+  $("th .btn-mini").tooltip({ container: 'body', title:'Clear filter' });
 
 });
Post/test.py +35 -0
index f48beec..0a358d1 100755
@@ -432,6 +432,41 @@ class AccountPagesRemovedTest(TestCase):
         self.assertEqual(client.get("/admin/login/").status_code, 200)
 
 
+@override_settings(ALLOWED_HOSTS=["testhost"])
+class SubmittedHtmlTest(TestCase):
+    """Submitted data must only ever reach the page escaped."""
+
+    def submit(self, **fields):
+        with open("test-data/test-payload.json") as f:
+            payload = json.load(f)
+        payload.update(fields)
+        payload['failures'][0]['package'] = '<b>evil</b>-1.0'
+        payload['failures'][0]['task'] = 'do_<b>evil</b>'
+        client = Client(HTTP_HOST="testhost")
+        data = urllib.parse.urlencode({'data': json.dumps(payload)})
+        self.assertEqual(client.post("/ClientPost/", data, "application/json").status_code, 200)
+        return client, BuildFailure.objects.get()
+
+    def test_failure_fields_are_escaped(self):
+        client, bf = self.submit()
+        response = client.get("/Errors/Latest/")
+        self.assertNotContains(response, "<b>evil</b>")
+        self.assertContains(response, "Filter by &lt;b&gt;evil&lt;/b&gt;: do_&lt;b&gt;evil&lt;/b&gt;")
+
+    def test_commit_is_not_in_html_attributes(self):
+        client, bf = self.submit(branch_commit="master: <b>evil</b>")
+        response = client.get("/Errors/Latest/")
+        self.assertNotContains(response, "<b>evil</b>")
+        self.assertContains(response, 'data-commit="&lt;b&gt;evil&lt;/b&gt;"')
+        # With html enabled, Bootstrap inserts these as HTML.
+        self.assertNotContains(response, "data-content")
+        self.assertNotContains(response, "data-html")
+
+    def test_tooltips_are_not_html(self):
+        with open("Post/static/js/main.js") as f:
+            self.assertNotRegex(f.read(), r"tooltip\(\{[^}]*html: true")
+
+
 @patch("Post.management.commands.culldb.time.sleep")
 class CullDbTest(TestCase):
     def setUp(self):
templates/latest-errors.html +1 -6
index 2080210..d9c2890 100644
@@ -189,12 +189,7 @@
 
                 </td>
                 <td class="commit">
-                  <div class="btn" rel="popover"
-                      data-content='
-                      {{ build_fail.BUILD.COMMIT|escape}}
-                      <p><a href="#" class="filter visible" data-filter="{{build_fail.BUILD.COMMIT}}" data-type="commit" >Filter by commit</a>'
-                      data-html="true"
-                    >
+                  <div class="btn" rel="popover" data-commit="{{ build_fail.BUILD.COMMIT }}">
                     {{ build_fail.BUILD.COMMIT|truncatechars:10}}
 
                   </div>
Signature · 1 line
2.56.0
author
#2 lore raw

PATCH 2/2 Remove bleach and store submitted data as sent.

In reply to Tim Orling

bleach stored escaped text, so "<" or "&" showed as "&lt;" or "&amp;". Escaping on output covers every place the data is shown, and bleach is deprecated. Existing reports keep their escaped text.

AI-Generated: Claude Opus 5.5

---
 Post/parser.py   | 10 ----------
 Post/test.py     | 10 ++++++++++
 requirements.txt |  1 -
 3 files changed, 10 insertions(+), 11 deletions(-)
Post/parser.py +0 -10
index 092ce24..e221bcf 100644
@@ -10,7 +10,6 @@
 
 import json
 import re
-import bleach
 from Post.models import Build, BuildFailure, ErrorType
 from Post.validators import clean_link_back
 from django.conf import settings
@@ -30,15 +29,6 @@ class Parser:
         except Exception:
              return  { 'error' : 'Invalid json' }
 
-        # Bleach data going directly into the database so that
-        # displaying in any of the graphing doesn't introduce XSS
-        for key,val in jsondata.items():
-            if key == 'failures':
-                continue
-            if not isinstance(val, str):
-                continue
-            jsondata[key] = bleach.clean(val)
-
         b = Build.objects.create()
         try:
             b.MACHINE = str(jsondata['machine'])
Post/test.py +10 -0
index 0a358d1..51ae0b9 100755
@@ -462,6 +462,16 @@ class SubmittedHtmlTest(TestCase):
         self.assertNotContains(response, "data-content")
         self.assertNotContains(response, "data-html")
 
+    def test_build_fields_are_stored_as_sent_and_escaped(self):
+        client, bf = self.submit(machine="<script>alert(1)</script>")
+        # Stored as sent; escaped on output.
+        self.assertEqual(bf.BUILD.MACHINE, "<script>alert(1)</script>")
+        for url in ("/Errors/Latest/", "/Errors/Details/%d/" % bf.id,
+                    "/Errors/Statistics/MACHINE"):
+            response = client.get(url)
+            self.assertEqual(response.status_code, 200, url)
+            self.assertNotContains(response, "<script>alert", msg_prefix=url)
+
     def test_tooltips_are_not_html(self):
         with open("Post/static/js/main.js") as f:
             self.assertNotRegex(f.read(), r"tooltip\(\{[^}]*html: true")
requirements.txt +0 -1
index 40f3433..59c10a9 100644
@@ -1,4 +1,3 @@
 Django>=5.2.18,<5.3
 python-Levenshtein==0.27.4
-bleach==6.4.0
 mysqlclient==2.3.0
Signature · 1 line
2.56.0