security-headers
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSecurity Headers
安全响应头
Write the HTTP security headers for a site and put them where the platform wants them: Cloudflare, Vercel, Netlify, nginx or Next.js. No squirrel CLI is needed; is enough.
curlHeaders reduce the damage of some attacks (clickjacking, MIME sniffing, downgrade to http, injected scripts). They do not fix a vulnerable app. A missing header is a finding to fix; a full set of headers is not proof the site is secure.
为站点编写 HTTP 安全响应头,并将它们放在对应平台要求的位置:Cloudflare、Vercel、Netlify、nginx 或 Next.js。无需 squirrel CLI; 即可。
curl安全响应头可以减少某些攻击造成的损害(点击劫持、MIME 嗅探、降级到 HTTP、脚本注入),但不能修复存在漏洞的应用。缺失的响应头是应当修复的安全问题;配置完整的一组响应头并不能证明站点安全。
Step 1: see what is sent now
第 1 步:查看当前返回的响应头
bash
curl -sI https://example.com | grep -i -E '^(strict-transport|content-security|x-frame|x-content-type|referrer-policy|permissions-policy|cross-origin|set-cookie)'Check a page, not only the homepage, and the response a browser gets (not a cached one from a CDN with other rules). Note which headers are already set and by what: the app, the CDN or the host.
bash
curl -sI https://example.com | grep -i -E '^(strict-transport|content-security|x-frame|x-content-type|referrer-policy|permissions-policy|cross-origin|set-cookie)'检查一个具体页面,而不只是首页,并检查浏览器实际收到的响应(而不是 CDN 按其他规则缓存的响应)。记录已设置哪些响应头,以及它们由谁设置:应用、CDN 还是主机。
Step 2: the baseline set
第 2 步:基础响应头集合
OWASP's secure headers project (https://secure-headers-project.owasp.org/best-practices) proposes this baseline:
| Header | Value | Notes |
|---|---|---|
| | HTTPS only. Start with a short |
| | |
| start from | Then allow only what the site really loads. See step 3 |
| | Old browsers. CSP |
| | OWASP proposes |
| | List only features the site does not use; add others as needed |
If the site must be framed by a partner, use and drop (it cannot name a host).
frame-ancestors https://partner.exampleX-Frame-OptionsOWASP 的 secure headers 项目(https://secure-headers-project.owasp.org/best-practices)建议采用以下基础配置:
| 响应头 | 值 | 说明 |
|---|---|---|
| | 仅适用于 HTTPS。对于不确定的站点,先使用较短的 |
| | |
| 从 | 然后仅允许站点实际需要加载的内容。参见第 3 步 |
| | 适用于旧版浏览器。CSP 的 |
| | OWASP 建议使用 |
| | 仅列出站点不使用的功能;根据需要添加其他功能 |
如果站点必须允许合作伙伴嵌入,请使用 ,并移除 (它无法指定主机名)。
frame-ancestors https://partner.exampleX-Frame-OptionsStep 3: roll CSP out safely
第 3 步:安全地逐步启用 CSP
A strict CSP breaks pages that rely on inline scripts, third-party widgets and analytics. Do not ship it blind:
- Send it as first, with a
Content-Security-Policy-Report-Only/report-toendpoint if the user has one, and watch the browser console.report-uri - Replace with nonces or hashes where the framework supports them (Next.js and others can issue a per-request nonce).
'unsafe-inline' - Add each legitimate origin to the right directive (,
script-src,style-src,img-src,connect-src).frame-src - Switch to enforcing when the console is clean on the main flows (sign-in, checkout, forms).
Content-Security-Policy
严格的 CSP 会破坏依赖内联脚本、第三方组件和分析脚本的页面。不要在未经验证的情况下直接上线:
- 先以 发送;如果用户已有
Content-Security-Policy-Report-Only/report-to端点,则一并配置,并观察浏览器控制台。report-uri - 在框架支持的情况下,用 nonce 或 hash 替代 (Next.js 等框架可以为每个请求签发 nonce)。
'unsafe-inline' - 将每个合法来源添加到正确的指令(directive)中(、
script-src、style-src、img-src、connect-src)。frame-src - 当主要流程(登录、结账、表单)在控制台中没有报错时,再切换为强制执行 。
Content-Security-Policy
Step 4: put it where it belongs
第 4 步:将配置放在正确的位置
Next.js ():
next.config.tsts
const headers = [
{ key: "Strict-Transport-Security", value: "max-age=63072000; includeSubDomains" },
{ key: "X-Content-Type-Options", value: "nosniff" },
{ key: "X-Frame-Options", value: "DENY" },
{ key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
{ key: "Permissions-Policy", value: "camera=(), microphone=(), geolocation=()" },
];
export default { async headers() { return [{ source: "/(.*)", headers }]; } };Vercel ():
vercel.jsonjson
{ "headers": [{ "source": "/(.*)", "headers": [
{ "key": "X-Content-Type-Options", "value": "nosniff" },
{ "key": "X-Frame-Options", "value": "DENY" },
{ "key": "Referrer-Policy", "value": "strict-origin-when-cross-origin" }
] }] }Netlify ( in the publish directory):
_headers/*
Strict-Transport-Security: max-age=63072000; includeSubDomains
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()nginx (in the block; adds the header on error responses too):
serveralwaysnginx
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;nginx drops parent lines when a nested sets its own: repeat them there.
add_headerlocationCloudflare: Rules, Transform Rules, Modify Response Header: add each header as "Set static" for the hostname. Turn on Always Use HTTPS in SSL/TLS, Edge Certificates. Keep one source for each header: if the origin also sets it, check the response for a duplicate.
WordPress, Shopify, Webflow: headers are set at the host or CDN in front (a Cloudflare rule, the host's or panel). Shopify and Webflow limit what the owner can set; say what is not changeable instead of promising it.
.htaccessNext.js():
next.config.tsts
const headers = [
{ key: "Strict-Transport-Security", value: "max-age=63072000; includeSubDomains" },
{ key: "X-Content-Type-Options", value: "nosniff" },
{ key: "X-Frame-Options", value: "DENY" },
{ key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
{ key: "Permissions-Policy", value: "camera=(), microphone=(), geolocation=()" },
];
export default { async headers() { return [{ source: "/(.*)", headers }]; } };Vercel():
vercel.jsonjson
{ "headers": [{ "source": "/(.*)", "headers": [
{ "key": "X-Content-Type-Options", "value": "nosniff" },
{ "key": "X-Frame-Options", "value": "DENY" },
{ "key": "Referrer-Policy", "value": "strict-origin-when-cross-origin" }
] }] }Netlify(发布目录中的 ):
_headers/*
Strict-Transport-Security: max-age=63072000; includeSubDomains
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()nginx(放在 块中; 表示错误响应也会添加响应头):
serveralwaysnginx
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;当嵌套的 设置了自己的 时,nginx 会丢弃父级的 配置,因此需要在该位置重复添加。
locationadd_headeradd_headerCloudflare:在 Rules、Transform Rules、Modify Response Header 中,按主机名将每个响应头设置为“Set static”。在 SSL/TLS、Edge Certificates 中启用 Always Use HTTPS。每个响应头只保留一个来源;如果源站也设置了同一响应头,请检查响应中是否出现重复项。
WordPress、Shopify、Webflow:响应头需要在前置的主机或 CDN 中设置,例如 Cloudflare 规则、主机的 或控制面板。Shopify 和 Webflow 会限制所有者可配置的内容;应明确说明无法修改的部分,而不是承诺可以修改。
.htaccessStep 5: verify
第 5 步:验证
Re-run the check against the live URL, load the main flows with the console open for CSP violations, and, if the squirrel CLI is installed, confirm with the matching audit rules:
curl -sIbash
squirrel audit https://example.com --rule-include security/csp,security/hsts,security/x-frame-options,security/x-content-type,security/referrer-policy,security/permissions-policy --passive --format llm--passive--ephemeral重新针对实时 URL 运行 检查,打开控制台并加载主要流程以检查 CSP 违规;如果已安装 squirrel CLI,还可以使用匹配的审计规则进行确认:
curl -sIbash
squirrel audit https://example.com --rule-include security/csp,security/hsts,security/x-frame-options,security/x-content-type,security/referrer-policy,security/permissions-policy --passive --format llm--passive--ephemeralStop condition
停止条件
Stop when the live response carries the agreed headers on the pages checked, nothing the site needs is blocked in the console, and CSP is enforcing (or the user has accepted report-only for now, written down). Tell the user which headers the platform did not let them set.
当检查过的页面在实时响应中包含约定的响应头、控制台中没有站点所需的功能被阻止,且 CSP 已强制执行(或用户接受暂时保持仅报告模式并已记录)时停止。告知用户哪些响应头因平台限制而无法设置。
Hand off
后续处理
Everything else in security (leaks, exposed files, cookies, probes): . Supabase: . Gate it: .
security-auditsupabase-security-auditwebsite-ci-gate安全领域的其他内容(信息泄露、暴露文件、cookie、探测):。Supabase:。设置门禁:。
security-auditsupabase-security-auditwebsite-ci-gate