概述Subresource Integrity(SRI)在加载外部脚本/样式时校验内容哈希,防止 CDN 污染与篡改。浏览器在下载完成后验证哈希,失败则拒绝执行/应用。跨域资源需配合 `crossorigin`。示例与用法<link rel="stylesheet" href="https://cdn.example.com/app.css" integrity="sha384-BASE64HASH" crossorigin="anonymous"> <script src="https://cdn.example.com/app.js" integrity="sha512-BASE64HASH" crossorigin="anonymous"></script> 工程建议哈希算法:优先 `sha384` 或 `sha512`;在构建阶段生成并注入。跨域与缓存:设置 `crossorigin="anonymous"`,并确保响应具备 `Access-Control-Allow-Origin` 允许获取;更新版本时同步刷新哈希与缓存键。失败与回退:哈希不匹配时提供本地镜像与降级路径;记录失败率。与 CSP 协同:限制外部来源并结合 SRI;避免 `unsafe-inline`。参考与验证W3C SRI 规范:https://www.w3.org/TR/SRI/MDN SRI 文档:https://developer.mozilla.org/docs/Web/Security/Subresource_Integrityweb.dev SRI 介绍:https://web.dev/articles/subresource-integrity

发表评论 取消回复