Skip to content

Commit a19ebf7

Browse files
authored
Ensure JWS serializers only throw InvalidArgumentException (#513)
1 parent bc76770 commit a19ebf7

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/Library/Core/Util/JsonConverter.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Jose\Component\Core\Util;
66

7-
use RuntimeException;
7+
use InvalidArgumentException;
88
use Throwable;
99
use const JSON_THROW_ON_ERROR;
1010
use const JSON_UNESCAPED_SLASHES;
@@ -17,12 +17,21 @@ public static function encode(mixed $payload): string
1717
try {
1818
return json_encode($payload, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
1919
} catch (Throwable $throwable) {
20-
throw new RuntimeException('Invalid content.', $throwable->getCode(), $throwable);
20+
throw new InvalidArgumentException('Invalid content.', $throwable->getCode(), $throwable);
2121
}
2222
}
2323

2424
public static function decode(string $payload): mixed
2525
{
26-
return json_decode($payload, true, 512, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
26+
try {
27+
return json_decode(
28+
$payload,
29+
true,
30+
512,
31+
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
32+
);
33+
} catch (Throwable $throwable) {
34+
throw new InvalidArgumentException('Unsupported input.', $throwable->getCode(), $throwable);
35+
}
2736
}
2837
}

tests/Component/KeyManagement/UrlKeySetFactoryTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace Jose\Tests\Component\KeyManagement;
66

77
use Http\Mock\Client;
8+
use InvalidArgumentException;
89
use Jose\Component\KeyManagement\JKUFactory;
910
use Jose\Component\KeyManagement\X5UFactory;
10-
use JsonException;
1111
use Nyholm\Psr7\Factory\Psr17Factory;
1212
use PHPUnit\Framework\Attributes\Test;
1313
use PHPUnit\Framework\TestCase;
@@ -52,8 +52,8 @@ public function iCanGetAKeySetFromAJWKUrl(): void
5252
#[Test]
5353
public function theJWKUrlIsValidButDoesNotContainAKeySet(): void
5454
{
55-
$this->expectException(JsonException::class);
56-
$this->expectExceptionMessage('Syntax error');
55+
$this->expectException(InvalidArgumentException::class);
56+
$this->expectExceptionMessage('Unsupported input.');
5757

5858
$response = $this->messageFactory->createResponse(200);
5959
$response->getBody()
@@ -104,8 +104,8 @@ public function iCanGetAKeySetFromAX509Url(): void
104104
#[Test]
105105
public function theX509UrlIsValidButDoesNotContainAKeySet(): void
106106
{
107-
$this->expectException(JsonException::class);
108-
$this->expectExceptionMessage('Syntax error');
107+
$this->expectException(InvalidArgumentException::class);
108+
$this->expectExceptionMessage('Unsupported input.');
109109

110110
$response = $this->messageFactory->createResponse(200);
111111
$response->getBody()

0 commit comments

Comments
 (0)