@@ -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}
0 commit comments