Skip to content

排查生产缺 HSTS/CORS 头,并让 CSP 与 Cloudflare 机器人检测共存 - #126

Merged
todosdev[bot] merged 2 commits into
mainfrom
tds/conv-01a14cb2-4683-7783-8842-6fc94ecaee74
Oct 18, 2026
Merged

todosdev[bot] merged 2 commits into
mainfrom
tds/conv-01a14cb2-4683-7783-8842-6fc94ecaee74

Conversation

@todosdev

@todosdev todosdev Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add a per-request CSP nonce to HTML responses so Cloudflare Bot Fight Mode's injected inline JavaScript Detections snippet is no longer blocked by script-src 'self' 'unsafe-eval' — without loosening to 'unsafe-inline'.
  • Normalize APP_URL (.trim()) before HSTS gating and CORS origin matching, so a stray leading/trailing space in the variable can't silently drop Strict-Transport-Security or break Access-Control-Allow-Origin echoing (the likely cause of both headers missing in production).
  • Add the minimal security headers that matter on a 302 (Referrer-Policy: no-referrer, HSTS on https) to short-link redirect responses.

Changes

  • apps/worker/src/lib/security.ts
    • Replace the static SPA_CSP with spaCsp(nonce?); append 'nonce-<uuid>' to script-src only when a nonce is supplied.
    • Extract a shared HSTS constant; .trim() APP_URL before startsWith('https').
  • apps/worker/src/index.ts
    • SPA fallback: mint a fresh nonce for text/html responses and pass it into spaCsp(); static assets get no nonce (meaningless there).
    • CORS: .trim() APP_URL when building the allowed-origin list.
  • apps/worker/src/routes/redirect.ts
    • Extract redirectHeaders(); all four 302 paths now emit Referrer-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)
    • Unit + routing tests for spaCsp, HSTS gating/trim, CORS echoing (incl. leading-space APP_URL), nonce uniqueness across requests, API responses having no nonce, and redirect headers.

Testing

  • pnpm check, pnpm typecheck, pnpm build, pnpm test all pass — worker 46 tests, including 8 new security-header tests.
  • Local wrangler dev + headless Chrome: /login renders the full Sign In form with 0 CSP violations; nonce is unique per request; no HSTS emitted locally under an http APP_URL (as expected).
  • Analytics charts / TOTP QR were not re-driven in a browser this turn (they require a logged-in account). The change only adds a nonce to script-src and leaves unsafe-eval, img-src data:, and style-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.

Todos 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
@todosdev
todosdev Bot merged commit c674c31 into main Oct 18, 2026
4 checks passed
@mapxn
mapxn deleted the tds/conv-01a14cb2-4683-7783-8842-6fc94ecaee74 branch October 18, 2026 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants