-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Add the ability for voter to return decision reason #46493
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef68205
[Security] Add the ability for voter to return decision reason
alamirault 5e4733b
[Security] Vote can have multiple messages
alamirault a23e15b
Rebase for 6.3
alamirault 068a117
Fix rebase tests
alamirault 5a3bf54
Rebase for 7.1
alamirault 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
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
108 changes: 108 additions & 0 deletions
108
src/Symfony/Component/Security/Core/Authorization/A
F438
ccessDecision.php
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,108 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authorization; | ||
|
||
use Symfony\Component\Security\Core\Authorization\Voter\Vote; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; | ||
|
||
/** | ||
* An AccessDecision is returned by an AccessDecisionManager and contains the access verdict and all the related votes. | ||
* | ||
* @author Dany Maillard <danymaillard93b@gmail.com> | ||
*/ | ||
final class AccessDecision | ||
{ | ||
/** | ||
* @param int $access One of the VoterInterface::ACCESS_* constants | ||
* @param Vote[] $votes | ||
*/ | ||
private function __construct(private readonly int $access, private readonly array $votes = []) | ||
{ | ||
} | ||
|
||
public function getAccess(): int | ||
{ | ||
return $this->access; | ||
} | ||
|
||
public function isGranted(): bool | ||
{ | ||
return VoterInterface::ACCESS_GRANTED === $this->access; | ||
} | ||
|
||
public function isAbstain(): bool | ||
{ | ||
return VoterInterface::ACCESS_ABSTAIN === $this->access; | ||
} | ||
|
||
public function isDenied(): bool | ||
{ | ||
return VoterInterface::ACCESS_DENIED === $this->access; | ||
} | ||
|
||
/** | ||
* @param Vote[] $votes | ||
*/ | ||
public static function createGranted(array $votes = []): self | ||
{ | ||
return new self(VoterInterface::ACCESS_GRANTED, $votes); | ||
} | ||
|
||
/** | ||
* @param Vote[] $votes | ||
*/ | ||
public static function createDenied(array $votes = []): self | ||
{ | ||
return new self(VoterInterface::ACCESS_DENIED, $votes); | ||
} | ||
|
||
/** | ||
* @return Vote[] | ||
*/ | ||
public function getVotes(): array | ||
{ | ||
return $this->votes; | ||
} | ||
|
||
/** | ||
* @return Vote[] | ||
*/ | ||
public function getGrantedVotes(): array | ||
{ | ||
return $this->getVotesByAccess(Voter::ACCESS_GRANTED); | ||
} | ||
|
||
/** | ||
* @return Vote[] | ||
*/ | ||
public function getAbstainedVotes(): array | ||
{ | ||
return $this->getVotesByAccess(Voter::ACCESS_ABSTAIN); | ||
} | ||
|
||
/** | ||
* @return Vote[] | ||
*/ | ||
public function getDeniedVotes(): array | ||
{ | ||
return $this->getVotesByAccess(Voter::ACCESS_DENIED); | ||
} | ||
|
||
/** | ||
* @return Vote[] | ||
*/ | ||
private function getVotesByAccess(int $access): array | ||
{ | ||
return array_filter($this->votes, static fn (Vote $vote): bool => $vote->getAccess() === $access); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
use Symfony\Component\Security\Core\Authorization\Strategy\AccessDecisionStrategyInterface; | ||
use Symfony\Component\Security\Core\Authorization\Strategy\AffirmativeStrategy; | ||
use Symfony\Component\Security\Core\Authorization\Voter\CacheableVoterInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Vote; | ||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; | ||
use Symfony\Component\Security\Core\Exception\InvalidArgumentException; | ||
|
||
|
@@ -26,12 +27,6 @@ | |
*/ | ||
final class AccessDecisionManager implements AccessDecisionManagerInterface | ||
{ | ||
private const VALID_VOTES = [ | ||
VoterInterface::ACCESS_GRANTED => true, | ||
VoterInterface::ACCESS_DENIED => true, | ||
VoterInterface::ACCESS_ABSTAIN => true, | ||
]; | ||
|
||
private iterable $voters; | ||
private array $votersCacheAttributes = []; | ||
private array $votersCacheObject = []; | ||
|
@@ -46,11 +41,35 @@ public function __construct(iterable $voters = [], ?AccessDecisionStrategyInterf | |
$this->strategy = $strategy ?? new AffirmativeStrategy(); | ||
} | ||
|
||
public function getDecision(TokenInterface $token, array $attributes, mixed $object = null, bool $allowMultipleAttributes = false): AccessDecision | ||
{ | ||
// Special case for AccessListener, do not remove the right side of the condition before 6.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to remove then now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related to #46493 (comment) This was not removed on 6.x. I don't know why, and its impacts (CVE-2020-5275) |
||
if (\count($attributes) > 1 && !$allowMultipleAttributes) { | ||
throw new InvalidArgumentException(sprintf('Passing more than one Security attribute to "%s()" is not supported.', __METHOD__)); | ||
} | ||
|
||
if (method_exists($this->strategy, 'getDecision')) { | ||
$decision = $this->strategy->getDecision( | ||
$this->collectVotes($token, $attributes, $object) | ||
); | ||
} else { | ||
$decision = $this->strategy->decide( | ||
$this->collectResults($token, $attributes, $object) | ||
) ? AccessDecision::createGranted() : AccessDecision::createDenied(); | ||
} | ||
|
||
return $decision; | ||
} | ||
|
||
/** | ||
* @param bool $allowMultipleAttributes Whether to allow passing multiple values to the $attributes array | ||
* | ||
* @deprecated since Symfony 7.1, use {@see getDecision()} instead. | ||
*/ | ||
public function decide(TokenInterface $token, array $attributes, mixed $object = null, bool $allowMultipleAttributes = false): bool | ||
{ | ||
trigger_deprecation('symfony/security-core', '7.1', 'Method "%s::decide()" has been deprecated, use "%s::getDecision()" instead.', __CLASS__, __CLASS__); | ||
|
||
// Special case for AccessListener, do not remove the right side of the condition before 6.0 | ||
if (\count($attributes) > 1 && !$allowMultipleAttributes) { | ||
throw new InvalidArgumentException(sprintf('Passing more than one Security attribute to "%s()" is not supported.', __METHOD__)); | ||
|
@@ -62,17 +81,33 @@ public function decide(TokenInterface $token, array $attributes, mixed $object = | |
} | ||
|
||
/** | ||
* @return \Traversable<int, int> | ||
* @return \Traversable<int, Vote> | ||
*/ | ||
private function collectResults(TokenInterface $token, array $attributes, mixed $object): \Traversable | ||
private function collectVotes(TokenInterface $token, array $attributes, mixed $object): \Traversable | ||
{ | ||
foreach ($this->getVoters($attributes, $object) as $voter) { | ||
$result = $voter->vote($token, $object, $attributes); | ||
if (!\is_int($result) || !(self::VALID_VOTES[$result] ?? false)) { | ||
throw new \LogicException(sprintf('"%s::vote()" must return one of "%s" constants ("ACCESS_GRANTED", "ACCESS_DENIED" or "ACCESS_ABSTAIN"), "%s" returned.', get_debug_type($voter), VoterInterface::class, var_export($result, true))); | ||
if (method_exists($voter, 'getVote')) { | ||
yield $voter->getVote($token, $object, $attributes); | ||
} else { | ||
$result = $voter->vote($token, $object, $attributes); | ||
yield match ($result) { | ||
VoterInterface::ACCESS_GRANTED => Vote::createGranted(), | ||
VoterInterface::ACCESS_DENIED => Vote::createDenied(), | ||
VoterInterface::ACCESS_ABSTAIN => Vote::createAbstain(), | ||
default => throw new \LogicException(sprintf('"%s::vote()" must return one of "%s" constants ("ACCESS_GRANTED", "ACCESS_DENIED" or "ACCESS_ABSTAIN"), "%s" returned.', get_debug_type($voter), VoterInterface::class, var_export($result, true))), | ||
}; | ||
} | ||
} | ||
} | ||
|
||
yield $result; | ||
/** | ||
* @return \Traversable<int, int> | ||
*/ | ||
private function collectResults(TokenInterface $token, array $attributes, mixed $object): \Traversable | ||
{ | ||
/** @var Vote $vote */ | ||
foreach ($this->collectVotes($token, $attributes, $object) as $vote) { | ||
yield $vote->getAccess(); | ||
} | ||
} | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$allowMultipleAttributes
is indecide
method (but not in interface). I think we can remove this arg and the check WDYT ?Original comment:
Special case for AccessListener, do not remove the right side of the condition before 6.0
related to GHSA-g4m9-5hpf-hx72