Skip to content

Commit

Permalink
Throw an exception when signing route if a parameter key is 'expires'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien Nikolaou committed Jul 22, 2021
1 parent f625e62 commit cd49e7e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/Illuminate/Routing/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ public function signedRoute($name, $parameters = [], $expiration = null, $absolu
);
}

if (array_key_exists('expires', $parameters)) {
throw new InvalidArgumentException(
'"Expires" is a reserved parameter when generating signed routes. Please rename your route parameter.'
);
}

if ($expiration) {
$parameters = $parameters + ['expires' => $this->availableAt($expiration)];
}
Expand Down
9 changes: 5 additions & 4 deletions tests/Integration/Routing/UrlSigningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
use InvalidArgumentException;
use Orchestra\Testbench\TestCase;

/**
Expand Down Expand Up @@ -41,14 +42,14 @@ public function testTemporarySignedUrls()

public function testTemporarySignedUrlsWithExpiresParameter()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('reserved');

Route::get('/foo/{id}', function (Request $request, $id) {
return $request->hasValidSignature() ? 'valid' : 'invalid';
})->name('foo');

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

public function testSignedUrlWithUrlWithoutSignatureParameter()
Expand Down
21 changes: 21 additions & 0 deletions tests/Routing/RoutingUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,27 @@ public function testSignedUrlParameterCannotBeNamedSignature()

Request::create($url->signedRoute('foo', ['signature' => 'bar']));
}

public function testSignedUrlParameterCannotBeNamedExpires()
{
$url = new UrlGenerator(
$routes = new RouteCollection,
$request = Request::create('http://www.foo.com/')
);
$url->setKeyResolver(function () {
return 'secret';
});

$route = new Route(['GET'], 'foo/{expires}', ['as' => 'foo', function () {
//
}]);
$routes->add($route);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('reserved');

Request::create($url->signedRoute('foo', ['expires' => 253402300799]));
}
}

class RoutableInterfaceStub implements UrlRoutable
Expand Down

0 comments on commit cd49e7e

Please sign in to comment.