From b184401ebe369cbae174965d94244c9156228746 Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Tue, 12 Mar 2024 11:20:58 +0100 Subject: [PATCH] [Security] Correctly initialize the voter property --- .../DataCollector/SecurityDataCollector.php | 1 + .../SecurityDataCollectorTest.php | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php b/src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php index 01eea81a38315..72c76964bcbde 100644 --- a/src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php +++ b/src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php @@ -145,6 +145,7 @@ public function collect(Request $request, Response $response, ?\Throwable $excep // collect voters and access decision manager information if ($this->accessDecisionManager instanceof TraceableAccessDecisionManager) { $this->data['voter_strategy'] = $this->accessDecisionManager->getStrategy(); + $this->data['voters'] = []; foreach ($this->accessDecisionManager->getVoters() as $voter) { if ($voter instanceof TraceableVoter) { diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php b/src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php index ae706830738f3..ea70292f8dca7 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php @@ -400,6 +400,36 @@ public function dispatch(object $event, ?string $eventName = null): object $this->assertSame($dataCollector->getVoterStrategy(), $strategy, 'Wrong value returned by getVoterStrategy'); } + public function testGetVotersIfAccessDecisionManagerHasNoVoters() + { + $strategy = MainConfiguration::STRATEGY_AFFIRMATIVE; + + $accessDecisionManager = $this->createMock(TraceableAccessDecisionManager::class); + + $accessDecisionManager + ->method('getStrategy') + ->willReturn($strategy); + + $accessDecisionManager + ->method('getVoters') + ->willReturn([]); + + $accessDecisionManager + ->method('getDecisionLog') + ->willReturn([[ + 'attributes' => ['view'], + 'object' => new \stdClass(), + 'result' => true, + 'voterDetails' => [], + ]]); + + $dataCollector = new SecurityDataCollector(null, null, null, $accessDecisionManager, null, null, true); + + $dataCollector->collect(new Request(), new Response()); + + $this->assertEmpty($dataCollector->getVoters()); + } + public static function provideRoles(): array { return [