Skip to content

Commit 8f96404

Browse files
committed
Add a basic test for SVG sanitation
1 parent 4b78a93 commit 8f96404

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

webapp/tests/Unit/Utils/UtilsTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,54 @@ public function provideTestGetImageSize(): Generator
713713
yield [__DIR__ . '/../../../public/images/DOMjudgelogo.svg', 510, 1122];
714714
}
715715

716+
public function testSanitizeSvg(): void
717+
{
718+
$dirty = <<<EOF
719+
<?xml version="1.0" encoding="utf-8" ?>
720+
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
721+
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve">
722+
<rect fill="url('http://example.com/benis.svg')" x="0" y="0" width="1000" height="1000"></rect>
723+
<rect fill="url('https://example.com/benis.svg')" x="0" y="0" width="1000" height="1000"></rect>
724+
<rect fill=" url( ' https://example.com/benis.svg ' ) " x="0" y="0" width="1000" height="1000"></rect>
725+
<rect fill="url('ftp://192.168.2.1/benis.svg')" x="0" y="0" width="1000" height="1000"></rect>
726+
<rect fill="url('//example.com/benis.svg')" x="0" y="0" width="1000" height="1000"></rect>
727+
<rect fill="url('/benis.svg')" x="0" y="0" width="1000" height="1000"></rect>
728+
<rect fill="url('#benis.svg')" x="0" y="0" width="1000" height="1000"></rect>
729+
<g id="righteye" class="eye">
730+
<path id="iris-2" data-name="iris" class="cls-4" d="M241.4,143.6s18.5,11.9,36,7.1,29.6-15.8,27.2-24.6c-1.7-6-9.8-9.4-20.3-9.4a59.21,59.21,0,0,0-15.6,2.2,37.44,37.44,0,0,0-12.4,6.4,60.14,60.14,0,0,0-14.9,18.3" transform="translate(-9.7 -9.3)"/>
731+
<path id="lid" class="cls-11" d="M304.5,124.4c-1.7-6-9.8-9.4-20.3-9.4a59.21,59.21,0,0,0-15.6,2.2,37.44,37.44,0,0,0-12.4,6.4,61.21,61.21,0,0,0-14.9,18.1" transform="translate(-9.7 -9.3)"/>
732+
<path id="pupil-2" data-name="pupil" class="cls-12" d="M256.7,126.1c2.5,9.2,11,14.8,18.9,12.6s12.3-11.4,9.8-20.6a16.59,16.59,0,0,0-1.2-3.1,59.21,59.21,0,0,0-15.6,2.2,37.44,37.44,0,0,0-12.4,6.4,9.23,9.23,0,0,0,.5,2.5" transform="translate(-9.7 -9.3)"/>
733+
<path id="eyelash-2" data-name="eyelash" class="cls-13" d="M302.9,122.3c7.7,2.5,17-5,20.8-16.8M292,115.7c7.6,2.8,17.2-4.4,21.4-16M277,115.1c8.1-.3,14.3-10.5,13.9-22.8" transform="translate(-9.7 -9.3)"/>
734+
<path id="reflection-2" data-name="reflection" class="cls-14" d="M271.1,127.1c0,3.6-2.6,6.5-5.8,6.5s-5.8-2.9-5.8-6.5,2.6-6.4,5.8-6.4,5.8,2.9,5.8,6.4" transform="translate(-9.7 -9.3)"/>
735+
</g>
736+
<a href="javascript:alert(2)">test 1</a>
737+
<a xlink:href="javascript:alert(2)">test 2</a>
738+
<a href="#test3">test 3</a>
739+
<a xlink:href="#test">test 4</a>
740+
741+
<a href="data:data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' onload='alert(88)'%3E%3C/svg%3E">test 5</a>
742+
<a xlink:href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' onload='alert(88)'%3E%3C/svg%3E">test 6</a>
743+
<use xlink:href="defs.svg#icon-1"/>
744+
<line onload="alert(2)" fill="none" stroke="#000000" stroke-miterlimit="10" x1="119" y1="84.5" x2="454" y2="84.5"/>
745+
<line fill="none" stroke="#000000" stroke-miterlimit="10" x1="111.212" y1="102.852" x2="112.032" y2="476.623"/>
746+
<line fill="none" stroke="#000000" stroke-miterlimit="10" x1="198.917" y1="510.229" x2="486.622" y2="501.213"/>
747+
<line fill="none" stroke="#000000" stroke-miterlimit="10" x1="484.163" y1="442.196" x2="89.901" y2="60.229"/>
748+
<line fill="none" stroke="#000000" stroke-miterlimit="10" x1="101.376" y1="478.262" x2="443.18" y2="75.803"/>
749+
<line fill="none" stroke="#000000" stroke-miterlimit="10" x1="457.114" y1="126.623" x2="458.753" y2="363.508"/>
750+
<this>shouldn't be here</this>
751+
<script>alert(1);</script>
752+
<line fill="none" stroke="#000000" stroke-miterlimit="10" x1="541.54" y1="299.573" x2="543.179" y2="536.458"/>
753+
754+
</svg>
755+
EOF;
756+
$clean = Utils::sanitizeSvg($dirt);
757+
self::assert(!str_contains($clean, "script"));
758+
self::assert(!str_contains($clean, "alert"));
759+
self::assert(!str_contains($clean, "shouldn't be here"));
760+
self::assert(!str_contains($clean, "example.com"));
761+
self::assert(str_contains($clean, '<line fill="none" stroke="#000000" stroke-miterlimit="10" x1="541.54" y1="299.573" x2="543.179" y2="536.458"/>'));
762+
}
763+
716764
/**
717765
* Test that the wrapUnquoted function returns the correct result
718766
*/

0 commit comments

Comments
 (0)