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
12 changes: 9 additions & 3 deletions .ci-tools/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,12 @@ parameters:
count: 1
path: ../src/Bundle/DependencyInjection/Source/KeyManagement/JKUSource.php

-
rawMessage: Cannot access offset 'allow_large_key_sets' on mixed.
identifier: offsetAccess.nonOffsetAccessible
count: 1
path: ../src/Bundle/DependencyInjection/Source/KeyManagement/JKUSource.php

-
rawMessage: Cannot access offset 'client' on mixed.
identifier: offsetAccess.nonOffsetAccessible
Expand Down Expand Up @@ -1461,7 +1467,7 @@ parameters:
-
rawMessage: 'Cannot call method booleanNode() on mixed.'
identifier: method.nonObject
count: 1
count: 2
path: ../src/Bundle/DependencyInjection/Source/KeyManagement/JKUSource.php

-
Expand All @@ -1479,13 +1485,13 @@ parameters:
-
rawMessage: 'Cannot call method end() on mixed.'
identifier: method.nonObject
count: 5
count: 6
path: ../src/Bundle/DependencyInjection/Source/KeyManagement/JKUSource.php

-
rawMessage: 'Cannot call method info() on mixed.'
identifier: method.nonObject
count: 2
count: 3
path: ../src/Bundle/DependencyInjection/Source/KeyManagement/JKUSource.php

-
Expand Down
16 changes: 13 additions & 3 deletions src/Bundle/DependencyInjection/Source/KeyManagement/JKUSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('jku_source.php');
$loader->load('jku_commands.php');
$container->setAlias('jose.http_client', $configs[$this->name()]['client']);
if (isset($configs[$this->name()]['follow_redirects'])) {
$followRedirects = $configs[$this->name()]['follow_redirects'] ?? null;
$allowLargeKeySets = $configs[$this->name()]['allow_large_key_sets'] ?? null;
if ($followRedirects !== null || $allowLargeKeySets !== null) {
foreach ([JKUFactory::class, X5UFactory::class] as $factory) {
$container->getDefinition($factory)
->addArgument($configs[$this->name()]['follow_redirects']);
$definition = $container->getDefinition($factory)
->addArgument($followRedirects);
if ($allowLargeKeySets !== null) {
$definition->addArgument($allowLargeKeySets);
}
}
}
}
Expand All @@ -49,6 +54,11 @@ public function getNodeDefinition(NodeDefinition $node): void
->info('HTTP Client used to retrieve key sets.')
->isRequired()
->end()
->booleanNode('allow_large_key_sets')
->info(
'True loads the key sets of more than 4096 JSON values, false refuses them. When it is not set, they are loaded with a deprecation, as 5.0.0 refuses them by default.'
)
->end()
->booleanNode('follow_redirects')
->info(
'False refuses the redirects of the key set URLs, true follows them. When it is not set, they are followed with a deprecation, as 5.0.0 refuses them by default.'
Expand Down
4 changes: 2 additions & 2 deletions src/Bundle/EnvVarProcessor/KeyEnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function getEnv(string $prefix, string $name, Closure $getEnv): mixed
$env = $getEnv($name);

return match ($prefix) {
'jwk' => JWK::createFromJson($env),
'jwkset' => JWKSet::createFromJson($env),
'jwk' => JWK::createFromJson($env, true),
'jwkset' => JWKSet::createFromJson($env, true),
default => throw new RuntimeException(sprintf('Unsupported prefix "%s".', $prefix)),
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/Library/Console/AddKeyIntoKeysetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private function getKeyset(InputInterface $input): JWKSet
if (! is_string($jwkset)) {
throw new InvalidKeySetException('The argument must be a valid JWKSet.');
}
$json = JsonConverter::decode($jwkset);
$json = JsonConverter::decode($jwkset, true);
if (! is_array($json)) {
throw new InvalidKeySetException('The argument must be a valid JWKSet.');
}
Expand All @@ -61,7 +61,7 @@ private function getKey(InputInterface $input): JWK
if (! is_string($jwk)) {
throw new InvalidKeyException('The argument must be a valid JWK.');
}
$json = JsonConverter::decode($jwk);
$json = JsonConverter::decode($jwk, true);
if (! is_array($json)) {
throw new InvalidKeyException('The argument must be a valid JWK.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Console/GetThumbprintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (! is_string($hash)) {
throw new InvalidArgumentException('Invalid hash algorithm');
}
$json = JsonConverter::decode($jwk);
$json = JsonConverter::decode($jwk, true);
if (! is_array($json)) {
throw new InvalidArgumentException('Invalid input.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Console/KeyAnalyzerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private function getKey(InputInterface $input): JWK
if (! is_string($jwk)) {
throw new InvalidKeyException('Invalid JWK');
}
$json = JsonConverter::decode($jwk);
$json = JsonConverter::decode($jwk, true);
if (! is_array($json)) {
throw new InvalidKeyException('Invalid JWK.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Console/KeysetAnalyzerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function getKeyset(InputInterface $input): JWKSet
if (! is_string($jwkset)) {
throw new InvalidKeySetException('Invalid JWKSet');
}
$json = JsonConverter::decode($jwkset);
$json = JsonConverter::decode($jwkset, true);
if (! is_array($json)) {
throw new InvalidKeySetException('Invalid JWKSet');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Console/MergeKeysetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$keySets = $input->getArgument('jwksets');
$newJwkset = new JWKSet([]);
foreach ($keySets as $keySet) {
$json = JsonConverter::decode($keySet);
$json = JsonConverter::decode($keySet, true);
if (! is_array($json)) {
throw new InvalidKeySetException('The argument must be a valid JWKSet.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Console/OptimizeRsaKeyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (! is_string($jwk)) {
throw new InvalidKeyException('Invalid JWK');
}
$json = JsonConverter::decode($jwk);
$json = JsonConverter::decode($jwk, true);
if (! is_array($json)) {
throw new InvalidKeyException('Invalid JWK');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Console/PemConverterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (! is_string($jwk)) {
throw new InvalidKeyException('Invalid JWK');
}
$json = JsonConverter::decode($jwk);
$json = JsonConverter::decode($jwk, true);
if (! is_array($json)) {
throw new InvalidKeyException('Invalid JWK.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Console/Pkcs8ConverterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (! is_string($jwk)) {
throw new InvalidKeyException('Invalid JWK');
}
$json = JsonConverter::decode($jwk);
$json = JsonConverter::decode($jwk, true);
if (! is_array($json)) {
throw new InvalidKeyException('Invalid JWK.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Console/PublicKeyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private function getKey(InputInterface $input): JWK
if (! is_string($jwk)) {
throw new InvalidKeyException('Invalid JWK');
}
$json = JsonConverter::decode($jwk);
$json = JsonConverter::decode($jwk, true);
if (! is_array($json)) {
throw new InvalidKeyException('Invalid JWK');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Console/PublicKeysetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private function getKeyset(InputInterface $input): JWKSet
if (! is_string($jwkset)) {
throw new InvalidKeySetException('Invalid JWKSet');
}
$json = JsonConverter::decode($jwkset);
$json = JsonConverter::decode($jwkset, true);
if (! is_array($json)) {
throw new InvalidKeySetException('Invalid JWKSet');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Library/Console/RotateKeysetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private function getKeyset(InputInterface $input): JWKSet
if (! is_string($jwkset)) {
throw new InvalidKeySetException('Invalid JWKSet');
}
$json = JsonConverter::decode($jwkset);
$json = JsonConverter::decode($jwkset, true);
if (! is_array($json)) {
throw new InvalidKeySetException('Invalid JWKSet');
}
Expand All @@ -68,7 +68,7 @@ private function getKey(InputInterface $input): JWK
if (! is_string($jwk)) {
throw new InvalidKeyException('Invalid JWK');
}
$json = JsonConverter::decode($jwk);
$json = JsonConverter::decode($jwk, true);
if (! is_array($json)) {
throw new InvalidKeyException('Invalid JWK');
}
Expand Down
22 changes: 20 additions & 2 deletions src/Library/Core/JWK.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
use Jose\Component\Core\Exception\InvalidKeyException;
use Jose\Component\Core\Exception\UnsupportedAlgorithmException;
use Jose\Component\Core\Util\Base64UrlSafe;
use Jose\Component\Core\Util\BoundedJsonDecoder;
use Jose\Component\Core\Util\InheritanceChecker;
use JsonException;
use JsonSerializable;
use Override;
use SensitiveParameter;
use function array_key_exists;
use function func_get_arg;
use function func_num_args;
use function in_array;
use function is_array;
use function is_string;
Expand Down Expand Up @@ -51,10 +56,23 @@ public function __construct(array $values)

/**
* Creates a JWK object using the given Json string.
*
* A second argument, read with func_get_arg(1) until it is part of the signature in 5.0.0, bounds the values of
* the document: true decodes a document of any number of values, false refuses a document of more than
* BoundedJsonDecoder::MAX_VALUES values, and without it, or with null, such a document is decoded with a
* deprecation, as 5.0.0 refuses it by default. The JsonException of json_decode() is thrown again as a copy:
* the arguments recorded in its trace include the document, and the key with it.
*
* @throws JsonException if the document is not valid JSON
*/
public static function createFromJson(string $json): self
public static function createFromJson(#[SensitiveParameter] string $json): self
{
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
BoundedJsonDecoder::checkDocument($json, func_num_args() > 1 ? func_get_arg(1) : null, __METHOD__);
try {
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
throw new JsonException($exception->getMessage(), $exception->getCode());
}
if (! is_array($data)) {
throw new InvalidArgumentException('Invalid argument.');
}
Expand Down
22 changes: 20 additions & 2 deletions src/Library/Core/JWKSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
use Jose\Component\Core\Exception\InvalidArgumentException;
use Jose\Component\Core\Exception\InvalidKeyException;
use Jose\Component\Core\Exception\InvalidKeySetException;
use Jose\Component\Core\Util\BoundedJsonDecoder;
use Jose\Component\Core\Util\InheritanceChecker;
use JsonException;
use JsonSerializable;
use SensitiveParameter;
use Traversable;
use function array_key_exists;
use function count;
use function func_get_arg;
use function func_num_args;
use function in_array;
use function is_array;
use function is_int;
Expand Down Expand Up @@ -73,10 +78,23 @@ public static function createFromKeyData(array $data): self

/**
* Creates a JWKSet object using the given Json string.
*
* A second argument, read with func_get_arg(1) until it is part of the signature in 5.0.0, bounds the values of
* the document: true decodes a document of any number of values, false refuses a document of more than
* BoundedJsonDecoder::MAX_VALUES values, and without it, or with null, such a document is decoded with a
* deprecation, as 5.0.0 refuses it by default. The JsonException of json_decode() is thrown again as a copy:
* the arguments recorded in its trace include the document, and the key with it.
*
* @throws JsonException if the document is not valid JSON
*/
public static function createFromJson(string $json): self
public static function createFromJson(#[SensitiveParameter] string $json): self
{
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
BoundedJsonDecoder::checkDocument($json, func_num_args() > 1 ? func_get_arg(1) : null, __METHOD__);
try {
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
throw new JsonException($exception->getMessage(), $exception->getCode());
}
if (! is_array($data)) {
throw new InvalidKeySetException('Invalid argument.');
}
Expand Down
56 changes: 54 additions & 2 deletions src/Library/Core/Util/BoundedJsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use Jose\Component\Core\Exception\JsonBoundExceededException;
use Jose\Component\Core\Exception\UnrecognizedSerializationException;
use SensitiveParameter;
use function is_bool;
use function preg_replace;
use function sprintf;
use function str_replace;
use function substr_count;
use function trigger_deprecation;
use const JSON_ERROR_DEPTH;

/**
Expand Down Expand Up @@ -48,10 +50,14 @@ public function decode(#[SensitiveParameter] string $json): mixed
));
}
try {
return JsonConverter::decode($json);
return JsonConverter::decode($json, true);
} catch (InvalidArgumentException $exception) {
if ($exception->getCode() !== JSON_ERROR_DEPTH) {
throw $exception;
throw new InvalidArgumentException(
'Unsupported input.',
$exception->getCode(),
$exception->getPrevious()
);
}
$this->exceededMaxDepth = true;
throw new JsonBoundExceededException(
Expand Down Expand Up @@ -102,6 +108,52 @@ public function hasExceededMaxValues(): bool
return $this->remainingValues < 0;
}

/**
* Applies the second argument of a public method that decodes a JSON document on its own, read with
* func_get_arg(1) until it is part of the signature in 5.0.0: true decodes a document of any number of values,
* false refuses a document of more than MAX_VALUES values, and null, or no argument, decodes such a document with
* a deprecation, as 5.0.0 refuses it by default.
*
* @param string $method the method that decodes the document, such as "Jose\Component\Core\JWK::createFromJson"
*
* @throws InvalidArgumentException if the argument is neither a boolean nor null
* @throws JsonBoundExceededException if the argument is false and the document holds more than MAX_VALUES values
*/
public static function checkDocument(#[SensitiveParameter] string $json, mixed $unbounded, string $method): void
{
if ($unbounded !== null && ! is_bool($unbounded)) {
throw new InvalidArgumentException(sprintf(
'The second argument of "%s()" must be a boolean: true decodes a document of any number of values, false refuses one of more than %d.',
$method,
self::MAX_VALUES
));
}
if ($unbounded === true || ! self::exceedsMaxValues($json)) {
return;
}
if ($unbounded === false) {
throw new JsonBoundExceededException(sprintf(
'The JSON document holds more JSON values than the %d that are decoded.',
self::MAX_VALUES
));
}
trigger_deprecation(
'web-token/jwt-framework',
'4.3.0',
'Decoding a JSON document of more than %d values is deprecated and "%s()" will refuse it by default in 5.0.0. Pass true as the second argument to keep decoding it, or false to refuse it now.',
self::MAX_VALUES,
$method
);
}

/**
* Whether a JSON document holds more than MAX_VALUES values, counted as decode() counts them.
*/
public static function exceedsMaxValues(#[SensitiveParameter] string $json): bool
{
return self::countValues($json) > self::MAX_VALUES;
}

/**
* Counts the brackets, braces and commas found outside the string literals.
*/
Expand Down
Loading
Loading