Skip to content

Commit 9528c0b

Browse files
authored
Merge pull request #3249 from dlubitz/revert-sanitize-svg
Revert "BUGFIX: Sanitize uploaded svg files for suspicious contents"
2 parents c301443 + db8f06e commit 9528c0b

File tree

5 files changed

+6
-100
lines changed

5 files changed

+6
-100
lines changed

Neos.Flow/Classes/ResourceManagement/ResourceManager.php

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
* source code.
1212
*/
1313

14-
use enshrined\svgSanitize\Sanitizer;
1514
use Neos\Flow\Annotations as Flow;
1615
use Neos\Flow\Log\Utility\LogEnvironment;
1716
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
1817
use Neos\Flow\Persistence\PersistenceManagerInterface;
19-
use Neos\Utility\MediaTypes;
2018
use Neos\Utility\ObjectAccess;
2119
use Neos\Flow\ResourceManagement\Storage\StorageInterface;
2220
use Neos\Flow\ResourceManagement\Storage\WritableStorageInterface;
@@ -162,30 +160,14 @@ public function importResource($source, $collectionName = ResourceManager::DEFAU
162160
$collection = $this->collections[$collectionName];
163161

164162
try {
165-
if (is_resource($source)) {
166-
$mediaType = MediaTypes::getMediaTypeFromResource($source);
167-
if ($this->isSanitizingRequired($mediaType)) {
168-
$content = stream_get_contents($source);
169-
$resource = $this->importResourceFromContent($content, '', $collectionName, $forcedPersistenceObjectIdentifier);
170-
} else {
171-
$resource = $collection->importResource($source);
172-
}
173-
} else {
174-
$resource = fopen($source, 'rb');
175-
$mediaType = MediaTypes::getMediaTypeFromResource($resource);
176-
fclose($resource);
177-
if ($this->isSanitizingRequired($mediaType)) {
178-
$content = file_get_contents($source);
179-
$resource = $this->importResourceFromContent($content, '', $collectionName, $forcedPersistenceObjectIdentifier);
180-
} else {
181-
$resource = $collection->importResource($source);
182-
}
183-
$pathInfo = UnicodeFunctions::pathinfo($source);
184-
$resource->setFilename($pathInfo['basename']);
185-
}
163+
$resource = $collection->importResource($source);
186164
if ($forcedPersistenceObjectIdentifier !== null) {
187165
ObjectAccess::setProperty($resource, 'Persistence_Object_Identifier', $forcedPersistenceObjectIdentifier, true);
188166
}
167+
if (!is_resource($source)) {
168+
$pathInfo = UnicodeFunctions::pathinfo($source);
169+
$resource->setFilename($pathInfo['basename']);
170+
}
189171
} catch (Exception $exception) {
190172
throw new Exception(sprintf('Importing a file into the resource collection "%s" failed: %s', $collectionName, $exception->getMessage()), 1375197120, $exception);
191173
}
@@ -224,11 +206,6 @@ public function importResourceFromContent($content, $filename, $collectionName =
224206
throw new Exception(sprintf('Tried to import a file into the resource collection "%s" but no such collection exists. Please check your settings and the code which triggered the import.', $collectionName), 1380878131);
225207
}
226208

227-
$mediaType = MediaTypes::getMediaTypeFromFileContent($content);
228-
if ($this->isSanitizingRequired($mediaType)) {
229-
$content = $this->sanitizeImportedFileContent($mediaType, $content, $filename);
230-
}
231-
232209
/* @var CollectionInterface $collection */
233210
$collection = $this->collections[$collectionName];
234211

@@ -631,44 +608,6 @@ protected function initializeCollections()
631608
}
632609
}
633610

634-
/**
635-
* Decide weather the given media-type has to be sanitized
636-
* for now this only checks svg file to solve the issue here https://nvd.nist.gov/vuln/detail/CVE-2023-37611
637-
*
638-
* @todo create a feature from this and allow to register code for sanitizing file content before importing
639-
*/
640-
protected function isSanitizingRequired(string $mediaType): bool
641-
{
642-
return $mediaType === 'image/svg+xml';
643-
}
644-
645-
/**
646-
* Sanitize file content and remove content that is suspicious
647-
* for now this only checks svg file to solve the issue here https://nvd.nist.gov/vuln/detail/CVE-2023-37611
648-
*
649-
* @todo create a feature from this and allow to register code for sanitizing file content before importing
650-
*/
651-
protected function sanitizeImportedFileContent(string $mediaType, string $content, $filename = ''): string
652-
{
653-
if ($mediaType === 'image/svg+xml') {
654-
// @todo: Simplify again when https://github.com/darylldoyle/svg-sanitizer/pull/90 is merged and released.
655-
$previousXmlErrorHandling = libxml_use_internal_errors(true);
656-
$sanitizer = new Sanitizer();
657-
$sanitizedContent = $sanitizer->sanitize($content);
658-
libxml_clear_errors();
659-
libxml_use_internal_errors($previousXmlErrorHandling);
660-
$issues = $sanitizer->getXmlIssues();
661-
if ($issues && count($issues) > 0) {
662-
if ($sanitizedContent === false) {
663-
throw new Exception('Sanitizing of suspicious file "' . $filename . '" failed during import.', 1695395560);
664-
}
665-
$content = $sanitizedContent;
666-
$this->logger->warning(sprintf('Imported file "%s" contained suspicious content and was sanitized.', $filename), $issues);
667-
}
668-
}
669-
return $content;
670-
}
671-
672611
/**
673612
* Prepare an uploaded file to be imported as resource object. Will check the validity of the file,
674613
* move it outside of upload folder if open_basedir is enabled and check the filename.

Neos.Flow/composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@
5151

5252
"composer/composer": "^2.2.8",
5353

54-
"egulias/email-validator": "^2.1.17 || ^3.0",
55-
"enshrined/svg-sanitize": "^0.16.0"
54+
"egulias/email-validator": "^2.1.17 || ^3.0"
5655
},
5756
"require-dev": {
5857
"vimeo/psalm": "~4.30.0",

Neos.Utility.MediaTypes/Classes/MediaTypes.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,21 +1830,6 @@ public static function getMediaTypeFromFileContent(string $fileContent): string
18301830
return isset(self::$mediaTypeToFileExtension[$mediaType]) ? $mediaType : 'application/octet-stream';
18311831
}
18321832

1833-
/**
1834-
* Returns a Media Type based on the given resource
1835-
*
1836-
* @param resource $resource The resource to determine the media type from
1837-
* @return string The IANA Internet Media Type
1838-
*/
1839-
public static function getMediaTypeFromResource($resource): string
1840-
{
1841-
if (!is_resource($resource)) {
1842-
throw new \TypeError('Argument "resource" has to be a resource');
1843-
}
1844-
$mediaType = self::trimMediaType(mime_content_type($resource));
1845-
return isset(self::$mediaTypeToFileExtension[$mediaType]) ? $mediaType : 'application/octet-stream';
1846-
}
1847-
18481833
/**
18491834
* Returns the primary filename extension based on the given Media Type.
18501835
*

Neos.Utility.MediaTypes/Tests/Unit/MediaTypesTest.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,6 @@ public function getMediaTypeFromFileContent(string $filename, string $expectedMe
6868
self::assertSame($expectedMediaType, MediaTypes::getMediaTypeFromFileContent($fileContent));
6969
}
7070

71-
/**
72-
* @test
73-
* @dataProvider filesAndMediaTypes
74-
*/
75-
public function getMediaTypeFromResource(string $filename, string $expectedMediaType)
76-
{
77-
$filePath = __DIR__ . '/Fixtures/' . $filename;
78-
$resource = is_file($filePath) ? fopen($filePath, 'rb') : fopen('data://text/plain,', 'rb');
79-
if ($resource !== false) {
80-
self::assertSame($expectedMediaType, MediaTypes::getMediaTypeFromResource($resource));
81-
fclose($resource);
82-
} else {
83-
$this->fail('fixture ' . $filePath . ' could not be read');
84-
}
85-
}
86-
8771
/**
8872
* Data Provider
8973
*/

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"neos/composer-plugin": "^2.0",
3333
"composer/composer": "^2.2.8",
3434
"egulias/email-validator": "^2.1.17 || ^3.0",
35-
"enshrined/svg-sanitize": "^0.16.0",
3635
"typo3fluid/fluid": "~2.7.0",
3736
"guzzlehttp/psr7": "^1.8.4",
3837
"ext-mbstring": "*"

0 commit comments

Comments
 (0)