Skip to content

Commit

Permalink
use svg-sanitize to sanitize SVG files
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Feb 20, 2018
1 parent 936ceac commit 60d693d
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"ifixit/php-akismet" : "1.*",
"google/recaptcha" : "~1.1",
"dapphp/securimage" : "3.6.*",
"enshrined/svg-sanitize" : "0.8.2",

"bower-asset/jquery" : "1.12.3",
"bower-asset/jquery-timeago" : "1.5.2",
Expand Down
135 changes: 134 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions ext/handle_svg/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
* Description: Handle static SVG files. (No thumbnail is generated for SVG files)
*/

use enshrined\svgSanitize\Sanitizer;

class SVGFileHandler extends Extension {
public function onDataUpload(DataUploadEvent $event) {
if($this->supported_ext($event->type) && $this->check_contents($event->tmpname)) {
$hash = $event->hash;
move_upload_to_archive($event);

$sanitizer = new Sanitizer();
$sanitizer->removeRemoteReferences(true);
$dirtySVG = file_get_contents($event->tmpname);
$cleanSVG = $sanitizer->sanitize($dirtySVG);
file_put_contents(warehouse_path("images", $hash), $cleanSVG);

send_event(new ThumbnailGenerationEvent($event->hash, $event->type));
$image = $this->create_image_from_data(warehouse_path("images", $hash), $event->metadata);
if(is_null($image)) {
Expand Down Expand Up @@ -46,7 +54,12 @@ public function onPageRequest(PageRequestEvent $event) {

$page->set_type("image/svg+xml");
$page->set_mode("data");
$page->set_data(file_get_contents(warehouse_path("images", $hash)));

$sanitizer = new Sanitizer();
$sanitizer->removeRemoteReferences(true);
$dirtySVG = file_get_contents(warehouse_path("images", $hash));
$cleanSVG = $sanitizer->sanitize($dirtySVG);
$page->set_data($cleanSVG);
}
}

Expand Down
8 changes: 8 additions & 0 deletions ext/handle_svg/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@ public function testSVGHander() {
# FIXME: test that the thumb works
# FIXME: test that it gets displayed properly
}

public function testAbuiveSVG() {

This comment has been minimized.

Copy link
@lsiudut

lsiudut Feb 21, 2018

Abusive?

This comment has been minimized.

Copy link
@shish

shish Feb 22, 2018

Author Owner

Gah :P

This comment has been minimized.

Copy link
@lsiudut

lsiudut Feb 22, 2018

Yea not that it matters, this is just my horrible habit from reviewing diffs :D

$this->log_in_as_user();
$image_id = $this->post_image("tests/alert.svg", "something");
$this->get_page("post/view/$image_id");
$this->get_page("get_svg/$image_id");
$this->assert_no_content("script");
}
}

8 changes: 8 additions & 0 deletions tests/alert.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 60d693d

Please sign in to comment.