Private downloads with signed URLs: the visitor never picks the filename
If a customer invoice URL can be guessed, it is not private. A signed URL expires in minutes and refuses edits; the rest of the job is making sure the file path never comes from the visitor.
Why the Private Disk Never Gets a Public URL
Laravel's local disk points at storage/app/private by default. The public disk points at storage/app/public and is exposed through a symlink at public/storage: anything dropped there is reachable by URL. Putting client documents on the public disk is publishing them; anyone who guesses the name downloads the file, and with directory listing on, guessing is not even required. A hard-to-guess name does not save you either: logs, backups and indexes exist, and a link forwarded by email or leaked through a Referer header never expires.
A Signed Route Expires and Refuses Edits
The signature is a hash of the URL plus its expiry, keyed with the application secret. You build the link on the server and validate it on the route with the signed middleware, which returns 403 when the signature does not match or the time is up:
Route::get('/downloads/{document}', [DownloadController::class, 'download'])
->name('files.download')
->middleware('signed');Build the link when you are about to hand it out, not while rendering the list:
Read also
$url = URL::temporarySignedRoute(
'files.download',
now()->plus(minutes: 5),
['document' => $document->id]
);Validating by hand is equivalent: $request->hasValidSignature() gives you the boolean and you pick the status code, which the docs illustrate with abort(401). Pick one and stay consistent. If the signature was generated without a domain, for instance behind a proxy that rewrites the host, the middleware becomes signed:relative.
What the Signature Covers, and What Slips Through
Everything in the query string is part of the hash, so swapping the document id breaks the link. hasValidSignatureWhileIgnoring(['page']) exists to leave presentation parameters out, but the documentation is blunt about it: ignoring a parameter lets anyone change it. Ignore only what cannot change the response — page number, locale, sort order — and never the file id or its name.
temporaryUrl(): One Line and the Disk Does the Rest
Storage::temporaryUrl() returns a time-limited URL on both s3 and local. On local you must enable it per disk, because nothing is served by default:
'local' => [
'driver' => 'local',
'root' => storage_path('app/private'),
'serve' => true,
'throw' => false,
],With that in place Laravel registers a storage.{disk} route that validates the signature and streams the file through ServeFile. On S3 the file leaves the bucket as a presigned URL without touching your PHP, and you can request response headers:
$url = Storage::temporaryUrl('reports/2026.pdf', now()->plus(minutes: 5), [
'ResponseContentDisposition' => 'attachment; filename="2026.pdf"',
]);Two limits worth knowing before you promise anything: local support landed in Laravel 11.23, so older projects may not have serve in their local block and the call fails; and on S3 a presigned URL lasts at most seven days with IAM credentials, while role credentials die with the role session no matter how long you ask for.
You Pick the Path, Not the Visitor
The robust pattern does not sign a path that arrives from outside: it signs an id. The URL /downloads/{document} resolves the file in the controller from a database column, and only then serves it:
public function download(Document $document)
{
return Storage::disk('local')
->download($document->path, $document->original_name)
->withHeaders([
'Cache-Control' => 'private, no-store',
'X-Content-Type-Options' => 'nosniff',
]);
}Never build the path from a request parameter. A route with where('path', '.*') plus ../ in the value is the classic hole, and a signature does not close it: the attacker can sign their own path whenever your application signs it for them. If you truly must accept a path, normalise it with realpath() and confirm the result still sits inside the disk root.
The Original Filename Is Data, Not a Path
Store the file under a name you control — a uuid or a hash — and keep the user's filename in its own column. On download that name travels in Content-Disposition, with quotes and line breaks stripped so it cannot break the header. Add X-Content-Type-Options: nosniff so the browser does not reinterpret the type, and validate the real MIME on upload: the extension is the uploader's claim, the type is the content's fact.
Serving Files Without Burning PHP Workers: X-Accel-Redirect
With large files, every download holds a PHP-FPM worker for the whole transfer. Five simultaneous 200 MB downloads and the site stops answering for lack of workers. On nginx you fix it with an internal location pointing at the real directory plus one response header:
location /protected/ {
internal;
alias /var/www/app/current/storage/app/private/;
}The controller validates the signature as usual and returns X-Accel-Redirect with the internal path and no body: nginx handles the read, the ranges and the transfer, and the PHP worker is free immediately. Two details get forgotten: the path must be encoded, and Cache-Control: private, no-store belongs on this response too, so neither the CDN nor the browser keeps the file. On Apache the equivalent is X-Sendfile with mod_xsendfile; on a PaaS without your own nginx it does not apply and ServeFile is what you get.
Checklist and Test
Four checks before you call it done, all from curl or an incognito window:
- The URL without a signature returns 403.
- The URL with a tampered signature, changing the id, returns 403.
- After the expiry, it returns 403.
- The file is not reachable through
/storage/...or under its original name.
curl -s -o /dev/null -w '%{http_code}\n' https://app.example.com/downloads/42One last point that gets skipped: the signature does not mark usage. The link works until it expires, and if a document needs to be single-use that is a product decision — a table with the token, its expiry and a delete-on-serve. Laravel does not do that for you.
If you are wiring this on a fresh server, deploying Laravel 13 on a VPS covers the nginx and PHP-FPM side, and the production security checklist has the rest of the items people forget. Today's news story is the same mistake from the other side: a filename the attacker controls that ends up writing where it should not.

