![](/img/trans.png)
[英]Chrome extension policy error: Refused to execute inline event handler because it violates the following Content Security Policy directive
[英]Css inline style error in Chrome: “Refused to apply inline style because it violates the following Content Security Policy directive…”
我有以下 html 页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';"/>
<title>
</title>
</head>
<style>
.container {
display: flex;
display: -webkit-flex;
justify-content: center;
}
.label {
font-size: 20px;
text-align: center;
}
</style>
<body>
<div class="container">
<div class="label">Label content.</div>
</div>
</body>
</html>
尽管“内容安全策略”标签带有“不安全内联”,但未应用样式,我在 Chrome 中看到以下错误:
“拒绝应用内联样式,因为它违反了以下内容安全策略指令:“default-src 'self'”。要么是 'unsafe-inline' 关键字,一个哈希('sha256-daGc4DKtFpvn1iqhVz5mJJ4bXSwDGTnQKoxHdrHVEhc='),要么是一个随机数(' nonce-...') 是启用内联执行所必需的。还要注意,'style-src' 没有明确设置,所以 'default-src' 用作后备。”
可以做些什么来解决这个问题?
这可能是因为 HTML 中定义的 CSP 与您的 Web 服务器设置中的 CSP 不同。 对于 Apache,它位于文件“ .htaccess ”中。
但是,如果您添加具有全局“unsafe-inline”和“unsafe-eval”的 CSP,则根本没有安全性,并且 CSP 变得有点无用。
此外,考虑为 HTML 和 CSS 创建不同的文件,并在需要时导入样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.