Skip to content
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

Call not-found/bad-method handlers on invalid HTTP method #2141

Merged
merged 2 commits into from
Feb 6, 2017

Conversation

iansltx
Copy link
Contributor

@iansltx iansltx commented Feb 4, 2017

Partially resolves #2005. Router map() calls don't have method checking,
and we still don't link the valid methods in the request to valid
methods at the application level (which IMO should be since the router
doesn't enforce any sort of method limitation at this point), but now
you won't get an uncaught exception when someone hits you with an HTTP
message with verb CHICKEN.

Also revised tests to check app run handling rather than request create
handling, since internally to the request a bad method should still be
invalid/throw an exception, so we don't want to weaken that guarantee.
But we do want the application not to 500 at runtime.

Along with that, behavior (as tested) differs between whether we have a
route or not, since in any other case the former would give you a bad
method exception, while the latter would give you a not-found. So these
tweaks match up that behavior for obscure HTTP methods as well.

Finally, RouterInterface now has constants for route info array returns.
They're only used on the exceptional path, but they make it a bit
clearer why we're pulling various numeric keys out of the route, rather
than having a non-obvious contract with FastRoute.

Took extra care here to respect order of execution for the existing code
so anything relying on that order will still work. Likewise,
InvalidMethodException subclasses InvalidArgumentException and returns
the same message and code, so anything checking for exception strings
etc., as well as instanceof checks, will still work.

Credit to @nickdnk for building the first red test for this issue.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.9%) to 96.995% when pulling d74ec81 on iansltx:issue-2005 into 3623951 on slimphp:3.x.

@iansltx
Copy link
Contributor Author

iansltx commented Feb 4, 2017

I'll work on improving coverage back to where it should be. No need to merge until I do that.

Partially resolves slimphp#2005. Router map() calls don't have method checking,
and we still don't link the valid methods in the request to valid
methods at the application level (which IMO should be since the router
doesn't enforce any sort of method limitation at this point), but now
you won't get an uncaught exception when someone hits you with an HTTP
message with verb CHICKEN.

Also revised tests to check app run handling rather than request create
handling, since internally to the request a bad method should still be
invalid/throw an exception, so we don't want to weaken that guarantee.
But we do want the application not to 500 at runtime.

Along with that, behavior (as tested) differs between whether we have a
route or not, since in any other case the former would give you a bad
method exception, while the latter would give you a not-found. So these
tweaks match up that behavior for obscure HTTP methods as well.

Finally, RouterInterface now has constants for route info array returns.
They're only used on the exceptional path, but they make it a bit
clearer why we're pulling various numeric keys out of the route, rather
than having a non-obvious contract with FastRoute.

Took extra care here to respect order of execution for the existing code
so anything relying on that order will still work. Likewise,
InvalidMethodException subclasses InvalidArgumentException and returns
the same message and code, so anything checking for exception strings
etc., as well as instanceof checks, will still work.

Credit to @nickdnk for building the first red test for this issue.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 97.956% when pulling 1e4af6d on iansltx:issue-2005 into 3623951 on slimphp:3.x.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 97.956% when pulling f4d6f44 on iansltx:issue-2005 into 3623951 on slimphp:3.x.

@iansltx
Copy link
Contributor Author

iansltx commented Feb 4, 2017

...and by "improving coverage" I actually mean "fixing a completely broken first implementation", as it turns out. Now we're good to go though :)

Slim/App.php Outdated
$response = $this->container->get('response');

$response = $this->process($request, $response);
$response = !isset($e) ? $this->process($request, $response) : $this->processInvalidMethod($request, $response);
Copy link
Member

Choose a reason for hiding this comment

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

Please refactor this ternary into the try/catch

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 assume that means duplicating $response = $this->container->get('response'); to maintain existing application flow?

Copy link
Member

Choose a reason for hiding this comment

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

I think that it can go before the try?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Existing flow gets the request before the response so I wanted to maintain that order of execution. But sounds like we're cool with swapping order since the two should never be related anyway. Will push a rev once this talk's over.

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 it should matter. If it does, I'm not sure what I'll think!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cleaned up! Keep up with the feedback :)

Changes execution order a bit, but won't matter unless folks are doing
something really weird interaction-wise between container request and
response objects. And maybe not even then!
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 97.954% when pulling abea810 on iansltx:issue-2005 into 3623951 on slimphp:3.x.

@akrabat akrabat added this to the 3.8.0 milestone Feb 5, 2017
@akrabat akrabat merged commit abea810 into slimphp:3.x Feb 6, 2017
akrabat added a commit that referenced this pull request Feb 6, 2017
@akrabat
Copy link
Member

akrabat commented Feb 6, 2017

Thanks!

akrabat added a commit that referenced this pull request Feb 6, 2017
@iansltx iansltx deleted the issue-2005 branch February 6, 2017 18:11
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.

HTTP method outside whitelist causes 500
3 participants