问题描述
我已经设置了.net core 3.1 react template应用。
我已经像这样将csp添加到了应用程序中:
当尝试加载材质ui的样式时,前端React应用程序出现以下错误:
我如何阻止CSP阻止这些资源?
我不想允许不安全的内联。
我正在使用https://github.com/juunas11/aspnetcore-security-headers 对于支持的CSP实现,我假设.FromSelf()就足够了,因为我已经安装了用于material-ui-react的节点模块。
解决方法
内容安全策略认为是内联样式:
- 通过脚本
- Document.createElement('style') 通过脚本
- Element.setAttribute('styles')
CSP不将通过脚本进行的某些CSS操作视为内联样式:
- HTMLElement.style ,即 el.style.backgroundColor ='gray';
- CSSStyleSheet.insertRule()
- CSSGroupingRule.insertRule()
- CSSStyleDeclaration.cssText ,即 el.style.cssText ='color:blue; background-color:#fff;'
如果您不想在 style-src 指令中允许使用'unsafe-inline',则必须修改代码或使用外部样式构造:
- @import
- 链接:
; rel = stylesheet 通过HTTP标头进行样式绑定