Skip to content

Allowed HTTP Methods for Actions #10

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 3 commits into from
Aug 4, 2018
Merged

Conversation

AlexMaxHorkun
Copy link
Contributor

Limit actions to processing only requests with certain HTTP methods in a declarative way

Limit actions to processing only requests with certain HTTP methods in a declarative way
>}


To limit already existing Actions and get developers to know these new interfaces a temporary event listener will be added for controller_action_postdispatch event to log HTTP methods used with actions and then the full suite of functional tests (MTF and MFTF) would be ran to get information on as much actions as possible.
Copy link
Member

Choose a reason for hiding this comment

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

Can we leave logging in place to let extension developers know that they need to update their extensions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I remember we discussed this, I just forgot to mention it - we can add a debug message each time an action without HTTP method declared s processed

Copy link
Member

Choose a reason for hiding this comment

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

I'm ok with not having this if we going to have a mess detector rule.

>
>    if ($actionInstance instanceof $interface && $request->getMethod() !== $httpMethod) {
>
>        throw new NotFoundException(__(‘Page not found.’));
Copy link
Member

Choose a reason for hiding this comment

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

I remember that we agreed to return 405 response code or no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, we chose 404 because it's more consistent with what we do now and server must never return a 405 to GET and HEAD requests (see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405)

Copy link
Member

@melnikovi melnikovi left a comment

Choose a reason for hiding this comment

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

Please look at comments I left

@AlexMaxHorkun
Copy link
Contributor Author

Everybody's ok with adding a debug message each time an action without HTTP method restriction is found?

Copy link
Contributor

@buskamuza buskamuza left a comment

Choose a reason for hiding this comment

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

How do we guarantee that developers don't use just ActionInterface? Instead of a specific one.
I'd suggest to include a static test (ideally, phpcs sniff) to the Work Breakdown.


Request method will be validated in the FrontController before executing a matched action, somewhat like this:

>foreach ($interfaceMap as $interface => $httpMethod) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It shouldn't be necessary to iterate through all methods. You can do if (isset($interfaceMap[$request->getMethod()]). If doesn't exist, the method is not supported by the application. If $actionInstance does not implement the interface, also - an error.
The map will be opposite:

POST => HttpPostInterface
GET => HttpGetInterface
etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I'll update that part


To limit already existing Actions and get developers to know these new interfaces a temporary event listener will be added for controller_action_postdispatch event to log HTTP methods used with actions and then the full suite of functional tests (MTF and MFTF) would be ran to get information on as much actions as possible.

After corresponding Http*Method*ActionInterface will be added to every Action class we have information on. If an action is logged to be accessed only by GET methods it will allow only GET, if only by POST – allowing only POST etc. If by more than one methods – the Action class will not be updated.
Copy link
Contributor

Choose a reason for hiding this comment

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

If by more than one methods – the Action class will not be updated

Why not make it implement multiple interfaces? Or if it's not clear why it really supports multiple methods, create a task to review it (as it can be a security issue).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because we cannot be sure that an action actually expects multiple request methods or a test is invalid without investigation and if too many cases like that would be found it can take too match time to investigate them. I think I can write something like this: "If by more than one methods – the Action class will be updated to implement multiple interfaces or ignored." Would that be ok?

Copy link
Member

Choose a reason for hiding this comment

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

I agree with Olga and would propose for now agree that we will need to review each case and discuss one more time if turns out that there are too many cases that need to be reviewed. Or you already know?

@AlexMaxHorkun
Copy link
Contributor Author

I think adding a rule to dev/tests/static/framework/Magento/CodeMessDetector/* that will detect classes implementing ActionInterface but not any HTTP method aware interfaces should be sufficient enough to alert developers about using these new interfaces

 validations algorithm, updating actions and static tests
@AlexMaxHorkun
Copy link
Contributor Author

I have updated the document to address the concerns

@buskamuza
Copy link
Contributor

Everybody's ok with adding a debug message each time an action without HTTP method restriction is found?

It may be an overkill, if used in production mode.

@antonkril antonkril merged commit 770a6e1 into magento:master Aug 4, 2018
@schmengler
Copy link

In the example, a PageNotFound exception is thrown. The correct response in HTTP semantics would be a 405 Method Not Allowed status code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants