Skip to content

Commit 173f99f

Browse files
committed
[Server] Fail the request when the requestState binder throws
1 parent 09da54c commit 173f99f

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎src/Server/Stateless/RequestStateBinderInterface.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
* covered by the MAC of every state minted while answering the request, and
2323
* recomputed to verify the state the client echoes on the retry. A state
2424
* presented on a request that binds to another value fails verification like
25-
* a forged one. The value never goes on the wire.
25+
* a forged one. The value never goes on the wire. A binder that throws fails
26+
* the request with an internal error.
2627
*
2728
* Bind only what stays the same across the rounds of one call: the request
2829
* object carries neither `requestState` nor `inputResponses`, but `_meta`

‎src/Server/Stateless/StatelessProtocol.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,14 @@ private function dispatch(string $method, array $decoded, RequestMeta $meta, str
522522
$requestStateCodec = $this->requestStateCodec;
523523

524524
if (null !== $requestStateCodec && null !== $this->requestStateBinder) {
525-
$requestStateCodec = $requestStateCodec->withBinding($this->requestStateBinder->bind($request, $accessToken));
525+
try {
526+
$requestStateCodec = $requestStateCodec->withBinding($this->requestStateBinder->bind($request, $accessToken));
527+
} catch (\Throwable $e) {
528+
// Fails closed: an unbound codec would mint and accept states the binder is there to refuse.
529+
$this->logger->error('The requestState binder failed on a modern-era request.', ['method' => $method, 'exception' => $e]);
530+
531+
return StatelessResult::error(Error::forInternalError(self::INTERNAL_ERROR_MESSAGE, $id), 500);
532+
}
526533
}
527534

528535
try {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,23 @@ public function bind(Request $request, ?AccessToken $accessToken): string
775775
$this->assertSame([['books_flight', ['to' => 'BER'], 'alice']], $binder->calls);
776776
}
777777

778+
#[TestDox('a binder that throws fails the request with an internal error instead of escaping to the transport')]
779+
public function testThrowingBinderFailsTheRequest(): void
780+
{
781+
$binder = new class implements RequestStateBinderInterface {
782+
public function bind(Request $request, ?AccessToken $accessToken): string
783+
{
784+
throw new \RuntimeException('No principal claim on the token.');
785+
}
786+
};
787+
788+
$answer = self::bookAs(self::twoAskProtocol(binder: $binder), self::alice(), arguments: ['to' => 'BER']);
789+
790+
$this->assertSame(500, $answer['status']);
791+
$this->assertSame(Error::INTERNAL_ERROR, $answer['body']['error']['code']);
792+
$this->assertStringNotContainsString('principal', $answer['body']['error']['message']);
793+
}
794+
778795
#[TestDox('a state minted before a binder was configured is refused once one is')]
779796
public function testUnboundStateRefusedByABindingServer(): void
780797
{

0 commit comments

Comments
 (0)