|
| 1 | +import base64 |
| 2 | +import gzip |
1 | 3 | import unittest |
2 | 4 | from lxml.tests.common_imports import make_doctest |
3 | 5 |
|
@@ -143,6 +145,49 @@ def test_sneaky_import_in_style(self): |
143 | 145 | cleaned, |
144 | 146 | "%s -> %s" % (style_code, cleaned)) |
145 | 147 |
|
| 148 | + def test_svg_data_links(self): |
| 149 | + # Remove SVG images with potentially insecure content. |
| 150 | + svg = b'<svg onload="alert(123)" />' |
| 151 | + svgz = gzip.compress(svg) |
| 152 | + svg_b64 = base64.b64encode(svg).decode('ASCII') |
| 153 | + svgz_b64 = base64.b64encode(svgz).decode('ASCII') |
| 154 | + urls = [ |
| 155 | + "data:image/svg+xml;base64," + svg_b64, |
| 156 | + "data:image/svg+xml-compressed;base64," + svgz_b64, |
| 157 | + ] |
| 158 | + for url in urls: |
| 159 | + html = '<img src="%s">' % url |
| 160 | + s = lxml.html.fragment_fromstring(html) |
| 161 | + |
| 162 | + cleaned = lxml.html.tostring(clean_html(s)) |
| 163 | + self.assertEqual( |
| 164 | + b'<img src="">', |
| 165 | + cleaned, |
| 166 | + "%s -> %s" % (url, cleaned)) |
| 167 | + |
| 168 | + def test_image_data_links(self): |
| 169 | + data = b'123' |
| 170 | + data_b64 = base64.b64encode(data).decode('ASCII') |
| 171 | + urls = [ |
| 172 | + "data:image/jpeg;base64," + data_b64, |
| 173 | + "data:image/apng;base64," + data_b64, |
| 174 | + "data:image/png;base64," + data_b64, |
| 175 | + "data:image/gif;base64," + data_b64, |
| 176 | + "data:image/webp;base64," + data_b64, |
| 177 | + "data:image/bmp;base64," + data_b64, |
| 178 | + "data:image/tiff;base64," + data_b64, |
| 179 | + "data:image/x-icon;base64," + data_b64, |
| 180 | + ] |
| 181 | + for url in urls: |
| 182 | + html = '<img src="%s">' % url |
| 183 | + s = lxml.html.fragment_fromstring(html) |
| 184 | + |
| 185 | + cleaned = lxml.html.tostring(clean_html(s)) |
| 186 | + self.assertEqual( |
| 187 | + html.encode("UTF-8"), |
| 188 | + cleaned, |
| 189 | + "%s -> %s" % (url, cleaned)) |
| 190 | + |
146 | 191 | def test_formaction_attribute_in_button_input(self): |
147 | 192 | # The formaction attribute overrides the form's action and should be |
148 | 193 | # treated as a malicious link attribute |
|
0 commit comments