Skip to content

Commit b04aed6

Browse files
committed
chore: Improve SVG handling in link previews
Signed-off-by: Benjamin Frueh <benjamin.frueh@gmail.com>
1 parent 78909ea commit b04aed6

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
@@ -203,7 +203,7 @@ private function fetchReference(Reference $reference): void {
203203
$bodyStream = new LimitStream($stream, self::MAX_CONTENT_LENGTH, 0);
204204
$content = $bodyStream->getContents();
205205

206-
if ($contentType === 'image/svg+xml' && stripos(html_entity_decode($content, ENT_XML1), 'XSL/Transform') !== false) {
206+
if ($contentType === 'image/svg+xml' && $this->containsXslt($content)) {
207207
return;
208208
}
209209

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

0 commit comments

Comments
 (0)