HailBytes/security-headers

GitHub: HailBytes/security-headers

这是一个用于分析和评级Web应用HTTP安全头的工具,帮助识别并修复不安全配置。

Stars: 2 | Forks: 1

# @hailbytes/security-headers [![npm 版本](https://img.shields.io/npm/v/%40hailbytes%2Fsecurity-headers.svg)](https://www.npmjs.com/package/@hailbytes/security-headers) [![npm 下载量](https://img.shields.io/npm/dw/%40hailbytes%2Fsecurity-headers.svg)](https://www.npmjs.com/package/@hailbytes/security-headers) [![许可证:MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) [![包大小](https://img.shields.io/bundlephobia/minzip/%40hailbytes%2Fsecurity-headers)](https://bundlephobia.com/package/@hailbytes/security-headers) [![LinkedIn](https://img.shields.io/badge/LinkedIn-davidhailbytes-blue?logo=linkedin&style=flat)](https://www.linkedin.com/in/davidhailbytes/) ## 功能说明 获取(或接受原始头部对象)并对 7 个安全头部类别进行评分——HSTS、CSP、X-Frame-Options、X-Content-Type-Options、Referrer-Policy、Permissions-Policy 和 Cross-Origin 策略。返回 A+ 到 F 的字母等级、0-100 的百分比评分、每个头部的发现以及具体的修复步骤。 ## 安装说明 ``` npm install @hailbytes/security-headers # 或直接运行 npx @hailbytes/security-headers https://example.com ``` ## 快速开始 ### 命令行界面 ``` # 扫描一个URL并输出彩色报告 npx @hailbytes/security-headers https://example.com # 输出原始JSON npx @hailbytes/security-headers https://example.com --json # 作为CI门禁使用(在评级D或F时退出码为1) npx @hailbytes/security-headers https://staging.example.com || echo "Security headers gate failed" ``` ### 库 — 分析 URL ``` import { analyze } from '@hailbytes/security-headers'; const report = await analyze('https://example.com'); console.log(report.grade); // 'A+' | 'A' | 'B' | 'C' | 'D' | 'F' console.log(report.score); // 0–100 console.log(report.percentage); // 0–100 console.log(report.headers); // HeaderFinding[] ``` ### 库 — 分析原始头部(离线/测试中) ``` import { analyzeHeaders } from '@hailbytes/security-headers'; const report = analyzeHeaders({ 'strict-transport-security': 'max-age=31536000; includeSubDomains', 'content-security-policy': "default-src 'self'; form-action 'self'", 'x-frame-options': 'DENY', 'x-content-type-options': 'nosniff', 'referrer-policy': 'strict-origin-when-cross-origin', }); console.log(report.grade); // 'B' or higher for (const h of report.headers) { if (h.status !== 'good') { console.log(h.header, h.recommendations); } } ``` ## 报告结构 ``` interface SecurityHeaderReport { url?: string; grade: 'A+' | 'A' | 'B' | 'C' | 'D' | 'F'; score: number; maxScore: number; percentage: number; // 0–100 headers: HeaderFinding[]; // one per checked header analyzedAt: string; // ISO 8601 timestamp } interface HeaderFinding { header: string; // header name score: number; // points earned maxScore: number; // max available status: 'good' | 'warning' | 'missing' | 'error'; raw?: string; // raw header value findings: string[]; // what is wrong recommendations: string[]; // how to fix it } ``` ## 评分标准 | 等级 | 分数 | |---|---| | A+ | ≥ 90% | | A | ≥ 75% | | B | ≥ 60% | | C | ≥ 40% | | D | ≥ 20% | | F | < 20% | ## 检查的头部 | 头部 | 最高分 | 关键检查项 | |---|---|---| | Strict-Transport-Security | 20 | max-age ≥ 1 年,includeSubDomains,preload | | Content-Security-Policy | 30 | 存在性,无 unsafe-inline/eval,无通配符,已设置 form-action | | X-Frame-Options | 15 | DENY 或 SAMEORIGIN(或 CSP frame-ancestors) | | X-Content-Type-Options | 10 | nosniff | | Referrer-Policy | 10 | 仅限严格值 | | Permissions-Policy | 10 | 存在性 | | Cross-Origin Policies | 5 | COEP、COOP、CORP | ## 适用对象 安全工程师、DevSecOps 团队和需要在每次部署时进行自动化头部审计的 ASM 平台集成商、对每个目标范围运行此工具的渗透测试人员,以及希望无需离开终端即可验证其应用安全态势的开发者。 ## 另请参阅 - [`@hailbytes/asm-scope-parser`](https://github.com/HailBytes/asm-scope-parser) — 解析和规范攻击面范围定义 - [`@hailbytes/mcp-security-scanner`](https://github.com/HailBytes/mcp-security-scanner) — 针对 MCP 服务器配置的安全扫描器 - [HailBytes ASM](https://hailbytes.com/asm) — 攻击面管理平台 *属于 [HailBytes](https://hailbytes.com) 开源安全工具包的一部分。*
标签:Cross-Origin策略, CSP, GNU通用公共许可证, HSTS, HTTP安全头, MITM代理, Node.js, npm包, Permissions-Policy, Referrer-Policy, SOC Prime, Web安全, X-Frame-Options, 修复建议, 安全头检查, 安全标准, 安全评级, 安全配置, 开发工具, 数据可视化, 网络安全, 自动化攻击, 蓝队分析, 隐私保护