feat: add Fetch Metadata CSRF protection #10221
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.48) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 549 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.378
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #10221"
general
weight: 0.35
neznaika0
Description
This PR proposes adding Fetch Metadata-based CSRF protection to CodeIgniter.
CSRF tokens work, and this PR keeps them as the compatibility fallback. But the web platform and modern frameworks are clearly moving toward browser-provided request context as the better default for browser CSRF protection. Other frameworks have already moved in this direction, or have active work around the same idea, and OWASP documents Fetch Metadata as a defense-in-depth layer for CSRF protection.
The reason is pretty simple: token-based CSRF protection has always been awkward and a source of headaches for many developers. Forms need hidden fields, JavaScript requests need special headers, JSON requests need special handling, cached pages and long-lived tabs can produce stale-token failures, token regeneration can surprise users, and teams regularly end up debugging "why did this normal request fail?" instead of working on the application.
Fetch Metadata gives the framework a cleaner browser-native signal. Modern browsers send the
Sec-Fetch-Siteheader, which tells the server whether an unsafe request came from the same origin, the same site, or another site. That lets CodeIgniter ask a simpler question before touching token verification:If yes, same-origin browser requests can pass without token plumbing. If it is clearly cross-site, it can be rejected before token verification. If the browser does not provide enough information, CodeIgniter falls back to the existing token check.
This keeps the change conservative while still moving new applications in the right direction:
none, unknown, orsame-siteSec-Fetch-Sitevalues fall back to token verification.cross-siterequests are rejected before token verification.$csrfFetchMetadata = false.same-siterequests before token verification with$csrfFetchMetadataRejectSameSite = true.With Fetch Metadata enabled:
Sec-Fetch-Site: same-originpasses without a token.Sec-Fetch-Site: cross-siteis rejected.Sec-Fetch-Site: same-sitefalls back to token verification by default.none, or unknown values fall back to token verification.So this PR does not remove CSRF tokens. They remain supported and useful as the migration fallback, especially for upgraded applications. But the direction is intentional: new CodeIgniter applications should get a more modern, browser-native CSRF protection path by default, and token-heavy CSRF handling can gradually become the legacy compatibility path rather than the everyday developer experience.
The CSRF filter also adds
Vary: Sec-Fetch-Siteto normal shared responses, CSRF redirect responses, and CSRF exception responses for unsafe requests when Fetch Metadata protection is enabled, so caches do not mix responses across request contexts.Tests cover same-origin, same-site, cross-site, token fallback, token-only compatibility, upgraded config compatibility, redirect responses,
Varybehavior, and request body preservation.References
Sec-Fetch-Site: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Fetch-SiteChecklist: