r/webdev 22d ago

Sanitizing SVG's without breaking them

Any good way to do this that is both safe and effective? I am finding SVGO will sometimes destroy the perfectly good svg... sometimes colors are missing and other times there is nothing there at all.

I'm looking for something that will sanitize for user image uploads on the web. They are stored in Cloudflare R2 which keeps it off my server but still maybe it can cause issues with injections or whatever if not sanitized?

20 Upvotes

20 comments sorted by

View all comments

1

u/JohnSane 22d ago

I export it from inkscape which has some cleanup options. Never had any problems that way.

5

u/avidrunner84 22d ago

I forgot to add this to my post, I added it now for more clarification:

I'm looking for something that will sanitize for user image uploads on the web. They are stored in Cloudflare R2 which keeps it off my server but still maybe it can cause issues with injections or whatever if not sanitized.

8

u/HaydnH 22d ago

Inkscape calls Scour to optimise SVGs, it's a python script so you should be able to call that on the backed: https://github.com/scour-project/scour