Does the threat of XSS exists when loading an untrusted SVG file using the img
tag?
As in: <img src="untrusted.svg"/>
I've read that most browsers disable scripts in svg files loaded via the img
tag.
Does the threat of XSS exists when loading an untrusted SVG file using the img
tag?
As in: <img src="untrusted.svg"/>
I've read that most browsers disable scripts in svg files loaded via the img
tag.
This used to work in some browsers, but not anymore. However there is a related issue. If I as a unknowing user, right click and download the image, and then open it locally, it will likely open in the browser and the script will run. Which is a bit weird considering it's an image. I suppose if you right click and select "view image" that could also cause the script to run, because you open it diretly.
Yes, XSS threats do exists when using SVG, most browsers will not allow the script to run, but if it is sent via email then it could potentially run.
Some links to issues:
Scalable Vector Graphics and XSS
Why does this XSS vector work in svg but not in HTML?
SVG Fun Time - Firefox SVG Vector + Bypassing Chrome XSS Auditor
No, there is no risk of XSS in an <img>
tag.
But if the image can be loaded in an <img>
tag, then the person can also load the image directly. In that case, scripts are executed.
For example, <img src='/uploads/4bafa902.svg'>
will not execute scripts, but if someone clicks a link leading to https://example.com/uploads/4bafa902.svg
then any scripts inside the SVG will execute in the context of https://example.com/
(called an origin). Even with an HttpOnly
session ID cookie, the script can perform any calls into your application (at that origin) and either perform unwanted actions or exfiltrate obtained data to the attacker.
If you host SVGs on an origin where an application runs that uses any sort of cookies, or where a request can perform actions without user interaction (like CSRF, except it's originating from your own origin), or an origin which anyone put into their CORS allow header (leading to the same CSRF-like risk), then you need to either:
Content-Security-Policy
(CSP) response header set on SVG images which denies running those scripts. This is the one I use:
Content-Security-Policy: script-src 'none'; object-src 'none'; report-uri /cspreport
Both options have caveats:
The CSP header is often configured globally on the web server. If you set a custom one for SVG content responses, that might be overridden by the web server (either replaced, or two might be set in a random order and lead to unexpected results).
The cleaning/sanitizing is prone to oversights, and you also need to keep up with new (and perhaps obscure) browser features, similar to how ontouchstart
was defined for touchscreen devices only in 2013, nearly twenty years after onclick
was defined (maybe VR/AR headsets will get their own events for looking at an element, or something else we do not have yet today). Thus, I would not try to build my own, but rather trust to a project that focuses on cleaning SVGs against XSS such as DOMPurifier.
Having both defences probably gets you the best of both worlds: if the filter is circumvented, the CSP should still kick in, and vice versa.
© 2022 - 2024 — McMap. All rights reserved.