-
-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Certain svgs cause cairosvg to hang (perhaps stuck in an infinite loop) #382
Comments
Simple reproduction script: import cairosvg
svg = """<svg xmlns="http://www.w3.org/2000/svg">
<g>
<path fill="#FE6502" d="200 312.1h14v14h-14z"></path>
</g>
</svg>
"""
if __name__ == "__main__":
cairosvg.svg2png(
bytestring=svg.encode(),
write_to="test.png",
output_width=224, output_height=224
) |
For anyone facing this same issue, my workaround was to modify path.py to throw an exception when a max depth is reached while parsing paths: master...PWhiddy:CairoSVG:master This doesn't make it render successfully, but at least allows the error to be caught so the file can be skipped instead of hanging. |
Hi! It looks like the content of the By design, CairoSVG is bad at handling invalid SVG images and "easily" crashes / hangs in these cases. So, if the SVG is not valid, we won’t add anything to CairoSVG to handle this case. |
Hey thanks for your response! I think the design of easily crashing on invalid svgs is great, although hanging indefinitely makes things very difficult for some use cases. Particularly in a multiprocessing environment because it's not easy to gracefully timeout from outside the library itself. In my case I'm rendering a huge dataset of millions of svgs, a small number of which are invalid. Anyway, totally respect the design of not being overly concerned with invalid inputs, but just want to say that just a simple iteration limit to detect infinite loops (perhaps as an optional argument) similar to the one I used above would make many applications much easier to build. PS |
The easiest way for you is probably to define a global timeout. Using func_timeout is a great way to avoid a lot of different problems or attacks (malformed SVG, huge images, common SVG attacks…) |
Thanks for making this super useful library!
Rendering certain svgs with an invalid path causes the library to get stuck in an infinite loop.
This svg renders fine in other applications, and cairosvg renders other svgs for me no problem.
Hoping for some insight on this, even being able to timeout/error on these would be helpful!
The text was updated successfully, but these errors were encountered: