Skip to content

Commit 2c4f44e

Browse files
committed
Sanatize SVG files when uploading
1 parent 2b69473 commit 2c4f44e

File tree

4 files changed

+137
-2
lines changed

4 files changed

+137
-2
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"omines/datatables-bundle": "^0.9.1",
4444
"paragonie/sodium_compat": "^1.21",
4545
"part-db/label-fonts": "^1.0",
46+
"rhukster/dom-sanitizer": "^1.0",
4647
"runtime/frankenphp-symfony": "^0.2.0",
4748
"s9e/text-formatter": "^2.1",
4849
"scheb/2fa-backup-code": "^6.8.0",

composer.lock

Lines changed: 46 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Services/Attachments/AttachmentSubmitHandler.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class AttachmentSubmitHandler
6565
'htpasswd', ''];
6666

6767
public function __construct(protected AttachmentPathResolver $pathResolver, protected bool $allow_attachments_downloads,
68-
protected HttpClientInterface $httpClient, protected MimeTypesInterface $mimeTypes,
68+
protected HttpClientInterface $httpClient, protected MimeTypesInterface $mimeTypes, protected readonly SVGSanitizer $SVGSanitizer,
6969
protected FileTypeFilterTools $filterTools, /**
7070
* @var string The user configured maximum upload size. This is a string like "10M" or "1G" and will be converted to
7171
*/
@@ -214,6 +214,9 @@ public function handleUpload(Attachment $attachment, ?AttachmentUpload $upload):
214214
//Move the attachment files to secure location (and back) if needed
215215
$this->moveFile($attachment, $secure_attachment);
216216

217+
//Sanitize the SVG if needed
218+
$this->sanitizeSVGFiles($attachment);
219+
217220
//Rename blacklisted (unsecure) files to a better extension
218221
$this->renameBlacklistedExtensions($attachment);
219222

@@ -498,4 +501,32 @@ public function getMaximumAllowedUploadSize(): int
498501

499502
return $this->max_upload_size_bytes;
500503
}
504+
505+
/**
506+
* Sanatizes the given SVG file, if the attachment is an internal SVG file.
507+
* @param Attachment $attachment
508+
* @return Attachment
509+
*/
510+
protected function sanitizeSVGFiles(Attachment $attachment): Attachment
511+
{
512+
//We can not do anything on builtins or external ressources
513+
if ($attachment->isBuiltIn() || !$attachment->hasInternal()) {
514+
return $attachment;
515+
}
516+
517+
//Resolve the path to the file
518+
$path = $this->pathResolver->placeholderToRealPath($attachment->getInternalPath());
519+
520+
//Check if the file exists
521+
if (!file_exists($path)) {
522+
return $attachment;
523+
}
524+
525+
//Check if the file is an SVG
526+
if ($attachment->getExtension() === "svg") {
527+
$this->SVGSanitizer->sanitizeFile($path);
528+
}
529+
530+
return $attachment;
531+
}
501532
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
24+
namespace App\Services\Attachments;
25+
26+
use Rhukster\DomSanitizer\DOMSanitizer;
27+
28+
class SVGSanitizer
29+
{
30+
31+
/**
32+
* Sanitizes the given SVG string by removing any potentially harmful content (like inline scripts).
33+
* @param string $input
34+
* @return string
35+
*/
36+
public function sanitizeString(string $input): string
37+
{
38+
return (new DOMSanitizer(DOMSanitizer::SVG))->sanitize($input);
39+
}
40+
41+
/**
42+
* Sanitizes the given SVG file by removing any potentially harmful content (like inline scripts).
43+
* The sanitized content is written back to the file.
44+
* @param string $filepath
45+
*/
46+
public function sanitizeFile(string $filepath): void
47+
{
48+
//Open the file and read the content
49+
$content = file_get_contents($filepath);
50+
if ($content === false) {
51+
throw new \RuntimeException('Could not read file: ' . $filepath);
52+
}
53+
//Sanitize the content
54+
$sanitizedContent = $this->sanitizeString($content);
55+
//Write the sanitized content back to the file
56+
file_put_contents($filepath, $sanitizedContent);
57+
}
58+
}

0 commit comments

Comments
 (0)