Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Library/Core/Exception/UnrecognizedSerializationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Jose\Component\Core\Exception;

/**
* Thrown by a serializer when the input is not in its serialization at all: a compact serialization of another number
* of parts, or a JSON document that is not one of its JSON serializations. A serializer manager then tells the
* serializer the input belongs to from the others, and chains the exception of that serializer.
*/
class UnrecognizedSerializationException extends InvalidSerializationException
{
}
24 changes: 24 additions & 0 deletions src/Library/Core/Util/BoundedJsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Jose\Component\Core\Exception\InvalidArgumentException;
use Jose\Component\Core\Exception\JsonBoundExceededException;
use Jose\Component\Core\Exception\UnrecognizedSerializationException;
use SensitiveParameter;
use function preg_replace;
use function sprintf;
Expand Down Expand Up @@ -61,6 +62,29 @@ public function decode(#[SensitiveParameter] string $json): mixed
}
}

/**
* Decodes the JSON serialization of a token. A document that is not valid JSON is not such a serialization: it is
* rejected with an UnrecognizedSerializationException that keeps the message, the code and the previous exception
* of the error. A document beyond a bound is rejected as decode() rejects it.
*
* @throws JsonBoundExceededException if the document goes beyond a bound
* @throws UnrecognizedSerializationException if the document is not valid JSON
*/
public function decodeSerialization(#[SensitiveParameter] string $json): mixed
{
try {
return $this->decode($json);
} catch (JsonBoundExceededException $exception) {
throw $exception;
} catch (InvalidArgumentException $exception) {
throw new UnrecognizedSerializationException(
$exception->getMessage(),
$exception->getCode(),
$exception->getPrevious()
);
}
}

/**
* Whether a document given to this decoder was rejected for its depth.
*/
Expand Down
7 changes: 6 additions & 1 deletion src/Library/Encryption/Serializer/CompactSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Jose\Component\Core\Exception\InvalidSerializationException;
use Jose\Component\Core\Exception\LogicException;
use Jose\Component\Core\Exception\UnrecognizedSerializationException;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\BoundedJsonDecoder;
use Jose\Component\Encryption\JWE;
Expand All @@ -14,6 +15,7 @@
use Throwable;
use function count;
use function is_array;
use function preg_match;
use function sprintf;

final readonly class CompactSerializer implements JWESerializer
Expand Down Expand Up @@ -66,7 +68,7 @@ public function unserialize(string $input): JWE
{
$parts = explode('.', $input, 6);
if (count($parts) !== 5) {
throw new InvalidSerializationException('Unsupported input');
throw new UnrecognizedSerializationException('Unsupported input');
}

$decoder = new BoundedJsonDecoder();
Expand Down Expand Up @@ -94,6 +96,9 @@ public function unserialize(string $input): JWE
[new Recipient([], $encryptedKey)]
);
} catch (Throwable $throwable) {
if (preg_match('/^[A-Za-z0-9_-]*\z/', $parts[0]) !== 1) {
throw new UnrecognizedSerializationException('Unsupported input', $throwable->getCode(), $throwable);
}
throw new InvalidSerializationException(
$decoder->hasExceededMaxValues() || $decoder->hasExceededMaxDepth()
? $throwable->getMessage()
Expand Down
8 changes: 4 additions & 4 deletions src/Library/Encryption/Serializer/JSONFlattenedSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Jose\Component\Encryption\Serializer;

use Jose\Component\Core\Exception\InvalidSerializationException;
use Jose\Component\Core\Exception\UnrecognizedSerializationException;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\BoundedJsonDecoder;
use Jose\Component\Core\Util\JsonConverter;
Expand Down Expand Up @@ -64,9 +64,9 @@ public function serialize(JWE $jwe, ?int $recipientIndex = null): string
public function unserialize(string $input): JWE
{
$json = new BoundedJsonDecoder();
$data = $json->decode($input);
$data = $json->decodeSerialization($input);
if (! is_array($data)) {
throw new InvalidSerializationException('Unsupported input.');
throw new UnrecognizedSerializationException('Unsupported input.');
}
$this->checkData($data);

Expand Down Expand Up @@ -95,7 +95,7 @@ public function unserialize(string $input): JWE
private function checkData(?array $data): void
{
if ($data === null || ! isset($data['ciphertext']) || isset($data['recipients'])) {
throw new InvalidSerializationException('Unsupported input.');
throw new UnrecognizedSerializationException('Unsupported input.');
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Library/Encryption/Serializer/JSONGeneralSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Jose\Component\Encryption\Serializer;

use Jose\Component\Core\Exception\InvalidSerializationException;
use Jose\Component\Core\Exception\LogicException;
use Jose\Component\Core\Exception\UnrecognizedSerializationException;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\BoundedJsonDecoder;
use Jose\Component\Core\Util\JsonConverter;
Expand Down Expand Up @@ -72,9 +72,9 @@ public function serialize(JWE $jwe, ?int $recipientIndex = null): string
public function unserialize(string $input): JWE
{
$json = new BoundedJsonDecoder();
$data = $json->decode($input);
$data = $json->decodeSerialization($input);
if (! is_array($data)) {
throw new InvalidSerializationException('Unsupported input.');
throw new UnrecognizedSerializationException('Unsupported input.');
}
$this->checkData($data);

Expand Down Expand Up @@ -104,7 +104,7 @@ public function unserialize(string $input): JWE
private function checkData(?array $data): void
{
if ($data === null || ! isset($data['ciphertext']) || ! isset($data['recipients'])) {
throw new InvalidSerializationException('Unsupported input.');
throw new UnrecognizedSerializationException('Unsupported input.');
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/Library/Encryption/Serializer/JWESerializerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InvalidArgumentException;
use Jose\Component\Core\Exception\InvalidSerializationException;
use Jose\Component\Core\Exception\JsonBoundExceededException;
use Jose\Component\Core\Exception\UnrecognizedSerializationException;
use Jose\Component\Core\Exception\UnsupportedSerializerException;
use Jose\Component\Encryption\JWE;
use Throwable;
Expand Down Expand Up @@ -96,29 +97,34 @@ public function unserialize(string $input, ?string &$name = null): JWE
*
* When no serializer is able to convert the input, the exception of one of them is chained as the previous
* exception, so that the actual reason of the failure remains available to the caller. Only the serializer of the
* format of the input decodes it far enough to reach a bound of the JSON decoder: the last exception that goes back
* to such a bound is chained, and the exception of the last serializer otherwise.
* format of the input decodes it far enough to reach a bound of the JSON decoder, and a serializer rejects an input
* of another serialization with an UnrecognizedSerializationException. The manager chains the last exception that
* goes back to a bound, else the last exception of a serializer that recognized the input, else the exception of
* the last serializer.
*
* @param string $input A string that represents a JWE
*/
public function unserializeToken(string $input): UnserializationResult
{
$lastError = null;
$boundError = null;
$recognizedError = null;
foreach ($this->serializers as $serializer) {
try {
return new UnserializationResult($serializer->unserialize($input), $serializer->name());
} catch (InvalidArgumentException $invalidArgumentException) {
$lastError = $invalidArgumentException;
if (self::goesBackToAJsonBound($invalidArgumentException)) {
$boundError = $invalidArgumentException;
} elseif (! $invalidArgumentException instanceof UnrecognizedSerializationException) {
$recognizedError = $invalidArgumentException;
}

continue;
}
}

throw new InvalidSerializationException('Unsupported input.', 0, $boundError ?? $lastError);
throw new InvalidSerializationException('Unsupported input.', 0, $boundError ?? $recognizedError ?? $lastError);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Library/Signature/Serializer/CompactSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Jose\Component\Core\Exception\InvalidSerializationException;
use Jose\Component\Core\Exception\LogicException;
use Jose\Component\Core\Exception\UnrecognizedSerializationException;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\BoundedJsonDecoder;
use Jose\Component\Signature\JWS;
Expand All @@ -14,6 +15,7 @@
use Throwable;
use function count;
use function is_array;
use function preg_match;
use function sprintf;

final readonly class CompactSerializer extends Serializer
Expand Down Expand Up @@ -71,7 +73,7 @@ public function unserialize(#[SensitiveParameter] string $input): JWS
{
$parts = explode('.', $input, 4);
if (count($parts) !== 3) {
throw new InvalidSerializationException('Unsupported input');
throw new UnrecognizedSerializationException('Unsupported input');
}

$decoder = new BoundedJsonDecoder();
Expand All @@ -97,6 +99,9 @@ public function unserialize(#[SensitiveParameter] string $input): JWS

return $jws->addSignature($signature, $protectedHeader, $encodedProtectedHeader);
} catch (Throwable $throwable) {
if (preg_match('/^[A-Za-z0-9_-]*\z/', $parts[0]) !== 1) {
throw new UnrecognizedSerializationException('Unsupported input', $throwable->getCode(), $throwable);
}
throw new InvalidSerializationException(
$decoder->hasExceededMaxValues() || $decoder->hasExceededMaxDepth()
? $throwable->getMessage()
Expand Down
10 changes: 4 additions & 6 deletions src/Library/Signature/Serializer/JSONFlattenedSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Jose\Component\Signature\Serializer;

use Jose\Component\Core\Exception\InvalidSerializationException;
use Jose\Component\Core\Exception\UnrecognizedSerializationException;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\BoundedJsonDecoder;
use Jose\Component\Core\Util\JsonConverter;
Expand Down Expand Up @@ -58,12 +59,9 @@ public function serialize(JWS $jws, ?int $signatureIndex = null): string
public function unserialize(#[SensitiveParameter] string $input): JWS
{
$json = new BoundedJsonDecoder();
$data = $json->decode($input);
if (! is_array($data)) {
throw new InvalidSerializationException('Unsupported input.');
}
if (! isset($data['signature'])) {
throw new InvalidSerializationException('Unsupported input.');
$data = $json->decodeSerialization($input);
if (! is_array($data) || ! isset($data['signature'])) {
throw new UnrecognizedSerializationException('Unsupported input.');
}
$signature = Base64UrlSafe::decodeNoPadding($data['signature']);

Expand Down
10 changes: 4 additions & 6 deletions src/Library/Signature/Serializer/JSONGeneralSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Jose\Component\Core\Exception\InvalidPayloadException;
use Jose\Component\Core\Exception\InvalidSerializationException;
use Jose\Component\Core\Exception\LogicException;
use Jose\Component\Core\Exception\UnrecognizedSerializationException;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\BoundedJsonDecoder;
use Jose\Component\Core\Util\JsonConverter;
Expand Down Expand Up @@ -75,12 +76,9 @@ public function serialize(JWS $jws, ?int $signatureIndex = null): string
public function unserialize(#[SensitiveParameter] string $input): JWS
{
$json = new BoundedJsonDecoder();
$data = $json->decode($input);
if (! is_array($data)) {
throw new InvalidSerializationException('Unsupported input.');
}
if (! isset($data['signatures'])) {
throw new InvalidSerializationException('Unsupported input.');
$data = $json->decodeSerialization($input);
if (! is_array($data) || ! isset($data['signatures'])) {
throw new UnrecognizedSerializationException('Unsupported input.');
}

$isPayloadEncoded = null;
Expand Down
12 changes: 9 additions & 3 deletions src/Library/Signature/Serializer/JWSSerializerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InvalidArgumentException;
use Jose\Component\Core\Exception\InvalidSerializationException;
use Jose\Component\Core\Exception\JsonBoundExceededException;
use Jose\Component\Core\Exception\UnrecognizedSerializationException;
use Jose\Component\Core\Exception\UnsupportedSerializerException;
use Jose\Component\Signature\JWS;
use SensitiveParameter;
Expand Down Expand Up @@ -95,29 +96,34 @@ public function unserialize(#[SensitiveParameter] string $input, ?string &$name
*
* When no serializer is able to convert the input, the exception of one of them is chained as the previous
* exception, so that the actual reason of the failure remains available to the caller. Only the serializer of the
* format of the input decodes it far enough to reach a bound of the JSON decoder: the last exception that goes back
* to such a bound is chained, and the exception of the last serializer otherwise.
* format of the input decodes it far enough to reach a bound of the JSON decoder, and a serializer rejects an input
* of another serialization with an UnrecognizedSerializationException. The manager chains the last exception that
* goes back to a bound, else the last exception of a serializer that recognized the input, else the exception of
* the last serializer.
*
* @param string $input A string that represents a JWS
*/
public function unserializeToken(#[SensitiveParameter] string $input): UnserializationResult
{
$lastError = null;
$boundError = null;
$recognizedError = null;
foreach ($this->serializers as $serializer) {
try {
return new UnserializationResult($serializer->unserialize($input), $serializer->name());
} catch (InvalidArgumentException $invalidArgumentException) {
$lastError = $invalidArgumentException;
if (self::goesBackToAJsonBound($invalidArgumentException)) {
$boundError = $invalidArgumentException;
} elseif (! $invalidArgumentException instanceof UnrecognizedSerializationException) {
$recognizedError = $invalidArgumentException;
}

continue;
}
}

throw new InvalidSerializationException('Unsupported input.', 0, $boundError ?? $lastError);
throw new InvalidSerializationException('Unsupported input.', 0, $boundError ?? $recognizedError ?? $lastError);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Component/Core/JoseExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Jose\Component\Core\Exception\MissingPayloadRuntimeException;
use Jose\Component\Core\Exception\RangeException as JoseRangeException;
use Jose\Component\Core\Exception\RuntimeException as JoseRuntimeException;
use Jose\Component\Core\Exception\UnrecognizedSerializationException;
use Jose\Component\Core\Exception\UnsupportedAlgorithmException;
use Jose\Component\Core\Exception\UnsupportedCurveException;
use Jose\Component\Core\Exception\UnsupportedSerializerException;
Expand Down Expand Up @@ -74,6 +75,7 @@ final class JoseExceptionTest extends TestCase
MissingPayloadRuntimeException::class => RuntimeException::class,
JoseRangeException::class => RangeException::class,
JoseRuntimeException::class => RuntimeException::class,
UnrecognizedSerializationException::class => InvalidArgumentException::class,
UnsupportedAlgorithmException::class => InvalidArgumentException::class,
UnsupportedCurveException::class => InvalidArgumentException::class,
UnsupportedSerializerException::class => InvalidArgumentException::class,
Expand Down
Loading
Loading