Skip to content

Commit

Permalink
Sanitize SVG images
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Jul 7, 2021
1 parent c3886c9 commit 1d72b74
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -28,7 +28,8 @@
"nyholm/psr7": "^1.2",
"doctrine/dbal": "~2.0",
"psr/http-message": "~1.0",
"voku/portable-ascii": "^1.4"
"voku/portable-ascii": "^1.4",
"enshrined/svg-sanitize": "^0.14"
},
"require-dev": {
"php-coveralls/php-coveralls": "~2.0",
Expand Down
9 changes: 9 additions & 0 deletions lib/mwlib/src/MW/Media/Image/Svg.php
Expand Up @@ -10,6 +10,8 @@

namespace Aimeos\MW\Media\Image;

use enshrined\svgSanitize\Sanitizer;


/**
* Image class for SVG files
Expand Down Expand Up @@ -40,6 +42,13 @@ public function __construct( string $content, string $mimetype, array $options )
$content = $string;
}

$sanitizer = new Sanitizer();
$sanitizer->removeRemoteReferences( true );

if( ( $content = $sanitizer->sanitize( $content ) ) === false ) {
throw new \Aimeos\MW\Media\Exception( 'Invalid SVG file: ' . print_r( $sanitizer->getXmlIssues(), true ) );
}

if( ( $this->svg = @simplexml_load_string( $content ) ) === false ) {
throw new \Aimeos\MW\Media\Exception( 'Invalid SVG file' );
}
Expand Down
2 changes: 1 addition & 1 deletion lib/mwlib/tests/MW/Media/Image/SvgTest.php
Expand Up @@ -70,7 +70,7 @@ public function testSaveContent()
$media = new \Aimeos\MW\Media\Image\Svg( $this->content, 'image/svg+xml', [] );
$result = $media->save();

$this->assertStringStartsWith( '<?xml version="1.0" standalone="yes"?>', $result );
$this->assertStringStartsWith( '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>', $result );
}


Expand Down

0 comments on commit 1d72b74

Please sign in to comment.