generated fromwinjs-dev/winjs-plugin-template
- Notifications
You must be signed in to change notification settings - Fork0
WinJS plugin to provide support for security.
License
NotificationsYou must be signed in to change notification settings
winjs-dev/winjs-plugin-security
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
一个为 WinJS 项目提供安全增强功能的插件,主要用于生成 SRI(Subresource Integrity)属性。
- 自动为 HTML 文件中的
<script>和<link>标签生成 SRI 属性 - 支持 SHA-256、SHA-384、SHA-512 哈希算法(可配置)
- 自动添加
crossorigin="anonymous"属性以确保 SRI 正常工作 - 仅在生产环境下生效,开发环境自动跳过
pnpm add @winner-fed/plugin-security
在你的.winrc.ts 配置文件中添加插件配置:
import{defineConfig}from'@winner-fed/winjs';exportdefaultdefineConfig({plugins:['@winner-fed/plugin-security'],security:{sri:true// 启用 SRI 功能},});
- 类型:
boolean | { algorithm: 'sha256' | 'sha384' | 'sha512' } - 默认值: 需要手动设置
- 描述: 是否启用 SRI(子资源完整性)功能,以及可选的哈希算法配置
当设置为true 或{} 时,插件会:
- 扫描构建后的 HTML 文件
- 为所有带有
src属性的<script>标签添加integrity属性 - 为所有带有
href属性的<link>标签添加integrity属性 - 自动添加
crossorigin="anonymous"属性(如果不存在)
你也可以通过对象方式指定哈希算法:
security:{sri:{algorithm:'sha512'// 可选 'sha256' | 'sha384' | 'sha512',默认 'sha512'}}
<!DOCTYPE html><html><head><linkrel="stylesheet"href="/assets/app.css"></head><body><scriptsrc="/assets/app.js"></script></body></html>
<!DOCTYPE html><html><head><linkrel="stylesheet"href="/assets/app.css"integrity="sha512-ABC123..."crossorigin="anonymous"></head><body><scriptsrc="/assets/app.js"integrity="sha512-XYZ789..."crossorigin="anonymous"></script></body></html>
SRI(子资源完整性)是一种安全特性,允许浏览器验证获取的资源(例如从 CDN 获取的资源)没有被恶意修改。当浏览器加载资源时,会计算资源的哈希值并与integrity 属性中指定的哈希值进行比较。如果哈希值不匹配,浏览器将拒绝加载该资源。
对于<script> 标签来说,结果为拒绝执行其中的代码;对于 CSS links 来说,结果为不加载其中的样式。
关于 SRI 的更多内容,可以查看Subresource Integrity - MDN。
- 此插件仅在生产构建时生效,开发环境会自动跳过
- 需要确保资源文件在构建输出目录中可访问
integrity属性必须与crossorigin属性配合使用才能正常工作
MIT
About
WinJS plugin to provide support for security.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published
Contributors3
Uh oh!
There was an error while loading.Please reload this page.