Skip to content

Commit 9efd9e4

Browse files
Merge pull request #450 from components-web-app/fix/447-component-voter-duplicate-keys
Keep every page ComponentVoter finds for a component (#447)
2 parents 1625424 + 7cab768 commit 9efd9e4

6 files changed

Lines changed: 160 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Every change that reaches `main` adds a line under **Unreleased**. When a versio
99

1010
### Fixed
1111
- Memory no longer grows request after request in a worker (FrankenPHP worker mode). `ResourceMetadataProvider` kept every serialised resource and its `_metadata`, and API Platform's skolem IRI converter, which is not tagged `kernel.reset` (api-platform/core#8681), kept every `_metadata` object it gave a `/.well-known/genid/` IRI, each keeping the other's objects alive. Both are now reset between requests. `PropagateUpdatesListener` is reset too, so a flush that fails no longer leaves its entities to be purged and published by the next request's flush, and the user email factories no longer keep the last user they emailed ([#449](https://github.com/components-web-app/api-components-bundle/pull/449))
12+
- A component on a live routed page is readable anonymously when it is also placed on an unrouted page. `ComponentVoter` collected the component's pages with `iterator_to_array()`, which kept the repeated keys of the collections it `yield from`s, so a later page replaced the routed one and the visitor got a 401 ([#450](https://github.com/components-web-app/api-components-bundle/pull/450))
1213

1314
## [2.0.0-alpha.20](https://github.com/components-web-app/api-components-bundle/compare/2.0.0-alpha.19...2.0.0-alpha.20) - 2026-10-13
1415

‎CLAUDE.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ API Platform routes are prefixed by `RoutingPrefixResourceMetadataCollectionFact
169169

170170
A Route reaches its own page **and every ancestor** through `parentPage`/`parentPageData`. `RouteReachabilityResolver` finds, for a routable with no route, any descendant route that passes `RouteVoter` (two-repository `findBy`, visited-id set, early exit, memoised). Used by `RoutableVoter` (which also checks page data using a `Page` as template) and `ComponentVoter::voteByRoute`.
171171

172+
- **Never `iterator_to_array()` a generator built with `yield from` without `false`** (#447): each inner collection's keys start at 0, so later items replace earlier ones. `ComponentVoter` lost a component's routed page that way when the component was also on an unrouted page.
172173
- `ComponentVoter::voteByRoute` abstains (public) for a component in **no** page and denies for a component in pages none of which are reachable, falling through to `routable_security` so admins still see it. Denying both broke 86 scenarios with unplaced components.
173174
- Reachability is not denormalised. A column cannot express `route_security`, and an edge table was built and rejected (maintained state, rebuild command, upgrade step). A routeless page is absent from `GET /_/pages`.
174175
- **`AbstractPage::$isReachableWithoutRoute` (#381) applies only to a page with no route**, and stands in for a live route at that node of the same walk, so a flagged page reaches its ancestors and its components, page data template and manifest follow with no voter change. A page with a route ignores it: its route decides (`liveAt`, draft included, and `route_security`). A flagged routeless page is still held back unless its **nearest routed ancestor** passes `RouteVoter`, exactly as for a routed child. Admin-only on read and write (`ApiProperty` security, like `Route.liveAt`); a flagged page stays out of `GET /_/pages`. The flag on a template does not reach its page data. `reachable_without_route.feature` is the authority.

‎features/bootstrap/DoctrineContext.php‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,49 @@ public function thereIsARoutelessParentPageDataWithARoutedChild(string $path): v
957957
$this->manager->clear();
958958
}
959959

960+
/**
961+
* @Given there is a component on the routed Page :path that is also placed on a routeless Page
962+
*/
963+
public function thereIsAComponentOnARoutedPageAlsoPlacedOnARoutelessPage(string $path): void
964+
{
965+
$routedPage = new Page();
966+
$routedPage->isTemplate = false;
967+
$routedPage->reference = 'shared component routed page';
968+
$routedPage->addComponentGroup($this->buildReachabilityComponentGroup('routed page group'));
969+
$this->timestampedHelper->persistTimestampedFields($routedPage, true);
970+
$this->manager->persist($routedPage);
971+
972+
$route = new Route();
973+
$route->setPath($path)->setName($path)->setPage($routedPage);
974+
$this->timestampedHelper->persistTimestampedFields($route, true);
975+
$this->manager->persist($route);
976+
$this->manager->flush();
977+
978+
$component = $this->iriConverter->getResourceFromIri($this->restContext->resources['parent_component']);
979+
$routelessGroup = new ComponentGroup();
980+
$routelessGroup->reference = 'routeless page group';
981+
$routelessGroup->location = 'routeless page group';
982+
$this->timestampedHelper->persistTimestampedFields($routelessGroup, true);
983+
$this->manager->persist($routelessGroup);
984+
985+
$position = new ComponentPosition();
986+
$position->componentGroup = $routelessGroup;
987+
$position->component = $component;
988+
$position->sortValue = 0;
989+
$this->timestampedHelper->persistTimestampedFields($position, true);
990+
$this->manager->persist($position);
991+
992+
$routelessPage = new Page();
993+
$routelessPage->isTemplate = false;
994+
$routelessPage->reference = 'shared component routeless page';
995+
$routelessPage->addComponentGroup($routelessGroup);
996+
$this->timestampedHelper->persistTimestampedFields($routelessPage, true);
997+
$this->manager->persist($routelessPage);
998+
999+
$this->manager->flush();
1000+
$this->manager->clear();
1001+
}
1002+
9601003
/**
9611004
* @Given there is a routeless Page with a component and no routed descendant
9621005
*/

‎features/main/security.feature‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ Feature: Restrict loading of components and routes
237237
When I send a "GET" request to the resource "parent_component"
238238
Then the response status code should be 200
239239

240+
Scenario: A component on a routed page is readable anonymously when it is also placed on a routeless page
241+
Given there is a component on the routed Page "/shared-component" that is also placed on a routeless Page
242+
When I send a "GET" request to the resource "parent_component"
243+
Then the response status code should be 200
244+
240245
Scenario: A component in a routeless page which nothing routes to is not readable anonymously
241246
Given there is a routeless Page with a component and no routed descendant
242247
When I send a "GET" request to the resource "parent_component"

‎src/Security/Voter/ComponentVoter.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function voteOnAttribute(string $attribute, mixed $subject, TokenInter
7171
$subject = $this->getPublishedSubject($subject);
7272

7373
$pagesGenerator = $this->getComponentPages($subject);
74-
$pages = iterator_to_array($pagesGenerator);
74+
$pages = iterator_to_array($pagesGenerator, false);
7575

7676
$routeVoteResult = $this->voteByRoute($pages);
7777
if ($routeVoteResult) {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Silverback API Components Bundle Project
5+
*
6+
* (c) Daniel West <daniel@silverback.is>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Silverback\ApiComponentsBundle\Tests\Security\Voter;
13+
14+
use ApiPlatform\Metadata\IriConverterInterface;
15+
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
16+
use Doctrine\Common\Collections\ArrayCollection;
17+
use Doctrine\Persistence\ManagerRegistry;
18+
use PHPUnit\Framework\TestCase;
19+
use Silverback\ApiComponentsBundle\AttributeReader\PublishableAttributeReader;
20+
use Silverback\ApiComponentsBundle\DataProvider\PageDataProvider;
21+
use Silverback\ApiComponentsBundle\Entity\Core\ComponentGroup;
22+
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition;
23+
use Silverback\ApiComponentsBundle\Entity\Core\Page;
24+
use Silverback\ApiComponentsBundle\Entity\Core\Route;
25+
use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker;
26+
use Silverback\ApiComponentsBundle\Helper\Route\RouteReachabilityResolver;
27+
use Silverback\ApiComponentsBundle\Security\Voter\ComponentVoter;
28+
use Silverback\ApiComponentsBundle\Tests\Functional\TestBundle\Entity\DummyComponent;
29+
use Symfony\Bundle\SecurityBundle\Security;
30+
use Symfony\Component\HttpFoundation\Request;
31+
use Symfony\Component\HttpFoundation\RequestStack;
32+
use Symfony\Component\HttpKernel\HttpKernelInterface;
33+
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
34+
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
35+
36+
class ComponentVoterTest extends TestCase
37+
{
38+
public function test_a_component_on_a_routed_page_is_granted_whichever_other_pages_it_is_also_on(): void
39+
{
40+
$routedPage = $this->page(new Route());
41+
$routelessPage = $this->page(null);
42+
43+
self::assertSame(VoterInterface::ACCESS_GRANTED, $this->vote($this->component([$routedPage], [$routelessPage]), $routedPage));
44+
self::assertSame(VoterInterface::ACCESS_GRANTED, $this->vote($this->component([$routelessPage], [$routedPage]), $routedPage));
45+
}
46+
47+
public function test_a_component_only_on_routeless_pages_is_denied(): void
48+
{
49+
$routedPage = $this->page(new Route());
50+
51+
self::assertSame(VoterInterface::ACCESS_DENIED, $this->vote($this->component([$this->page(null)], [$this->page(null)]), $routedPage));
52+
}
53+
54+
private function vote(DummyComponent $component, Page $routedPage): int
55+
{
56+
$security = $this->createStub(Security::class);
57+
$security->method('isGranted')->willReturnCallback(static fn (string $attribute, mixed $subject) => $subject === $routedPage->getRoute());
58+
$pageDataProvider = $this->createStub(PageDataProvider::class);
59+
$pageDataProvider->method('findPageDataComponentMetadata')->willReturn([]);
60+
$pageDataProvider->method('findPageDataResourcesByPages')->willReturn([]);
61+
$accessChecker = $this->createStub(ResourceAccessCheckerInterface::class);
62+
$accessChecker->method('isGranted')->willReturn(false);
63+
$publishableStatusChecker = $this->createStub(PublishableStatusChecker::class);
64+
$publishableStatusChecker->method('getAttributeReader')->willReturn(new PublishableAttributeReader($this->createStub(ManagerRegistry::class)));
65+
$requestStack = new RequestStack();
66+
$requestStack->push(Request::create('/component/dummy_components/1'));
67+
68+
$voter = new ComponentVoter(
69+
$pageDataProvider,
70+
$this->createStub(IriConverterInterface::class),
71+
$this->createStub(HttpKernelInterface::class),
72+
$requestStack,
73+
$publishableStatusChecker,
74+
$this->createStub(ManagerRegistry::class),
75+
new RouteReachabilityResolver($this->createStub(ManagerRegistry::class), $security),
76+
$accessChecker,
77+
"is_granted('ROLE_ADMIN')",
78+
);
79+
80+
return $voter->vote(new NullToken(), $component, [ComponentVoter::READ_COMPONENT]);
81+
}
82+
83+
/**
84+
* @param list<Page> ...$pagesPerGroup
85+
*/
86+
private function component(array ...$pagesPerGroup): DummyComponent
87+
{
88+
$component = new DummyComponent();
89+
foreach ($pagesPerGroup as $pages) {
90+
$group = new ComponentGroup();
91+
$group->pages = new ArrayCollection($pages);
92+
$position = new ComponentPosition();
93+
$position->componentGroup = $group;
94+
$component->addComponentPosition($position);
95+
}
96+
97+
return $component;
98+
}
99+
100+
private function page(?Route $route): Page
101+
{
102+
$page = new Page();
103+
if ($route) {
104+
$page->setRoute($route);
105+
}
106+
107+
return $page;
108+
}
109+
}

0 commit comments

Comments
 (0)