spatie/url-signer
Generate and verify signed URLs with expiration timestamps using a shared secret. spatie/url-signer appends expires and signature parameters, letting you safely share time-limited links (e.g., in emails) and validate them server-side with a simple API.
UrlSigner facade) and integrates seamlessly with routes, middleware, and caching (e.g., storing signed URLs in Redis for reuse). Works well with Laravel’s request validation pipeline.signed route model binding: Limited to routes, not arbitrary URLs.// app/Http/Middleware/ValidateSignedUrl.php
public function handle(Request $request, Closure $next) {
if ($request->has('signature')) {
$urlSigner = app(UrlSigner::class);
if (!$urlSigner->validate($request->fullUrl())) {
abort(403);
}
}
return $next($request);
}
UrlSigner easily in PHPUnit; test edge cases (e.g., expired URLs, tampered signatures).| Risk Area | Mitigation Strategy |
|---|---|
| Key Management | Store signing keys in Laravel’s .env (e.g., URL_SIGNER_KEY). Rotate keys via migrations. |
| Performance | SHA-256 signing is CPU-light; benchmark under load if generating URLs at scale. |
| Clock Skew | Use now()->addSeconds($ttl) with UTC timestamps to avoid timezone issues. |
| URL Length Limits | Test with long URLs (e.g., S3 presigned URLs); may hit browser/email client limits. |
| Dependency Bloat | Package is ~1MB (minimal); no external services required. |
UrlSigner via app(UrlSigner::class) or bind it in AppServiceProvider.VerifyCsrfToken or custom middleware.cache()->remember()) if reused.new Sha256UrlSigner(config('app.url_signer_key'))).Mail or Notifiable), frontend redirects, or API responses.UrlSigner::sign() in Tinker).Route::get('/download/{id}', function ($id) {
$url = UrlSigner::sign(route('download', $id), 3600); // 1-hour TTL
return redirect($url);
})->middleware('throttle:100'); // Rate-limit URL generation
$url = cache()->remember("signed_url:{$userId}", now()->addHours(1), function () {
return UrlSigner::sign($downloadUrl, 3600);
});
Log::warning('Invalid signed URL')).UrlSigner to add custom claims (e.g., user ID, IP restrictions).validate() to add rate limiting or IP checks.urlencode() internally; test with special chars (e.g., ? in query strings).composer require spatie/url-signer.php artisan vendor:publish --tag="url-signer-config"..env: URL_SIGNER_KEY=your_32_byte_base64_key.UrlSigner (mock time/keys).url_signer_key_v2 to .env).config() caching to reload keys without restarting.now()->addMinutes(5)).$urlSigner->validate($url, true); // Returns array with error details
How can I help you explore Laravel packages today?