diff --git a/composer.json b/composer.json index 8d8a2c0a0da..4018f33190b 100644 --- a/composer.json +++ b/composer.json @@ -84,6 +84,7 @@ "symfony/finder": "^5.4 || ^6.0", "symfony/form": "^5.4 || ^6.0", "symfony/framework-bundle": "^5.4 || ^6.0", + "symfony/html-sanitizer": "^5.4 || ^6.0", "symfony/http-client": "^5.4 || ^6.0", "symfony/http-foundation": "^5.4 || ^6.0", "symfony/http-kernel": "^5.4 || ^6.0", @@ -252,10 +253,13 @@ "lint-rector": [ "Composer\\Config::disableProcessTimeout", "@php bin/websiteconsole cache:warmup --env=dev", + "@php bin/adminconsole cache:warmup --env=dev", "@php vendor/bin/rector process --dry-run" ], "rector": [ "Composer\\Config::disableProcessTimeout", + "@php bin/websiteconsole cache:warmup --env=dev", + "@php bin/adminconsole cache:warmup --env=dev", "@php vendor/bin/rector process" ], "lint-composer": "@composer validate --strict", diff --git a/src/Sulu/Bundle/MediaBundle/FileInspector/FileInspectorInterface.php b/src/Sulu/Bundle/MediaBundle/FileInspector/FileInspectorInterface.php new file mode 100644 index 00000000000..25e45d8494e --- /dev/null +++ b/src/Sulu/Bundle/MediaBundle/FileInspector/FileInspectorInterface.php @@ -0,0 +1,24 @@ +getContent(); + + if ($this->containsUnsafeContent($svg)) { + throw new UnsafeFileException($file); + } + + $cleanedUpSvg = $this->htmlSanitizer->sanitize($svg); + $safeSvg = $this->htmlSanitizerSafe->sanitize($svg); + + if ($this->normalizeString($cleanedUpSvg) !== $this->normalizeString($safeSvg)) { + throw new UnsafeFileException($file); + } + + return $file; + } + + private function containsUnsafeContent(string $svg): bool + { + \libxml_use_internal_errors(true); + $dom = new \DOMDocument(); + $dom->loadXML($svg, \LIBXML_NOENT | \LIBXML_DTDLOAD); + \libxml_clear_errors(); + + foreach (self::UNSAFE_ELEMENTS as $element) { + if ($dom->getElementsByTagName($element)->length > 0) { + return true; + } + } + + $xpath = new \DOMXPath($dom); + $xpath->registerNamespace('svg', 'http://www.w3.org/2000/svg'); + $xpath->registerNamespace('xlink', 'http://www.w3.org/1999/xlink'); + + foreach (self::UNSAFE_ATTRIBUTES as $attr) { + $query = "//*[starts-with(name(@*), '$attr') or @*[contains(., 'javascript:')]]"; + $node = $xpath->query($query); + if (false === $node || $node->length > 0) { + return true; + } + } + + // Check for event handlers + $eventHandlerQuery = "//*[@*[starts-with(name(), 'on')]]"; + $node = $xpath->query($eventHandlerQuery); + if (false === $node || $node->length > 0) { + return true; + } + + // Check for data URIs + if (\str_contains($svg, 'data:')) { + return true; + } + + return false; + } + + private function normalizeString(string $input): string + { + return \strtolower((string) \preg_replace('/\s+/', '', $input)); + } +} diff --git a/src/Sulu/Bundle/MediaBundle/FileInspector/SvgSanitizerFactory.php b/src/Sulu/Bundle/MediaBundle/FileInspector/SvgSanitizerFactory.php new file mode 100644 index 00000000000..7fcdc30b6ea --- /dev/null +++ b/src/Sulu/Bundle/MediaBundle/FileInspector/SvgSanitizerFactory.php @@ -0,0 +1,64 @@ +allowElement('svg', ['width', 'height', 'viewBox']) + ->allowElement('g', ['id']) + ->allowElement('path', ['d', 'fill', 'stroke', 'stroke-width', 'class']) + ->allowElement('circle', ['cx', 'cy', 'r', 'fill', 'stroke', 'stroke-width', 'class']) + ->allowElement('rect', ['x', 'y', 'width', 'height', 'fill', 'stroke', 'stroke-width', 'class']) + ->allowElement('line', ['x1', 'y1', 'x2', 'y2', 'stroke', 'stroke-width', 'class']) + ->allowElement('polyline', ['points', 'fill', 'stroke', 'stroke-width', 'class']) + ->allowElement('polygon', ['points', 'fill', 'stroke', 'stroke-width', 'class']) + ->allowElement('text', ['x', 'y', 'font-family', 'font-size', 'fill', 'class']) + ->allowElement('style') + ->allowAttribute('class', '*') + ->allowAttribute('style', '*'); + + return new HtmlSanitizer($config); + } + + public function create(): HtmlSanitizerInterface + { + $config = (new HtmlSanitizerConfig()) + ->allowElement('svg', ['width', 'height', 'viewBox']) + ->allowElement('g', ['id']) + ->allowElement('path', ['d', 'fill', 'stroke', 'stroke-width', 'class']) + ->allowElement('circle', ['cx', 'cy', 'r', 'fill', 'stroke', 'stroke-width', 'class']) + ->allowElement('rect', ['x', 'y', 'width', 'height', 'fill', 'stroke', 'stroke-width', 'class']) + ->allowElement('line', ['x1', 'y1', 'x2', 'y2', 'stroke', 'stroke-width', 'class']) + ->allowElement('polyline', ['points', 'fill', 'stroke', 'stroke-width', 'class']) + ->allowElement('polygon', ['points', 'fill', 'stroke', 'stroke-width', 'class']) + ->allowElement('text', ['x', 'y', 'font-family', 'font-size', 'fill', 'class']) + ->allowElement('style') + ->allowAttribute('class', '*') + ->allowAttribute('style', '*') + ->dropAttribute('xlink:href', '*') + ->dropAttribute('href', '*'); + + return new HtmlSanitizer($config); + } +} diff --git a/src/Sulu/Bundle/MediaBundle/FileInspector/UnsafeFileException.php b/src/Sulu/Bundle/MediaBundle/FileInspector/UnsafeFileException.php new file mode 100644 index 00000000000..5d6e0766cf1 --- /dev/null +++ b/src/Sulu/Bundle/MediaBundle/FileInspector/UnsafeFileException.php @@ -0,0 +1,31 @@ +getClientOriginalName(), + )); + } + + public function getUploadedFile(): UploadedFile + { + return $this->uploadedFile; + } +} diff --git a/src/Sulu/Bundle/MediaBundle/FileInspector/UploadFileSubscriber.php b/src/Sulu/Bundle/MediaBundle/FileInspector/UploadFileSubscriber.php new file mode 100644 index 00000000000..32db1ac1e58 --- /dev/null +++ b/src/Sulu/Bundle/MediaBundle/FileInspector/UploadFileSubscriber.php @@ -0,0 +1,71 @@ + + */ + public static function getSubscribedEvents(): array + { + return [ + 'kernel.request' => 'onKernelRequest', + ]; + } + + /** + * @var FileInspectorInterface[] + */ + private array $fileInspectors; + + /** + * @param iterable $fileInspectors + */ + public function __construct( + iterable $fileInspectors, + ) { + $this->fileInspectors = \iterator_to_array($fileInspectors); + } + + public function onKernelRequest(RequestEvent $event): void + { + $request = $event->getRequest(); + + /** + * @var string $key + * @var UploadedFile $file + */ + foreach ($request->files as $key => $file) { + foreach ($this->fileInspectors as $fileInspector) { + $mimeType = $file->getClientMimeType(); + if (null !== $mimeType && $fileInspector->supports($mimeType)) { + try { + $request->files->set($key, $fileInspector->inspect($file)); + } catch (UnsafeFileException $exception) { + throw new BadRequestHttpException($exception->getMessage(), $exception); + } + } + } + } + } +} diff --git a/src/Sulu/Bundle/MediaBundle/Resources/config/services.xml b/src/Sulu/Bundle/MediaBundle/Resources/config/services.xml index 30241d314d7..01d71f336b0 100644 --- a/src/Sulu/Bundle/MediaBundle/Resources/config/services.xml +++ b/src/Sulu/Bundle/MediaBundle/Resources/config/services.xml @@ -509,5 +509,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Sulu/Bundle/MediaBundle/Tests/Unit/SvgInspector/SvgFileInspectorTest.php b/src/Sulu/Bundle/MediaBundle/Tests/Unit/SvgInspector/SvgFileInspectorTest.php new file mode 100644 index 00000000000..d74fc39e74d --- /dev/null +++ b/src/Sulu/Bundle/MediaBundle/Tests/Unit/SvgInspector/SvgFileInspectorTest.php @@ -0,0 +1,79 @@ + ['', true]; + yield 'simple svg with text' => ['text', true]; + yield 'svg with attributes' => ['', true]; + yield 'svg with path' => ['', true]; + yield 'svg with style' => ['', true]; + + // Potentially unsafe SVGs + yield 'svg with script tag' => ['', false]; + yield 'svg with event handler' => ['', false]; + yield 'svg with iframe' => ['', false]; + yield 'svg with external reference' => ['', false]; + yield 'svg with data URI' => ['', false]; + yield 'svg with javascript in attribute' => ['Click me', false]; + + // Additional potentially unsafe SVGs + yield 'svg with onload event' => ['', false]; + yield 'svg with foreignObject' => ['', false]; + yield 'svg with base64 encoded script' => ['', false]; + yield 'svg with setTimeout' => ['', false]; + yield 'svg with external script' => ['', false]; + yield 'svg with animate element' => ['', false]; + yield 'svg with onclick event in text' => ['Click me!', false]; + yield 'svg with external fetch' => ['', false]; + yield 'svg with conditional script' => ['', false]; + yield 'svg with phishing link' => ['Click here for a prize!', false]; + } + + /** + * @dataProvider provideSvgs + */ + public function testIsSafe(string $svg, bool $expectedSafe): void + { + if (!$expectedSafe) { + $this->expectException(UnsafeFileException::class); + } + + $factory = new SvgSanitizerFactory(); + + \file_put_contents('test.svg', $svg); + $uploadedFile = new UploadedFile('test.svg', 'test.svg', 'image/svg+xml'); + + $svgSafetyInspector = new SvgFileInspector($factory->create(), $factory->createSafe()); + + $result = $svgSafetyInspector->inspect($uploadedFile); + + if ($expectedSafe) { + $this->assertSame($uploadedFile, $result); + } + } +} diff --git a/src/Sulu/Bundle/MediaBundle/Tests/Unit/SvgInspector/UploadFileSubscriberTest.php b/src/Sulu/Bundle/MediaBundle/Tests/Unit/SvgInspector/UploadFileSubscriberTest.php new file mode 100644 index 00000000000..b85391fbf7a --- /dev/null +++ b/src/Sulu/Bundle/MediaBundle/Tests/Unit/SvgInspector/UploadFileSubscriberTest.php @@ -0,0 +1,110 @@ + + */ + private ObjectProphecy $svgInspector; + + private UploadFileSubscriber $subscriber; + + protected function setUp(): void + { + $this->svgInspector = $this->prophesize(FileInspectorInterface::class); + $this->subscriber = new UploadFileSubscriber(new \ArrayObject([$this->svgInspector->reveal()])); + } + + public function testGetSubscribedEvents(): void + { + $this->assertSame( + ['kernel.request' => 'onKernelRequest'], + UploadFileSubscriber::getSubscribedEvents(), + ); + } + + public function testOnKernelRequestWithSafeSvg(): void + { + $mimeType = 'image/svg+xml'; + $uploadedFile = new UploadedFile('test.svg', 'test.svg', $mimeType, 0, true); + + $request = new Request([], [], [], [], ['file' => $uploadedFile]); + $event = $this->createRequestEvent($request); + + $this->svgInspector->supports($mimeType)->willReturn(true); + $this->svgInspector->inspect($uploadedFile)->willReturn($uploadedFile); + + $this->subscriber->onKernelRequest($event); + + // If we reach here without exception, the test passes + $this->addToAssertionCount(1); + } + + public function testOnKernelRequestWithUnsafeSvg(): void + { + $this->expectException(BadRequestHttpException::class); + + $mimeType = 'image/svg+xml'; + $uploadedFile = new UploadedFile('test.svg', 'test.svg', $mimeType, 0, true); + + $request = new Request([], [], [], [], ['file' => $uploadedFile]); + $event = $this->createRequestEvent($request); + + $this->svgInspector->supports($mimeType)->willReturn(true); + $this->svgInspector->inspect($uploadedFile)->willThrow(new UnsafeFileException($uploadedFile)); + + + $this->subscriber->onKernelRequest($event); + } + + public function testOnKernelRequestWithNonSvgFile(): void + { + $mimeType = 'image/jpeg'; + $uploadedFile = new UploadedFile('test.svg', 'test.svg', $mimeType, 0, true); + + $request = new Request([], [], [], [], ['file' => $uploadedFile]); + $event = $this->createRequestEvent($request); + + $this->svgInspector->supports($mimeType)->willReturn(false); + $this->svgInspector->inspect($uploadedFile)->shouldNotBeCalled(); + + $this->subscriber->onKernelRequest($event); + + // If we reach here without exception, the test passes + $this->addToAssertionCount(1); + } + + private function createRequestEvent(Request $request): RequestEvent + { + $kernel = $this->prophesize(HttpKernelInterface::class); + + return new RequestEvent($kernel->reveal(), $request, HttpKernelInterface::MAIN_REQUEST); + } +}