Skip to content

Commit 0d0c88c

Browse files
committed
Sanitize uploaded SVG files
1 parent f6e8355 commit 0d0c88c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

main/inc/lib/fileUpload.lib.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* @todo test and reorganise
1313
*/
1414

15+
use enshrined\svgSanitize\Sanitizer;
16+
1517
/**
1618
* Changes the file name extension from .php to .phps
1719
* Useful for securing a site.
@@ -191,6 +193,22 @@ function process_uploaded_file($uploaded_file, $show_output = true)
191193
return true;
192194
}
193195

196+
function sanitizeSvgFile(string $fullPath)
197+
{
198+
$fileType = mime_content_type($fullPath);
199+
200+
if ('image/svg+xml' !== $fileType) {
201+
return;
202+
}
203+
204+
$svgContent = file_get_contents($fullPath);
205+
206+
$sanitizer = new Sanitizer();
207+
$cleanSvg = $sanitizer->sanitize($svgContent);
208+
209+
file_put_contents($fullPath, $cleanSvg);
210+
}
211+
194212
/**
195213
* This function does the save-work for the documents.
196214
* It handles the uploaded file and adds the properties to the database
@@ -394,6 +412,7 @@ function handle_uploaded_document(
394412
$fileExists = file_exists($fullPath);
395413

396414
if (moveUploadedFile($uploadedFile, $fullPath)) {
415+
sanitizeSvgFile($fullPath);
397416
chmod($fullPath, $filePermissions);
398417

399418
if ($fileExists && $docId) {
@@ -577,6 +596,7 @@ function handle_uploaded_document(
577596
$filePath = $uploadPath.$fileSystemName;
578597

579598
if (moveUploadedFile($uploadedFile, $fullPath)) {
599+
sanitizeSvgFile($fullPath);
580600
chmod($fullPath, $filePermissions);
581601
// Put the document data in the database
582602
$documentId = add_document(

0 commit comments

Comments
 (0)