85 lines
5.1 KiB
JavaScript
85 lines
5.1 KiB
JavaScript
// Pages Functions 安全头门禁(2026-10-20 加,审计 H1 的自动化护栏)。
|
||
//
|
||
// 背景(为什么会加):
|
||
// Cloudflare Pages 的 `_headers` **不作用于 Pages Functions 响应**
|
||
// (https://developers.cloudflare.com/pages/configuration/headers/)——
|
||
// 静态资源(app.js / style.css)有 CSP + nosniff,而 `/api/*` 与 `/plugins/*` 这两个
|
||
// Functions 此前**一个安全头都没有**。性质是**纵深防御缺口**(每个响应本就显式设了
|
||
// 安全的 Content-Type,不是可利用漏洞),但缺兜底意味着:将来新增端点时漏设
|
||
// Content-Type,浏览器会自行嗅探类型 ⇒ 可能演变成 XSS。
|
||
// 此前这两条路径**本地完全测不到**(serve-test.js 只服务 dist/ 静态资源),
|
||
// 「本地过、线上挂」这一类缺陷无法被 e2e 发现 ⇒ 补上本地 Functions 模拟 + 本用例。
|
||
//
|
||
// 本用例守三件事:
|
||
// ① 每个 Function 响应的**每个分支**都带 X-Content-Type-Options: nosniff + Referrer-Policy: no-referrer;
|
||
// ② Function 响应**不含** `_headers` 里的 CSP —— 显式钉死「本地与线上一致:_headers 不作用于 Function」;
|
||
// (这条最关键:若哪天本地"好心"给 Function 套上 CSP,线上差异就被掩盖了,② 会先红)
|
||
// ③ 静态资源**仍然**带 CSP —— 防止修 Functions 时误伤静态资源的既有防护。
|
||
//
|
||
// 依赖:scripts/serve-test.js 的本地 Functions 模拟(/api/* 与 /plugins/* 路由)。
|
||
// 该模拟**只透传 Function 自己的 header**,不叠加 _headers —— 与线上语义一致。
|
||
|
||
const { test, expect } = require('./_pw-coverage')
|
||
|
||
const baseURL = process.env.IWB_BASE_URL || 'http://127.0.0.1:8787'
|
||
test.use({ baseURL, viewport: { width: 1280, height: 900 } })
|
||
|
||
// 断言某个响应带 Functions 必带的安全头
|
||
async function expectFunctionSecurityHeaders(res, who) {
|
||
const h = res.headers() // APIResponse.headers() 是同步的,返回小写 header 名映射
|
||
expect(h['x-content-type-options'], who + ' 应有 X-Content-Type-Options: nosniff').toBe('nosniff')
|
||
expect(h['referrer-policy'], who + ' 应有 Referrer-Policy: no-referrer').toBe('no-referrer')
|
||
}
|
||
|
||
test('Functions 响应带 nosniff + Referrer-Policy(且不含 _headers 的 CSP)', async ({ request }) => {
|
||
// ① /plugins/index.json(清单)
|
||
const manifest = await request.get('/plugins/index.json')
|
||
expect(manifest.status(), '/plugins/index.json 应 200').toBe(200)
|
||
await expectFunctionSecurityHeaders(manifest, '/plugins/index.json')
|
||
|
||
// ② /plugins/<内容寻址>.js(不可变缓存的插件字节)
|
||
const body = await manifest.text()
|
||
const file = (JSON.parse(body)[0] || {}).file
|
||
expect(file, '本地 mock 清单应含一个插件').toBeTruthy()
|
||
const pluginJs = await request.get('/plugins/' + file)
|
||
expect(pluginJs.status(), '/plugins/' + file + ' 应 200').toBe(200)
|
||
await expectFunctionSecurityHeaders(pluginJs, '/plugins/' + file)
|
||
|
||
// ③ /plugins 未知键 → 404 分支也必须带安全头
|
||
const missing = await request.get('/plugins/plugin-does-not-exist.js')
|
||
expect(missing.status(), '/plugins 未知键应 404').toBe(404)
|
||
await expectFunctionSecurityHeaders(missing, '/plugins 404')
|
||
|
||
// ④ /api/img 各分支:非白名单 host → 403
|
||
const forbidden = await request.get('/api/img?url=' + encodeURIComponent('https://evil.example/a.png'))
|
||
expect(forbidden.status(), '/api/img 非白名单 host 应 403').toBe(403)
|
||
await expectFunctionSecurityHeaders(forbidden, '/api/img 403')
|
||
|
||
// ⑤ /api/img 非 https → 400
|
||
const badProto = await request.get('/api/img?url=' + encodeURIComponent('http://d1.cloudfront.net/a.png'))
|
||
expect(badProto.status(), '/api/img 非 https 应 400').toBe(400)
|
||
await expectFunctionSecurityHeaders(badProto, '/api/img 400')
|
||
|
||
// ⑥ /api/img 成功分支(上游由 serve-test 本地 mock,不依赖外部图床)
|
||
const ok = await request.get('/api/img?url=' + encodeURIComponent('https://d1.cloudfront.net/a.png'))
|
||
expect(ok.status(), '/api/img 白名单 host 应 200').toBe(200)
|
||
await expectFunctionSecurityHeaders(ok, '/api/img 200')
|
||
|
||
// ② 反向断言:Function 响应**不含** CSP —— 与线上一致(_headers 不作用于 Functions)。
|
||
// 这条防止本地模拟"好心"叠加 CSP,从而掩盖真实的线上差异。
|
||
for (const res of [manifest, pluginJs, missing, forbidden, badProto, ok]) {
|
||
const csp = res.headers()['content-security-policy']
|
||
expect(csp, 'Function 响应不应带 CSP(_headers 不作用于 Functions;线上亦如此)').toBeFalsy()
|
||
}
|
||
})
|
||
|
||
test('静态资源仍带 CSP(修 Functions 时不得误伤静态资源防护)', async ({ request }) => {
|
||
const app = await request.get('/app.js')
|
||
expect(app.status(), '/app.js 应 200').toBe(200)
|
||
const csp = app.headers()['content-security-policy']
|
||
expect(csp, '静态资源应有 Content-Security-Policy').toBeTruthy()
|
||
expect(csp, 'CSP 应限制 script-src 为 self').toContain("script-src 'self'")
|
||
expect(csp, 'CSP 不应允许 unsafe-eval').not.toContain('unsafe-eval')
|
||
expect(app.headers()['x-content-type-options'], '静态资源应有 nosniff').toBe('nosniff')
|
||
})
|