security-headers

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Security 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;
curl
is enough.
Headers 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:
HeaderValueNotes
Strict-Transport-Security
max-age=63072000; includeSubDomains
HTTPS only. Start with a short
max-age
(for example 300) on a site you are unsure about. Add
preload
only when every subdomain is HTTPS for good, since it is hard to undo
X-Content-Type-Options
nosniff
Content-Security-Policy
start from
default-src 'self'; base-uri 'self'; object-src 'none'; form-action 'self'; frame-ancestors 'none'
Then allow only what the site really loads. See step 3
X-Frame-Options
DENY
(or
SAMEORIGIN
if you frame your own pages)
Old browsers. CSP
frame-ancestors
is the modern control; OWASP advises setting both for defense in depth
Referrer-Policy
strict-origin-when-cross-origin
(or
no-referrer
)
OWASP proposes
no-referrer
; the middle value keeps analytics working
Permissions-Policy
camera=(), microphone=(), geolocation=()
List only features the site does not use; add others as needed
If the site must be framed by a partner, use
frame-ancestors https://partner.example
and drop
X-Frame-Options
(it cannot name a host).
响应头值说明
Strict-Transport-Security
max-age=63072000; includeSubDomains
仅适用于 HTTPS。对于不确定的站点,先使用较短的
max-age
(例如 300)。只有在所有子域名都能稳定使用 HTTPS 时才添加
preload
,因为撤销起来很困难
X-Content-Type-Options
nosniff
Content-Security-Policy
从
default-src 'self'; base-uri 'self'; object-src 'none'; form-action 'self'; frame-ancestors 'none'
开始
然后仅允许站点实际需要加载的内容。参见第 3 步
X-Frame-Options
DENY
(如果需要嵌入自己的页面,则使用
SAMEORIGIN
)
适用于旧版浏览器。CSP 的
frame-ancestors
是现代控制方式;OWASP 建议同时设置两者以实现纵深防御
Referrer-Policy
strict-origin-when-cross-origin
(或
no-referrer
)
OWASP 建议使用
no-referrer
;中间值可以保留分析脚本的正常工作
Permissions-Policy
camera=(), microphone=(), geolocation=()
仅列出站点不使用的功能;根据需要添加其他功能
如果站点必须允许合作伙伴嵌入,请使用
frame-ancestors https://partner.example
,并移除
X-Frame-Options
(它无法指定主机名)。

Step 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:
  1. Send it as
    Content-Security-Policy-Report-Only
    first, with a
    report-to
    /
    report-uri
    endpoint if the user has one, and watch the browser console.
  2. Replace
    'unsafe-inline'
    with nonces or hashes where the framework supports them (Next.js and others can issue a per-request nonce).
  3. Add each legitimate origin to the right directive (
    script-src
    ,
    style-src
    ,
    img-src
    ,
    connect-src
    ,
    frame-src
    ).
  4. Switch to enforcing
    Content-Security-Policy
    when the console is clean on the main flows (sign-in, checkout, forms).
严格的 CSP 会破坏依赖内联脚本、第三方组件和分析脚本的页面。不要在未经验证的情况下直接上线:
  1. 先以
    Content-Security-Policy-Report-Only
    发送;如果用户已有
    report-to
    /
    report-uri
    端点,则一并配置,并观察浏览器控制台。
  2. 在框架支持的情况下,用 nonce 或 hash 替代
    'unsafe-inline'
    (Next.js 等框架可以为每个请求签发 nonce)。
  3. 将每个合法来源添加到正确的指令(directive)中(
    script-src
    、
    style-src
    、
    img-src
    、
    connect-src
    、
    frame-src
    )。
  4. 当主要流程(登录、结账、表单)在控制台中没有报错时,再切换为强制执行
    Content-Security-Policy
    。

Step 4: put it where it belongs

第 4 步:将配置放在正确的位置

Next.js (
next.config.ts
):
ts
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.json
):
json
{ "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
in the publish directory):
/*
  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
server
block;
always
adds the header on error responses too):
nginx
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
add_header
lines when a nested
location
sets its own: repeat them there.
Cloudflare: 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
.htaccess
or panel). Shopify and Webflow limit what the owner can set; say what is not changeable instead of promising it.
Next.js(
next.config.ts
):
ts
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.json
):
json
{ "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(放在
server
块中;
always
表示错误响应也会添加响应头):
nginx
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;
当嵌套的
location
设置了自己的
add_header
时,nginx 会丢弃父级的
add_header
配置,因此需要在该位置重复添加。
Cloudflare:在 Rules、Transform Rules、Modify Response Header 中,按主机名将每个响应头设置为“Set static”。在 SSL/TLS、Edge Certificates 中启用 Always Use HTTPS。每个响应头只保留一个来源;如果源站也设置了同一响应头,请检查响应中是否出现重复项。
WordPress、Shopify、Webflow:响应头需要在前置的主机或 CDN 中设置,例如 Cloudflare 规则、主机的
.htaccess
或控制面板。Shopify 和 Webflow 会限制所有者可配置的内容;应明确说明无法修改的部分,而不是承诺可以修改。

Step 5: verify

第 5 步:验证

Re-run the
curl -sI
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:
bash
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
sends nothing beyond the crawl. For sites the user does not own, add
--ephemeral
.
重新针对实时 URL 运行
curl -sI
检查,打开控制台并加载主要流程以检查 CSP 违规;如果已安装 squirrel CLI,还可以使用匹配的审计规则进行确认:
bash
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
。

Stop 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):
security-audit
. Supabase:
supabase-security-audit
. Gate it:
website-ci-gate
.
安全领域的其他内容(信息泄露、暴露文件、cookie、探测):
security-audit
。Supabase:
supabase-security-audit
。设置门禁:
website-ci-gate
。