Skip to content

Commit 21040e6

Browse files
cicnaviclaude
andcommitted
Keep the client secret out of the session store
The authorization request travels in the processing chain state, which every authentication processing filter reads and which SimpleSAMLphp serializes into the session store whenever a filter shows the End-User a page of its own (consent, a warning page, MFA). The request's client went with it, holding its secret in plain text and the hash of its registration access token; the client's metadata in the state left the secret out but not the hash. - ClientEntity::withoutCredentials() (declared in ClientEntityInterface) gives a copy of the client with an empty secret and no registration access token hash. - AuthenticationService::prepareStateArray() puts a copy of the request into the state whose client is that copy, and leaves the hash out of the client's metadata as well as the secret. The request handed in is not changed. Nothing after the login needs either: the request is completed with the client's identifier, redirect URIs and metadata, and the token endpoint authenticates a client against the one the repository holds. The protocol cache keeps client state arrays, not objects, so it is not affected. Checked end to end through a filter page on the dev OP: the saved session holds the client but not its secret (it held it once before), and the code is still redeemed with the secret. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Assisted-by: Google Antigravity (gemini-3.8-flash-high review)
1 parent 038f2ff commit 21040e6

5 files changed

Lines changed: 147 additions & 2 deletions

File tree

‎src/Entities/ClientEntity.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,22 @@ public function restoreSecret(string $secret): ClientEntityInterface
310310
}
311311

312312

313+
/**
314+
* A copy of the client without what authenticates it: an empty secret, and no hash of a registration access
315+
* token. For a client kept where its secret must not go, as the processing chain's state
316+
* (AuthenticationService::prepareStateArray()). The copy must not be stored (ClientRepository::update() would
317+
* write the empty secret over the client's), nor a client authenticated against it.
318+
*/
319+
public function withoutCredentials(): ClientEntityInterface
320+
{
321+
$client = clone $this;
322+
$client->secret = '';
323+
$client->registrationAccessToken = null;
324+
325+
return $client;
326+
}
327+
328+
313329
public function getDescription(): string
314330
{
315331
return $this->description;

‎src/Entities/Interfaces/ClientEntityInterface.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ public function getSecret(): string;
2020
public function restoreSecret(string $secret): self;
2121

2222

23+
/**
24+
* A copy of the client without what authenticates it: an empty secret, and no hash of a registration access
25+
* token.
26+
*/
27+
public function withoutCredentials(): self;
28+
29+
2330
public function getDescription(): string;
2431

2532

‎src/Services/AuthenticationService.php‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,14 @@ public function prepareStateArray(
249249

250250
$state['Oidc'] = [
251251
'OpenIdProviderMetadata' => $this->opMetadataService->getMetadata(),
252+
// Without what authenticates the client, as the request below.
252253
'RelyingPartyMetadata' => array_filter(
253254
$clientArray,
254-
fn(/** @param array-key $key */ $key) => $key !== 'secret',
255+
fn(/** @param array-key $key */ $key) => !in_array(
256+
$key,
257+
[ClientEntity::KEY_SECRET, ClientEntity::KEY_REGISTRATION_ACCESS_TOKEN],
258+
true,
259+
),
255260
ARRAY_FILTER_USE_KEY,
256261
),
257262
// Read as the grants validate the request (AuthCodeGrant::$allowedAuthorizationHttpMethods): from the
@@ -277,7 +282,7 @@ function (/** @param array-key $key */ $key) {
277282
$state[State::RESTART] = $request->getUri()->__toString();
278283

279284
// Data required after we get back from a ProcessingChain redirect
280-
$state['authorizationRequest'] = $authorizationRequest;
285+
$state['authorizationRequest'] = $this->withClientWithoutCredentials($authorizationRequest);
281286
$state['authSourceId'] = $authSimple->getAuthSource()->getAuthId();
282287

283288
// Where the client is answered if the End-User cancels in an authentication processing filter.
@@ -289,6 +294,29 @@ function (/** @param array-key $key */ $key) {
289294
}
290295

291296

297+
/**
298+
* The request as the processing chain's state keeps it: a copy whose client is without what authenticates it
299+
* (ClientEntityInterface::withoutCredentials()). The state goes into the session store whenever an
300+
* authentication processing filter shows the End-User a page of its own (State::saveState()), and every filter
301+
* reads it. Nothing after the login needs the secret or the registration access token hash: the request is
302+
* completed with the client's identifier, redirect URIs and metadata, and a client authenticates at the token
303+
* endpoint against the client the repository holds.
304+
*/
305+
protected function withClientWithoutCredentials(
306+
OAuth2AuthorizationRequestInterface $authorizationRequest,
307+
): OAuth2AuthorizationRequestInterface {
308+
$client = $authorizationRequest->getClient();
309+
if (!$client instanceof ClientEntityInterface) {
310+
return $authorizationRequest;
311+
}
312+
313+
$authorizationRequest = clone $authorizationRequest;
314+
$authorizationRequest->setClient($client->withoutCredentials());
315+
316+
return $authorizationRequest;
317+
}
318+
319+
292320
/**
293321
* The redirect an error response to the authorization request takes: its redirect URI, which the grants have
294322
* validated, its `state`, its response mode, and the Pushed Authorization Request it was made with, if any. A

‎tests/unit/src/Entities/ClientEntityTest.php‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,40 @@ protected function assertHashIs(?string $expected, ClientEntity $entity): void
834834
}
835835

836836

837+
/**
838+
* The copy for where the secret must not go has neither the secret nor the registration access token hash, and
839+
* is otherwise the client, of its class; the client keeps both.
840+
*
841+
* @throws \JsonException
842+
*/
843+
public function testACopyWithoutCredentialsHasNoSecretNorRegistrationAccessTokenHash(): void
844+
{
845+
$this->isConfidential = true;
846+
$entity = $this->entityWith(
847+
jwks: ['keys' => [['kty' => 'EC', 'kid' => 'rp-1']]],
848+
extraMetadata: ['id_token_signed_response_alg' => 'RS256'],
849+
registrationAccessToken: 'hash-1',
850+
);
851+
852+
$copy = $entity->withoutCredentials();
853+
854+
$this->assertInstanceOf(ClientEntity::class, $copy);
855+
$this->assertNotSame($entity, $copy);
856+
$this->assertSame('', $copy->getSecret());
857+
$this->assertHashIs(null, $copy);
858+
$this->assertSame(
859+
array_merge(
860+
$entity->getState(),
861+
[ClientEntity::KEY_SECRET => '', ClientEntity::KEY_REGISTRATION_ACCESS_TOKEN => null],
862+
),
863+
$copy->getState(),
864+
);
865+
866+
$this->assertSame('secret', $entity->getSecret());
867+
$this->assertHashIs('hash-1', $entity);
868+
}
869+
870+
837871
/**
838872
* @return array<string, array{0: ?array<string,mixed>, 1: ?string}>
839873
*/

‎tests/unit/src/Services/AuthenticationServiceTest.php‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ public function testItConstructsStateArray(): void
511511
'entityid' => $state['Oidc']['RelyingPartyMetadata']['id'],
512512
];
513513
$state[State::RESTART] = self::URI;
514+
// The request mock's client is a stub of League's interface, not a client of the module's, so the request goes
515+
// into the state as it is (testTheStateCarriesNoneOfTheClientCredentials() has one of the module's).
514516
$state['authorizationRequest'] = $this->authorizationRequestMock;
515517
$state['authSourceId'] = '';
516518
// The request mock has no redirect URI, so there is no redirect to keep.
@@ -528,6 +530,64 @@ public function testItConstructsStateArray(): void
528530
}
529531

530532

533+
/**
534+
* The state goes into the session store whenever an authentication processing filter shows the End-User a page,
535+
* and every filter reads it: the request in it is a copy whose client has neither the secret nor the
536+
* registration access token hash, and the client's metadata neither. The request handed in keeps its client.
537+
*
538+
* @throws \SimpleSAML\Error\AuthSource
539+
*/
540+
public function testTheStateCarriesNoneOfTheClientCredentials(): void
541+
{
542+
$secret = 'the-client-secret';
543+
$registrationAccessTokenHash = 'the-registration-access-token-hash';
544+
$client = new ClientEntity(
545+
'clientid',
546+
$secret,
547+
'Client',
548+
'Description',
549+
[self::REDIRECT_URI],
550+
['openid'],
551+
true,
552+
true,
553+
registrationAccessToken: $registrationAccessTokenHash,
554+
);
555+
$authorizationRequest = new AuthorizationRequest();
556+
$authorizationRequest->setClient($client);
557+
558+
$state = $this->mock()->prepareStateArray(
559+
$this->authSimpleMock,
560+
$client,
561+
$this->serverRequestMock,
562+
$authorizationRequest,
563+
);
564+
565+
$stateRequest = $state['authorizationRequest'];
566+
$this->assertInstanceOf(AuthorizationRequest::class, $stateRequest);
567+
$this->assertNotSame($authorizationRequest, $stateRequest);
568+
$stateClient = $stateRequest->getClient();
569+
$this->assertInstanceOf(ClientEntity::class, $stateClient);
570+
$this->assertSame('clientid', $stateClient->getIdentifier());
571+
$this->assertSame([self::REDIRECT_URI], $stateClient->getRedirectUri());
572+
$this->assertSame('', $stateClient->getSecret());
573+
$this->assertNull($stateClient->getRegistrationAccessTokenHash());
574+
575+
$relyingPartyMetadata = $state['Oidc']['RelyingPartyMetadata'];
576+
$this->assertSame('clientid', $relyingPartyMetadata[ClientEntity::KEY_ID]);
577+
$this->assertArrayNotHasKey(ClientEntity::KEY_SECRET, $relyingPartyMetadata);
578+
$this->assertArrayNotHasKey(ClientEntity::KEY_REGISTRATION_ACCESS_TOKEN, $relyingPartyMetadata);
579+
580+
// As State::saveState() writes it.
581+
$serializedState = serialize($state);
582+
$this->assertStringNotContainsString($secret, $serializedState);
583+
$this->assertStringNotContainsString($registrationAccessTokenHash, $serializedState);
584+
585+
$this->assertSame($client, $authorizationRequest->getClient());
586+
$this->assertSame($secret, $client->getSecret());
587+
$this->assertSame($registrationAccessTokenHash, $client->getRegistrationAccessTokenHash());
588+
}
589+
590+
531591
/**
532592
* The processing chain's state keeps the request's redirect, so that a filter's cancel is answered to the
533593
* client.

0 commit comments

Comments
 (0)