Dealing with expired signed URLs in Laravel

Original – by Freek Van der Herten – 2 minute read

Out of the box, Laravel comes with the ability to generate "signed" URLs. These URLs have a hash in their query string that verifies that the URL was not modified.

At Flare, we use these signed URLs to add action links in mail notifications. The action links allow users to snooze and resolve errors right from the mail without having to be logged in. Pretty convenient!

My buddy Dries Vints noticed a slight drawback. He got a mail from Flare that contains these action links. A few hours after the mail arrived, he clicked one of the action links. This is what he saw.

screenshot

This error screen is confusing: you might think that the links in the mail are invalid. To keep things secure, we use a short lifetime for our signed URLs. Dries got this screen because the link had expired.

We can improve on this by creating a dedicated error message when clicking expired or invalid links. Luckily, this is not that difficult.

When you try to validate a signed URL and the validation fails, Laravel will throw a dedicated exception Illuminate\Routing\Exceptions\InvalidSignatureException In your exception handler, you can listen for that exception and render a dedicated view.

// in app/Exceptions/Handler.php

use Illuminate\Routing\Exceptions\InvalidSignatureException;

public function register()
{
   $this->renderable(function (InvalidSignatureException $exception) {
      return response()->view('error.link-expired', status: 403);
   });
}

With that code in place, this is what Dries will see when clicking another expired link in the future.

screenshot

And that is all there is to it. To avoid confusions for your users, I highly recommend setting up a dedicated error message when using signed URLs.

Thanks for bringing this to my attention, Dries.

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

Webmentions

Nguyễn Trần Chung liked on 9th May 2021
Mubassir Hayat retweeted on 9th May 2021
Victor Guedes liked on 8th May 2021
João Oliveira liked on 8th May 2021
Preslav Rachev liked on 8th May 2021
JUNO_OKYO ⚡ liked on 8th May 2021
J Cobb liked on 8th May 2021
Olivier Grenet liked on 8th May 2021
Dejan Blazeski liked on 8th May 2021
Andy retweeted on 8th May 2021
Andy liked on 8th May 2021
Noah Gillard liked on 8th May 2021
Noah Gillard retweeted on 8th May 2021
Fabio Ivona liked on 8th May 2021
Fabio Ivona retweeted on 8th May 2021
JSK liked on 7th May 2021
Yannick Yayo liked on 7th May 2021
buddha lama liked on 7th May 2021
Vaggelis Yfantis liked on 7th May 2021
This is one of the best advices / assets / tools for laravel developers.
Stefan Verweij liked on 7th May 2021
Dmitry Astratyan liked on 7th May 2021
Evert Ramos liked on 7th May 2021
Evert Ramos retweeted on 7th May 2021
Willan Correia liked on 7th May 2021
Paint retweeted on 7th May 2021
Laravel Digest liked on 7th May 2021
Laravel Digest retweeted on 7th May 2021
Salman Zafar liked on 7th May 2021
Keiron Skillett liked on 7th May 2021
Angad Dubey liked on 7th May 2021
Robert Keli liked on 7th May 2021
William retweeted on 7th May 2021
Eddy Cortez liked on 7th May 2021
Hamed liked on 7th May 2021
Muhammad Furqan Abid liked on 7th May 2021
Jigal Sanders liked on 7th May 2021
Arthur LORENT liked on 7th May 2021
Andrea Marco Sartori liked on 7th May 2021
Mario L liked on 6th May 2021
Khalid Chatar liked on 6th May 2021
Derek D liked on 6th May 2021
Martin Keck liked on 6th May 2021
ali ali liked on 6th May 2021
Alexander Maiorov liked on 6th May 2021
Phil Rowley liked on 6th May 2021
Tauseef shah liked on 6th May 2021
Ralph Morris liked on 6th May 2021
Stephen Moon liked on 6th May 2021
Lee Stables replied on 6th May 2021
Ooo will take a look
Phil Rowley replied on 6th May 2021
@lee_stables on topic! ;)
Sowren Sen liked on 6th May 2021
Marc-André Martin liked on 6th May 2021
Camiant liked on 6th May 2021
Chris Barr liked on 6th May 2021
Steve McDougall liked on 6th May 2021
Jorge González liked on 6th May 2021
Leandro Ahmad liked on 6th May 2021
bivinvinod liked on 6th May 2021
FoFa liked on 6th May 2021
Christopher liked on 6th May 2021
Théo Pirkl liked on 6th May 2021
Ajith Lal liked on 6th May 2021
Richard van Baarsen liked on 6th May 2021
Robin Hood liked on 6th May 2021
Outa™ retweeted on 6th May 2021
Ajith Lal retweeted on 6th May 2021
John Philip liked on 6th May 2021
Kruchy liked on 6th May 2021
Outa™ liked on 6th May 2021
Lennart Fischer liked on 6th May 2021
Jackpump liked on 6th May 2021
Felipe 🍕 liked on 6th May 2021
Azizullah Samim liked on 6th May 2021
smknstd liked on 6th May 2021
Parthasarathi G K liked on 6th May 2021
Mahfuz Shaikh replied on 6th May 2021
Thanks a lot, other day I was trying to implement that.
Weeb Developer retweeted on 6th May 2021
Rama Adi Nugraha retweeted on 6th May 2021
kronos_I retweeted on 6th May 2021
J.O.Y retweeted on 6th May 2021
Brandon Surowiec retweeted on 6th May 2021
Mohammad Nur retweeted on 6th May 2021
Deven Jahnke retweeted on 6th May 2021
Ari bobo retweeted on 6th May 2021
ArielMejiaDev retweeted on 6th May 2021
ArielMejiaDev liked on 6th May 2021
Kevin Ullyott liked on 6th May 2021
MeterPeaker liked on 6th May 2021
Bill Richards liked on 6th May 2021
Christian Klemp liked on 6th May 2021
Scorp974 liked on 6th May 2021
Dumitru Botezatu liked on 6th May 2021
Mahfuz Shaikh liked on 6th May 2021
Geidel Guerra liked on 6th May 2021
Giovanni López Orenday liked on 6th May 2021
liked on 6th May 2021
Ero-brunnin liked on 6th May 2021
Weeb Developer liked on 6th May 2021
Craig Paul retweeted on 6th May 2021
Samuel Moy Millán  liked on 6th May 2021
Kristoffer retweeted on 6th May 2021
Akash Shukla liked on 6th May 2021
Judah🔥🚀 liked on 6th May 2021
Tally Schmeits liked on 6th May 2021
Reece M liked on 6th May 2021
Mohammad Nur liked on 6th May 2021
Alex liked on 6th May 2021
Matthew Poulter liked on 6th May 2021
Edward Westbury liked on 6th May 2021
Patrick Silburn liked on 6th May 2021
J.O.Y liked on 6th May 2021
devzaim liked on 6th May 2021
Kristoffer liked on 6th May 2021
Kamau Wanyee liked on 6th May 2021
David Alexander liked on 6th May 2021
Craig Paul liked on 6th May 2021
Hayden liked on 6th May 2021
Robert Askam retweeted on 6th May 2021
jr retweeted on 6th May 2021
Robert Askam liked on 6th May 2021
Oilmone liked on 6th May 2021
NAbeel Yousaf 🇵🇰 liked on 6th May 2021
Padam Shankhadev liked on 6th May 2021
owls two-shots liked on 6th May 2021
Paul Grimes liked on 6th May 2021
Michael Dyrynda retweeted on 6th May 2021
Divan liked on 6th May 2021
Paulius Jasiulis liked on 6th May 2021
Robin Dirksen liked on 6th May 2021
Wyatt liked on 6th May 2021
Simon Blonér liked on 6th May 2021
Alex Clark replied on 6th May 2021
TIL, didn’t even know about signed urls in Laravel. Very cool!
Khai Rahman retweeted on 6th May 2021
Alexandr Nita retweeted on 6th May 2021
Jamie Shiers liked on 6th May 2021
Abrar Ahmad liked on 6th May 2021
Tom Witkowski liked on 6th May 2021
Khai Rahman liked on 6th May 2021
Hamnavoe Solutions liked on 6th May 2021
Towhid liked on 6th May 2021
Nour-eddine liked on 6th May 2021
Alexandr Nita liked on 6th May 2021
Antonio Dal Sie liked on 6th May 2021
Leonardo Prabangkoro liked on 6th May 2021
Dries Vints retweeted on 6th May 2021
Jeanne retweeted on 6th May 2021
Daniel retweeted on 6th May 2021
فرودو liked on 6th May 2021
PHP Synopsis retweeted on 6th May 2021
Bezhan Salleh retweeted on 6th May 2021
James liked on 6th May 2021
Darshan... liked on 6th May 2021
Qisthi Ramadhani liked on 6th May 2021
معاذ liked on 6th May 2021
Elo liked on 6th May 2021
Zubair Mohsin liked on 6th May 2021
Thijs Kok liked on 6th May 2021
Sasa Blagojevic liked on 6th May 2021
Peter Brinck 🤘 liked on 6th May 2021
Jonny Nott liked on 6th May 2021
imabug liked on 6th May 2021
Danny Le Page liked on 6th May 2021
Thibault Lavoisey liked on 6th May 2021
Bezhan Salleh liked on 6th May 2021
Quintus van Vuuren liked on 6th May 2021
Onno Lissenberg liked on 6th May 2021
Zysk Technologies liked on 6th May 2021
Dries Vints liked on 6th May 2021
βliss Jäspis liked on 6th May 2021
Jan 🧘🏻‍♂️ liked on 6th May 2021
Daniel liked on 6th May 2021
Karl liked on 6th May 2021
Bernardo liked on 6th May 2021
Taylor Maguire replied on 6th May 2021
Very timely. Thanks!
. liked on 6th May 2021
Flare retweeted on 6th May 2021
Spatie retweeted on 6th May 2021
Yasuke's Stan liked on 6th May 2021
James Smith liked on 6th May 2021
Yasuke's Stan retweeted on 6th May 2021
Bjarn Bronsveld liked on 6th May 2021
Faruk Nasir liked on 6th May 2021