Review main Merged

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

Benni Mack 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
    • Garvin Hicking +1
    • Simon Schaufelberger +1
  • Verified V +2 Done
    • Georg Ringer +2
    • Oliver Klee +1
    • Garvin Hicking +1
    • Simon Schaufelberger +1
    • core-ci +1 CI

Issues 1

Files 19

Review log 2333

Show 58 earlier messages
  1. Benni Mack uploaded patch set 1 04/23/26, 09:44

  2. core-ci voted on patch set 1 V -1 04/23/26, 10:03 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/101017

  3. Garvin Hicking uploaded patch set 2 V -1 05/29/26, 08:58

    Prevent DI where not possible
  4. core-ci voted on patch set 2 V -1 05/29/26, 09:08 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/102596

  5. Garvin Hicking uploaded patch set 3 V -1 05/29/26, 09:19

  6. core-ci voted on patch set 3 V +1 05/29/26, 09:27 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/102599

  7. Christian Kuhn commented on patch set 3 05/29/26, 12:45

    AbstractSvgIconProvider.php:64

    that's not correct, is it?! inject*() does work in abstracts, that's the (nearly sole) useful purpose of inject* methods: use it in abstracts so an abstract does not pollute __construct().

    SvgImageProcessor.php:98

    what blocks it? i don't see a reason quickly.

    ImageInfo.php:139

    probably DI as well.
  8. Christian Kuhn commented on patch set 3 CR -1 05/29/26, 14:09

    on it.
  1. Christian Kuhn uploaded patch set 4 CR -1 V +1 05/29/26, 17:43

  2. core-ci voted on patch set 4 V -1 05/29/26, 17:57 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/102625

  3. Christian Kuhn uploaded patch set 5 V -1 05/29/26, 18:12

  4. core-ci voted on patch set 5 V -1 05/29/26, 18:20 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/102626

  5. Christian Kuhn uploaded patch set 6 V -1 05/29/26, 18:31

  6. core-ci voted on patch set 6 V -1 05/29/26, 18:39 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/102627

  7. Christian Kuhn commented on patch set 6 05/29/26, 20:36

    SvgImageProcessor.php:98 Reply

    Done

    ImageInfo.php:139 Reply

    actually not: ImageInfo extends FileInfo extends \SplFileInfo, which is a data object with __construct() for the file-path. So ImageInfo and FileInfo blend service aspects into a data object, should be considered broken, needing refactoring. I'll add a @todo.
  8. Christian Kuhn uploaded patch set 7 V -1 05/29/26, 20:37

  9. Christian Kuhn commented on patch set 7 CR -1 05/29/26, 20:38

    getting closer, but still fiddling with some details.
  10. core-ci voted on patch set 7 V -1 05/29/26, 20:45 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/102631

  11. Christian Kuhn uploaded patch set 8 CR -1 V -1 05/30/26, 00:50

  12. Christian Kuhn commented on patch set 8 05/30/26, 00:52

    AbstractSvgIconProvider.php:64 Reply

    Done: Using inject() methods in abstract now. added a compiler pass to 'public:true' all classes that implement IconProviderInterface to not break existing custom consumers.
  13. core-ci voted on patch set 8 V -1 05/30/26, 00:59 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/102642

  14. Christian Kuhn uploaded patch set 9 V -1 05/30/26, 01:28

  15. core-ci voted on patch set 9 V +1 05/30/26, 01:35 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/102645

  16. Christian Kuhn uploaded patch set 10 V +1 05/30/26, 01:55

  17. Christian Kuhn commented on patch set 10 CR +1 05/30/26, 01:56

    This change is ready for review.

    Reply

    Done

    Reply

    Done
  18. Christian Kuhn commented on patch set 10 05/30/26, 01:56

    +1 reading - i think i'm done with this for now. what do you think?
  19. core-ci voted on patch set 10 V +1 05/30/26, 02:03 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/102647

  20. Garvin Hicking commented on patch set 10 CR +1 V +1 05/30/26, 19:13

    This (still) works for me. Thanks for brushing up the DI portion of this and also noticing more room for improvement. Also, I think the class structure is now really well designed and leaves us breathing room for refactoring and future enhancement. The type-hinting with the SvgDocument is clever. The object itselfs remains pure, a service class remains with pure application methods, and the factory only does factory stuff. For me, this is a general concept we need to enforce in TYPO3, because also LLM Agents will profit from clear structure and strong rules, and we can properly document this in an architecture file: "Have stateless services, pass objects to manipulation methods, abstain from fluent API in favor of clear cross-cutting concerns". This structure here would IMO be well applicable to the further GD/Image reworks of @benjamin.kott@outlook.com - we could similarly model the effect processing. I think a drawback of not having a fluent API is worth all the other upsides. I think having multiple views on this and creating multiple iterative improvements in this patch with a very clean outcome is worth having more discussion. I really hope that you, Benji, can also find peace with this variation of your work and it can be factored into your related chain/patches, and be a base for rework of those. I'm really sorry it has taken me very long to get into this patch due to "life reasons". I can see a lot of worth from the work you did in this area. I am sure we can move the other patches forward together. Getting of my soapbox: I think this patch is quite sound and I would have no objections to backport this to v14, probably even down to v13. Thanks!
  21. Oliver Klee voted on patch set 10 V +1 05/31/26, 09:42

  22. Christian Kuhn wrote 06/18/26, 23:09

    Removed reviewer Christian Kuhn with the following votes: * Code-Review+1 by Christian Kuhn
  23. Simon Schaufelberger voted on patch set 10 CR +1 V +1 06/23/26, 14:07

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

  25. Georg Ringer merged patch set 11 07/10/26, 18:28