Skip to content

Commit 869eea6

Browse files
committed
Apply the owner filter to clients taken from the cache
1 parent 8f59c77 commit 869eea6

4 files changed

Lines changed: 129 additions & 4 deletions

File tree

‎src/Controllers/Admin/ClientController.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ public function edit(Request $request): Response
266266
$this->logger->warning($message, $updatedClient->getState());
267267
$this->sessionMessagesService->addMessage($message);
268268
} else {
269-
$this->clientRepository->update($updatedClient);
269+
$authedUserId = $this->authorization->isAdmin() ? null : $this->authorization->getUserId();
270+
$this->clientRepository->update($updatedClient, $authedUserId);
270271

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

‎src/Repositories/ClientRepository.php‎

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

9999
if (is_array($cachedState)) {
100-
return $this->clientEntityFactory->fromState($cachedState);
100+
$clientEntity = $this->clientEntityFactory->fromState($cachedState);
101+
102+
if ($this->isOwnedBy($clientEntity, $owner)) {
103+
return $clientEntity;
104+
}
101105
}
102106

103107
/**
@@ -143,7 +147,11 @@ public function findByEntityIdentifier(string $entityIdentifier, ?string $owner
143147
$cachedState = $this->protocolCache?->get(null, $this->getCacheKey($entityIdentifier));
144148

145149
if (is_array($cachedState)) {
146-
return $this->clientEntityFactory->fromState($cachedState);
150+
$clientEntity = $this->clientEntityFactory->fromState($cachedState);
151+
152+
if ($this->isOwnedBy($clientEntity, $owner)) {
153+
return $clientEntity;
154+
}
147155
}
148156

149157
/**
@@ -210,11 +218,23 @@ public function findFederatedByEntityIdentifier(
210218
return $clientEntity;
211219
}
212220

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

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,30 @@ public function testCanEdit(): void
396396
$this->sut()->edit($request);
397397
}
398398

399+
/**
400+
* A user managing their own clients saves their client with the owner filter, as a secret reset and a delete
401+
* do; an administrator saves any client.
402+
*/
403+
public function testAnEditIsSavedForTheOwnerOnly(): void
404+
{
405+
$request = Request::create('/edit?client_id=clientId', 'GET', ['client_id' => 'clientId']);
406+
$this->authorizationMock->method('isAdmin')->willReturn(false);
407+
$this->authorizationMock->method('getUserId')->willReturn('user@example.org');
408+
$this->clientEntityMock->method('getRegistrationType')->willReturn(RegistrationTypeEnum::Manual);
409+
$this->clientEntityMock->method('getIdentifier')->willReturn('clientId');
410+
$this->clientRepositoryMock->expects($this->once())->method('findById')
411+
->with('clientId', 'user@example.org')->willReturn($this->clientEntityMock);
412+
$updatedClientMock = $this->createMock(ClientEntityInterface::class);
413+
$this->clientFormMock->method('isSuccess')->willReturn(true);
414+
$this->clientFormMock->method('getValues')->willReturn($this->sampleFormData);
415+
$this->clientEntityFactoryMock->method('fromData')->willReturn($updatedClientMock);
416+
417+
$this->clientRepositoryMock->expects($this->once())->method('update')
418+
->with($updatedClientMock, 'user@example.org');
419+
420+
$this->sut()->edit($request);
421+
}
422+
399423
public function testWontEditIfClientEntityIdentifierExists(): void
400424
{
401425
$request = Request::create(

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,65 @@ public function testCanFindByIdFromCache(): void
389389
$this->assertInstanceOf(ClientEntityInterface::class, $sut->findById('clientid'));
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+
public function testACachedClientIsNotReturnedToALookupByEntityIdentifierForAnotherOwner(): void
410+
{
411+
$client = self::getClient('clientid', owner: 'otherUser', entityId: 'entityId');
412+
$this->repository->add($client);
413+
$this->clientEntityFactoryMock->method('fromState')->willReturn($client);
414+
415+
$sut = $this->repositoryWithCacheHolding(['state']);
416+
417+
$this->assertNull($sut->findByEntityIdentifier('entityId', 'homer@example.com'));
418+
$this->assertSame($client, $sut->findByEntityIdentifier('entityId', 'otherUser'));
419+
$this->assertSame($client, $sut->findByEntityIdentifier('entityId'));
420+
}
421+
422+
/**
423+
* A cached client with another owner is looked up in the database, which has the owner the client has now.
424+
*/
425+
public function testTheDatabaseAnswersForAnOwnerTheCachedClientDoesNotHave(): void
426+
{
427+
$staleClient = self::getClient('clientid', owner: 'otherUser');
428+
$currentClient = self::getClient('clientid', owner: 'homer@example.com');
429+
$this->repository->add($currentClient);
430+
$this->clientEntityFactoryMock->expects($this->exactly(2))->method('fromState')
431+
->willReturnOnConsecutiveCalls($staleClient, $currentClient);
432+
433+
$sut = $this->repositoryWithCacheHolding(['state']);
434+
435+
$this->assertSame($currentClient, $sut->findById('clientid', 'homer@example.com'));
436+
}
437+
438+
protected function repositoryWithCacheHolding(array $state): ClientRepository
439+
{
440+
$protocolCacheMock = $this->createMock(ProtocolCache::class);
441+
$protocolCacheMock->method('get')->willReturn($state);
442+
443+
return new ClientRepository(
444+
new ModuleConfig(),
445+
Database::getInstance(),
446+
$protocolCacheMock,
447+
$this->clientEntityFactoryMock,
448+
);
449+
}
450+
392451
public function testCanFindByEntityIdentifier(): void
393452
{
394453
$client = self::getClient(id: 'clientId', entityId: 'entityId');
@@ -444,6 +503,27 @@ public function testCanFindAllFederated(): void
444503
$this->assertCount(1, $this->repository->findAllFederated());
445504
}
446505

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

0 commit comments

Comments
 (0)