Skip to content

Commit

Permalink
Splitted JWT must be checked runtime for string emptiness
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed Oct 12, 2022
1 parent 9ee686c commit 938ed73
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ vendor/composer/installed.json: composer.json composer.lock

.PHONY: phpunit
phpunit:
@php -d assert.exception=1 -d zend.assertions=1 vendor/bin/phpunit
php -d assert.exception=1 -d zend.assertions=1 vendor/bin/phpunit $(PHPUNIT_FLAGS)

.PHONY: infection
infection:
Expand Down
10 changes: 10 additions & 0 deletions src/Token/InvalidTokenStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ public static function missingOrNotEnoughSeparators(): self
return new self('The JWT string must have two dots');
}

public static function missingHeaderPart(): self
{
return new self('The JWT string is missing the Header part');
}

public static function missingClaimsPart(): self
{
return new self('The JWT string is missing the Claim part');
}

public static function arrayExpected(string $part): self
{
return new self($part . ' must be an array');
Expand Down
11 changes: 8 additions & 3 deletions src/Token/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Lcobucci\JWT\Token as TokenInterface;

use function array_key_exists;
use function assert;
use function count;
use function explode;
use function is_array;
Expand All @@ -27,8 +26,14 @@ public function __construct(private readonly Decoder $decoder)
public function parse(string $jwt): TokenInterface
{
[$encodedHeaders, $encodedClaims, $encodedSignature] = $this->splitJwt($jwt);
assert($encodedHeaders !== '');
assert($encodedClaims !== '');

if ($encodedHeaders === '') {
throw InvalidTokenStructure::missingHeaderPart();
}

if ($encodedClaims === '') {
throw InvalidTokenStructure::missingClaimsPart();
}

$header = $this->parseHeader($encodedHeaders);

Expand Down
36 changes: 36 additions & 0 deletions test/unit/Token/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,42 @@ public function parseMustRaiseExceptionWhenTokenDoesNotHaveThreeParts(): void
$parser->parse('.');
}

/**
* @test
*
* @covers ::__construct
* @covers ::parse
* @covers ::splitJwt
* @covers \Lcobucci\JWT\Token\InvalidTokenStructure
*/
public function parseMustRaiseExceptionWhenTokenDoesNotHaveHeaders(): void
{
$parser = $this->createParser();

$this->expectException(InvalidTokenStructure::class);
$this->expectExceptionMessage('The JWT string is missing the Header part');

$parser->parse('.b.c');
}

/**
* @test
*
* @covers ::__construct
* @covers ::parse
* @covers ::splitJwt
* @covers \Lcobucci\JWT\Token\InvalidTokenStructure
*/
public function parseMustRaiseExceptionWhenTokenDoesNotHaveClaims(): void
{
$parser = $this->createParser();

$this->expectException(InvalidTokenStructure::class);
$this->expectExceptionMessage('The JWT string is missing the Claim part');

$parser->parse('a..c');
}

/**
* @test
*
Expand Down

0 comments on commit 938ed73

Please sign in to comment.