跳到主要内容
uencoder

Use Subresource Integrity (SRI) 为外部 resources

SRI ensures 外部 scripts/styles 有不 是 tampered 具有通过 verifying 其 hash. Learn 到生成和 验证完整性 attributes.

  1. 1

    SRI adds 完整性属性到 <脚本> 和 <link> tags: 完整性="sha384-<base64-hash>". browser computes hash 的 downloaded 资源和 compares 它到 完整性属性. 如果它们不 匹配, 资源是 blocked. 此 prevents CDN compromise 或 MITM attacks 从 injecting malicious 代码 via 外部 resources.

  2. 2
    哈希计算器 →

    计算 the SHA-384 hash of the external resource (script or CSS file). Base64-encode the hash. The integrity attribute is: integrity="sha384-<base64-hash>". Use SHA-384 (not SHA-256) for SRI — it is the recommended algorithm. The crossorigin attribute is also required for SRI on cross-origin resources.

  3. 3
    SHA-3 / Keccak 哈希计算器 →

    Alternatively, use SHA-3-256 为 SRI. While SHA-384 是标准, SHA-3 是 also supported 通过现代 browsers. Use strongest 可用 algorithm. hash 必须是 computed 遍历 exact bytes 的资源 — 任何 modification (whitespace, minification) changes hash.

常见问题

什么 happens 如果 SRI hash 不匹配?
browser blocks 资源和 reports violation 到 console. page 可以 break 如果资源是 必要. 此是 desired 行为 — 它是 better 到 break than 到加载 tampered 代码. Monitor SRI violations 和更新 hashes 当你 intentionally 更新资源.
SRI protect 针对所有 CDN attacks?
否. SRI protects 针对 modification 的资源内容 (tampering, MITM). 它不 protect 针对: CDN going 下 (use fallback), CDN serving 不同资源 (错误 URL), 或 CDN 日志记录你的 traffic. SRI 是 one 层的 防御 — 组合具有 HTTPS, CSP, 和 trusted CDNs.

解决此任务的工作流

相关工具