跳到正文
Skillpedia
安全75k · #3041

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/subresource-integrity/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#3041
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能