Skip to content

Enforce the compatibility CSP by default and stop allowing eval - #35

Merged
TungYenTsai merged 1 commit into
mainfrom
fix/csp-enforce-compat
Nov 6, 2026
Merged

TungYenTsai merged 1 commit into
mainfrom
fix/csp-enforce-compat

Conversation

@TungYenTsai

Copy link
Copy Markdown
Collaborator

CSP 收斂第 1 步。原本 CSP 只有 report-only,實際上不阻擋任何內容,黑箱掃描(AppScan)通常視為未設定 CSP;政策也允許 'unsafe-eval'。

修改:

  • CSP_MODE 預設由 report-only 改為 enforce。
  • compat 政策移除 'unsafe-eval',仍允許 inline script/style(Yii registerJs、AdminLTE 需要),畫面行為不變。
  • CSP_MODE=report-only 保留為立即還原手段,只要改 .env,不需重新部署。
  • 單元測試改用新的 Response 物件,避免沿用其他測試的標頭;新增預設 enforce、report-only 還原、compat 不含 eval 的案例。
  • CHANGELOG(含升級注意)、.env.example、SECURITY.md、SETUP.md 部署檢查表、威脅模型、E2E README 已更新。

移除 'unsafe-eval' 的依據:

  1. Collect CSP violations in browser E2E with an optional strict policy #34 的 E2E 盤點(strict report-only,179 項)中 eval 違規為 0。
  2. 實際載入的前端資產(frontend/、kartik、Yii、jQuery、Bootstrap、AdminLTE dist、select2,含壓縮檔)皆未使用 eval 或 new Function。唯一命中的是 AdminLTE 內附但本系統未載入的 dropzone、inputmask、pdfmake。

仍有的限制:E2E 未涵蓋的畫面可能有未知依賴。正式環境若發現功能被擋,可設 CSP_MODE=report-only。

驗證(dev6,PHP 8.2.35,HEAD dad75b9,測試伺服器不設任何 CSP 變數,也就是使用新預設):

  • 回應標頭為 Content-Security-Policy(強制),script-src 'self' 'unsafe-inline'。
  • SecurityHeadersBootstrapTest 13 tests/27 assertions 通過。
  • Acceptance 129 tests/379 assertions 通過。
  • 本機 Chrome 完整 E2E 179/179 通過,CSP 違規 0 筆。
  • 完整 unit/functional 以本 PR CI 為準;Gitleaks 未發現問題。

下一步(另開 PR):為 registerJs 加 nonce、將 6 種 inline 事件處理改為事件監聽,移除 script-src 'unsafe-inline'。

🤖 Generated with Claude Code

The CSP was report-only, so it blocked nothing and scanners treated the
site as having no policy. The E2E inventory found no eval use, and none
of the loaded frontend assets call eval or new Function, so drop
'unsafe-eval' from the compat policy and enforce it by default.
CSP_MODE=report-only remains as an immediate rollback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TungYenTsai
TungYenTsai merged commit 123cfb2 into main Nov 6, 2026
2 checks passed
@TungYenTsai
TungYenTsai deleted the fix/csp-enforce-compat branch November 6, 2026 08:26
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.

1 participant