Skip to main content

Overview

revolution/laravel-fetch-metadata is a security middleware package that validates the Sec-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.
For the full Fetch Metadata specification, see the MDN documentation.

Installation

Registering middleware aliases

Register middleware aliases in bootstrap/app.php.
You can also use just some of the middleware. The alias names can be anything you like.

Middleware overview

SecFetchSite

The Sec-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

The Sec-Fetch-Mode header describes the request mode. By default, navigate and cors are allowed. See the MDN documentation for details.

SecFetchDest

The Sec-Fetch-Dest header describes the destination resource type of the request. See the MDN documentation for details.

SecFetchUser

The Sec-Fetch-User header indicates whether the request was initiated by user interaction. The only value is ?1 (user interaction).
Using the SecFetchUser middleware will also block search engine crawlers and AI agents. Do not use it on public pages that need to be indexed.

Routing examples

Basic usage

Specifying allowed values via parameters

Specifying multiple parameters

Without an alias

Combining multiple middleware

Error handling

When a Sec-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, the PreventRequestForgery 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.
Last modified on August 2, 2026