Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Security] Make request always available to #[IsGranted] #48080

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

Conversation

HypeMC
Copy link
Contributor

@HypeMC HypeMC commented Nov 2, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #48071 (comment)
License MIT
Doc PR -

Currently, the request is only available to the #[IsGranted] attribute when it's a controller argument, eg:

#[IsGranted(attribute: 'SOME_ATTRIBUTE', subject: 'request')]
public function index(Request $request)
{
}

#[IsGranted(
    attribute: 'SOME_ATTRIBUTE',
    subject: new Expression('args["request"].query.get("foo")'),
)]
public function index(Request $request)
{
}

However, since the $request variable might not always be needed in the controller itself, it seems kind of weird to have to add it as an argument just so the #[IsGranted] attribute could work. With this PR, the request will always be available to the attribute:

#[IsGranted(attribute: 'SOME_ATTRIBUTE', subject: 'request')]
public function index()
{
}

#[IsGranted(
    attribute: 'SOME_ATTRIBUTE',
    subject: new Expression('request.query.get("foo")'),
)]
public function index()
{
}

Don't know if this qualifies as a tweak for 6.2 or feature for 6.3.

@carsonbot carsonbot changed the title [Security] Make request always available to #[IsGranted] Make request always available to #[IsGranted] Nov 2, 2022
@carsonbot carsonbot changed the title Make request always available to #[IsGranted] [Security] Make request always available to #[IsGranted] Nov 2, 2022
@nicolas-grekas
Copy link
Member

Thank you @HypeMC.

@nicolas-grekas nicolas-grekas merged commit 97e08c3 into symfony:6.2 Nov 2, 2022
@nicolas-grekas
Copy link
Member

@HypeMC can you please send a PR to the doc to mention this?

@HypeMC HypeMC deleted the make-request-always-available-to-isgranted branch November 2, 2022 16:32
Copy link
Member

@stof stof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the case of using the request variable in an expression is not covered by tests

'args' => $arguments,
]);
}

if (!\array_key_exists($subjectRef, $arguments)) {
if ('request' === $subjectRef) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should add that special case. It can be surprising as strings mean argument names. Having special string values makes things harder to explain request means the current request only when you don't have an argument named $request in this code).
To me, voting on the Request object is something that case be solved by using the Expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, will create a followup PR to address this issue.

@HypeMC
Copy link
Contributor Author

HypeMC commented Nov 3, 2022

the case of using the request variable in an expression is not covered by tests

@stof There are two tests that check if the request variable is passed to the evaluate method, shouldn't that cover the usage? Since the ExpressionLanguage class is mocked, the expressions are never really evaluated, so it shouldn't matter whether the expression actually contains the request variable or not.

@stof
Copy link
Member

stof commented Nov 3, 2022

indeed, this is probably covered by your update of the existing tests using expressions.

chalasr added a commit that referenced this pull request Nov 4, 2022
…ect (HypeMC)

This PR was merged into the 6.2 branch.

Discussion
----------

[Security] Remove special case for `#[IsGranted()]` subject

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #48080 (comment)
| License       | MIT
| Doc PR        | -

Addresses a comment by `@stof`

Instead of having `request` as a special case, an expression can be used instead:

```diff
-#[IsGranted(attribute: 'SOME_ATTRIBUTE', subject: 'request')]
+#[IsGranted(attribute: 'SOME_ATTRIBUTE', subject: new Expression('request'))]
public function index()
{
}
```

Commits
-------

3e0ac4f [Security] Remove special case for #[IsGranted()] subject
fabpot added a commit to symfony/symfony-docs that referenced this pull request Mar 24, 2023
… (HypeMC)

This PR was merged into the 6.2 branch.

Discussion
----------

[Security] Use expression for `#[IsGranted()]` subject

symfony/symfony#46978
symfony/symfony#48080
symfony/symfony#48102

Commits
-------

9d4045f [Security] Use expression for #[IsGranted()] subject
weaverryan pushed a commit to symfony/symfony-docs that referenced this pull request Mar 28, 2023
… (HypeMC)

This PR was merged into the 6.2 branch.

Discussion
----------

[Security] Use expression for `#[IsGranted()]` subject

symfony/symfony#46978
symfony/symfony#48080
symfony/symfony#48102

Commits
-------

9d4045f [Security] Use expression for #[IsGranted()] subject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide same variables for IsGranted attribute as for SecurityAttribute
5 participants