Skip to content

Collect CSP violations in browser E2E with an optional strict policy - #34

Merged
TungYenTsai merged 3 commits into
mainfrom
test/e2e-csp-violations
Nov 6, 2026
Merged

TungYenTsai merged 3 commits into
mainfrom
test/e2e-csp-violations

Conversation

@TungYenTsai

Copy link
Copy Markdown
Collaborator

改用強制 CSP 前的盤點工具,取代原規劃的 CSP 回報端點。回報端點需要一個免登入、免 CSRF 的 POST 網址,Checkmarx 可能列出 CSRF 停用與 Log Forging;改為在瀏覽器 E2E 收集,不新增任何對外網址。

目前的 CSP(compat)允許 inline script/style 與 eval,所以瀏覽器不會回報這些依賴。

修改:

  • CSP_POLICY=strict:script-src、style-src 只允許 'self',並加 'report-sample' 讓報告附上前 40 字。預設仍為 compat,正式行為不變;strict 只供測試環境搭配 CSP_MODE=report-only 使用(只回報、不阻擋)。
  • E2E 在每頁註冊 securitypolicyviolation 監聽,結束時依指令列出摘要,並寫入 tests/_output/e2e/csp-violations.json。違規只紀錄,不影響通過與否。
  • config/test.php 加入 SecurityHeadersBootstrap。原本只有 config/web.php 載入,所以 acceptance 與 E2E 都在沒有 CSP 等安全標頭的情況下執行,與正式環境不同。
  • .env.example、E2E README、CHANGELOG 已更新。

盤點結果(測試伺服器 CSP_POLICY=strict report-only,E2E 179 項):

指令 不重複 主要來源
script-src-elem 約 405 Yii registerJs 的 inline script(jQuery(window).on('load'…、字型大小、krajee dialog 設定、ActiveForm 等)
script-src-attr 66 版面的 SetFont()(54 頁)、投票頁 chkClick()、getQuestions()、voteRule()
style-src-elem 約 168 registerCss(.os-host > .os-padding、.header-pt 等)
style-src-attr 約 660 style="..." 屬性與套件動態加入的樣式
eval 0 E2E 涵蓋畫面未使用 eval

驗證(dev6,PHP 8.2.35):

  • SecurityHeadersBootstrapTest 12 tests/25 assertions 通過。
  • 測試設定送出安全標頭後,acceptance 129 tests/379 assertions 通過;完整 Chrome E2E 179/179 通過(第二次未重載 fixture,依資料狀態略過 2 項,177/177)。
  • 完整 unit/functional 以本 PR CI 為準;Gitleaks 未發現問題。

🤖 Generated with Claude Code

TungYenTsai and others added 3 commits November 6, 2026 13:34
The current CSP allows inline scripts, styles and eval, so browsers never
report those dependencies. Add CSP_POLICY=strict (report-only in test
environments) and have the E2E harness record every
securitypolicyviolation event, summarising them per directive and page.
The default policy and production behaviour are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Acceptance and browser E2E ran without CSP and the other security
headers because only config/web.php registered the bootstrap, so the
CSP violation collection had nothing to observe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TungYenTsai
TungYenTsai merged commit 3d2ed0a into main Nov 6, 2026
2 checks passed
@TungYenTsai
TungYenTsai deleted the test/e2e-csp-violations branch November 6, 2026 06:06
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