|
| 1 | +from django.test import TestCase, override_settings |
| 2 | +from events.html_sanitizer import sanitize_html |
| 3 | +from django.conf import settings |
| 4 | + |
| 5 | + |
| 6 | +class SanitizeHTMLTests(TestCase): |
| 7 | + def setUp(self): |
| 8 | + # Define allowed tags, attributes, and styles from settings |
| 9 | + self.allowed_tags = settings.ALLOWED_HTML_TAGS_INPUT |
| 10 | + self.allowed_attrs = settings.ALLOWED_HTML_ATTRIBUTES_INPUT |
| 11 | + self.allowed_styles = settings.ALLOWED_HTML_STYLES_INPUT |
| 12 | + |
| 13 | + def test_allowed_tags_preserved(self): |
| 14 | + """Ensure that allowed tags are preserved in the output.""" |
| 15 | + input_html = "<p>This is a <strong>test</strong> paragraph.</p>" |
| 16 | + expected_output = "<p>This is a <strong>test</strong> paragraph.</p>" |
| 17 | + sanitized = sanitize_html( |
| 18 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 19 | + ) |
| 20 | + self.assertEqual(sanitized, expected_output) |
| 21 | + |
| 22 | + def test_disallowed_tags_removed(self): |
| 23 | + """Ensure that disallowed tags are removed from the output.""" |
| 24 | + input_html = "<p>This is a <script>alert('XSS');</script> test.</p>" |
| 25 | + expected_output = "<p>This is a test.</p>" |
| 26 | + sanitized = sanitize_html( |
| 27 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 28 | + ) |
| 29 | + self.assertEqual(sanitized, expected_output) |
| 30 | + |
| 31 | + def test_allowed_attributes_preserved(self): |
| 32 | + """Ensure that allowed attributes are preserved.""" |
| 33 | + input_html = '<a href="https://example.com" title="Example">Link</a>' |
| 34 | + expected_output = '<a href="https://example.com" title="Example">Link</a>' |
| 35 | + sanitized = sanitize_html( |
| 36 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 37 | + ) |
| 38 | + self.assertEqual(sanitized, expected_output) |
| 39 | + |
| 40 | + def test_disallowed_attributes_removed(self): |
| 41 | + """Ensure that disallowed attributes are removed.""" |
| 42 | + input_html = '<a href="https://example.com" onclick="alert(\'XSS\')">Link</a>' |
| 43 | + expected_output = '<a href="https://example.com">Link</a>' |
| 44 | + sanitized = sanitize_html( |
| 45 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 46 | + ) |
| 47 | + self.assertEqual(sanitized, expected_output) |
| 48 | + |
| 49 | + def test_allowed_styles_preserved(self): |
| 50 | + """Ensure that allowed CSS styles are preserved.""" |
| 51 | + input_html = '<p style="color: red; font-weight: bold;">Styled text</p>' |
| 52 | + expected_output = '<p style="color: red; font-weight: bold">Styled text</p>' |
| 53 | + sanitized = sanitize_html( |
| 54 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 55 | + ) |
| 56 | + self.assertEqual(sanitized, expected_output) |
| 57 | + |
| 58 | + def test_malformed_html(self): |
| 59 | + """Ensure that malformed HTML is handled gracefully.""" |
| 60 | + input_html = "<p>This is <b>bold<i> and italic</p>" |
| 61 | + expected_output = "<p>This is <b>bold<i> and italic</i></b></p>" |
| 62 | + sanitized = sanitize_html( |
| 63 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 64 | + ) |
| 65 | + self.assertEqual(sanitized, expected_output) |
| 66 | + |
| 67 | + def test_escape_entities(self): |
| 68 | + """Ensure that HTML entities are preserved.""" |
| 69 | + input_html = "5 < 10 & 10 > 5" |
| 70 | + expected_output = "5 < 10 & 10 > 5" # If using convert_charrefs=False |
| 71 | + sanitized = sanitize_html( |
| 72 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 73 | + ) |
| 74 | + self.assertEqual(sanitized, expected_output) |
| 75 | + |
| 76 | + def test_href_sanitization(self): |
| 77 | + """Ensure that href attributes do not contain 'javascript:'.""" |
| 78 | + input_html = "<a href=\"javascript:alert('XSS')\">Bad Link</a>" |
| 79 | + expected_output = "<a>Bad Link</a>" # 'href' removed due to unsafe scheme |
| 80 | + sanitized = sanitize_html( |
| 81 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 82 | + ) |
| 83 | + self.assertEqual(sanitized, expected_output) |
| 84 | + |
| 85 | + def test_style_with_malicious_content(self): |
| 86 | + """Ensure that style attributes do not contain malicious content.""" |
| 87 | + input_html = ( |
| 88 | + "<p style=\"color: red; background-image: url('javascript:alert(1)');\">Test</p>" |
| 89 | + ) |
| 90 | + expected_output = '<p style="color: red">Test</p>' |
| 91 | + |
| 92 | + sanitized = sanitize_html( |
| 93 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 94 | + ) |
| 95 | + self.assertEqual(sanitized, expected_output) |
| 96 | + |
| 97 | + def test_completely_malicious_input(self): |
| 98 | + """Ensure that completely malicious input is sanitized appropriately.""" |
| 99 | + input_html = '<img src="x" onerror="alert(1)" /><script>alert("XSS")</script>' |
| 100 | + expected_output = '<img src="x"/>' |
| 101 | + |
| 102 | + sanitized = sanitize_html( |
| 103 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 104 | + ) |
| 105 | + self.assertEqual(sanitized, expected_output) |
| 106 | + |
| 107 | + def test_preserve_allowed_and_remove_disallowed_mixed(self): |
| 108 | + """Ensure that allowed and disallowed elements are correctly handled when mixed.""" |
| 109 | + input_html = """ |
| 110 | + <p>This is a <strong>strong</strong> and <em>emphasized</em> text.</p> |
| 111 | + <script>alert('XSS');</script> |
| 112 | + <a href="https://example.com" onclick="stealCookies()">Example Link</a> |
| 113 | + <div style="color: green; font-size: 12px;">Div content</div> |
| 114 | + """ |
| 115 | + expected_output = """ |
| 116 | + <p>This is a <strong>strong</strong> and <em>emphasized</em> text.</p> |
| 117 | +
|
| 118 | + <a href="https://example.com">Example Link</a> |
| 119 | +
|
| 120 | + <div style="color: green; font-size: 12px">Div content</div> |
| 121 | + """.strip() |
| 122 | + |
| 123 | + sanitized = sanitize_html( |
| 124 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 125 | + ) |
| 126 | + |
| 127 | + # Normalize spaces: |
| 128 | + sanitized = " ".join(sanitized.split()) |
| 129 | + expected_output = " ".join(expected_output.split()) |
| 130 | + |
| 131 | + self.assertEqual(sanitized, expected_output) |
| 132 | + |
| 133 | + def test_allow_links_with_safe_href(self): |
| 134 | + """Ensure that links with safe href attributes are preserved.""" |
| 135 | + input_html = '<a href="https://example.com" title="Example">Visit Example</a>' |
| 136 | + expected_output = ( |
| 137 | + '<a href="https://example.com" title="Example">Visit Example</a>' |
| 138 | + ) |
| 139 | + sanitized = sanitize_html( |
| 140 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 141 | + ) |
| 142 | + self.assertEqual(sanitized, expected_output) |
| 143 | + |
| 144 | + def test_allow_empty_style_attribute(self): |
| 145 | + """Ensure that empty style attributes are removed.""" |
| 146 | + input_html = '<p style="">No styles</p>' |
| 147 | + expected_output = "<p>No styles</p>" |
| 148 | + sanitized = sanitize_html( |
| 149 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 150 | + ) |
| 151 | + self.assertEqual(sanitized, expected_output) |
| 152 | + |
| 153 | + def test_preserve_text_only(self): |
| 154 | + """Ensure that plain text without HTML remains unchanged.""" |
| 155 | + input_html = "Just plain text without HTML." |
| 156 | + expected_output = "Just plain text without HTML." |
| 157 | + sanitized = sanitize_html( |
| 158 | + input_html, self.allowed_tags, self.allowed_attrs, self.allowed_styles |
| 159 | + ) |
| 160 | + self.assertEqual(sanitized, expected_output) |
| 161 | + |
| 162 | + @override_settings( |
| 163 | + ALLOWED_HTML_TAGS_INPUT=["p", "a"], |
| 164 | + ALLOWED_HTML_ATTRIBUTES_INPUT=["href"], |
| 165 | + ALLOWED_HTML_STYLES_INPUT=[], |
| 166 | + ) |
| 167 | + def test_dynamic_allowed_tags(self): |
| 168 | + """Ensure that sanitizer uses dynamically overridden settings.""" |
| 169 | + input_html = ( |
| 170 | + "<p>Paragraph with" |
| 171 | + '<a href="https://example.com" title="Example">link</a>' |
| 172 | + "and <b>bold</b>.</p>" |
| 173 | + ) |
| 174 | + # With settings overridden, 'a' allows 'href' only, 'b' is disallowed |
| 175 | + expected = '<p>Paragraph with <a href="https://example.com">link</a> and .</p>' |
| 176 | + sanitized = sanitize_html( |
| 177 | + html=input_html, |
| 178 | + allowed_tags=settings.ALLOWED_HTML_TAGS_INPUT, |
| 179 | + allowed_attrs=settings.ALLOWED_HTML_ATTRIBUTES_INPUT, |
| 180 | + allowed_styles=settings.ALLOWED_HTML_STYLES_INPUT, |
| 181 | + ) |
| 182 | + self.assertEqual(sanitized, expected) |
0 commit comments