Skip to content

Commit cfbf7d1

Browse files
committed
Apply the owner filter to clients taken from the cache
A client found in the protocol cache was returned to an owner-restricted lookup whatever its owner. The admin UI limits a user with the permission to their own clients that way, so with the protocol cache enabled such a user could reach other users' clients. Also add the owner filter with AND to queries whose WHERE is followed by a line break (it produced a second WHERE), and pass the owner to update() when an edit is saved, as resetting the secret and deleting do.
1 parent 7a78e14 commit cfbf7d1

4 files changed

Lines changed: 130 additions & 4 deletions

File tree

‎src/Controllers/Admin/ClientController.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ public function edit(Request $request): Response
280280
$this->logger->warning($message, $updatedClient->getState());
281281
$this->sessionMessagesService->addMessage($message);
282282
} else {
283-
$this->clientRepository->update($updatedClient);
283+
$authedUserId = $this->authorization->isAdmin() ? null : $this->authorization->getUserId();
284+
$this->clientRepository->update($updatedClient, $authedUserId);
284285

285286
// Also persist allowed origins for this client.
286287
is_array($allowedOrigins = $form->getValues('array')['allowed_origin'] ?? []) ||

‎src/Repositories/ClientRepository.php‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ public function findById(string $clientIdentifier, ?string $owner = null): ?Clie
9191
$cachedState = $this->protocolCache?->get(null, $this->getCacheKey($clientIdentifier));
9292

9393
if (is_array($cachedState)) {
94-
return $this->clientEntityFactory->fromState($cachedState);
94+
$clientEntity = $this->clientEntityFactory->fromState($cachedState);
95+
96+
if ($this->isOwnedBy($clientEntity, $owner)) {
97+
return $clientEntity;
98+
}
9599
}
96100

97101
/**
@@ -138,7 +142,11 @@ public function findByEntityIdentifier(string $entityIdentifier, ?string $owner
138142
$cachedState = $this->protocolCache?->get(null, $this->getCacheKey($entityIdentifier));
139143

140144
if (is_array($cachedState)) {
141-
return $this->clientEntityFactory->fromState($cachedState);
145+
$clientEntity = $this->clientEntityFactory->fromState($cachedState);
146+
147+
if ($this->isOwnedBy($clientEntity, $owner)) {
148+
return $clientEntity;
149+
}
142150
}
143151

144152
/**
@@ -206,11 +214,24 @@ public function findFederatedByEntityIdentifier(
206214
}
207215

208216

217+
/**
218+
* Whether a client taken from the cache may be returned to a lookup restricted to an owner. The cache holds a
219+
* client whoever looked it up, the protocol endpoints included, so the owner filter the query applies has to be
220+
* applied to a cached client too. A client the owner does not have is looked up in the database, which answers
221+
* for the owner (and whose owner may have changed since the client was cached).
222+
*/
223+
private function isOwnedBy(ClientEntityInterface $clientEntity, ?string $owner): bool
224+
{
225+
return is_null($owner) || $clientEntity->getOwner() === $owner;
226+
}
227+
228+
209229
private function addOwnerWhereClause(string $query, array $params, ?string $owner = null): array
210230
{
211231
if (isset($owner)) {
212232
$params['ownerFilter'] = $owner;
213-
if (stripos($query, ' where ') > 0) {
233+
// A query written over several lines may have a line break next to its WHERE.
234+
if (preg_match('/\swhere\s/i', $query) === 1) {
214235
$query .= ' AND owner = :ownerFilter';
215236
} else {
216237
$query .= ' WHERE owner = :ownerFilter';

‎tests/unit/src/Controllers/Admin/ClientControllerTest.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,31 @@ public function testCanEdit(): void
428428
}
429429

430430

431+
/**
432+
* A user managing their own clients saves their client with the owner filter, as a secret reset and a delete
433+
* do; an administrator saves any client.
434+
*/
435+
public function testAnEditIsSavedForTheOwnerOnly(): void
436+
{
437+
$request = Request::create('/edit?client_id=clientId', 'GET', ['client_id' => 'clientId']);
438+
$this->authorizationMock->method('isAdmin')->willReturn(false);
439+
$this->authorizationMock->method('getUserId')->willReturn('user@example.org');
440+
$this->clientEntityMock->method('getRegistrationType')->willReturn(RegistrationTypeEnum::Manual);
441+
$this->clientEntityMock->method('getIdentifier')->willReturn('clientId');
442+
$this->clientRepositoryMock->expects($this->once())->method('findById')
443+
->with('clientId', 'user@example.org')->willReturn($this->clientEntityMock);
444+
$updatedClientMock = $this->createMock(ClientEntityInterface::class);
445+
$this->clientFormMock->method('isSuccess')->willReturn(true);
446+
$this->clientFormMock->method('getValues')->willReturn($this->sampleFormData);
447+
$this->clientEntityFactoryMock->method('fromData')->willReturn($updatedClientMock);
448+
449+
$this->clientRepositoryMock->expects($this->once())->method('update')
450+
->with($updatedClientMock, 'user@example.org');
451+
452+
$this->sut()->edit($request);
453+
}
454+
455+
431456
public function testWontEditIfClientEntityIdentifierExists(): void
432457
{
433458
$request = Request::create(

‎tests/unit/src/Repositories/ClientRepositoryTest.php‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,69 @@ public function testCanFindByIdFromCache(): void
389389
}
390390

391391

392+
/**
393+
* The protocol endpoints cache every client they look up, with no owner. A user managing their own clients
394+
* (the `client` permission) must not be given such a client by its ID when it is not theirs.
395+
*/
396+
public function testACachedClientIsNotReturnedToALookupForAnotherOwner(): void
397+
{
398+
$client = self::getClient('clientid', owner: 'otherUser');
399+
$this->repository->add($client);
400+
$this->clientEntityFactoryMock->method('fromState')->willReturn($client);
401+
402+
$sut = $this->repositoryWithCacheHolding(['state']);
403+
404+
$this->assertNull($sut->findById('clientid', 'homer@example.com'));
405+
$this->assertSame($client, $sut->findById('clientid', 'otherUser'));
406+
$this->assertSame($client, $sut->findById('clientid'));
407+
}
408+
409+
410+
public function testACachedClientIsNotReturnedToALookupByEntityIdentifierForAnotherOwner(): void
411+
{
412+
$client = self::getClient('clientid', owner: 'otherUser', entityId: 'entityId');
413+
$this->repository->add($client);
414+
$this->clientEntityFactoryMock->method('fromState')->willReturn($client);
415+
416+
$sut = $this->repositoryWithCacheHolding(['state']);
417+
418+
$this->assertNull($sut->findByEntityIdentifier('entityId', 'homer@example.com'));
419+
$this->assertSame($client, $sut->findByEntityIdentifier('entityId', 'otherUser'));
420+
$this->assertSame($client, $sut->findByEntityIdentifier('entityId'));
421+
}
422+
423+
424+
/**
425+
* A cached client with another owner is looked up in the database, which has the owner the client has now.
426+
*/
427+
public function testTheDatabaseAnswersForAnOwnerTheCachedClientDoesNotHave(): void
428+
{
429+
$staleClient = self::getClient('clientid', owner: 'otherUser');
430+
$currentClient = self::getClient('clientid', owner: 'homer@example.com');
431+
$this->repository->add($currentClient);
432+
$this->clientEntityFactoryMock->expects($this->exactly(2))->method('fromState')
433+
->willReturnOnConsecutiveCalls($staleClient, $currentClient);
434+
435+
$sut = $this->repositoryWithCacheHolding(['state']);
436+
437+
$this->assertSame($currentClient, $sut->findById('clientid', 'homer@example.com'));
438+
}
439+
440+
441+
protected function repositoryWithCacheHolding(array $state): ClientRepository
442+
{
443+
$protocolCacheMock = $this->createMock(ProtocolCache::class);
444+
$protocolCacheMock->method('get')->willReturn($state);
445+
446+
return new ClientRepository(
447+
new ModuleConfig(),
448+
Database::getInstance(),
449+
$protocolCacheMock,
450+
$this->clientEntityFactoryMock,
451+
);
452+
}
453+
454+
392455
public function testCanFindByEntityIdentifier(): void
393456
{
394457
$client = self::getClient(id: 'clientId', entityId: 'entityId');
@@ -448,6 +511,22 @@ public function testCanFindAllFederated(): void
448511
}
449512

450513

514+
/**
515+
* Their queries have a line break after WHERE, so the owner filter has to be added to them with AND.
516+
*/
517+
public function testTheQueriesWrittenOverSeveralLinesTakeAnOwner(): void
518+
{
519+
$client = self::getClient(id: 'clientId', owner: 'otherUser', entityId: 'entityId', federationJwks: []);
520+
$this->repository->add($client);
521+
$this->clientEntityFactoryMock->method('fromState')->willReturn($client);
522+
523+
$this->assertSame($client, $this->repository->findByEntityIdentifier('entityId', 'otherUser'));
524+
$this->assertNull($this->repository->findByEntityIdentifier('entityId', 'homer@example.com'));
525+
$this->assertCount(1, $this->repository->findAllFederated('otherUser'));
526+
$this->assertCount(0, $this->repository->findAllFederated('homer@example.com'));
527+
}
528+
529+
451530
public function testCanFindByEntityIdFromCache(): void
452531
{
453532
$protocolCacheMock = $this->createMock(ProtocolCache::class);

0 commit comments

Comments
 (0)