Skip to contentSkip to conversation

Fix existing tests to establish a know working baseline for future work.

PATCH New Piotr Buliński 1 participant 3 messages Oct 8

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 Fix existing tests to establish a know working baseline for future work.

---
 Post/test.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Post/test.py +6 -1
index 5acb68e..15aa4be 100755
@@ -2,7 +2,7 @@ import unittest
 import urllib.request, urllib.parse, urllib.error
 import json
 import re
-from django.test import Client
+from django.test import Client, override_settings
 from Post.models import BuildFailure, Build
 
 #Delete the data between tests
@@ -65,6 +65,11 @@ def compare_db_obj_with_payload(self, bf_object):
 
 class SimpleTest(unittest.TestCase):
     def setUp(self):
+        # The test clients submit with HTTP_HOST="testhost", so allow it here
+        # instead of hardcoding a test host into the project settings.
+        override = override_settings(ALLOWED_HOSTS=["testhost"])
+        override.enable()
+        self.addCleanup(override.disable)
         self.client = Client(HTTP_HOST="testhost")
         self.client_0_3 = Client(HTTP_HOST="testhost", HTTP_USER_AGENT="send-error-report/0.3")
 
#1 lore raw

PATCH 2/3 Add link back validator and use it in the parser.

This will prevent invalid URL (non-http(s)) from being recorded.

---
 Post/parser.py     |  3 +-
 Post/test.py       | 84 ++++++++++++++++++++++++++++++++++++++++++++++
 Post/validators.py | 42 +++++++++++++++++++++++
 3 files changed, 128 insertions(+), 1 deletion(-)
 create mode 100644 Post/validators.py
Post/parser.py +2 -1
index 08f471d..b6740d0 100644
@@ -11,6 +11,7 @@
 import json, re
 import bleach
 from Post.models import Build, BuildFailure, ErrorType
+from Post.validators import clean_link_back
 from django.conf import settings
 from django.utils import timezone
 from django.urls import reverse
@@ -48,7 +49,7 @@ class Parser:
             b.DISTRO = str(jsondata['distro'])
             b.NAME = str(jsondata['username'])
             b.EMAIL = str(jsondata['email'])
-            b.LINK_BACK = jsondata.get("link_back", None)
+            b.LINK_BACK = clean_link_back(jsondata.get("link_back", None))
             b.ERROR_TYPE = jsondata.get("error_type", ErrorType.RECIPE)
 
             # Extract the branch and commit
Post/test.py +84 -0

93 lines not on this page · raw message

Post/validators.py new file +42 -0

42 lines not on this page · raw message

#2 lore raw

PATCH 3/3 Add and use safe_url tag to prevent rendering of insecure link back URLs.

In reply to Piotr Buliński
---
 Post/templatetags/projecttags.py | 5 +++++
 templates/error-details.html     | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)
Post/templatetags/projecttags.py +5 -0
index 6c5ed94..a01989f 100644
@@ -3,9 +3,14 @@ import re
 from django import template
 from django.utils import timezone
 from django.template.defaultfilters import filesizeformat
+from Post.validators import clean_link_back
 
 register = template.Library()
 
[email protected](name = 'safe_url')
+def safe_url(value):
+    return clean_link_back(value) or ''
+
 @register.filter(name = 'sortcols')
 def sortcols(tablecols):
     return sorted(tablecols, key = lambda t: t['name'])
templates/error-details.html +2 -1
index 35bf0aa..5aa807d 100644
@@ -1,4 +1,5 @@
 {% extends "base.html" %}
+{% load projecttags %}
 {% block content %}
 <div class="row-fluid">
 
@@ -67,7 +68,7 @@
 
         {% if detail.BUILD.LINK_BACK %}
         <dt>Link back:</dt>
-        <dd><a href="{{detail.BUILD.LINK_BACK}}" target="_blank">{{detail.BUILD.LINK_BACK}}</a></dd>
+        <dd><a href="{{detail.BUILD.LINK_BACK|safe_url}}" target="_blank" rel="nofollow noopener noreferrer">{{detail.BUILD.LINK_BACK}}</a></dd>
         {% endif %}
 
         <dt></a>Similar errors:</dt>