完整性检查
策略文件必须使用与绝对 URL 关联的浏览器完整性属性兼容的子资源完整性字符串的完整性检查。
如果资源与清单中列出的完整性不匹配,则会抛出错误。
允许加载文件 checked.js
的示例策略文件:
{
"resources": {
"./app/checked.js": {
"integrity": "sha384-SggXRQHwCG8g+DktYYzxkXRIkTiEYWBHqev0xnpCxYlqMBufKZHAHQM3/boDaI/0"
}
}
}
策略清单中列出的每个资源都可以采用以下格式之一来确定其位置:
当加载资源时,整个 URL 必须匹配,包括搜索参数和哈希片段。
尝试加载 ./a.js
时不会使用 ./a.js?b
,反之亦然。
要生成完整性字符串,则可以使用 printf "sha384-$(cat checked.js | openssl dgst -sha384 -binary | base64)"
等脚本。
完整性可以指定为布尔值 true
,以接受任何对本地开发有用的资源主体。
不建议在生产中这样做,因为它会允许资源的意外更改被认为是有效的。
Policy files must use integrity checks with Subresource Integrity strings compatible with the browser integrity attribute associated with absolute URLs.
When using require()
all resources involved in loading are checked for
integrity if a policy manifest has been specified. If a resource does not match
the integrity listed in the manifest, an error will be thrown.
An example policy file that would allow loading a file checked.js
:
{
"resources": {
"./app/checked.js": {
"integrity": "sha384-SggXRQHwCG8g+DktYYzxkXRIkTiEYWBHqev0xnpCxYlqMBufKZHAHQM3/boDaI/0"
}
}
}
Each resource listed in the policy manifest can be of one the following formats to determine its location:
- A relative url string to a resource from the manifest such as
./resource.js
,../resource.js
, or/resource.js
. - A complete url string to a resource such as
file:///resource.js
.
When loading resources the entire URL must match including search parameters
and hash fragment. ./a.js?b
will not be used when attempting to load
./a.js
and vice versa.
To generate integrity strings, a script such as
printf "sha384-$(cat checked.js | openssl dgst -sha384 -binary | base64)"
can be used.
Integrity can be specified as the boolean value true
to accept any
body for the resource which can be useful for local development. It is not
recommended in production since it would allow unexpected alteration of
resources to be considered valid.