简体   繁体   English

内容安全策略允许内联样式,没有不安全的内联

[英]Content Security Policy allow inline style without unsafe-inline

Using content security policy without style-src 'unsafe-inline' how do you allow styles like this? 使用没有style-src'unsafe-inline'的内容安全策略,你如何允许这样的样式?

 <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 我已经尝试向他们添加一个nonce并将该nonce添加到CSP头但这似乎不起作用

 <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?? 有没有办法在不添加'unsafe-inline'指令的情况下执行此操作?

根据https://bugzilla.mozilla.org/show_bug.cgi?id=855326#c35不支持样式属性的随机数

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: 或者在HTML5中:

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

and process the data via an external included JavaScript (to avoid violating "script-src" csp): 并通过外部包含的JavaScript处理数据(以避免违反“script-src”csp):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 拒绝加载样式表,因为它违反了以下内容安全策略指令:“style-src &#39;self&#39; &#39;unsafe-inline&#39;” - Refused to load the stylesheet because it violates the following Content Security Policy directive: "style-src 'self' 'unsafe-inline'" 使用 Content-Security-Policy 和 JavaScript 避免 `script-src 'unsafe-inline'` - Avoiding `script-src 'unsafe-inline'` with Content-Security-Policy and JavaScript 内容安全策略元标记“不安全内联”不起作用 - Content-Security-Policy meta tag 'unsafe-inline' doesn't work 除了在CSP策略中添加“ unsafe-inline”以添加内联样式attr之外,setAttribute()是否还有其他选择? - Is there any alternative to setAttribute() other than adding 'unsafe-inline' in CSP policy for adding inline style attr? CSP style-src: &#39;unsafe-inline&#39; - 值得吗? - CSP style-src: 'unsafe-inline' - is it worth it? “style-src &#39;self&#39; &#39;nonce-rAnd0m&#39;” 是否比“style-src &#39;self&#39; &#39;unsafe-inline&#39;” 有任何安全优势? - Is there any security benefit of "style-src 'self' 'nonce-rAnd0m'" over "style-src 'self' 'unsafe-inline'"? 内容安全策略与可变内联样式(CSP与CSS) - Content Security Policy vs variable inline style (CSP vs CSS) Chrome 中的 CSS 内联样式错误:“拒绝应用内联样式,因为它违反了以下内容安全策略指令……” - Css inline style error in Chrome: “Refused to apply inline style because it violates the following Content Security Policy directive…” CSP-当动态放置页面元素时,如何解决style-src unsafe-inline - CSP - How to solve style-src unsafe-inline -when having dynamically positioned page elements “style-src &#39;self&#39; https://maxcdn.bootstrapcdn.com/bootstrap/”。 &#39;unsafe-inline&#39; 关键字,一个散列 - "style-src 'self' https://maxcdn.bootstrapcdn.com/bootstrap/". Either the 'unsafe-inline' keyword, a hash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM