Is there any way to make user uploaded SVG images safe from code injection etc?

戏子无情 提交于 2020-12-30 08:23:23

问题


I want to display user uploaded SVG images on a website, but they're quite open to exploits:

  • https://security.stackexchange.com/questions/11384/exploits-or-other-security-risks-with-svg-upload
  • https://security.stackexchange.com/questions/36447/img-tag-vulnerability

For example, arbitrary javascript can be embedded in SVG. There's also issues with performance exploits, but I'd consider those lower priority.

Is there any mechanism to make SVG somewhat safe and only use it as an image? Can I simply trust <img src="/media/user-uploaded-image.svg" />?

Wikipedia/Wikimedia Commons hosts SVG files. Does anyone know what measures they take to prevent SVG exploits?


回答1:


Wikipedia/Wikimedia Commons hosts SVG files. Does anyone know what measures they take to prevent SVG exploits?

They serve the uploaded files from a separate hostname, specifically upload.wikimedia.org. You can cross-site-script into there all you like but it doesn't get you anything: it lives in a different origin to en.wikipedia.org and can't touch its cookies or interact with its script.

This is ultimately the only airtight way to handle file uploads, and what most of the big players do. It is just too difficult to do a thorough scan for all the many obscure XSS possibilities that exist when you allow arbitrary files.

Can I simply trust <img src="/media/user-uploaded-image.svg" />?

It doesn't really matter what <img> does—the user can simply be navigated directly to the SVG address and it'll execute script full-page in the site's origin.




回答2:


If you embed SVGs as an <image> it shouldn't be able to execute scripts. See here: https://www.w3.org/wiki/SVG_Security

Of course you can also parse the document before processing and apply the same filters and regex you would apply to an html file.



来源:https://stackoverflow.com/questions/33387722/is-there-any-way-to-make-user-uploaded-svg-images-safe-from-code-injection-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!