Skip to content

Commit 989a854

Browse files
committed
[Server] Encode the canonical arguments deep enough for any decoded request
1 parent 9f3a072 commit 989a854

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/Server/Stateless/OriginatingRequestBinder.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
*/
4343
final class OriginatingRequestBinder implements RequestStateBinderInterface
4444
{
45+
// A canonical map nests three levels deep, and requests decode up to json_decode()'s default of 512.
46+
private const ENCODE_DEPTH = 3 * 512;
47+
4548
/**
4649
* @param (\Closure(AccessToken): ?string)|null $principal identifies the user and client a token was issued
4750
* for, stable across a token refresh; null when the token
@@ -103,7 +106,7 @@ private static function defaultPrincipal(AccessToken $accessToken): ?string
103106
private static function encode(mixed $value): string
104107
{
105108
try {
106-
return json_encode($value, \JSON_THROW_ON_ERROR);
109+
return json_encode($value, \JSON_THROW_ON_ERROR, self::ENCODE_DEPTH);
107110
} catch (\JsonException $e) {
108111
throw new RuntimeException(\sprintf('Failed to encode the requestState binding as JSON: %s', $e->getMessage()), previous: $e);
109112
}

‎tests/Unit/Server/Stateless/OriginatingRequestBinderTest.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,27 @@ public function testPrincipalResolverReturningNullFailsClosed(): void
154154
$this->assertNotSame($binder->bind(self::call(), self::token()), $binder->bind(self::call(), self::token()));
155155
}
156156

157+
#[TestDox('deeply nested arguments still bind, and their deepest leaf counts')]
158+
public function testDeeplyNestedArgumentsBind(): void
159+
{
160+
$nested = static function (string $leaf): array {
161+
$value = $leaf;
162+
163+
for ($depth = 0; $depth < 300; ++$depth) {
164+
$value = ['k' => $value];
165+
}
166+
167+
return $value;
168+
};
169+
170+
$binder = new OriginatingRequestBinder();
171+
172+
$this->assertNotSame(
173+
$binder->bind(self::call(arguments: $nested('preview')), self::token()),
174+
$binder->bind(self::call(arguments: $nested('execute')), self::token()),
175+
);
176+
}
177+
157178
#[TestDox('a resource read binds to its URI')]
158179
public function testResourceReadBindsToItsUri(): void
159180
{

0 commit comments

Comments
 (0)