An SVG sanitizer for PHP
This package is auto-updated.
Last update: 2023-11-18 15:39:34 UTC
This is my attempt at building a decent SVG sanitizer in PHP. The work is largely borrowed from DOMPurify.
enshrined/svg-sanitize through composer or download the repo and include the old way!
Using this is fairly easy. Create a new instance of
enshrined\svgSanitize\Sanitizer and then call the
sanitize whilst passing in your dirty SVG/XML
use enshrined\svgSanitize\Sanitizer; // Create a new sanitizer instance $sanitizer = new Sanitizer(); // Load the dirty svg $dirtySVG = file_get_contents('filthy.svg'); // Pass it to the sanitizer and get it back clean $cleanSVG = $sanitizer->sanitize($dirtySVG); // Now do what you want with your clean SVG/XML data
This will either return a sanitized SVG/XML string or boolean
false if XML parsing failed (usually due to a badly formatted file).
You may pass your own whitelist of tags and attributes by using the
Sanitizer::setAllowedAttrs methods respectively.
These methods require that you implement the
You have the option to remove attributes that reference remote files, this will stop HTTP leaks but will add an overhead to the sanitizer.
This defaults to false, set to true to remove references.
You may use the
getXmlIssues() method to return an array of issues that occurred during sanitization.
This may be useful for logging or providing feedback to the user on why an SVG was refused.
$issues = $sanitizer->getXmlIssues();
You can minify the XML output by calling
There is a demo available at: http://svg.enshrined.co.uk/
I've just released a WordPress plugin containing this code so you can sanitize your WordPress uploads. It's available from the WordPress plugin directory: https://wordpress.org/plugins/safe-svg/
This SVG sanitizer library is used per default in the core of TYPO3 v9 and later versions. See corresponding changelog entry for more details.
You can run these by running
vendor/bin/phpunit from the base directory of this package.
Thanks to the work by gudmdharalds there's now a standalone scanner that can be used via the CLI.
Any errors will be output in JSON format. See the PR for an example.
Use it as follows:
php svg-scanner.php ~/svgs/myfile.svg
More extensive testing for the SVGs/XML would be lovely, I'll try and add these soon. If you feel like doing it for me, please do and make a PR!