Skip to content

Commit 41c097d

Browse files
authored
Merge pull request #819 from web-token/fix/json-decoding-bounds
fix(json): bound the JSON documents decoded outside of a token
2 parents 4d427a4 + 645d0cc commit 41c097d

34 files changed

Lines changed: 1305 additions & 46 deletions

‎.ci-tools/phpstan-baseline.neon‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,12 @@ parameters:
14341434
count: 1
14351435
path: ../src/Bundle/DependencyInjection/Source/KeyManagement/JKUSource.php
14361436

1437+
-
1438+
rawMessage: Cannot access offset 'allow_large_key_sets' on mixed.
1439+
identifier: offsetAccess.nonOffsetAccessible
1440+
count: 1
1441+
path: ../src/Bundle/DependencyInjection/Source/KeyManagement/JKUSource.php
1442+
14371443
-
14381444
rawMessage: Cannot access offset 'client' on mixed.
14391445
identifier: offsetAccess.nonOffsetAccessible
@@ -1461,7 +1467,7 @@ parameters:
14611467
-
14621468
rawMessage: 'Cannot call method booleanNode() on mixed.'
14631469
identifier: method.nonObject
1464-
count: 1
1470+
count: 2
14651471
path: ../src/Bundle/DependencyInjection/Source/KeyManagement/JKUSource.php
14661472

14671473
-
@@ -1479,13 +1485,13 @@ parameters:
14791485
-
14801486
rawMessage: 'Cannot call method end() on mixed.'
14811487
identifier: method.nonObject
1482-
count: 5
1488+
count: 6
14831489
path: ../src/Bundle/DependencyInjection/Source/KeyManagement/JKUSource.php
14841490

14851491
-
14861492
rawMessage: 'Cannot call method info() on mixed.'
14871493
identifier: method.nonObject
1488-
count: 2
1494+
count: 3
14891495
path: ../src/Bundle/DependencyInjection/Source/KeyManagement/JKUSource.php
14901496

14911497
-

‎src/Bundle/DependencyInjection/Source/KeyManagement/JKUSource.php‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@ public function load(array $configs, ContainerBuilder $container): void
2929
$loader->load('jku_source.php');
3030
$loader->load('jku_commands.php');
3131
$container->setAlias('jose.http_client', $configs[$this->name()]['client']);
32-
if (isset($configs[$this->name()]['follow_redirects'])) {
32+
$followRedirects = $configs[$this->name()]['follow_redirects'] ?? null;
33+
$allowLargeKeySets = $configs[$this->name()]['allow_large_key_sets'] ?? null;
34+
if ($followRedirects !== null || $allowLargeKeySets !== null) {
3335
foreach ([JKUFactory::class, X5UFactory::class] as $factory) {
34-
$container->getDefinition($factory)
35-
->addArgument($configs[$this->name()]['follow_redirects']);
36+
$definition = $container->getDefinition($factory)
37+
->addArgument($followRedirects);
38+
if ($allowLargeKeySets !== null) {
39+
$definition->addArgument($allowLargeKeySets);
40+
}
3641
}
3742
}
3843
}
@@ -49,6 +54,11 @@ public function getNodeDefinition(NodeDefinition $node): void
4954
->info('HTTP Client used to retrieve key sets.')
5055
->isRequired()
5156
->end()
57+
->booleanNode('allow_large_key_sets')
58+
->info(
59+
'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.'
60+
)
61+
->end()
5262
->booleanNode('follow_redirects')
5363
->info(
5464
'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.'

‎src/Bundle/EnvVarProcessor/KeyEnvVarProcessor.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public function getEnv(string $prefix, string $name, Closure $getEnv): mixed
2020
$env = $getEnv($name);
2121

2222
return match ($prefix) {
23-
'jwk' => JWK::createFromJson($env),
24-
'jwkset' => JWKSet::createFromJson($env),
23+
'jwk' => JWK::createFromJson($env, true),
24+
'jwkset' => JWKSet::createFromJson($env, true),
2525
default => throw new RuntimeException(sprintf('Unsupported prefix "%s".', $prefix)),
2626
};
2727
}

‎src/Library/Console/AddKeyIntoKeysetCommand.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private function getKeyset(InputInterface $input): JWKSet
4747
if (! is_string($jwkset)) {
4848
throw new InvalidKeySetException('The argument must be a valid JWKSet.');
4949
}
50-
$json = JsonConverter::decode($jwkset);
50+
$json = JsonConverter::decode($jwkset, true);
5151
if (! is_array($json)) {
5252
throw new InvalidKeySetException('The argument must be a valid JWKSet.');
5353
}
@@ -61,7 +61,7 @@ private function getKey(InputInterface $input): JWK
6161
if (! is_string($jwk)) {
6262
throw new InvalidKeyException('The argument must be a valid JWK.');
6363
}
64-
$json = JsonConverter::decode($jwk);
64+
$json = JsonConverter::decode($jwk, true);
6565
if (! is_array($json)) {
6666
throw new InvalidKeyException('The argument must be a valid JWK.');
6767
}

‎src/Library/Console/GetThumbprintCommand.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5454
if (! is_string($hash)) {
5555
throw new InvalidArgumentException('Invalid hash algorithm');
5656
}
57-
$json = JsonConverter::decode($jwk);
57+
$json = JsonConverter::decode($jwk, true);
5858
if (! is_array($json)) {
5959
throw new InvalidArgumentException('Invalid input.');
6060
}

‎src/Library/Console/KeyAnalyzerCommand.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private function getKey(InputInterface $input): JWK
7070
if (! is_string($jwk)) {
7171
throw new InvalidKeyException('Invalid JWK');
7272
}
73-
$json = JsonConverter::decode($jwk);
73+
$json = JsonConverter::decode($jwk, true);
7474
if (! is_array($json)) {
7575
throw new InvalidKeyException('Invalid JWK.');
7676
}

‎src/Library/Console/KeysetAnalyzerCommand.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function getKeyset(InputInterface $input): JWKSet
8585
if (! is_string($jwkset)) {
8686
throw new InvalidKeySetException('Invalid JWKSet');
8787
}
88-
$json = JsonConverter::decode($jwkset);
88+
$json = JsonConverter::decode($jwkset, true);
8989
if (! is_array($json)) {
9090
throw new InvalidKeySetException('Invalid JWKSet');
9191
}

‎src/Library/Console/MergeKeysetCommand.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3535
$keySets = $input->getArgument('jwksets');
3636
$newJwkset = new JWKSet([]);
3737
foreach ($keySets as $keySet) {
38-
$json = JsonConverter::decode($keySet);
38+
$json = JsonConverter::decode($keySet, true);
3939
if (! is_array($json)) {
4040
throw new InvalidKeySetException('The argument must be a valid JWKSet.');
4141
}

‎src/Library/Console/OptimizeRsaKeyCommand.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3333
if (! is_string($jwk)) {
3434
throw new InvalidKeyException('Invalid JWK');
3535
}
36-
$json = JsonConverter::decode($jwk);
36+
$json = JsonConverter::decode($jwk, true);
3737
if (! is_array($json)) {
3838
throw new InvalidKeyException('Invalid JWK');
3939
}

‎src/Library/Console/PemConverterCommand.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3434
if (! is_string($jwk)) {
3535
throw new InvalidKeyException('Invalid JWK');
3636
}
37-
$json = JsonConverter::decode($jwk);
37+
$json = JsonConverter::decode($jwk, true);
3838
if (! is_array($json)) {
3939
throw new InvalidKeyException('Invalid JWK.');
4040
}

0 commit comments

Comments
 (0)