-
-
Notifications
You must be signed in to change notification settings - Fork 915
feat(subresource): Link Security #5290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d980afd
[Link] Start Link Security
KDederichs 29f9937
feat(provider): Auto Resolve Get Operation and Parameters
KDederichs 9ce8b6d
chore(CS): fix CS
KDederichs 637167c
feat(tests): Add DenyAccessListener tests
KDederichs 7b1bbb2
feat(tests): Add link security behat tests
KDederichs f784b1e
fix(test): fix mongodb document configuration
KDederichs 26e7057
fix(readlistner): fix error 500 on not existing entity
KDederichs 81113bc
feat(linksecurity): expand functionality to cover all combinations of…
KDederichs d074632
feat(linksecurity): add more tests
KDederichs 288d057
chore: fix cs
KDederichs 07320f4
chore: phpstan fix
KDederichs d3aef92
Merge remote-tracking branch 'upstream/main' into feature/link_security
KDederichs 952fe31
Merge branch 'main_upstream' into feature/link_security
KDederichs 6b2c8b6
Merge remote-tracking branch 'upstream/main' into feature/link_security
KDederichs 5581f09
fix: Move logic to refactored, now used, classes
KDederichs 76e035b
fix: refactor unit tests
KDederichs 16f14c8
fix: backport for legacy event system as well
KDederichs c374794
Revert "fix: backport for legacy event system as well"
KDederichs 896b383
refactor: Refactor ReadProvider.php and AccessCheckerProvider.php to …
KDederichs ee98312
Merge branch 'main' into feature/link_security
KDederichs 0d315f7
mark providers final, disable feature by default
KDederichs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
|
||
<service id="api_platform.state_provider.read_link" class="ApiPlatform\Symfony\Security\State\LinkedReadProvider" decorates="api_platform.state_provider.read"> | ||
<argument type="service" id="api_platform.state_provider.read_link.inner" /> | ||
<argument type="service" id="api_platform.state_provider.locator" /> | ||
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" /> | ||
</service> | ||
|
||
<service id="api_platform.state_provider.access_checker_linked" class="ApiPlatform\Symfony\Security\State\LinkAccessCheckerProvider" decorates="api_platform.state_provider.read_link"> | ||
<argument type="service" id="api_platform.state_provider.access_checker_linked.inner" /> | ||
<argument type="service" id="api_platform.security.resource_access_checker" /> | ||
</service> | ||
</services> | ||
</container> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Symfony\Security\State; | ||
|
||
use ApiPlatform\Metadata\HttpOperation; | ||
use ApiPlatform\Metadata\Link; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
use ApiPlatform\Symfony\Security\Exception\AccessDeniedException; | ||
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface; | ||
|
||
/** | ||
* Checks the individual parts of the linked resource for access rights. | ||
* | ||
* @experimental | ||
*/ | ||
final class LinkAccessCheckerProvider implements ProviderInterface | ||
{ | ||
public function __construct( | ||
private readonly ProviderInterface $decorated, | ||
private readonly ResourceAccessCheckerInterface $resourceAccessChecker | ||
) { | ||
} | ||
|
||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
{ | ||
$request = ($context['request'] ?? null); | ||
|
||
$data = $this->decorated->provide($operation, $uriVariables, $context); | ||
|
||
if ($operation instanceof HttpOperation && $operation->getUriVariables()) { | ||
foreach ($operation->getUriVariables() as $uriVariable) { | ||
if (!$uriVariable instanceof Link || !$uriVariable->getSecurity()) { | ||
continue; | ||
} | ||
|
||
$targetResource = $uriVariable->getFromClass() ?? $uriVariable->getToClass(); | ||
|
||
if (!$targetResource) { | ||
continue; | ||
} | ||
|
||
$propertyName = $uriVariable->getToProperty() ?? $uriVariable->getFromProperty(); | ||
$securityObjectName = $uriVariable->getSecurityObjectName(); | ||
|
||
if (!$securityObjectName) { | ||
$securityObjectName = $propertyName; | ||
} | ||
|
||
if (!$securityObjectName) { | ||
continue; | ||
} | ||
|
||
$resourceAccessCheckerContext = [ | ||
'object' => $data, | ||
'previous_object' => $request?->attributes->get('previous_data'), | ||
$securityObjectName => $request?->attributes->get($securityObjectName), | ||
'request' => $request, | ||
]; | ||
|
||
if (!$this->resourceAccessChecker->isGranted($targetResource, $uriVariable->getSecurity(), $resourceAccessCheckerContext)) { | ||
throw new AccessDeniedException($uriVariable->getSecurityMessage() ?? 'Access Denied.'); | ||
} | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Symfony\Security\State; | ||
|
||
use ApiPlatform\Exception\InvalidIdentifierException; | ||
use ApiPlatform\Exception\InvalidUriVariableException; | ||
use ApiPlatform\Metadata\HttpOperation; | ||
use ApiPlatform\Metadata\Link; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||
use ApiPlatform\State\Exception\ProviderNotFoundException; | ||
use ApiPlatform\State\ProviderInterface; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
/** | ||
* Checks if the linked resources have security attributes and prepares them for access checking. | ||
* | ||
* @experimental | ||
*/ | ||
final class LinkedReadProvider implements ProviderInterface | ||
{ | ||
public function __construct( | ||
private readonly ProviderInterface $decorated, | ||
private readonly ProviderInterface $locator, | ||
private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory | ||
) { | ||
} | ||
|
||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
{ | ||
$data = $this->decorated->provide($operation, $uriVariables, $context); | ||
|
||
if (!$operation instanceof HttpOperation) { | ||
return $data; | ||
} | ||
|
||
$request = ($context['request'] ?? null); | ||
|
||
if ($operation->getUriVariables()) { | ||
foreach ($operation->getUriVariables() as $key => $uriVariable) { | ||
if (!$uriVariable instanceof Link || !$uriVariable->getSecurity()) { | ||
continue; | ||
} | ||
|
||
$relationClass = $uriVariable->getFromClass() ?? $uriVariable->getToClass(); | ||
|
||
if (!$relationClass) { | ||
continue; | ||
} | ||
|
||
$parentOperation = $this->resourceMetadataCollectionFactory | ||
->create($relationClass) | ||
->getOperation($operation->getExtraProperties()['parent_uri_template'] ?? null); | ||
try { | ||
$relation = $this->locator->provide($parentOperation, [$uriVariable->getIdentifiers()[0] => $request?->attributes->all()[$key]], $context); | ||
} catch (ProviderNotFoundException) { | ||
$relation = null; | ||
} | ||
|
||
if (!$relation) { | ||
throw new NotFoundHttpException('Relation for link security not found.'); | ||
} | ||
|
||
try { | ||
$securityObjectName = $uriVariable->getSecurityObjectName(); | ||
|
||
if (!$securityObjectName) { | ||
$securityObjectName = $uriVariable->getToProperty() ?? $uriVariable->getFromProperty(); | ||
} | ||
|
||
$request?->attributes->set($securityObjectName, $relation); | ||
} catch (InvalidIdentifierException|InvalidUriVariableException $e) { | ||
throw new NotFoundHttpException('Invalid identifier value or configuration.', $e); | ||
} | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.