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

AccessDeniedException is converted to HTTP 302 even when request format is JSON #30099

Closed
Jontsa opened this issue Feb 7, 2019 · 6 comments
Closed

Comments

@Jontsa
Copy link
Contributor

Jontsa commented Feb 7, 2019

Description

When accessing a secured area using AJAX + JSON while not logged in, the ExceptionListener in Symfony Security component may convert AccessDeniedException to a HTTP 302 redirect to login page. The javascript can not really do anything with this since it is expecting JSON or HTTP error code.

Any other exception would be converted to JSON and passed back to browser with correct HTTP status code. But in this case, you need to create your own event listener to convert this exception in to JSON. This is the same issue as reported here: https://stackoverflow.com/questions/33240554/symfony2-security-annotation-turn-off-redirect-when-not-logged-in

See: Symfony\Component\Security\Http\Firewall\ExceptionListener::handleAccessDeniedException()

How to reproduce

  • Setup controller that throws AccessDeniedException
  • Setup controller that mocks a login page
  • Configure route to login page in security.yaml form_login > login_path
  • Try to access the first controller using AJAX + JSON
  • You expect a HTTP 403 error but instead you get HTTP 200 with login page contents

If needed, I can provide an example project in Github.

@xabbuh
Copy link
Member

xabbuh commented Feb 7, 2019

I think a small example project would indeed be very helpful for anyone wanting to look into it.

@Jontsa
Copy link
Contributor Author

Jontsa commented Feb 7, 2019

Here is a simple project where you have two end points for ajax. Both throw exception but AccessDeniedException does not trigger HTTP error.

  • git clone git@github.com:Jontsa/security-http-example.git
  • cd security-http-example/
  • composer install && bin/console server:start

@nicolas-grekas
Copy link
Member

@Jontsa would you be able to debug why?

@dimabory
Copy link
Contributor

dimabory commented Mar 1, 2019

@nicolas-grekas, @xabbuh Do you guys aware of this return statement? Looks like this produces an issue not only with redirecting but also with custom access denied handler (see #28229).

Btw, I think we can safely remove it.

@dimabory
Copy link
Contributor

dimabory commented Mar 4, 2019

@curry684
Copy link
Contributor

curry684 commented Apr 6, 2019

I reviewed this and I don't think there's an actual issue here. The XHR request itself contains no usable indication that the redirect response is not valid, so Symfony is correct in following up with the redirect based on its configuration. And as @dimabory said you can/should just change that config line which fixes your issue correctly.

The return statement may be a real issue but it has its own dedicated issue already.

Status: reviewed

@fabpot fabpot closed this as completed Apr 6, 2019
fabpot added a commit that referenced this issue Apr 10, 2019
This PR was merged into the 3.4 branch.

Discussion
----------

[Security] Rework firewall's access denied rule

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ~~#30099~~, #28229
| License       | MIT
| Doc PR        |

Follow tickets provided above to reproduce bugs. (there are also some project examples)

~~In addition, I'm looking for someone who knows an answer to [this](#30099 (comment)) regarding rework in this PR.~~

Commits
-------

5790859 Rework firewall access denied rule
symfony-splitter pushed a commit to symfony/security that referenced this issue Apr 10, 2019
This PR was merged into the 3.4 branch.

Discussion
----------

[Security] Rework firewall's access denied rule

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ~~#30099~~, #28229
| License       | MIT
| Doc PR        |

Follow tickets provided above to reproduce bugs. (there are also some project examples)

~~In addition, I'm looking for someone who knows an answer to [this](symfony/symfony#30099 (comment)) regarding rework in this PR.~~

Commits
-------

5790859275 Rework firewall access denied rule
symfony-splitter pushed a commit to symfony/security-http that referenced this issue Apr 10, 2019
This PR was merged into the 3.4 branch.

Discussion
----------

[Security] Rework firewall's access denied rule

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ~~#30099~~, #28229
| License       | MIT
| Doc PR        |

Follow tickets provided above to reproduce bugs. (there are also some project examples)

~~In addition, I'm looking for someone who knows an answer to [this](symfony/symfony#30099 (comment)) regarding rework in this PR.~~

Commits
-------

5790859275 Rework firewall access denied rule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants