|
| 1 | +import tempfile |
| 2 | + |
| 3 | +from django.test import TestCase |
| 4 | + |
| 5 | +from ..sanitizer import sanitize_svg_content, sanitize_svg_file |
| 6 | + |
| 7 | + |
| 8 | +class SanitizeSvgFileTests(TestCase): |
| 9 | + def test_sanitize_svg_content_removes_script_tags(self): |
| 10 | + bad_svg_content = ( |
| 11 | + '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">' |
| 12 | + '<circle cx="25" cy="25" r="25" fill="green" />' |
| 13 | + "<script>//<![CDATA[" |
| 14 | + 'alert("I am malicious >:)")' |
| 15 | + "//]]></script>" |
| 16 | + "<g>" |
| 17 | + '<rect class="btn" x="0" y="0" width="10" height="10" fill="red" />' |
| 18 | + "</g>" |
| 19 | + "</svg>" |
| 20 | + ) |
| 21 | + sanitized_svg_content = ( |
| 22 | + '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">' |
| 23 | + '<circle cx="25" cy="25" r="25" fill="green"></circle>' |
| 24 | + "//" |
| 25 | + 'alert("I am malicious >:)")' |
| 26 | + "//" |
| 27 | + "<g>" |
| 28 | + '<rect x="0" y="0" width="10" height="10" fill="red"></rect>' |
| 29 | + "</g>" |
| 30 | + "</svg>" |
| 31 | + ) |
| 32 | + |
| 33 | + with tempfile.TemporaryFile() as temp_file: |
| 34 | + temp_file.write(bad_svg_content.encode("utf-8")) |
| 35 | + |
| 36 | + # Assert that sanitize_svg_content removed the script tag |
| 37 | + sanitized_svg_file = sanitize_svg_file(temp_file) |
| 38 | + self.assertEqual( |
| 39 | + sanitized_svg_file.read().decode("utf-8"), sanitized_svg_content |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +class SanitizeSvgContentTests(TestCase): |
| 44 | + def test_sanitize_svg_content_removes_script_tags(self): |
| 45 | + bad_svg_content = ( |
| 46 | + '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">' |
| 47 | + '<circle cx="25" cy="25" r="25" fill="green" />' |
| 48 | + "<script>//<![CDATA[" |
| 49 | + 'alert("I am malicious >:)")' |
| 50 | + "//]]></script>" |
| 51 | + "<g>" |
| 52 | + '<rect class="btn" x="0" y="0" width="10" height="10" fill="red" />' |
| 53 | + "</g>" |
| 54 | + "</svg>" |
| 55 | + ) |
| 56 | + sanitized_svg_content = ( |
| 57 | + '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">' |
| 58 | + '<circle cx="25" cy="25" r="25" fill="green"></circle>' |
| 59 | + "//" |
| 60 | + 'alert("I am malicious >:)")' |
| 61 | + "//" |
| 62 | + "<g>" |
| 63 | + '<rect x="0" y="0" width="10" height="10" fill="red"></rect>' |
| 64 | + "</g>" |
| 65 | + "</svg>" |
| 66 | + ) |
| 67 | + |
| 68 | + # Assert that sanitize_svg_content removed the script tag |
| 69 | + sanitized_bad_svg_content = sanitize_svg_content(bad_svg_content) |
| 70 | + self.assertEqual(sanitized_svg_content, sanitized_bad_svg_content) |
| 71 | + |
| 72 | + def test_sanitize_svg_content_removes_event_handlers(self): |
| 73 | + bad_svg_content = ( |
| 74 | + '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">' |
| 75 | + '<circle cx="25" cy="25" r="25" fill="green" onclick="alert(\'forget about me?\')" />' |
| 76 | + "<g>" |
| 77 | + '<rect class="btn" x="0" y="0" width="10" height="10" fill="red" onload="alert(\'click!\')" />' |
| 78 | + "</g>" |
| 79 | + "</svg>" |
| 80 | + ) |
| 81 | + sanitized_svg_content = ( |
| 82 | + '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">' |
| 83 | + '<circle cx="25" cy="25" r="25" fill="green"></circle>' |
| 84 | + "<g>" |
| 85 | + '<rect x="0" y="0" width="10" height="10" fill="red"></rect>' |
| 86 | + "</g>" |
| 87 | + "</svg>" |
| 88 | + ) |
| 89 | + |
| 90 | + # Assert that sanitize_svg_content removed the event handlers |
| 91 | + sanitized_bad_svg_content = sanitize_svg_content(bad_svg_content) |
| 92 | + self.assertEqual(sanitized_svg_content, sanitized_bad_svg_content) |
0 commit comments