Skip to content

Commit 027965b

Browse files
committed
Add a utility to sanitize SVG files
The sanitizer library is the underlying library of several popular extensions for large CMS systems.
1 parent 6789703 commit 027965b

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

webapp/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"doctrine/doctrine-migrations-bundle": "^3.2",
6464
"doctrine/orm": "^2.14",
6565
"eligrey/filesaver": "2.*",
66+
"enshrined/svg-sanitize": "^0.21.0",
6667
"fortawesome/font-awesome": "6.*",
6768
"friendsofsymfony/rest-bundle": "^3.5",
6869
"ircmaxell/password-compat": "*",

webapp/composer.lock

Lines changed: 46 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp/src/Utils/Utils.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use DateTime;
55
use Doctrine\Inflector\InflectorFactory;
6+
use enshrined\svgSanitize\Sanitizer as SvgSanitizer;
67
use Symfony\Component\HttpFoundation\StreamedResponse;
78
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
89

@@ -717,6 +718,15 @@ public static function getImageSize(string $filename): array
717718
return [$width, $height, $width / $height];
718719
}
719720

721+
public static function sanitizeSvg(string $svgContents): string | false
722+
{
723+
$sanitizer = new SvgSanitizer();
724+
$sanitizer->removeRemoteReferences(true);
725+
$sanitizer->minify(true);
726+
727+
return $sanitizer->sanitize($svgContents);
728+
}
729+
720730
/**
721731
* Returns TRUE iff string $haystack starts with string $needle.
722732
*/

0 commit comments

Comments
 (0)