From 9655ae529b627895c1faf9d8942def7bacb22a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Dufraisse?= Date: Fri, 4 Feb 2022 11:40:46 +0100 Subject: [PATCH] feat(svg): sanitize file jsut after saving on server --- composer.json | 8 ++-- composer.lock | 68 ++++++++++++++++++++++++++------ tools/attach/libs/attach.lib.php | 23 +++++++++++ tools/attach/libs/qq.lib.php | 3 ++ 4 files changed, 88 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 97c718f73..ded240958 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,8 @@ "scripts": { "test": "phpunit --do-not-cache-result --stderr tests", "post-install-cmd": [ - "@composer install --working-dir ./tools/autoupdate/" + "@composer install --working-dir ./tools/autoupdate/", + "@php -r \"array_map('unlink', glob('vendor/enshrined/svg-sanitize/tests/data/*.svg'));\"" ], "post-update-cmd": [ "@composer update --working-dir ./tools/autoupdate/" @@ -16,11 +17,12 @@ }, "require": { "php": "^7.3 || ^8.0", + "ext-json": "*", + "ext-mysqli": "*", "caxy/php-htmldiff": "^0.1.13", "doctrine/annotations": "^1.11", "doctrine/cache": "^1.10", - "ext-json": "*", - "ext-mysqli": "*", + "enshrined/svg-sanitize": "^0.14.1", "oomphinc/composer-installers-extender": "^2.0", "phpmailer/phpmailer": "^6.2", "symfony/config": "^5.1", diff --git a/composer.lock b/composer.lock index f271d0b02..03164085f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a106897404dfaebd331e5f54b89ccf18", + "content-hash": "215040317134baa2df64f1754c381c80", "packages": [ { "name": "caxy/php-htmldiff", @@ -465,6 +465,52 @@ ], "time": "2022-01-12T08:27:12+00:00" }, + { + "name": "enshrined/svg-sanitize", + "version": "0.14.1", + "source": { + "type": "git", + "url": "https://github.com/darylldoyle/svg-sanitizer.git", + "reference": "307b42066fb0b76b5119f5e1f0826e18fefabe95" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/darylldoyle/svg-sanitizer/zipball/307b42066fb0b76b5119f5e1f0826e18fefabe95", + "reference": "307b42066fb0b76b5119f5e1f0826e18fefabe95", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-libxml": "*", + "php": "^7.0 || ^8.0" + }, + "require-dev": { + "codeclimate/php-test-reporter": "^0.1.2", + "phpunit/phpunit": "^6.5 || ^8.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "enshrined\\svgSanitize\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-2.0-or-later" + ], + "authors": [ + { + "name": "Daryll Doyle", + "email": "daryll@enshrined.co.uk" + } + ], + "description": "An SVG sanitizer for PHP", + "support": { + "issues": "https://github.com/darylldoyle/svg-sanitizer/issues", + "source": "https://github.com/darylldoyle/svg-sanitizer/tree/0.14.1" + }, + "time": "2021-08-09T23:46:54+00:00" + }, { "name": "ezyang/htmlpurifier", "version": "v4.14.0", @@ -2725,16 +2771,16 @@ }, { "name": "twig/twig", - "version": "v3.3.7", + "version": "v3.3.8", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "8f168c6ffa3ce76d1786b3cd52275424a3fc675b" + "reference": "972d8604a92b7054828b539f2febb0211dd5945c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/8f168c6ffa3ce76d1786b3cd52275424a3fc675b", - "reference": "8f168c6ffa3ce76d1786b3cd52275424a3fc675b", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/972d8604a92b7054828b539f2febb0211dd5945c", + "reference": "972d8604a92b7054828b539f2febb0211dd5945c", "shasum": "" }, "require": { @@ -2785,7 +2831,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.3.7" + "source": "https://github.com/twigphp/Twig/tree/v3.3.8" }, "funding": [ { @@ -2797,7 +2843,7 @@ "type": "tidelift" } ], - "time": "2022-01-03T21:15:37+00:00" + "time": "2022-02-04T06:59:48+00:00" }, { "name": "yeswiki/theme-margot", @@ -2926,12 +2972,12 @@ }, "type": "library", "autoload": { - "psr-4": { - "DeepCopy\\": "src/DeepCopy/" - }, "files": [ "src/DeepCopy/deep_copy.php" - ] + ], + "psr-4": { + "DeepCopy\\": "src/DeepCopy/" + } }, "notification-url": "https://packagist.org/downloads/", "license": [ diff --git a/tools/attach/libs/attach.lib.php b/tools/attach/libs/attach.lib.php index 5c82890a6..f123206cb 100644 --- a/tools/attach/libs/attach.lib.php +++ b/tools/attach/libs/attach.lib.php @@ -34,6 +34,7 @@ # voir actions/attach.php ppour la documentation # copyrigth Eric Feldstein 2003-2004 +use enshrined\svgSanitize\Sanitizer; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use YesWiki\Core\Service\LinkTracker; @@ -734,6 +735,9 @@ public function performUpload() $srcFile = $_FILES['upFile']['tmp_name']; if (move_uploaded_file($srcFile, $destFile)) { chmod($destFile, 0644); + if ($ext === "svg") { + $this->sanitizeSVGfile($destFile); + } header("Location: " . $this->wiki->href("", $this->wiki->GetPageTag(), "")); } else { echo "
" . _t('ERROR_MOVING_TEMPORARY_FILE') . "
\n"; @@ -1173,5 +1177,24 @@ public function redimensionner_image($image_src, $image_dest, $largeur, $hauteur return $imgTrans->targetFile; } } + + /** + * @param string $content of svg + * @return string $content + */ + public function sanitizeSVG(string $content): string + { + $sanitizer = new Sanitizer(); + return $sanitizer->sanitize($content); + } + + /** + * @param string $filePath svg + */ + public function sanitizeSVGfile(string $filePath) + { + $content = file_get_contents($filePath); + file_put_contents($filePath, $this->sanitizeSVG($content)); + } } } diff --git a/tools/attach/libs/qq.lib.php b/tools/attach/libs/qq.lib.php index b2634b125..c738e456f 100644 --- a/tools/attach/libs/qq.lib.php +++ b/tools/attach/libs/qq.lib.php @@ -204,6 +204,9 @@ public function handleUpload($uploadDirectory, $replaceOldFile = false) ob_end_clean(); if ($this->file->save($fullfilename)) { + if ($ext === "svg") { + $attach->sanitizeSVGfile($fullfilename); + } return array_map('utf8_encode', array('success'=>true, 'filename'=>$fullfilename, 'simplefilename'=>$filename . '.' . $ext, 'extension'=>$ext)); } else { return array_map(