Skip to content

Commit 29e9d6b

Browse files
committed
Require the publishable permission to write CWA content (#569)
1 parent d4d41e6 commit 29e9d6b

24 files changed

Lines changed: 694 additions & 170 deletions

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Every change that reaches `main` adds a line under **Unreleased**. When a versio
44

55
## Unreleased
66

7+
### Breaking
8+
- Writing CWA content (pages, layouts, routes, component groups and their order, component positions, every component including `/move`, and every page data class, project classes included) requires `publishable.permission` (or the class's `#[Publishable(isGranted:)]`), ANDed with any `security` the operation already has; opt an operation or resource out with `extraProperties: ['publishable_permission' => false]`. Form `/submit` stays public ([#569](https://github.com/components-web-app/api-components-bundle/issues/569))
9+
710
## [2.0.0-alpha.39](https://github.com/components-web-app/api-components-bundle/compare/2.0.0-alpha.38...2.0.0-alpha.39) - 2026-10-30
811

912
### Added

‎CLAUDE.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ Resources are fetched and cached **individually**. Never embed related data outs
294294

295295
### Security and users
296296

297+
- **Writing CWA content needs the publishable permission, whatever the application's access rules** (#569). `ContentWriteSecurityResourceMetadataCollectionFactory`, the outermost bundle metadata decorator (decoration priority -1, so it sees every operation the other bundle factories add, `/move` and uploads included), gives every `POST`/`PUT`/`PATCH`/`DELETE` operation of `Page`, `Layout`, `Route`, `ComponentGroup`, `ComponentGroupOrder`, `ComponentPosition` and every `AbstractComponent` and `AbstractPageData` subclass, project classes included, the `security` `publishable.permission`, or the class's own `#[Publishable(isGranted:)]` when it sets one (the permission `PublishableStatusChecker` resolves for the class). It goes in `security`, not `securityPostDenormalize`: the expression needs no object, and `security` runs before deserialization, so a refused write never reaches the processors. **An operation's own `security` is ANDed with it, never replaced and never an opt-out**: `Route`'s write operations carry `read_route` and `RoutableResourceMetadataCollectionFactory` gives page and page data writes `read_routable` (and `routable_security` on `POST`), which are read checks that granted any reader the write. **The opt-out is `extraProperties: [ContentWriteSecurityResourceMetadataCollectionFactory::OPT_OUT_EXTRA_PROPERTY => false]`** on an operation, or on `#[ApiResource]` for every operation of the resource; then the operation's own `security` (or none) is all that applies. Form `/submit` (`POST` and the validating `PATCH`) opts out, and the test bundle's `DummyPublishableComponent` does, so the publishable tests can still write as a user and anonymously. Reads are untouched. `ROLE_USER` never implies editing: a test that writes content logs in as an admin. `Security\ContentWriteSecurityTest` is the authority.
297298
- **A `PUT` on a user merges, never replaces** (#426). API Platform's `standard_put` defaults to `true` on 4.x and 5.x: the body is denormalized into a new object and every property is copied onto the stored one, including those nobody can write (password hash, tokens), so they took the constructor's defaults. `UserResourceMetadataCollectionFactory` sets `standard_put: false` on every `Put` of an `AbstractUser`, over any application default. `User\UserPutTest` pins it.
298299
- **User email links** (#316): `RefererUrlResolver` uses `Origin`, else `Referer`, reduced to `scheme://host[:port]`, only if it matches `user.email_links.allowed_origins` (anchored by the bundle as `{\A(?:p)\z}i`), else `default_origin`, else the link is refused. The request host is not implicitly trusted. Request input may only be a `RelativeUrlPath`. **Never build an outbound link from a request header without an allow-list.**
299300
- **A refused email link depends on which kind of email it is** (#326). Where the email is the job (password reset, both resends), the request is a 400 that changes nothing: build and send the email before flushing the token it carries. After a completed write (every email from `UserWriteNotifier::notify()`), the write keeps its status and `UserMailer::afterWrite()` logs the refusal at error; the verify and change-email emails serve both kinds, so the notifier must call their `…AfterWrite` variants. Only `UnparseableRequestHeaderException` is caught.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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\ApiPlatform\Metadata\Resource;
13+
14+
use ApiPlatform\Metadata\HttpOperation;
15+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
16+
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
17+
use Silverback\ApiComponentsBundle\ApiResource\ComponentGroupOrder;
18+
use Silverback\ApiComponentsBundle\AttributeReader\PublishableAttributeReader;
19+
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
20+
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData;
21+
use Silverback\ApiComponentsBundle\Entity\Core\ComponentGroup;
22+
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition;
23+
use Silverback\ApiComponentsBundle\Entity\Core\Layout;
24+
use Silverback\ApiComponentsBundle\Entity\Core\Page;
25+
use Silverback\ApiComponentsBundle\Entity\Core\Route;
26+
27+
/**
28+
* @author Daniel West <daniel@silverback.is>
29+
*/
30+
final readonly class ContentWriteSecurityResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
31+
{
32+
public const string OPT_OUT_EXTRA_PROPERTY = 'publishable_permission';
33+
34+
private const array CONTENT_CLASSES = [
35+
Page::class,
36+
Layout::class,
37+
Route::class,
38+
ComponentGroup::class,
39+
ComponentGroupOrder::class,
40+
ComponentPosition::class,
41+
AbstractComponent::class,
42+
AbstractPageData::class,
43+
];
44+
45+
private const array WRITE_METHODS = [
46+
HttpOperation::METHOD_POST,
47+
HttpOperation::METHOD_PUT,
48+
HttpOperation::METHOD_PATCH,
49+
HttpOperation::METHOD_DELETE,
50+
];
51+
52+
public function __construct(
53+
private ResourceMetadataCollectionFactoryInterface $decorated,
54+
private PublishableAttributeReader $publishableAttributeReader,
55+
private string $permission,
56+
) {
57+
}
58+
59+
public function create(string $resourceClass): ResourceMetadataCollection
60+
{
61+
$resourceMetadataCollection = $this->decorated->create($resourceClass);
62+
63+
if (!$this->isContent($resourceClass)) {
64+
return $resourceMetadataCollection;
65+
}
66+
67+
$permission = $this->permission($resourceClass);
68+
foreach ($resourceMetadataCollection as $i => $resource) {
69+
$operations = $resource->getOperations();
70+
if (null === $operations) {
71+
continue;
72+
}
73+
foreach ($operations as $key => $operation) {
74+
if (!\in_array($operation->getMethod(), self::WRITE_METHODS, true) || false === ($operation->getExtraProperties()[self::OPT_OUT_EXTRA_PROPERTY] ?? true)) {
75+
continue;
76+
}
77+
$operations->add($key, $operation->withSecurity(self::security($permission, $operation->getSecurity())));
78+
}
79+
$resourceMetadataCollection[$i] = $resource->withOperations($operations);
80+
}
81+
82+
return $resourceMetadataCollection;
83+
}
84+
85+
private function isContent(string $resourceClass): bool
86+
{
87+
foreach (self::CONTENT_CLASSES as $contentClass) {
88+
if (is_a($resourceClass, $contentClass, true)) {
89+
return true;
90+
}
91+
}
92+
93+
return false;
94+
}
95+
96+
private function permission(string $resourceClass): string
97+
{
98+
if (!$this->publishableAttributeReader->isConfigured($resourceClass)) {
99+
return $this->permission;
100+
}
101+
102+
return $this->publishableAttributeReader->getConfiguration($resourceClass)->isGranted ?? $this->permission;
103+
}
104+
105+
private static function security(string $permission, ?string $security): string
106+
{
107+
if (null === $security || '' === $security) {
108+
return $permission;
109+
}
110+
111+
return \sprintf('(%s) and (%s)', $permission, $security);
112+
}
113+
}

‎src/DependencyInjection/SilverbackApiComponentsExtension.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Persistence\ManagerRegistry;
1515
use Ramsey\Uuid\Doctrine\UuidType;
16+
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\ContentWriteSecurityResourceMetadataCollectionFactory;
1617
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\RoutableResourceMetadataCollectionFactory;
1718
use Silverback\ApiComponentsBundle\AttributeReader\UploadableAttributeReader;
1819
use Silverback\ApiComponentsBundle\Command\ImagineGenerateCommand;
@@ -277,6 +278,9 @@ public function load(array $configs, ContainerBuilder $container): void
277278
$definition = $container->findDefinition(RoutableResourceMetadataCollectionFactory::class);
278279
$definition->setArgument('$securityStr', $config['routable_security']);
279280

281+
$definition = $container->findDefinition(ContentWriteSecurityResourceMetadataCollectionFactory::class);
282+
$definition->setArgument('$permission', $config['publishable']['permission']);
283+
280284
$definition = $container->findDefinition(MercureAuthorization::class);
281285
$definition->setArgument('$cookieSameSite', $config['mercure']['cookie']['samesite']);
282286
$definition->setArgument('$hubName', $config['mercure']['hub_name']);

‎src/Entity/Component/Form.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ApiPlatform\Metadata\Put;
2222
use Doctrine\ORM\Mapping as ORM;
2323
use Silverback\ApiComponentsBundle\Annotation as Silverback;
24+
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\ContentWriteSecurityResourceMetadataCollectionFactory;
2425
use Silverback\ApiComponentsBundle\DataProvider\StateProvider\FormStateProvider;
2526
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
2627
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait;
@@ -52,6 +53,7 @@
5253
write: false,
5354
serialize: true,
5455
requirements: ['id' => '[^/]+'],
56+
extraProperties: [ContentWriteSecurityResourceMetadataCollectionFactory::OPT_OUT_EXTRA_PROPERTY => false],
5557
),
5658
new Post(
5759
name: '_api_/forms/{id}/submit{._format}_post',
@@ -62,6 +64,7 @@
6264
write: false,
6365
serialize: true,
6466
requirements: ['id' => '[^/]+'],
67+
extraProperties: [ContentWriteSecurityResourceMetadataCollectionFactory::OPT_OUT_EXTRA_PROPERTY => false],
6568
),
6669
]
6770
)]

‎src/Resources/config/services.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Property\ComponentPropertyMetadataFactory;
3535
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Property\ImagineFiltersPropertyMetadataFactory;
3636
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\ComponentResourceMetadataFactory;
37+
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\ContentWriteSecurityResourceMetadataCollectionFactory;
3738
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\RoutableResourceMetadataCollectionFactory;
3839
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\RoutingPrefixResourceMetadataCollectionFactory;
3940
use Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource\UploadableResourceMetadataCollectionFactory;
@@ -2038,6 +2039,18 @@
20382039
);
20392040
$services->alias(UserResourceMetadataCollectionFactory::class, 'silverback.api_components.api_platform.metadata.resource.user_factory');
20402041

2042+
$services
2043+
->set('silverback.api_components.api_platform.metadata.resource.content_write_security_factory')
2044+
->class(ContentWriteSecurityResourceMetadataCollectionFactory::class)
2045+
->decorate('api_platform.metadata.resource.metadata_collection_factory', null, -1)
2046+
->args(
2047+
[
2048+
new Reference('silverback.api_components.api_platform.metadata.resource.content_write_security_factory.inner'),
2049+
new Reference(PublishableAttributeReader::class),
2050+
]
2051+
);
2052+
$services->alias(ContentWriteSecurityResourceMetadataCollectionFactory::class, 'silverback.api_components.api_platform.metadata.resource.content_write_security_factory');
2053+
20412054
$services
20422055
->set(VerifyEmailAddressAction::class)
20432056
->public()

0 commit comments

Comments
 (0)