-
-
Notifications
You must be signed in to change notification settings - Fork 266
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
Why perform validation method before authorization verification ? #481
Comments
There are pros and cons with both implementations. If we perform authorization before validation then there might be inputs that should not be there. Therefore, implementing this method will just replace one problem with another. |
If I remember correctly in classic laravel http request usually the authorization it is performed before the input validations. |
Yes, Laravel authorizes before validation. No, we should not make it more complicated by providing a configuration option. I'm on the fence about which method to use, so let's wait for @mfn to provide his input. He's on vacay right now 🌴 |
@rebing Can $context be added as a second argument to the |
@fvdung Yes, but it would be a breaking change |
This is not unambiguously clear. If we stick to Laravel, what you define in your middleware is the authentication, which we can thus say it's always performed before any validation. Inside your request, you then usually validate your request data and then decide if the request is authorized. Because it's not unreasonable to expect to have to use the request data to actually figure out if you are allowed to access something. In other words: for non trivial tasks, you need to consider the input request to decide if a user is allowed to do something ("authorization"), hence validation before authorization makes sense.
This sounds reasonable to me; I was thinking about this with all the refactoring lately when I touched the code surrounding it but haven't gotten around doing it.
Are we sure? I'd love following semver with this package, but adding a new argument which is simply ignored for existing code shouldn't be a problem, or is it? If so, I'd love to get some example code to better understand this, thanks 😄 My TL;DR
|
@mfn Nice write up, I guess it's decided that we stay with validation before authorization as it is for now. Regarding add context, I'm not sure yet how we could do it without a breaking change. We have to modify
to
where If we remove the function from Perhaps someone has a better idea? |
🤦♂️ Absolutely correct, it's a breaking change. Totally forgot the contract. There was never any talk (AFAIK) about semver until I mentioned it today. Personally I'd really love do it, which in fact means: the next release would be 3.0 But there was never a discussion if this project should even do it, if we all want it, etc. Any feelings about this? FTR: https://semver.org/ |
I think that we should follow semver guidelines and release 3.0 as the next version. Unless there is a better way of handling the authorization without a breaking change. |
I think adding the An idea for this is to build an authorization function similar to |
I would wager in this case, don't use the It's really nothing more a convenience method to throw a specific exception; I would say anything leaving that "comfort zone" should simply just implement
That's exactly my experience and for that reason, in non-trivial projects I can't use it. Authorization is so much baked into the (my) application, I can't simply extract it into a separate step like suggested by rebing |
Ok thanks for the clarifications, I try with the resolve way because in some scenarios Feel free to close the issue and thank you again. |
I am also unable to use the authorization method, instead have to use custom Traits. |
@fvdung looks interesting but you can elaborate what / how this project can benefit from the trait approach, specifically? I checked it out but it's not obvious to me; thank you! |
The use of traits as helper makes the code more flexible. The authorize method can be called anywhere in a hook of GraphQL. |
@fvdung I must be missing something and I confess I still didn't get it :| Can you give a more practical example how a/the trait would improve things? |
You can refer to this document in the core of laravel. |
I think everything which came up here was tackled:
|
Sorry to drag up an old thread, but I just wanted to add some thoughts on this. I think we should rethink the order of authorization and validation. In my opinion, authorize should always come first for a couple of reasons.
public function rules(array $args = []): array
{
$user = request()->user();
return [
'name' => ['string', 'max:255'],
'email' => ['string', 'email', 'max:255', "unique:users,email,{$user->id}"],
'password' => ['string', 'min:8'],
'billingAddress1' => ['string', 'max:255'],
'billingAddress2' => ['nullable', 'string', 'max:255'],
'billingCity' => ['string', 'max:255'],
'billingCounty' => ['string', 'max:255'],
'billingPostcode' => ['string', 'max:255'],
'billingCountry' => ['string', 'max:255'],
];
} I understand the pros/cons for both approaches, but I think the pros of running authorization first outweigh the cons. |
That's some good arguments and TBH I'm also not so confident about my "TL;DR" from #481 (comment) anymore. From where I currently stand, authorization before validation indeed makes a lot of sense. |
So, re-opened this to gather more feedback / arguments how we should do it, pro-cons, what options are we having for those we "need it the other way around", security considerations in mind, backward compatibility breaks, etc. I think this should be clarified before jumping into code (though I don't mind PRs + tests for discussion) |
These are valid points indeed. I see that the Laravel framework also follows the logic of:
Especially from a security standpoint we should definitely consider this change, but need to be careful here, since it's a breaking change. You have my +1 though. |
Great, thank you @rebing @chrisnharvey yes, starting a PR now would be good for design/discussion, thank you! |
Hi all,
But wait, I “try” to see beyond my horizon, Currently: schema validation -> data validation (business logic) -> authorization Is it correct to think so? I want to deepen as soon as I can in this, schema / business logic / graphql philosophy, maybe i can find something in these resources: Package like graphql-constraint-directive (for Apollo Server) works like middleware. So currently I think the best approach is in this pr #594 (middleware pattern) with some ability in configs for manage this (orders, add, remove, ..) |
Currently the GraphQL guidelines provide for the authorization and validation phase to be carried out by the business logic layer of the application (see https://graphql.org/learn/thinking-in-graphs/#business-logic-layer and https://graphql.org/learn/authorization). |
I am happy to see that PR #594 has been merged 🚀, so at the moment I think we can close the discussion (also because I don't see other ideas). |
Versions:
Question:
About issue resolved in #407 in my opinion it was better to check first authorize method and than perform validation rules, this prevents any superfluous and possibly redundant validation queries.
For example Rule::exists check in db if the input data exists, even if I might not have permission to do this. I think this generates bypass the acl controls.
What do you think ? Where I'm wrong ?
Thanks
The text was updated successfully, but these errors were encountered: