All Projects → darylldoyle → Svg Sanitizer

darylldoyle / Svg Sanitizer

Licence: gpl-2.0
A PHP SVG/XML Sanitizer

Projects that are alternatives of or similar to Svg Sanitizer

Deck
Slide Decks
Stars: ✭ 261 (-6.79%)
Mutual labels:  xml, svg
Snowflake
❄️ SVG in Swift
Stars: ✭ 924 (+230%)
Mutual labels:  xml, svg
Leanify
lightweight lossless file minifier/optimizer
Stars: ✭ 694 (+147.86%)
Mutual labels:  xml, svg
Onthefly
🔗 Generate TinySVG, HTML and CSS on the fly
Stars: ✭ 37 (-86.79%)
Mutual labels:  xml, svg
Svgdom
Straightforward DOM implementation to make SVG.js run headless on Node.js
Stars: ✭ 154 (-45%)
Mutual labels:  xml, svg
Minify
Go minifiers for web formats
Stars: ✭ 2,824 (+908.57%)
Mutual labels:  xml, svg
Macsvg
macSVG - An open-source macOS app for designing HTML5 SVG (Scalable Vector Graphics) art and animation with a WebKit web view ➤➤➤
Stars: ✭ 789 (+181.79%)
Mutual labels:  xml, svg
I7j Pdfhtml
pdfHTML is an iText 7 add-on for Java that allows you to easily convert HTML and CSS into standards compliant PDFs that are accessible, searchable and usable for indexing.
Stars: ✭ 104 (-62.86%)
Mutual labels:  xml, svg
Svgo
Go Language Library for SVG generation
Stars: ✭ 1,779 (+535.36%)
Mutual labels:  xml, svg
Parse
Go parsers for web formats
Stars: ✭ 224 (-20%)
Mutual labels:  xml, svg
Swift Utils
A collection of handy swift utils
Stars: ✭ 253 (-9.64%)
Mutual labels:  xml, svg
Loaders.gl
Loaders for big data visualization. Website:
Stars: ✭ 272 (-2.86%)
Mutual labels:  xml
Floweaver
View flow data as Sankey diagrams
Stars: ✭ 266 (-5%)
Mutual labels:  svg
Androidsvgdrawable Plugin
Gradle plugin that generates qualified, density specific PNG drawables from SVG files at build time for your Android projects.
Stars: ✭ 263 (-6.07%)
Mutual labels:  svg
Dita Ot
DITA Open Toolkit — the open-source XML publishing engine for content authored in the Darwin Information Typing Architecture.
Stars: ✭ 279 (-0.36%)
Mutual labels:  xml
Jackrabbit
Mirror of Apache Jackrabbit
Stars: ✭ 273 (-2.5%)
Mutual labels:  xml
Tableexport
tableExport(table导出文件,支持json、csv、txt、xml、word、excel、image、pdf)
Stars: ✭ 261 (-6.79%)
Mutual labels:  xml
Xreader
XML, NEWS, RSS & Scrapping Reader maked in Xamarin, for educational purpose.
Stars: ✭ 259 (-7.5%)
Mutual labels:  xml
Php Curl Class
PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs
Stars: ✭ 2,903 (+936.79%)
Mutual labels:  xml
Waterfall Toolbar
Stars: ✭ 282 (+0.71%)
Mutual labels:  xml

svg-sanitizer

Build Status Test Coverage

This is my attempt at building a decent SVG sanitizer in PHP. The work is laregely borrowed from DOMPurify.

Installation

Either require enshrined/svg-sanitize through composer or download the repo and include the old way!

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

Basic Example

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).

Options

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

These methods require that you implement the enshrined\svgSanitize\data\TagInterface or enshrined\svgSanitize\data\AttributeInterface.

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.

This defaults to false, set to true to remove references.

$sanitizer->removeRemoteReferences(true);

Viewing Sanitization Issues

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();

Minification

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

Demo

There is a demo available at: http://svg.enshrined.co.uk/

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/

Drupal

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

TYPO3

An integration for TYPO3 CMS of this library is available as composer package t3g/svg-sanitizer at https://github.com/TYPO3GmbH/svg_sanitizer

Tests

You can run these by running vendor/bin/phpunit from the base directory of this package.

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.

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

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!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].