-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] NullToken signature #40758
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
Conversation
@@ -33,7 +33,7 @@ public function getCredentials() | |||
|
|||
public function getUser() | |||
{ | |||
return null; | |||
return ''; |
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.
Should we throw an exception here like we do in setUser
?
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.
Most callers have if (!token->getUser() instanceof UserInterface)
checks, I think it's good as is
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'm kind of mixed on this, if you have multiple user "types" e.g. User
& Token
- it's easier to catch an exception. Which in turn falls in line with how the new security system handles $userBadge->getUser()
|| $passport->getUser()
.
Hmm, I think I would be in favor of throwing an exception (forcing everyone to update their voters to check
|
This class is covered by the BC promise and this PR targets 5.2. Throwing now would be a BC break, IMHO we’d better do that on 5.3 (with a deprecation notice first). |
I'm not sure about this one, people already have to check if the user is an instance of their own user like: $user = $token->getUser();
if ($user instanceof MyAppUser) {
$user->getFooAttribute();
} Here are suggesting to add another check: if (!$token instance NullToken) {
$user = $token->getUser();
$user = $token->getUser();
if ($user instanceof MyAppUser) {
$user->getFooAttribute();
}
} Moreover we already have to check if TokenStorageInterface returns |
I think I forgot to add some extra context: My "issue" with maintaining |
We cannot assume that our code is exclusively used the way it's intended, unless we explicitly restrict its covered usage at time to introduce it. Even if we are in the middle of a rewrite, we should keep in mind that our libraires are used in the wild. Doing BC breaks on non-experimental code in patch releases is a no go. Behavior changes should be discussed on 5.x - and returning null as it is done currently is violating the contract, hence I think this patch is the right thing to do as a bugfix on 5.2. |
That's why I also said "This would mean it needs to be merged in 5.x." in my first comment :) I'm -0.5 on making this an empty string in 5.2. Imho, it doesn't add much: This class is new in 5.2; I really don't expect people to use it; and if they do, they already coped with the |
When I implemented the new security system and created a couple custom authenticators - I was kind of confused by the current return type of I think an exception would fall inline with other
Very good point - I've seen |
I agree with that stance, an exception would make things more complex to deal with. I'm 👍 with this PR, NullToken should respect the contract. |
Thank you @jderusse. |
The signature of
TokenInterface::getUser
does not allow returning null. ButNullToken
returnsnull
.symfony/src/Symfony/Component/Security/Core/Authentication/Token/TokenInterface.php
Lines 49 to 56 in 0f96ac7
This PR update
NullToken::getUser
to return an empty string instead of null.I wonder if the fix shouldn't be to change the return type in the interface, but that would be a BC break, right?