Content Security Policy allow inline style without unsafe-inline

☆樱花仙子☆ 提交于 2019-12-22 08:47:22

问题


Using content security policy without style-src 'unsafe-inline' how do you allow styles like this?

<span style="font-size: 16px;">Hello</span>

I've tried adding a nonce to them and adding that nonce to the CSP header but that doesn't seem to work

<span style="font-size: 16px;" nonce="0611873de7e2db5985c289fdfa946caee2ae1860">Hello</span>

"style-src 'nonce-0611873de7e2db5985c289fdfa946caee2ae1860' 'self'"

Is there any way to do this without adding the 'unsafe-inline' directive??


回答1:


According to https://bugzilla.mozilla.org/show_bug.cgi?id=855326#c35 nonces for style attributes isn't supported




回答2:


a common workaround for this issue is to write the inline-style (or inline-script) data to an input tag:

<input id="my-font-size" type="hidden" value="16" />

or in HTML5:

<input id="my-id" data-font-size="16" />

and process the data via an external included JavaScript (to avoid violating "script-src" csp):

$('span').css('font-size', $('#my-id').data('font-size'));


来源:https://stackoverflow.com/questions/32977166/content-security-policy-allow-inline-style-without-unsafe-inline

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