Skip to content

Ignore a malformed viewBox instead of raising TypeError - #507

Merged
deeplook merged 1 commit into
deeplook:mainfrom
eeshsaxena:fix/malformed-viewbox
Aug 17, 2026
Merged

Ignore a malformed viewBox instead of raising TypeError#507
deeplook merged 1 commit into
deeplook:mainfrom
eeshsaxena:fix/malformed-viewbox

Conversation

@eeshsaxena

Copy link
Copy Markdown

An SVG with a viewBox that isn't four numbers crashes svg2rlg:

import io
from svglib.svglib import svg2rlg
svg2rlg(io.StringIO('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100"><rect width="5" height="5"/></svg>'))
# TypeError: Box.__new__() missing 1 required positional argument: 'height'

get_box does values = [float(v) for v in view_box.split()] then return Box(*values) without checking the count, so a viewBox with the wrong number of values raises TypeError (and a non-numeric one raises ValueError). Per the SVG spec an invalid viewBox is ignored, so I fall through to the existing width/height fallback when it doesn't parse to exactly four numbers. Valid viewBoxes are unaffected.

Added a test; it raises TypeError on main and passes with the change, and the rest of test_basic.py still passes. Found it by fuzzing svg2rlg with mutated SVG.

get_box parsed the viewBox and returned Box(*values) without checking that it
had exactly four numbers, so a viewBox with the wrong number of values (or a
non-numeric one) raised TypeError/ValueError. Per the SVG spec an invalid
viewBox is ignored, so fall through to the width/height fallback.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for making your first pull request! Your contribution to svglib is highly appreciated!

@deeplook

Copy link
Copy Markdown
Owner

Thanks for the fix and tests!

@deeplook
deeplook merged commit 26b86e2 into deeplook:main Aug 17, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants