|
| 1 | +import io |
| 2 | + |
| 3 | +from django.test import SimpleTestCase |
| 4 | + |
| 5 | +from ..sanitizer import sanitize_svg_content, sanitize_svg_file |
| 6 | + |
| 7 | + |
| 8 | +class SanitizeSvgFileTests(SimpleTestCase): |
| 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 = b""" |
| 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 | + temp_file = io.BytesIO(bad_svg_content.encode("utf-8")) |
| 34 | + |
| 35 | + # Assert that sanitize_svg_content removed the script tag |
| 36 | + sanitized_svg_file = sanitize_svg_file(temp_file) |
| 37 | + self.assertEqual( |
| 38 | + sanitized_svg_file.read(), sanitized_svg_content |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +class SanitizeSvgContentTests(SimpleTestCase): |
| 43 | + def test_sanitize_svg_content_removes_script_tags(self): |
| 44 | + bad_svg_content = """ |
| 45 | + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"> |
| 46 | + <circle cx="25" cy="25" r="25" fill="green" /> |
| 47 | + <script>//<![CDATA[ |
| 48 | + alert("I am malicious >:)") |
| 49 | + //]]></script> |
| 50 | + <g> |
| 51 | + <rect class="btn" x="0" y="0" width="10" height="10" fill="red" /> |
| 52 | + </g> |
| 53 | + </svg> |
| 54 | + """ |
| 55 | + sanitized_svg_content = """ |
| 56 | + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"> |
| 57 | + <circle cx="25" cy="25" r="25" fill="green"></circle> |
| 58 | + // |
| 59 | + alert("I am malicious >:)") |
| 60 | + // |
| 61 | + <g> |
| 62 | + <rect x="0" y="0" width="10" height="10" fill="red"></rect> |
| 63 | + </g> |
| 64 | + </svg> |
| 65 | + """ |
| 66 | + |
| 67 | + # Assert that sanitize_svg_content removed the script tag |
| 68 | + sanitized_bad_svg_content = sanitize_svg_content(bad_svg_content) |
| 69 | + self.assertEqual(sanitized_svg_content, sanitized_bad_svg_content) |
| 70 | + |
| 71 | + def test_sanitize_svg_content_removes_event_handlers(self): |
| 72 | + bad_svg_content = """ |
| 73 | + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"> |
| 74 | + <circle cx="25" cy="25" r="25" fill="green" onclick="alert('forget about me?')" /> |
| 75 | + <g> |
| 76 | + <rect class="btn" x="0" y="0" width="10" height="10" fill="red" onload="alert('click!')" /> |
| 77 | + </g> |
| 78 | + </svg> |
| 79 | + """ |
| 80 | + sanitized_svg_content = """ |
| 81 | + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"> |
| 82 | + <circle cx="25" cy="25" r="25" fill="green"></circle> |
| 83 | + <g> |
| 84 | + <rect x="0" y="0" width="10" height="10" fill="red"></rect> |
| 85 | + </g> |
| 86 | + </svg> |
| 87 | + """ |
| 88 | + |
| 89 | + # Assert that sanitize_svg_content removed the event handlers |
| 90 | + sanitized_bad_svg_content = sanitize_svg_content(bad_svg_content) |
| 91 | + self.assertEqual(sanitized_svg_content, sanitized_bad_svg_content) |
0 commit comments