Skip to contentSkip to conversation

Post/parser: Use bleach to sanitse XSS input

PATCH In review Richard Purdie 3 participants 3 messages Mar 22, 2021 – Mar 23, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Post/parser: Use bleach to sanitse XSS input

Instead of searching for "<", use bleach to sanity input to avoid any XSS issues.

---
 Post/parser.py | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
Post/parser.py +9 -17
index f411e02..536e872 100644
@@ -9,6 +9,7 @@
 # Licensed under the MIT license, see COPYING.MIT for details
 
 import json, re
+import bleach
 from Post.models import Build, BuildFailure, ErrorType
 from django.conf import settings
 from django.utils import timezone
@@ -19,21 +20,6 @@ class Parser:
     def __init__(self, data):
         self.data = data.decode('utf-8')
 
-    # returns true if the values contain '<' char
-    # Ignore the failures field (which is an array anyway)
-    # Ignore any non-str fields too [YOCTO #14208]
-    def contains_tags (self, data):
-        for key,val in data.items():
-            if key == 'failures':
-                continue
-            
-            if not isinstance(val, str):
-                continue
-
-            if '<' in val:
-                return True
-        return False
-
     def parse(self, request):
         build_fails_logged = []
 
@@ -42,8 +28,14 @@ class Parser:
         except:
              return  { 'error' : 'Invalid json' }
 
-        if self.contains_tags(jsondata) == True:
-            return  { 'error' : 'Invalid characters in 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:
Signature · 1 line
2.30.2
#1 lore raw
Quoted 3 lines · Richard Purdie

Instead of searching for "<", use bleach to sanity input to avoid any XSS issues.

I will be able to test it. once its installed

Quoted 61 lines
---
  Post/parser.py | 26 +++++++++-----------------
  1 file changed, 9 insertions(+), 17 deletions(-)
Post/parser.py +9 -17
index f411e02..536e872 100644
@@ -9,6 +9,7 @@
  # Licensed under the MIT license, see COPYING.MIT for details
 
  import json, re
+import bleach
  from Post.models import Build, BuildFailure, ErrorType
  from django.conf import settings
  from django.utils import timezone
@@ -19,21 +20,6 @@ class Parser:
      def __init__(self, data):
          self.data = data.decode('utf-8')
 
-    # returns true if the values contain '<' char
-    # Ignore the failures field (which is an array anyway)
-    # Ignore any non-str fields too [YOCTO #14208]
-    def contains_tags (self, data):
-        for key,val in data.items():
-            if key == 'failures':
-                continue
-
-            if not isinstance(val, str):
-                continue
-
-            if '<' in val:
-                return True
-        return False
-
      def parse(self, request):
          build_fails_logged = []
 
@@ -42,8 +28,14 @@ class Parser:
          except:
               return  { 'error' : 'Invalid json' }
 
-        if self.contains_tags(jsondata) == True:
-            return  { 'error' : 'Invalid characters in 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:
#2 lore raw
In reply to Richard Purdie
Quoted 15 lines · Richard Purdie

[Please note: This e-mail is from an EXTERNAL e-mail address]

Instead of searching for "<", use bleach to sanity input to avoid any XSS issues.

---
  Post/parser.py | 26 +++++++++-----------------
  1 file changed, 9 insertions(+), 17 deletions(-)
Post/parser.py +0 -0
index f411e02..536e872 100644

Post/parser.py line 12

Quoted 4 lines
Post/parser.py +1 -0
@@ -9,6 +9,7 @@
  # Licensed under the MIT license, see COPYING.MIT for details
 
  import json, re
+import bleach

would need to add that to requirements.txt i tested it quickly and it gave me bleach not found

Quoted 30 lines
Post/parser.py +0 -16
  from Post.models import Build, BuildFailure, ErrorType
  from django.conf import settings
  from django.utils import timezone
@@ -19,21 +20,6 @@ class Parser:
      def __init__(self, data):
          self.data = data.decode('utf-8')
 
-    # returns true if the values contain '<' char
-    # Ignore the failures field (which is an array anyway)
-    # Ignore any non-str fields too [YOCTO #14208]
-    def contains_tags (self, data):
-        for key,val in data.items():
-            if key == 'failures':
-                continue
-
-            if not isinstance(val, str):
-                continue
-
-            if '<' in val:
-                return True
-        return False
-
      def parse(self, request):
          build_fails_logged = []
 
@@ -42,8 +28,14 @@ class Parser:
          except:
               return  { 'error' : 'Invalid json' }
 
-        if self.contains_tags(jsondata) == True:

Post/parser.py class Parser: · line 39

Quoted 10 lines
Post/parser.py +8 -1
-            return  { 'error' : 'Invalid characters in 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)
 

would it make more sense to bleach/sanitize the raw data before it got parsed into a json object so even 'failures' wouldn't contain any injection

Quoted 9 lines
Post/parser.py +0 -1
          b = Build.objects.create()
          try:
--

2.30.2