Skip to content

Commit

Permalink
feat(svg): sanitize file jsut after saving on server
Browse files Browse the repository at this point in the history
  • Loading branch information
J9rem committed Feb 9, 2022
1 parent 95e2339 commit 9655ae5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
8 changes: 5 additions & 3 deletions composer.json
Expand Up @@ -8,19 +8,21 @@
"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/"
]
},
"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",
Expand Down
68 changes: 57 additions & 11 deletions composer.lock

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

23 changes: 23 additions & 0 deletions tools/attach/libs/attach.lib.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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 "<div class=\"alert alert-error alert-danger\">" . _t('ERROR_MOVING_TEMPORARY_FILE') . "</div>\n";
Expand Down Expand Up @@ -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));
}
}
}
3 changes: 3 additions & 0 deletions tools/attach/libs/qq.lib.php
Expand Up @@ -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(
Expand Down

0 comments on commit 9655ae5

Please sign in to comment.