I have set up my Content-Security-Policy (CSP) with the following definition for style-src
:
style-src 'self' 'nonce-somenonce'
Then, in my markup I include SVG's like this:
<svg role="img" title="Clock">
<use xlink:href="/Content/Styles/svg/sprite.symbol.svg#icon-clock" xmlns:xlink="http://www.w3.org/1999/xlink"></use>
</svg>
The external SVG contains multiple symbols with paths:
<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<symbol viewBox="0 0 40 40" id="arrow-right" xmlns="http://www.w3.org/2000/svg">
<path d="M29 20l-5-5v4H11v2h13v4l5-5z"/>
</symbol>
<symbol viewBox="0 0 22 22" id="icon-clock" xmlns="http://www.w3.org/2000/svg">
<path fill="#c20418" class="atcls-1" d="M17.727 9.777h-5.5v-5.5a1.222 ..."/>
</symbol>
</svg>
Chrome refuses to allow this code, and I'm not sure how to allow it without 'unsafe-inline'
. SVG is not a nonceable element according to these two tests:
I looked at this thread, which suggests grouping the SVG into a <style>
and nonce it, but I can't find out how to do this, and whether it works with an external definition.
There is also this thread suggesting to use JS to set the style, but I again can't find how this would work with SVG's and <use>
.
A report generated by the browser looks like this:
{
"csp-report": {
"document-uri": "https://mypage.xyz/content/styles/svg/sprite.symbol.svg",
"referrer": "",
"violated-directive": "style-src-elem",
"effective-directive": "style-src-elem",
"original-policy": "default-src 'self';script-src 'strict-dynamic' 'nonce-LwpcXyjstsjZdWhOBquhhaxTEe+TFHbaAKYGal+sV3I=' 'unsafe-inline' 'unsafe-eval' http: https:;style-src 'self' 'nonce-LwpcXyjstsjZdWhOBquhhaxTEe+TFHbaAKYGal+sV3I=' 'unsafe-inline' fonts.googleapis.com;child-src 'self';connect-src 'self';img-src 'self';frame-src 'self';object-src 'none';font-src 'self';media-src 'self';manifest-src 'self';prefetch-src 'self';worker-src 'none';base-uri 'self';form-action 'self'; frame-ancestors 'self';upgrade-insecure-requests;report-uri /api/csp-reports",
"disposition": "report",
"blocked-uri": "inline",
"line-number": 1,
"source-file": "https://mypage.xyz/content/styles/svg/sprite.symbol.svg",
"status-code": 0,
"script-sample": ""
}
}
Adding style-src-elem
to the header also does not solve it, even though the report states that this directive is violated. And this is also not implemented in Firefox at all. In Chrome, I get the following error which is contradicting since a nonce is clearly defined, even in the error message:
Refused to apply inline style because it violates the following Content Security Policy directive: "style-src-elem 'self' 'nonce-Ma4zjvbuXdD3iwOLjAXsiJ2Qqk2TvVEQlT1efIY+qgE='". Either the 'unsafe-inline' keyword, a hash ('sha256-Ptir/SKEXaGsdPsQ11Srj5YgYg3GDQ1ZV8flKlU21lI='), or a nonce ('nonce-...') is required to enable inline execution.