Skip to content

[Security] Improve error handling in OIDC access token handlers #50695

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 1 commit into from

Conversation

chalasr
Copy link
Member

@chalasr chalasr commented Jun 18, 2023

Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets -
License MIT
Doc PR not needed

This PR fine-tunes the way errors are handled in OIDC token handlers by catching more specific exceptions from the underlying JWT library and moving error-free assignments out of try/catch blocks, which makes 2 OIDC-specific exception classes (experimental) useless.


// Verify the signature
if (!$jwsVerifier->verifyWithKey($jws, $this->jwk, 0)) {
throw new InvalidSignatureException();
Copy link
Member Author

@chalasr chalasr Jun 18, 2023

Choose a reason for hiding this comment

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

throwing specific exceptions to catch them immediately is pointless, the BadCredentialsException's message (not obfuscated messageKey) is enough. If we want to provide extension points to allow e.g. fixing the token on the fly, we can do it by dispatching specific events.

@chalasr chalasr force-pushed the oidc-error-handling branch from 7fb11f5 to 2c4df90 Compare June 18, 2023 15:48
// UserLoader argument can be overridden by a UserProvider on AccessTokenAuthenticator::authenticate
return new UserBadge($claims[$this->claim], fn () => $this->createUser($claims), $claims);
} catch (\Exception $e) {
$this->logger?->error('An error while decoding and validating the token.', [
Copy link
Member Author

Choose a reason for hiding this comment

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

missing word -> #50696

@chalasr chalasr force-pushed the oidc-error-handling branch from 2c4df90 to 7f0f235 Compare June 18, 2023 15:52
@maxbeckers
Copy link
Contributor

maxbeckers commented Jun 20, 2023

@chalasr did you see the test failure? might be related to the change:

  1. Symfony\Bundle\SecurityBundle\Tests\Functional\AccessTokenTest::testOidcSuccess
    Symfony\Component\DependencyInjection\Exception\InvalidParameterTypeException: Invalid definition for service "security.access_token_handler.main": argument 6 of "Symfony\Component\Security\Http\AccessToken\Oidc\OidcTokenHandler::__construct()" accepts "Psr\Clock\ClockInterface", "Psr\Log\NullLogger" passed.

@chalasr chalasr force-pushed the oidc-error-handling branch from 7f0f235 to 18498d7 Compare June 22, 2023 23:23
@chalasr chalasr force-pushed the oidc-error-handling branch from 18498d7 to 8adf786 Compare August 6, 2023 04:18
@chalasr
Copy link
Member Author

chalasr commented Aug 6, 2023

Comments addressed. The failing test should turn back to green once merged up to 7.0.

@nicolas-grekas
Copy link
Member

Can you please rebase? Can you also pleas update the description of the PR so that we can get what this is about? :)

@chalasr chalasr force-pushed the oidc-error-handling branch from 8adf786 to b4e1042 Compare October 2, 2023 01:00
@chalasr
Copy link
Member Author

chalasr commented Oct 2, 2023

@nicolas-grekas done

@@ -6,6 +6,8 @@ CHANGELOG

* `UserValueResolver` no longer implements `ArgumentValueResolverInterface`
* Deprecate calling the constructor of `DefaultLoginRateLimiter` with an empty secret
* [BC BREAK] Remove experimental `InvalidSignatureException` and `MissingClaimException` classes from the `AccessToken\Oidc\Exception` namespace
Copy link
Member

Choose a reason for hiding this comment

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

This BC break is not worth it IMO. you could deprecate this 2 exceptions instead and remove them in 7.0

) {
if (!$clock || $clock instanceof LoggerInterface) {
$this->clock = new Clock();
Copy link
Member

Choose a reason for hiding this comment

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

You also need to account for the case where the clock is pass explicitly in the old position instead of ignoring it and assuming that the default clock is used.

@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@chalasr
Copy link
Member Author

chalasr commented Jan 12, 2025

That logic is getting updated in some more recent PRs. I may resume it later if needed.

@chalasr chalasr closed this Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants