Skip to content

Commit cd49e7e

Browse files
author
Sébastien Nikolaou
committed
Throw an exception when signing route if a parameter key is 'expires'
1 parent f625e62 commit cd49e7e

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/Illuminate/Routing/UrlGenerator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,12 @@ public function signedRoute($name, $parameters = [], $expiration = null, $absolu
328328
);
329329
}
330330

331+
if (array_key_exists('expires', $parameters)) {
332+
throw new InvalidArgumentException(
333+
'"Expires" is a reserved parameter when generating signed routes. Please rename your route parameter.'
334+
);
335+
}
336+
331337
if ($expiration) {
332338
$parameters = $parameters + ['expires' => $this->availableAt($expiration)];
333339
}

tests/Integration/Routing/UrlSigningTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Carbon;
99
use Illuminate\Support\Facades\Route;
1010
use Illuminate\Support\Facades\URL;
11+
use InvalidArgumentException;
1112
use Orchestra\Testbench\TestCase;
1213

1314
/**
@@ -41,14 +42,14 @@ public function testTemporarySignedUrls()
4142

4243
public function testTemporarySignedUrlsWithExpiresParameter()
4344
{
45+
$this->expectException(InvalidArgumentException::class);
46+
$this->expectExceptionMessage('reserved');
47+
4448
Route::get('/foo/{id}', function (Request $request, $id) {
4549
return $request->hasValidSignature() ? 'valid' : 'invalid';
4650
})->name('foo');
4751

48-
Carbon::setTestNow(Carbon::create(2018, 1, 1));
49-
$this->assertIsString($url = URL::temporarySignedRoute('foo', now()->addMinutes(5), ['id' => 1, 'expires' => 253402300799]));
50-
Carbon::setTestNow(Carbon::create(2018, 1, 1)->addMinutes(10));
51-
$this->assertSame('invalid', $this->get($url)->original);
52+
URL::temporarySignedRoute('foo', now()->addMinutes(5), ['id' => 1, 'expires' => 253402300799]);
5253
}
5354

5455
public function testSignedUrlWithUrlWithoutSignatureParameter()

tests/Routing/RoutingUrlGeneratorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,27 @@ public function testSignedUrlParameterCannotBeNamedSignature()
664664

665665
Request::create($url->signedRoute('foo', ['signature' => 'bar']));
666666
}
667+
668+
public function testSignedUrlParameterCannotBeNamedExpires()
669+
{
670+
$url = new UrlGenerator(
671+
$routes = new RouteCollection,
672+
$request = Request::create('http://www.foo.com/')
673+
);
674+
$url->setKeyResolver(function () {
675+
return 'secret';
676+
});
677+
678+
$route = new Route(['GET'], 'foo/{expires}', ['as' => 'foo', function () {
679+
//
680+
}]);
681+
$routes->add($route);
682+
683+
$this->expectException(InvalidArgumentException::class);
684+
$this->expectExceptionMessage('reserved');
685+
686+
Request::create($url->signedRoute('foo', ['expires' => 253402300799]));
687+
}
667688
}
668689

669690
class RoutableInterfaceStub implements UrlRoutable

0 commit comments

Comments
 (0)