[13.x] Improve origin verification in PreventRequestForgery#59198
Open
SanderMuller wants to merge 1 commit intolaravel:13.xfrom
Open
[13.x] Improve origin verification in PreventRequestForgery#59198SanderMuller wants to merge 1 commit intolaravel:13.xfrom
SanderMuller wants to merge 1 commit intolaravel:13.xfrom
Conversation
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
This PR makes three focused improvements to the
PreventRequestForgerymiddleware shipped in #58400:Handle
Sec-Fetch-Site: noneas a safe value. The browser setsnonewhen there is no external origin involved (e.g., forms onfile://pages, browser extensions). This is a bugfix fororiginOnlymode, which incorrectly throwsOriginMismatchExceptionfor these requests. Go 1.25 and the OWASP Fetch Metadata policy both treatnoneas safe.Fall back to
Originheader whenSec-Fetch-Siteis absent. Clients that don't sendSec-Fetch-Site(plain HTTP contexts, older browsers, some WebViews) are forced through token validation even when theOriginheader already proves the request is same-origin. This adds a lightweightOriginvsHostcomparison using Symfony'sgetSchemeAndHttpHost(). In default mode, mismatches fall through totokensMatch(). InoriginOnlymode, a matchingOriginnow correctly passes instead of throwing.Helpful error message on HTTP in
originOnlymode. Browsers don't sendSec-Fetch-Siteover insecure connections. WhenoriginOnlyis enabled on plain HTTP, the exception now says "Origin verification requires a secure connection" instead of the generic "Origin mismatch", so developers know exactly what to fix.Changes
hasValidOrigin(): acceptsnone, falls back to Origin-vs-Host, improved error on HTTPoriginMatchesHost(): newprotectedmethod (6 lines), comparesOriginheader against$request->getSchemeAndHttpHost()Precedent
CrossOriginProtection: same three-tier strategy (Sec-Fetch-Site → Origin → allow non-browser)noneas safe, recommends Origin fallback