Repository navigation
Expand file tree
/
Copy pathBoundedJsonDecoder.php
More file actions
114 lines (102 loc) · 4.22 KB
/
Copy pathBoundedJsonDecoder.php
File metadata and controls
114 lines (102 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace Jose\Component\Core\Util;
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;
use function str_replace;
use function substr_count;
use const JSON_ERROR_DEPTH;
/**
* Decodes the JSON documents of one token, which together may hold at most MAX_VALUES values. json_decode() only
* limits the depth: a document of brackets decodes to a hundred times its size, and colliding member names make it
* quadratic.
*
* A document rejected for the values it holds or for its depth is rejected with a JsonBoundExceededException, whose
* message names the bound; the other errors of json_decode() keep the exception of JsonConverter::decode().
*
* @internal
*/
final class BoundedJsonDecoder
{
public const MAX_VALUES = 4096;
private int $remainingValues = self::MAX_VALUES;
private bool $exceededMaxDepth = false;
/**
* @throws JsonBoundExceededException if the document is nested too deeply, or if the documents given to this
* decoder hold more than MAX_VALUES values. The code of the exception is that of
* the JSON error, 0 for the values.
* @throws InvalidArgumentException if the document is not valid JSON
*/
public function decode(#[SensitiveParameter] string $json): mixed
{
$this->remainingValues -= self::countValues($json);
if ($this->remainingValues < 0) {
throw new JsonBoundExceededException(sprintf(
'The JSON documents of the token hold more JSON values than the %d that are decoded.',
self::MAX_VALUES
));
}
try {
return JsonConverter::decode($json);
} catch (InvalidArgumentException $exception) {
if ($exception->getCode() !== JSON_ERROR_DEPTH) {
throw $exception;
}
$this->exceededMaxDepth = true;
throw new JsonBoundExceededException(
'A JSON document of the token is nested too deeply.',
$exception->getCode(),
$exception->getPrevious()
);
}
}
/**
* 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.
*/
public function hasExceededMaxDepth(): bool
{
return $this->exceededMaxDepth;
}
/**
* Whether the documents given to this decoder hold more than MAX_VALUES values. The document that went beyond the
* bound was rejected before it was decoded, and so are the documents given after it.
*/
public function hasExceededMaxValues(): bool
{
return $this->remainingValues < 0;
}
/**
* Counts the brackets, braces and commas found outside the string literals.
*/
private static function countValues(#[SensitiveParameter] string $json): int
{
$structure = preg_replace('/"[^"]*+"/', '', str_replace(['\\\\', '\\"'], '', $json)) ?? $json;
return substr_count($structure, '[') + substr_count($structure, '{') + substr_count($structure, ',');
}
}