Skip to content

Commit

Permalink
fix: should return null if received an invalid encrypted cookie token.
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-lee-lb committed Mar 27, 2021
1 parent ab00f2d commit 898b6d7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Http/Parser/Cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Contracts\Encryption\DecryptException;
use Tymon\JWTAuth\Contracts\Http\Parser as ParserContract;

class Cookies implements ParserContract
Expand Down Expand Up @@ -41,7 +42,11 @@ public function __construct($decrypt = true)
public function parse(Request $request)
{
if ($this->decrypt && $request->hasCookie($this->key)) {
return Crypt::decrypt($request->cookie($this->key));
try {
return Crypt::decrypt($request->cookie($this->key));
} catch (DecryptException $ex) {
return null;
}
}

return $request->cookie($this->key);
Expand Down
23 changes: 23 additions & 0 deletions tests/Http/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Contracts\Encryption\DecryptException;
use Mockery;
use Tymon\JWTAuth\Contracts\Http\Parser as ParserContract;
use Tymon\JWTAuth\Http\Parser\AuthHeaders;
Expand Down Expand Up @@ -314,6 +315,28 @@ public function it_should_return_the_token_from_a_crypted_cookie()
$this->assertTrue($parser->hasToken());
}

/** @test */
public function it_should_has_no_token_from_a_invalid_encrypted_cookie()
{
$request = Request::create('foo', 'POST', [], ['token' => 'foobar']);

$parser = new Parser($request);
$parser->setChain([
new AuthHeaders,
new QueryString,
new InputSource,
new RouteParams,
new Cookies(true),
]);

Crypt::shouldReceive('decrypt')
->with('foobar')
->andThrow(new DecryptException());

$this->assertSame($parser->parseToken(), null);
$this->assertTrue(!$parser->hasToken());
}

/** @test */
public function it_should_return_the_token_from_route()
{
Expand Down

0 comments on commit 898b6d7

Please sign in to comment.