2017 © Pedro Peláez
 

library svg-sanitize

An SVG sanitizer for PHP

image

enshrined/svg-sanitize

An SVG sanitizer for PHP

  • Thursday, July 19, 2018
  • by darylldoyle
  • Repository
  • 15 Watchers
  • 123 Stars
  • 158,229 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 7 Forks
  • 2 Open issues
  • 27 Versions
  • 47 % Grown

The README.md

svg-sanitizer

Build Status Test Coverage, (*1)

This is my attempt at building a decent SVG sanitizer in PHP. The work is largely borrowed from DOMPurify., (*2)

Installation

Either require enshrined/svg-sanitize through composer or download the repo and include the old way!, (*3)

Usage

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, (*4)

Basic Example, (*5)

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

Output

This will either return a sanitized SVG/XML string or boolean false if XML parsing failed (usually due to a badly formatted file)., (*6)

Options

You may pass your own whitelist of tags and attributes by using the Sanitizer::setAllowedTags and Sanitizer::setAllowedAttrs methods respectively., (*7)

These methods require that you implement the enshrined\svgSanitize\data\TagInterface or enshrined\svgSanitize\data\AttributeInterface., (*8)

Remove remote references

You have the option to remove attributes that reference remote files, this will stop HTTP leaks but will add an overhead to the sanitizer., (*9)

This defaults to false, set to true to remove references., (*10)

$sanitizer->removeRemoteReferences(true);, (*11)

Viewing Sanitization Issues

You may use the getXmlIssues() method to return an array of issues that occurred during sanitization., (*12)

This may be useful for logging or providing feedback to the user on why an SVG was refused., (*13)

$issues = $sanitizer->getXmlIssues();, (*14)

Minification

You can minify the XML output by calling $sanitizer->minify(true);., (*15)

Demo

There is a demo available at: http://svg.enshrined.co.uk/, (*16)

WordPress

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/, (*17)

Drupal

Michael Potter has kindly created a Drupal module for this library which is available at: https://www.drupal.org/project/svg_sanitizer, (*18)

TYPO3

This SVG sanitizer library is used per default in the core of TYPO3 v9 and later versions. See corresponding changelog entry for more details., (*19)

Tests

You can run these by running vendor/bin/phpunit from the base directory of this package., (*20)

Standalone scanning of files via CLI

Thanks to the work by gudmdharalds there's now a standalone scanner that can be used via the CLI., (*21)

Any errors will be output in JSON format. See the PR for an example., (*22)

Use it as follows: php svg-scanner.php ~/svgs/myfile.svg, (*23)

To-Do

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!, (*24)

The Versions

19/07 2018

dev-master

9999999-dev

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

19/07 2018

0.9.0

0.9.0.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

06/12 2017

0.8.2

0.8.2.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

06/12 2017

0.8.1

0.8.1.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

06/12 2017

0.8.0

0.8.0.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

31/08 2017

0.7.2

0.7.2.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

28/06 2017

0.7.1

0.7.1.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

20/06 2017

0.7.0

0.7.0.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

12/06 2017

0.6.0

0.6.0.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

09/06 2017

0.5.3.1

0.5.3.1

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

26/02 2017

0.5.3

0.5.3.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

23/02 2017

0.5.2

0.5.2.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

21/02 2017

0.5.1

0.5.1.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

21/02 2017

dev-develop

dev-develop

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

21/02 2017

0.5.0

0.5.0.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

02/12 2016

0.4.1

0.4.1.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

10/10 2016

0.4.0

0.4.0.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

27/02 2016

0.3.0

0.3.0.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

07/07 2015

0.2.1

0.2.1.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

26/06 2015

0.2.0

0.2.0.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

25/06 2015

0.1.6

0.1.6.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

24/06 2015

0.1.5

0.1.5.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

21/06 2015

0.1.4

0.1.4.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

21/06 2015

0.1.3

0.1.3.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

20/06 2015

0.1.2

0.1.2.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

20/06 2015

0.1.1

0.1.1.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires

19/06 2015

0.1.0

0.1.0.0

An SVG sanitizer for PHP

  Sources   Download

GPL-2.0+

The Development Requires