'strict-dynamic' 的外部脚本散列需要脚本标签上的 "integrity" 属性?
External script hash with 'strict-dynamic' requires "integrity" attribute on script tag?
我正在处理网站的 Content Security Policy, specifically the strict-dynamic
关键字。
我的 test site 有两个文件:
index.html:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1,user-scalable=yes" />
<meta charset="UTF-8" />
<title>csp-test</title>
</head>
<body>
<h1>csp-test</h1>
<script src="./index.js"></script>
</body>
</html>
index.js:
console.log('foo');
我正在考虑使用 hash-based 方法来允许脚本。
以下是我在节点脚本中生成散列的方法:
const input = fs.readFileSync("/path/to/index.js");
crypto.createHash("sha256").update(input, 'utf8').digest('base64')
这里是 index.js
的结果:1kOLrDKT3TBiHLcnxiGsc7HF/lyVJKLhoZDSn0UwCfo=
使用此哈希,我将 CSP 配置更新为:
default-src 'self'; script-src 'self' 'strict-dynamic' 'sha256-1kOLrDKT3TBiHLcnxiGsc7HF/lyVJKLhoZDSn0UwCfo=' 'unsafe-inline'; object-src 'none'; base-uri 'self'
当我将此值作为响应 header(使用 ModHeader Chrome 扩展名)和此名称包含时 Content-Security-Policy-Report-Only
我仍然在控制台中收到错误消息:
[Report Only] Refused to load the script 'http://localhost:5000/index.js' because it violates the following Content Security Policy directive: "script-src 'self' 'strict-dynamic' 'sha256-1kOLrDKT3TBiHLcnxiGsc7HF/lyVJKLhoZDSn0UwCfo=' 'unsafe-inline'". 'strict-dynamic' is present, so host-based allowlisting is disabled. Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
我可以通过在我的脚本标签上设置 integrity
属性来消除这个错误:
<script integrity="sha256-1kOLrDKT3TBiHLcnxiGsc7HF/lyVJKLhoZDSn0UwCfo=" src="./index.js"></script>
我的问题是:为什么我需要添加 integrity
属性?
我没有在 documentation 中看到它,并且需要添加此属性会使我们的构建过程进一步复杂化。
除了需要指定此属性之外,还有其他选择吗?
Why did I need to add the integrity attribute? I'm not seeing it
mentioned in the documentation and needing to add this attribute would
further complicate our build process.
MDN 只包含解释 CSP 工作原理的常见内容。所有细节都在 CSP spec
'hash-value'
令牌的使用假设外部脚本已经具有 integrity=
属性(来自第三方 CDN 的脚本)。对于自己的脚本,使用 'nonce-value'
令牌更容易。
此外,Firefox does not support 'hash-value' for allowing external scripts, only for internal ones. Safari - too.
Is there an alternative to needing to specify this attribute?
没办法,不幸的是。只有内置脚本 <script>...</script>
不需要 integrity=
attr,如果它们的哈希包含在 script-src
指令中,将被自动允许。
I'm working on a site's Content Security Policy, specifically the
strict-dynamic keyword.
小心,Safari 仍然 not support 'strict-dynamic'
.
Here is how I'm generating the hash in a node script:
const input = `fs.readFileSync("/path/to/index.js");
crypto.createHash("sha256").update(input, 'utf8').digest('base64')
外部脚本的内容does not need在哈希之前转换为UTF8,只有内联脚本需要转码。
此外,CSP 规范要求所有 '-' 字符替换为 '+',以及所有 '_'字符替换为散列 value 中的“/”。然后添加 'sha256-' 前缀。
我正在处理网站的 Content Security Policy, specifically the strict-dynamic
关键字。
我的 test site 有两个文件:
index.html:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1,user-scalable=yes" />
<meta charset="UTF-8" />
<title>csp-test</title>
</head>
<body>
<h1>csp-test</h1>
<script src="./index.js"></script>
</body>
</html>
index.js:
console.log('foo');
我正在考虑使用 hash-based 方法来允许脚本。
以下是我在节点脚本中生成散列的方法:
const input = fs.readFileSync("/path/to/index.js");
crypto.createHash("sha256").update(input, 'utf8').digest('base64')
这里是 index.js
的结果:1kOLrDKT3TBiHLcnxiGsc7HF/lyVJKLhoZDSn0UwCfo=
使用此哈希,我将 CSP 配置更新为:
default-src 'self'; script-src 'self' 'strict-dynamic' 'sha256-1kOLrDKT3TBiHLcnxiGsc7HF/lyVJKLhoZDSn0UwCfo=' 'unsafe-inline'; object-src 'none'; base-uri 'self'
当我将此值作为响应 header(使用 ModHeader Chrome 扩展名)和此名称包含时 Content-Security-Policy-Report-Only
我仍然在控制台中收到错误消息:
[Report Only] Refused to load the script 'http://localhost:5000/index.js' because it violates the following Content Security Policy directive: "script-src 'self' 'strict-dynamic' 'sha256-1kOLrDKT3TBiHLcnxiGsc7HF/lyVJKLhoZDSn0UwCfo=' 'unsafe-inline'". 'strict-dynamic' is present, so host-based allowlisting is disabled. Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
我可以通过在我的脚本标签上设置 integrity
属性来消除这个错误:
<script integrity="sha256-1kOLrDKT3TBiHLcnxiGsc7HF/lyVJKLhoZDSn0UwCfo=" src="./index.js"></script>
我的问题是:为什么我需要添加 integrity
属性?
我没有在 documentation 中看到它,并且需要添加此属性会使我们的构建过程进一步复杂化。
除了需要指定此属性之外,还有其他选择吗?
Why did I need to add the integrity attribute? I'm not seeing it mentioned in the documentation and needing to add this attribute would further complicate our build process.
MDN 只包含解释 CSP 工作原理的常见内容。所有细节都在 CSP spec
'hash-value'
令牌的使用假设外部脚本已经具有 integrity=
属性(来自第三方 CDN 的脚本)。对于自己的脚本,使用 'nonce-value'
令牌更容易。
此外,Firefox does not support 'hash-value' for allowing external scripts, only for internal ones. Safari - too.
Is there an alternative to needing to specify this attribute?
没办法,不幸的是。只有内置脚本 <script>...</script>
不需要 integrity=
attr,如果它们的哈希包含在 script-src
指令中,将被自动允许。
I'm working on a site's Content Security Policy, specifically the strict-dynamic keyword.
小心,Safari 仍然 not support 'strict-dynamic'
.
Here is how I'm generating the hash in a node script:
const input = `fs.readFileSync("/path/to/index.js");
crypto.createHash("sha256").update(input, 'utf8').digest('base64')
外部脚本的内容does not need在哈希之前转换为UTF8,只有内联脚本需要转码。
此外,CSP 规范要求所有 '-' 字符替换为 '+',以及所有 '_'字符替换为散列 value 中的“/”。然后添加 'sha256-' 前缀。