LessonPHPLaravelSecurity

Signed URLs in Laravel: signedRoute, Expiry and the signed Middleware

Cream envelope with a green ribbon and glowing emerald wax seal standing beside a brass hourglass on a dark wooden desk
On this page
  1. Creating a signed URL
  2. Validating the request
  3. Read signed values from the query string
  4. Quick audit checklist
  5. Sources

Quick answer: a signed URL lets you email a public link — unsubscribe, download, verify — and later reject any change to the path or query string. It is not a login, and it is not an authorization check. Generate the link with URL::signedRoute() or URL::temporarySignedRoute(), validate it with the signed middleware, and read the signed values with $request->query().

Creating a signed URL

Laravel appends a signature hash to the query string of a named route. The docs describe this as a way to confirm that the URL has not been modified since you created it. The usual example is a public unsubscribe link:

use Illuminate\Support\Facades\URL;

return URL::signedRoute('unsubscribe', ['user' => 1]);

return URL::temporarySignedRoute(
    'unsubscribe',
    now()->plus(minutes: 30),
    ['user' => 1]
);

temporarySignedRoute() embeds an expiry. When Laravel later validates the URL, it checks that this timestamp has not passed. The Laravel 13.x docs use now()->plus(minutes: 30) for a 30-minute link (and now()->plus(minutes: 5) on the fluent Uri helper). Prefer a temporary link for anything sent by email.

If you generate a relative signed URL with absolute: false, the hash does not include the domain:

URL::signedRoute('unsubscribe', ['user' => 1], absolute: false);

You must then validate it as a relative signature, or the check will fail.

Validating the request

There are two documented ways to check the signature.

hasValidSignature() on the request returns a boolean. The docs’ example calls abort(401) when it is missing:

Route::get('/unsubscribe/{user}', function (Request $request) {
    if (! $request->hasValidSignature()) {
        abort(401);
    }
    // ...
})->name('unsubscribe');

The signed middleware (Illuminate\Routing\Middleware\ValidateSignature) is usually clearer. If the signature is missing or wrong, the middleware returns HTTP 403:

Route::post('/unsubscribe/{user}', function (Request $request) {
    // ...
})->name('unsubscribe')->middleware('signed');

For links created with absolute: false, use middleware('signed:relative').

hasValidSignatureWhileIgnoring(['page', 'order']) exists for the case where the frontend must append pagination or sort parameters. The docs warn that ignored parameters can be changed by anyone. Only ignore names you can afford to treat as unsigned.

When a temporary link has expired, Laravel shows the generic 403 error page. You can render a friendlier view by handling Illuminate\Routing\Exceptions\InvalidSignatureException in bootstrap/app.php:

use Illuminate\Routing\Exceptions\InvalidSignatureException;

->withExceptions(function (Exceptions $exceptions): void {
    $exceptions->render(function (InvalidSignatureException $e) {
        return response()->view('errors.link-expired', status: 403);
    });
})

Read signed values from the query string

This is the easy bug. The Laravel 13.x docs are explicit: a signature only covers the URL path and query string. A form body or a JSON body is not signed and can be changed. Laravel merges input sources and gives the request body precedence over the query string, so $request->input('email'), $request->all(), or $request->email can return an unsigned value that overrides the signed query parameter. That is also true of GET requests, because a GET request can still carry a JSON body.

Always read signed parameters with $request->query('email') (or the route parameter for values that are part of the path).

A valid signature also does not mean “this user may do this”. It only means the link was not altered and, for a temporary link, has not expired. If the action is sensitive, still authenticate the user or check a capability, and keep the expiry short. Treat a signed URL the way you treat a secret token: if the email leaks, anyone who has the link can open it until it expires.

Do not send signed links through an open redirect. And do not use a signed URL as a place to hide a secret you would not want in a log file; the whole URL often ends up in browser history, referrers and access logs.

Laravel 13.x also has fluent URI helpers, Uri::signedRoute() and Uri::temporarySignedRoute(), that follow the same rules — including now()->plus(minutes: 5) in the docs’ own example. Whichever helper you use, the validation side stays the same: signed or signed:relative, and query() for the values.

Quick audit checklist

  • Use temporarySignedRoute() for email and other outbound links, with a short expiry.
  • Put middleware('signed') (or signed:relative) on the target route.
  • Read signed parameters with $request->query() or the route parameter, never input() or $request->email.
  • Only use hasValidSignatureWhileIgnoring() for parameters that are safe to leave unsigned.
  • Customise InvalidSignatureException so an expired unsubscribe link does not look like a generic 403.
  • Remember that a signed URL is not authentication and not authorization.

Related lessons: Secure Random Tokens in PHP and Laravel and Open Redirects in PHP and Laravel.

This lesson was drafted with AI assistance and reviewed for accuracy against the Laravel 13.x URL generation documentation (signed URLs, validation, the signed middleware, and invalid-signature responses).

Sources

Need an authorized audit?

For a PHP, Laravel or WordPress site you own or are authorized to manage, we can agree a fixed-scope review in writing first. Pricing is contact for quote.

Get new briefs by email

Short alerts when a PHP or WordPress vulnerability needs action.

Double opt-in via Buttondown; unsubscribe any time. See our privacy policy.