Review 14.3 Merged

94756[BUGFIX] Sanitize and size SVG via dedicated SvgDocument classes

Georg Ringer CR +2 V +2 Expert +928 −298 updated 07/10/26, 18:28

Commit message

SVG handling in Core was spread across independent sites that each
shipped their own parser and rules, which produced three defects.

1) Image dimensions were read wrong in ImageInfo::extractSvgImageSizes().
The method preferred width/height over viewBox and cast non-numeric
values with (int), so an SVG declaring viewBox="0 0 400 200" together
with width="100%" height="100%" resolved to 100x100 and one with
width="10mm" height="20mm" resolved to 10x20. The viewBox fallback
used explode(' ', ...), so a spec-compliant comma-separated
viewBox="0,0,320,240" collapsed into a single token and returned
[0, 0].

2) SVG sanitization was unequal across inline renderers. Both
AbstractSvgIconProvider and the frontend SVG cObject only ran a
regex-based <script> strip and bypassed SvgSanitizer entirely,
leaving event handler attributes such as onclick/onload and remote
references like <use href="http://..."/> untouched in the rendered
markup. Invalid SVG in the frontend cObject additionally fatalled
via dom_import_simplexml(false).

3) Inline SVG markup was not cleaned up for HTML5 embedding.
AbstractSvgIconProvider emitted SVG with a redundant root
xmlns="http://www.w3.org/2000/svg", the legacy version attribute,
and no synthesised viewBox when only width/height were declared,
which broke responsive CSS scaling of inline icons.

The patch consolidates all SVG work into the dedicated namespace
TYPO3\CMS\Core\Imaging\Svg, split along a clear seam. SvgDocument is
a thin, type-hintable subclass of \DOMDocument: a loaded SVG can
be passed around with a real type while keeping the full DOM API.
SvgDocumentFactory is a stateless service that only creates
documents, parsing strings and files (fromString, fromFile) and
sanitizing markup in one step (fromStringAndSanitize).
SvgDocumentService holds the stateless operations on a document
handed to it: resolving pixel dimensions, serializing to plain XML
or to HTML5-ready inline markup without an XML prolog, and wrapping
a source SVG in a crop container (cropScale). All Core sites delegate
to these classes, so sizing rules, sanitization coverage and inline
clean-up are identical everywhere an SVG is touched.

With the new services being used in ext:install, whose failsafe
container performs no autowiring, the SVG service chain and the
providers are registered explicitly in ServiceProvider.php.

The @internal SvgManipulation class is removed.

Review

  • Code review CR +2 Done
    • Georg Ringer +2
  • Verified V +2 Done
    • Georg Ringer +2
    • core-ci +1 CI

Issues 1

Files 19

Review log 34

  1. Georg Ringer commented on patch set 1 07/10/26, 13:44

    Cherry Picked from branch main.
  2. core-ci voted on patch set 1 V +1 07/10/26, 14:18 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/104635

  3. Georg Ringer voted on patch set 1 CR +2 V +2 07/10/26, 18:28

  4. Georg Ringer merged patch set 2 07/10/26, 18:28