Overview
revolution/laravel-fetch-metadata is a security-focused middleware package that validatesSec-Fetch-* HTTP headers sent by browsers. It allows you to control which requests are permitted based on their origin relationship, mode, destination resource type, and user interaction status.
By leveraging the browser’s built-in security features, this package helps prevent malicious requests from unauthorized origins while maintaining a seamless experience for legitimate users.
In Laravel 13, the CSRF protection now uses the
Sec-Fetch-Site header for origin verification. See CSRF Protection for details.Installation
Register middleware aliases
Register middleware aliases inbootstrap/app.php.
Middleware classes
SecFetchSite
TheSec-Fetch-Site header indicates the relationship between the request initiator’s origin and the target’s origin. By default, this middleware allows same-origin and none (direct navigation).
| Value | Description |
|---|---|
same-origin | Request from the same origin |
same-site | Request from the same site, e.g. a subdomain |
cross-site | Request from a different site |
none | User-initiated navigation, e.g. typing a URL directly |
SecFetchMode
TheSec-Fetch-Mode header indicates the mode of the request. By default, this middleware allows navigate and cors.
| Value | Description |
|---|---|
navigate | Navigation request such as clicking a link or submitting a form |
cors | CORS request |
no-cors | No-CORS request |
same-origin | Same-origin request |
websocket | WebSocket connection request |
SecFetchDest
TheSec-Fetch-Dest header indicates 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-activated).
Usage in routing
Basic usage
Specify allowed values via parameters
Specify 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 performs origin verification using the Sec-Fetch-Site header as the first step of CSRF protection. This package allows you to leverage fetch metadata headers at a finer granularity to further strengthen your application’s security.
See CSRF Protection for details.
For the latest information, see the GitHub repository.