Overview
revolution/laravel-fetch-metadata is a security middleware package that validates theSec-Fetch-* HTTP headers sent by browsers. You can control which requests to allow based on the origin, mode, destination, and whether the request was user-initiated.
By leveraging browser-built-in security features, you can block malicious requests from unauthorized origins without harming the experience for legitimate users.
In Laravel 13, CSRF protection uses the
Sec-Fetch-Site header for origin validation. See CSRF protection for details.Installation
Registering middleware aliases
Register middleware aliases inbootstrap/app.php.
Middleware overview
SecFetchSite
TheSec-Fetch-Site header describes the relationship between the request’s origin and the target origin. By default, only same-origin and none (direct access) are allowed.
See the MDN documentation for details.
SecFetchMode
TheSec-Fetch-Mode header describes the request mode. By default, navigate and cors are allowed.
See the MDN documentation for details.
SecFetchDest
TheSec-Fetch-Dest header describes the destination resource type of the request.
See the MDN documentation for details.
SecFetchUser
TheSec-Fetch-User header indicates whether the request was initiated by user interaction. The only value is ?1 (user interaction).
Routing examples
Basic usage
Specifying allowed values via parameters
Specifying multiple parameters
Without an alias
Combining multiple middleware
Error handling
When aSec-Fetch-* header value is invalid, Symfony\Component\HttpKernel\Exception\BadRequestHttpException is thrown.
You can customize the response in bootstrap/app.php.
Relationship with CSRF protection
In Laravel 13, thePreventRequestForgery middleware now uses the Sec-Fetch-Site header for origin validation as the first step in CSRF protection. This package lets you apply Fetch Metadata headers at a finer grain to further harden your application.
See CSRF protection for details.
For the latest updates, see the GitHub repository.