Repository navigation
排查生产缺 HSTS/CORS 头,并让 CSP 与 Cloudflare 机器人检测共存 - #126
Merged
todosdev[bot] merged 2 commits intoOct 18, 2026
Merged
Conversation
added 2 commits
October 18, 2026 10:06
- Replace the static SPA_CSP with spaCsp(nonce) and stamp a fresh nonce onto HTML responses so Bot Fight Mode's injected inline challenge script is no longer blocked by script-src, without loosening to 'unsafe-inline'. - Extract redirectHeaders() so short-link 302s also send Referrer-Policy: no-referrer and HSTS (on https), closing the header gap. - Add security.test.ts covering the nonce, HSTS gating, and redirect headers. HSTS and CORS not echoing in production are left untouched: they stem from the production APP_URL variable/Cloudflare edge, not the code, and are reported with dashboard instructions rather than worked around. Checkpoint: ugmHKE24tXFtLoM_XCxZi
- Replace the static SPA_CSP with spaCsp(nonce) and stamp a fresh nonce onto HTML responses so Bot Fight Mode's injected inline challenge script is no longer blocked by script-src, without loosening to 'unsafe-inline'. - Normalize APP_URL with .trim() in securityHeaders, CORS, and redirectHeaders so leading/trailing whitespace can't silently suppress HSTS or CORS echoing. - Extract redirectHeaders() so short-link 302s also send Referrer-Policy: no-referrer and HSTS (on https). - Add security.test.ts covering nonce uniqueness, HSTS gating/trim, CORS echo, and redirect headers. Checkpoint: YgqPChNPtOvsC137HUU9b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
script-src 'self' 'unsafe-eval'— without loosening to'unsafe-inline'.APP_URL(.trim()) before HSTS gating and CORS origin matching, so a stray leading/trailing space in the variable can't silently dropStrict-Transport-Securityor breakAccess-Control-Allow-Originechoing (the likely cause of both headers missing in production).Referrer-Policy: no-referrer, HSTS on https) to short-link redirect responses.Changes
apps/worker/src/lib/security.tsSPA_CSPwithspaCsp(nonce?); append'nonce-<uuid>'toscript-srconly when a nonce is supplied.HSTSconstant;.trim()APP_URLbeforestartsWith('https').apps/worker/src/index.tstext/htmlresponses and pass it intospaCsp(); static assets get no nonce (meaningless there)..trim()APP_URLwhen building the allowed-origin list.apps/worker/src/routes/redirect.tsredirectHeaders(); all four 302 paths now emitReferrer-Policy: no-referrer,Cache-Control: no-store, and HSTS (on https). CSP / XCTO / XFO are intentionally omitted (empty body).apps/worker/test/security.test.ts(new)spaCsp, HSTS gating/trim, CORS echoing (incl. leading-spaceAPP_URL), nonce uniqueness across requests, API responses having no nonce, and redirect headers.Testing
pnpm check,pnpm typecheck,pnpm build,pnpm testall pass — worker 46 tests, including 8 new security-header tests.wrangler dev+ headless Chrome:/loginrenders the full Sign In form with 0 CSP violations; nonce is unique per request; no HSTS emitted locally under anhttpAPP_URL(as expected).script-srcand leavesunsafe-eval,img-src data:, andstyle-src 'unsafe-inline'untouched, so it cannot introduce new violations there; these pages were previously verified against the same CSP in the QA smoke run.