Skip to content

Commit ab914ec

Browse files
authored
Merge pull request #59839 from nextcloud/backport/59835/stable30
[stable30] chore: Improve SVG handling in link previews
2 parents 207bd9e + 28e9c49 commit ab914ec

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

lib/public/Collaboration/Reference/LinkReferenceProvider.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private function fetchReference(Reference $reference): void {
193193
$bodyStream = new LimitStream($stream, self::MAX_CONTENT_LENGTH, 0);
194194
$content = $bodyStream->getContents();
195195

196-
if ($contentType === 'image/svg+xml' && stripos(html_entity_decode($content, ENT_XML1), 'XSL/Transform') !== false) {
196+
if ($contentType === 'image/svg+xml' && $this->containsXslt($content)) {
197197
return;
198198
}
199199

@@ -230,4 +230,30 @@ public function getCacheKey(string $referenceId): ?string {
230230
public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string {
231231
return null;
232232
}
233+
234+
/**
235+
* Check if XML content contains XSLT transformations
236+
*
237+
* XSLT transformations in SVG files can cause memory exhaustion
238+
* in Chromium based browsers when rendered.
239+
*/
240+
private function containsXslt(string $xmlContent): bool {
241+
set_error_handler(function (int $code, string $message): bool {
242+
$this->logger->debug('Failed to parse XML content for XSLT check', ['error' => $message]);
243+
return true;
244+
});
245+
246+
$xml = simplexml_load_string($xmlContent);
247+
248+
restore_error_handler();
249+
250+
$namespaces = $xml ? $xml->getNamespaces(true) : [];
251+
foreach ($namespaces as $namespace) {
252+
if (stripos($namespace, 'XSL/Transform') !== false) {
253+
return true;
254+
}
255+
}
256+
257+
return false;
258+
}
233259
}

0 commit comments

Comments
 (0)