Skip to content

[Security] fixed issue where x509 authentication clears unrelated tokens #8283

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

Closed
wants to merge 2 commits into from

Conversation

alcaeus
Copy link
Contributor

@alcaeus alcaeus commented Jun 14, 2013

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #8226
License MIT
Doc PR symfony/symfony-docs#2741

TODO:

  • Feedback on change to make sure security is not affected
  • Fix other authentication listeners (they suffer the same problem)
  • Write unit tests for bug and maybe a few listener classes as well

This pull request is the summary of the problem mentioned in the ticket above.
It only fixes the "disappearing token" problem for one authentication provider, not all. If acceptable, the change needs to be applied to all authentication listeners since they always clear all tokens from the security context.

Any help or feedback is greatly appreciated.

@fabpot
Copy link
Member

fabpot commented Jun 17, 2013

I think it makes sense.

@alcaeus
Copy link
Contributor Author

alcaeus commented Jun 19, 2013

I updated all other authentication listeners that clear security tokens. I also hardened the check to also check for the provider key.
There is one minor issue that could arise: the AbstractAuthenticationListener class is responsible for handling authentication failure, including clearing the token. Since it could be used by any custom authentication listener which could set a token other than UsernamePasswordToken I'm not sure it makes a lot of sense to only clear those tokens. Any ideas on that?
Other than that I'm done with the changes, maybe one or two people could also just test it in their environments to make sure it works.

@@ -181,7 +181,10 @@ private function onFailure(GetResponseEvent $event, Request $request, Authentica
$this->logger->info(sprintf('Authentication request failed: %s', $failed->getMessage()));
}

$this->securityContext->setToken(null);
$token = $this->securityContext->getToken();
if (null !== $token && $token instanceof UsernamePasswordToken && $this->providerKey == $token->getProviderKey()) {
Copy link
Member

Choose a reason for hiding this comment

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

you should use a strict comparison

Copy link
Member

Choose a reason for hiding this comment

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

and you can remove null !== $token. null is not an instance of UsernamePasswordToken

@alcaeus
Copy link
Contributor Author

alcaeus commented Jun 19, 2013

Done. Thank you!

… tokens

This commit fixes an issue where authentication listeners clear all security tokens in case of authentication failure.
This behavior makes it impossible to combine certain authentication mechanisms, notably x509 with form-based login.
@alcaeus
Copy link
Contributor Author

alcaeus commented Jun 22, 2013

From my end this one is ready, I've also tried to test various combinations of authentication listeners which produced the desired results.
I've also squashed the commits into one.

@@ -38,10 +38,6 @@ public function __construct(SecurityContextInterface $securityContext, Authentic

protected function getPreAuthenticatedData(Request $request)
{
if (!$request->server->has($this->userKey)) {
Copy link
Member

Choose a reason for hiding this comment

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

Why have you removed this code?

Copy link
Member

Choose a reason for hiding this comment

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

@alcaeus Can you tell us more about this code removal? It does not look like it is related to the goal of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi! Sorry about the late reply, I've been struck down with the flu for the past week and wasn't able to do anything.
To be honest, that was the first piece of code I removed when trying to fix it. The exception is not caught in the abstract listener, which caused the original problem of cleared tokens.
On second thought, it probably should be left in so that code can react to the different cases ("no certificate provided" and "no data in certificate"). Can you give me another couple of days to check it out? I'll put the code back in and make sure the abstract listener reacts correctly to the exception.

@alcaeus
Copy link
Contributor Author

alcaeus commented Jul 10, 2013

The BadCredentialsException is back in (it really makes more sense this way), the AbstractPreAuthenticatedListener now takes care of handling that case. The exception handling is necessary to allow the authentication process to fall through to the next listener.

/**
* Clears a PreAuthenticatedToken for this provider (if present)
*
* @return void
Copy link
Member

Choose a reason for hiding this comment

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

this should be removed as we don't use mention the return value when it is null

@alcaeus
Copy link
Contributor Author

alcaeus commented Jul 19, 2013

Fabien, is this one good to merge or is there anything else that should be changed?

@fabpot
Copy link
Member

fabpot commented Jul 19, 2013

@alcaeus Looks good to me except that I did not see that you based your patch on 2.1, which is not maintained anymore. Can you reopen a new PR on 2.2 instead?

@alcaeus
Copy link
Contributor Author

alcaeus commented Jul 19, 2013

Dang, yeah. Will do. Do you need one on 2.3 as well?

@fabpot
Copy link
Member

fabpot commented Jul 19, 2013

No, just 2.2.

@alcaeus
Copy link
Contributor Author

alcaeus commented Jul 19, 2013

Replaced by the above PR.

fabpot added a commit that referenced this pull request Jul 20, 2013
This PR was merged into the 2.2 branch.

Discussion
----------

[Security] fixed issue where x509 authentication clears unrelated tokens

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #8226
| License       | MIT
| Doc PR        | symfony/symfony-docs#2825
| Notes          | Replaces PR #8283

TODO:
- [x] Feedback on change to make sure security is not affected
- [x] Fix other authentication listeners (they suffer the same problem)
- [x] Write unit tests for bug and maybe a few listener classes as well

This pull request is the summary of the problem mentioned in the ticket above.
It only fixes the "disappearing token" problem for one authentication provider, not all. If acceptable, the change needs to be applied to all authentication listeners since they always clear all tokens from the security context.

Commits
-------

2317443 [Security] fixed issue where authentication listeners clear unrelated tokens
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.

3 participants