|
| 1 | +import json |
| 2 | + |
| 3 | +from dojo.reports.widgets import report_widget_factory |
| 4 | +from unittests.dojo_test_case import DojoTestCase, versioned_fixtures |
| 5 | + |
| 6 | + |
| 7 | +@versioned_fixtures |
| 8 | +class TestCustomReportWysiwygXss(DojoTestCase): |
| 9 | + |
| 10 | + """ |
| 11 | + Regression tests for the Custom Report Builder "Custom Content" (WYSIWYG) |
| 12 | + widget. |
| 13 | +
|
| 14 | + The widget renders user-supplied markup with Django's |safe filter, so the |
| 15 | + content must be sanitized before it reaches the template. These tests drive |
| 16 | + the same data flow as the report builder -- custom-content.hidden_content -> |
| 17 | + report_widget_factory() -> WYSIWYGContent.content -> get_html() -> the |
| 18 | + |safe template -- and assert that active content cannot be emitted as raw |
| 19 | + executable markup while legitimate rich-text formatting survives. |
| 20 | + """ |
| 21 | + |
| 22 | + fixtures = ["dojo_testdata.json"] |
| 23 | + |
| 24 | + # The exact payload validated in the original report. |
| 25 | + SCRIPT_PAYLOAD = "<script>alert(document.domain)</script><img src=x onerror=alert(1)>" |
| 26 | + JS_URL_PAYLOAD = '<a href="javascript:alert(1)">click me</a>' |
| 27 | + SAFE_PAYLOAD = '<b>bold</b> <u>underline</u> <a href="https://example.com/safe">safe link</a>' |
| 28 | + # <img> is allowed, so an inline base64 image must survive... |
| 29 | + DATA_IMAGE_PAYLOAD = ( |
| 30 | + '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB' |
| 31 | + 'CAQAAAC1HAwCAAAAC0lEQVR42mNk+M8AAAMBAQDJ/pLvAAAAAElFTkSuQmCC" alt="pixel">' |
| 32 | + ) |
| 33 | + # ...but data: must be limited to images -- data:text/html on an <img src>... |
| 34 | + DATA_HTML_IMAGE_PAYLOAD = '<img src="data:text/html,<script>alert(1)</script>">' |
| 35 | + # ...and data: URLs must never be accepted on a link href. |
| 36 | + DATA_URL_LINK_PAYLOAD = '<a href="data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==">link</a>' |
| 37 | + # Event handlers must be stripped even from allow-listed tags. |
| 38 | + EVENT_HANDLER_PAYLOAD = '<div onclick="alert(1)">click</div>' |
| 39 | + |
| 40 | + def _render_custom_content(self, hidden_content, heading="Custom Content"): |
| 41 | + """Render a Custom Content widget the way the report builder does.""" |
| 42 | + widget_json = json.dumps([ |
| 43 | + {"custom-content": [ |
| 44 | + {"name": "heading", "value": heading}, |
| 45 | + {"name": "hidden_content", "value": hidden_content}, |
| 46 | + {"name": "page_break_after", "value": False}, |
| 47 | + ]}, |
| 48 | + ]) |
| 49 | + widgets = report_widget_factory(json_data=widget_json, request=None) |
| 50 | + return str(widgets["custom-content-0"].get_html()) |
| 51 | + |
| 52 | + def test_script_is_neutralized_and_image_handler_is_stripped(self): |
| 53 | + """<script> is escaped to inert text; <img> is kept but its onerror is removed.""" |
| 54 | + html = self._render_custom_content(self.SCRIPT_PAYLOAD) |
| 55 | + |
| 56 | + # <script> is escaped (rendered as inert text, not a raw tag)... |
| 57 | + self.assertNotIn("<script", html) |
| 58 | + # ...the image element itself is allowed... |
| 59 | + self.assertIn("<img", html) |
| 60 | + # ...but the event handler that made it dangerous is gone. |
| 61 | + self.assertNotIn("onerror", html) |
| 62 | + |
| 63 | + def test_data_url_image_is_preserved(self): |
| 64 | + """Inline data:image/ sources survive so pasted/base64 images still render.""" |
| 65 | + html = self._render_custom_content(self.DATA_IMAGE_PAYLOAD) |
| 66 | + |
| 67 | + self.assertIn("data:image/png;base64", html) |
| 68 | + |
| 69 | + def test_data_url_non_image_is_stripped(self): |
| 70 | + """data: on an <img src> is limited to images; data:text/html is dropped.""" |
| 71 | + html = self._render_custom_content(self.DATA_HTML_IMAGE_PAYLOAD) |
| 72 | + |
| 73 | + self.assertNotIn("data:text/html", html) |
| 74 | + self.assertNotIn("<script", html) |
| 75 | + |
| 76 | + def test_data_url_link_is_stripped(self): |
| 77 | + """data: URLs are never allowed on link hrefs (only on <img src>).""" |
| 78 | + html = self._render_custom_content(self.DATA_URL_LINK_PAYLOAD) |
| 79 | + |
| 80 | + self.assertNotIn("data:text/html", html) |
| 81 | + self.assertIn("link", html) # anchor text is preserved |
| 82 | + |
| 83 | + def test_obfuscated_data_url_link_is_stripped(self): |
| 84 | + """data: on a link href stays blocked even when its scheme is split by control chars.""" |
| 85 | + for payload in ( |
| 86 | + '<a href="da ta:text/html,alert(1)">l</a>', # newline in scheme |
| 87 | + '<a href="da	ta:text/html,alert(1)">l</a>', # tab in scheme |
| 88 | + ): |
| 89 | + html = self._render_custom_content(payload) |
| 90 | + self.assertNotIn("ta:text/html", html) |
| 91 | + |
| 92 | + def test_event_handler_on_allowed_tag_is_stripped(self): |
| 93 | + """Event-handler attributes are removed even from allow-listed tags.""" |
| 94 | + html = self._render_custom_content(self.EVENT_HANDLER_PAYLOAD) |
| 95 | + |
| 96 | + self.assertNotIn("onclick", html) |
| 97 | + self.assertIn("click", html) |
| 98 | + |
| 99 | + def test_javascript_url_is_stripped(self): |
| 100 | + """javascript: hrefs are removed; the anchor text is preserved.""" |
| 101 | + html = self._render_custom_content(self.JS_URL_PAYLOAD) |
| 102 | + |
| 103 | + self.assertNotIn("javascript:", html) |
| 104 | + self.assertIn("click me", html) |
| 105 | + |
| 106 | + def test_safe_formatting_is_preserved(self): |
| 107 | + """The formatting the WYSIWYG editor emits must pass through intact.""" |
| 108 | + html = self._render_custom_content(self.SAFE_PAYLOAD) |
| 109 | + |
| 110 | + self.assertIn("<b>bold</b>", html) |
| 111 | + self.assertIn("<u>underline</u>", html) |
| 112 | + self.assertIn('href="https://example.com/safe"', html) |
| 113 | + self.assertIn("safe link", html) |
| 114 | + |
| 115 | + def test_heading_is_escaped(self): |
| 116 | + """The heading is auto-escaped and cannot be used to inject markup.""" |
| 117 | + html = self._render_custom_content("benign", heading="<script>alert(1)</script>") |
| 118 | + |
| 119 | + self.assertNotIn("<script", html) |
0 commit comments