SVGReader: Detect CSS animated SVGs

This does a string check on animated and @keyframe in style elements
for SVGs. If there is a match it will treat this SVG as animated.

While in theory this could be a false positive, without fulling
parsing the CSS, the chances of that seem minor and there are no
real negative consequences either.

Bug: T332790
Change-Id: I4b8b0781e8f9135d9ab856f3ec06f5a76c66c9a8
This commit is contained in:
Derk-Jan Hartman 2023-11-05 20:52:58 +01:00
parent 4f2a354ded
commit 8d2d229b30
3 changed files with 38 additions and 0 deletions

View file

@ -282,6 +282,16 @@ class SVGReader {
}
}
switch ( $this->reader->localName ) {
case 'style':
$styleContents = $this->reader->readString();
if (
str_contains( $styleContents, 'animated' ) ||
str_contains( $styleContents, '@keyframes' )
) {
$this->debug( "HOUSTON WE HAVE ANIMATION" );
$this->metadata['animated'] = true;
}
break;
case 'script':
// Normally we disallow files with
// <script>, but its possible

View file

@ -0,0 +1,17 @@
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100">
<defs>
<style>
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
svg {
animation: spin 5s linear infinite;
}
.acircle {
opacity: 0.65;
}
</style>
</defs>
<circle class="acircle" cx="40" cy="40" r="30" stroke="red" fill="none"/>
</svg>

After

Width:  |  Height:  |  Size: 419 B

View file

@ -141,6 +141,17 @@ class SVGReaderTest extends \MediaWikiIntegrationTestCase {
'translations' => []
],
],
[
"$base/css-animated.svg",
[
'width' => 100,
'height' => 100,
'originalWidth' => '100',
'originalHeight' => '100',
'animated' => true,
'translations' => []
],
],
];
}