-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[6.2] getLastAuthenticationError(): Return value must be of type ?AuthenticationException, string returned #49166
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
Comments
Can you create a small example application that allows to reproduce your issue? |
Visit URI "/account/login" then try any credentials, because they'll all be wrong. (Unless of course you bother to make a valid user in the Doctrine DB.)
heh really? I wasn't expecting you'd need that much from me, but here you go: https://github.com/AdamReece-WebBox/symfony-issue-49166 Visit URI "/account/login", try any credentials (because they'll all be wrong), then the invalid return type error will happen. |
Thanks for the reproducer! It appears you set the exception message in the session, whereas you should store the exception itself: https://github.com/AdamReece-WebBox/symfony-issue-49166/blob/cf3731a3dcf00bc1c70bb6623fa013c0b8021e96/src/Security/AppAuthenticator.php#L284 Also,
|
Hi @MatTheCat, Thank you for the insight within the I've also looked into |
Symfony version(s) affected
6.2.5
Description
In an authenticator handler, e.g.
\App\Security\AppAuthenticator::authenticate()
, throwing an exception does not set the session correctly. This is based on the guide How to Write a Custom Authenticator.For example if you throw a bad credentials exception because a user identifier is not found:
Then in your authenticate controller you grab that error from the
AuthenticationUtils
service:This will cause an internal error because
\Symfony\Component\Security\Http\Authentication\AuthenticationUtils::getLastAuthenticationError()
has a return type hint of?AuthenticationException
, however when the thrown exception is read from the session here:The content of
$authenticationException
will bestring
instead of the original exception instance thrown, thus invalid for the return type hint. This results with a fatal error:Logging in successfully won't produce a problem because the
$error
will benull
.How to reproduce
Possible Solution
The session would need to contain a serialised instance of the authentication exception thrown, or the
getLastAuthenticationError()
would need to permit the return of a string as part of a union type withAuthenticationException
andnull
.Additional Context
I don't recall getting this problem in Symfony 6.1 or earlier though I could be wrong, we've only noticed it since 6.2.
The text was updated successfully, but these errors were encountered: