subresource-integrity
它能做什么
这项技能检查网页中的外部脚本和样式,并为它们添加完整性哈希值。适用于从自己服务器之外加载代码的开发者。
适用场景
- ✓当页面从 CDN 加载 JavaScript 或 CSS,却没有 integrity 属性的时候。
- ✓当你在审查包含外部 script 或 link 标签的 HTML 模板的时候。
- ✓当有人问如果 CDN 被入侵,该如何保护访客的时候。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/subresource-integrity/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的网站从 CDN 加载脚本。我该如何确保没有人能把它们替换成恶意代码?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会找出没有指纹的外部文件,并为其添加指纹。之后浏览器会把下载的每个文件与其指纹进行比较,并拦截任何不匹配的文件。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Add Subresource Integrity to external scripts
When you load JavaScript from a CDN, you're trusting that CDN completely — if it's compromised, attackers can serve malicious JavaScript to all your users. SRI adds a cryptographic hash to the tag; the browser refuses to execute the script if the hash doesn't match the downloaded content, protecting users even if the CDN is compromised or the URL is hijacked.
Quick Reference
- Add integrity="sha384-..." to <script> and <link> tags loading from CDNs
- Always pair integrity with crossorigin="anonymous"
- Generate hashes with openssl or online tools — CDN providers usually supply them
- SRI blocks execution if the file hash doesn't match, preventing CDN compromise attacks
Check
Find all external <script> and <link rel=stylesheet> tags in this HTML that load from CDNs and don't have integrity attributes. List each one.
Fix
Add appropriate integrity and crossorigin attributes to external CDN resources. Generate the SHA-384 hashes for each resource.
Explain
Explain Subresource Integrity, how it protects against CDN compromise, how to generate SRI hashes, and its limitations.
Code Review
Review templates, server-rendered HTML, and shared components that output markup related to Add Subresource Integrity to external scripts. Flag exact elements, attributes, and routes where the rendered HTML violates the rule.
For full implementation details, code examples, and framework-specific guidance,
see references/rule.md.
Rule page: https://frontendchecklist.io/rules/html/subresource-integrity
常见问题
什么是子资源完整性哈希?
它是一段描述文件确切内容的短代码。浏览器在运行文件之前,会将其与下载的文件进行比较。
我自己服务器上的文件也需要这样做吗?
这项技能主要针对外部 CDN 文件,你自己服务器托管的文件不是这里的主要目标。
这项技能有没有提到该方法的局限?
有。它也解释了其局限性,让你知道这项检查能防护什么,又不能防护什么。
详情
作者的原始描述
Use when reviewing templates, rendered HTML, or shared components related to Add Subresource Integrity to external scripts. Validate the final browser-facing markup, not just the source framework abstraction.
热度
- GitHub 星标
- 75k
- 排名
- #3041
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。