-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] json login listener: ensure a json response is sent on bad request #22585
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
[Security] json login listener: ensure a json response is sent on bad request #22585
Conversation
@@ -180,4 +180,11 @@ private function onFailure(Request $request, AuthenticationException $failed) | |||
|
|||
return $response; | |||
} | |||
|
|||
private function throwBadRequestHttpException(Request $request, $message, \Exception $previous = null) |
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.
I would not throw inside the method, as it means you have to inspect this method to know that the flow stops in the main method. Instead, create a method returning the exception, and keep throwing it in the main method
There was a problem hiding this comment.
Choose a reason for hiding this comment
8000The reason will be displayed to describe this comment to others. Learn more.
Indeed, makes sense. Thank you.
EDIT: Eventually it looks even better to simply catch, set format and re-throw from the main method.
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.
👍
Thank you @ogizanagi. |
…sent on bad request (ogizanagi) This PR was merged into the 3.3-dev branch. Discussion ---------- [Security] json login listener: ensure a json response is sent on bad request | Q | A | ------------- | --- | Branch? | master (3.3) | Bug fix? | yesish | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | N/A I would have simply recommended to set the proper format when declaring the route: ```yml # routing.yml api_login: path: /login defaults: { _format: json } ``` but, since #22477 has been merged, and considering #22477 (comment): > my point above regarding checking the content type is so that one could use form_login and json_login in parallel on the same routes and within the same firewall we may consider setting the request format to json when throwing the `BadRequestHttpException`, so used conjointly with the TwigBundle, the exception is rendered using the `exception.json.twig` template. ping @lsmith77 (An alternative would be to check the Accept header to set the request format to json if it's the preferred one instead of doing it each time we throw the exception. But Symfony never used such content negotiation AFAIK, and I think it's safe enough to assume someone sending json is expecting json as ouput for exceptions.) Commits ------- 4427cf9 [Security] json login listener: ensure a json response is sent on bad request
I would have simply recommended to set the proper format when declaring the route:
but, since #22477 has been merged, and considering #22477 (comment):
we may consider setting the request format to json when throwing the
BadRequestHttpException
, so used conjointly with the TwigBundle, the exception is rendered using theexception.json.twig
template.ping @lsmith77
(An alternative would be to check the Accept header to set the request format to json if it's the preferred one instead of doing it each time we throw the exception. But Symfony never used such content negotiation AFAIK, and I think it's safe enough to assume someone sending json is expecting json as ouput for exceptions.)