Skip to content

Commit 4884167

Browse files
committed
fix: filter unallowed params in callback
1 parent 22ebeef commit 4884167

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/Service/AuthorizationService.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ public function callback(
134134
?AuthSessionInterface $authSession = null,
135135
?int $maxAge = null
136136
): TokenSetInterface {
137-
$tokenSet = $this->tokenSetFactory->fromArray($params);
137+
$allowedParams = ['code', 'state', 'token_type', 'access_token', 'id_token', 'refresh_token', 'expires_in', 'code_verifier'];
138+
$tokenSet = $this->tokenSetFactory->fromArray(array_intersect_key(
139+
$params,
140+
array_fill_keys($allowedParams, true)
141+
));
138142

139143
$idToken = $tokenSet->getIdToken();
140144

tests/Service/AuthorizationServiceTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
use Facile\OpenIDClient\AuthMethod\AuthMethodFactoryInterface;
88
use Facile\OpenIDClient\AuthMethod\AuthMethodInterface;
9+
use Facile\OpenIDClient\Client\Client;
910
use Facile\OpenIDClient\Client\ClientInterface as OpenIDClient;
11+
use Facile\OpenIDClient\Client\Metadata\ClientMetadata;
1012
use Facile\OpenIDClient\Client\Metadata\ClientMetadataInterface;
1113
use Facile\OpenIDClient\Issuer\IssuerInterface;
1214
use Facile\OpenIDClient\Issuer\Metadata\IssuerMetadataInterface;
1315
use Facile\OpenIDClient\Service\AuthorizationService;
1416
use Facile\OpenIDClient\Token\IdTokenVerifierBuilderInterface;
17+
use Facile\OpenIDClient\Token\TokenSetFactory;
1518
use Facile\OpenIDClient\Token\TokenSetFactoryInterface;
1619
use Facile\OpenIDClient\Token\TokenSetInterface;
1720
use Facile\OpenIDClient\Token\TokenVerifierBuilderInterface;
@@ -123,4 +126,46 @@ public function testFetchTokenFromCode(): void
123126

124127
static::assertSame($tokenSet->reveal(), $service->grant($openIdClient->reveal(), $claims));
125128
}
129+
130+
public function testCallbackShouldNotProcessUnknownParams(): void
131+
{
132+
$tokenSetFactory = $this->prophesize(TokenSetFactoryInterface::class);
133+
$client = $this->prophesize(ClientInterface::class);
134+
$requestFactory = $this->prophesize(RequestFactoryInterface::class);
135+
$idTokenVerifierBuilder = $this->prophesize(IdTokenVerifierBuilderInterface::class);
136+
$tokenVerifierBuilder = $this->prophesize(TokenVerifierBuilderInterface::class);
137+
138+
$service = new AuthorizationService(
139+
$tokenSetFactory->reveal(),
140+
$client->reveal(),
141+
$requestFactory->reveal(),
142+
$idTokenVerifierBuilder->reveal(),
143+
$tokenVerifierBuilder->reveal()
144+
);
145+
146+
$issuer = $this->prophesize(IssuerInterface::class);
147+
$clientMetadata = ClientMetadata::fromArray([
148+
'client_id' => 'foobar',
149+
'client_secret' => 'secret',
150+
'redirect_uris' => [
151+
'http://localhost/callback',
152+
],
153+
]);
154+
$client = new Client(
155+
$issuer->reveal(),
156+
$clientMetadata
157+
);
158+
159+
// Build poc request
160+
$body = 'claims[iss]=foobar&claims[sub]=adminuser1'; // forge arbitrary claims
161+
$headers = ['test' => 'test'];
162+
$serverRequest = new \GuzzleHttp\Psr7\ServerRequest('POST', 'http://127.0.0.1:8082', $headers, $body);
163+
164+
$callbackParams = $service->getCallbackParams($serverRequest, $client);
165+
$tokenSet = $service->callback($client, $callbackParams); // tokenSet contains forged claims
166+
167+
$claims = $tokenSet->claims();
168+
169+
$this->assertSame([], $claims);
170+
}
126171
}

0 commit comments

Comments
 (0)