Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Security] Enforce maximum username length in UserBadge #46584

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

Merged
merged 1 commit into from
Jun 7, 2022

Conversation

wouterj
Copy link
Member

@wouterj wouterj commented Jun 5, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR -

In 2016, a CVE release was made to harden all build-in authenticators for session storage flooding attacks: https://symfony.com/blog/cve-2016-4423-large-username-storage-in-session With the new security system, we can extend the hardening to all authenticators by enforcing the maximum user length in the UserBadge.

I believe we can do this as a "bugfix" in 6.2 directly, based on this reasoning from the blog post: "To avoid any BC break, the limit is set to 4096 characters, which should be more than enough for normal usages." For full stability safety, I think it's better to not do this on 5.4.

@@ -27,6 +28,8 @@
*/
class UserBadge implements BadgeInterface
{
public const MAX_USERNAME_LENGTH = 4096;
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps out of context but why reintroducing username here? Why not user identifier?

Copy link
Member

Choose a reason for hiding this comment

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

We need a more advanced BC layer for that. I'm going to work on getting rid of that "username" leftovers in a dedicated PR if we don't do it here (there's also Security::LAST_USERNAME)

@fabpot
Copy link
Member

fabpot commented Jun 7, 2022

Thank you @wouterj.

@fabpot fabpot merged commit 959b22d into symfony:6.2 Jun 7, 2022
@wouterj wouterj deleted the security-usernamelengthcheck branch June 7, 2022 21:15
fabpot added a commit that referenced this pull request Jun 9, 2022
…halasr)

This PR was merged into the 6.2 branch.

Discussion
----------

[Security] Centralize max username length enforcement

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Cleans up the max username length enforcement across the board. Authenticators do not need to check it on their own since #46584.

Commits
-------

9fd1e82 [Security] Centralize max username length enforcement
@fabpot fabpot mentioned this pull request Oct 24, 2022
@Zuulka
Copy link

Zuulka commented Mar 27, 2023

EDIT : Opened a dedicated issue here

Hello there,

Simple thought about the way this has been resolved :

I believe we can do this as a "bugfix" in 6.2 directly, based on this reasoning from the blog post: "To avoid any BC break, the limit is set to 4096 characters, which should be more than enough for normal usages."

For most usages, it will probably be enough, but it implies there will be some case where it won't.
For instance, with oauth2, the implementation of knpuniversity/oauth2-client-bundle which states in the documentation (here) to send your oauth2 access token directly as the user badge "identifier" :

return new SelfValidatingPassport(

    new UserBadge($accessToken->getToken(), function() use ($accessToken, $client) {

[...]

Later, you're supposed to retrieve your identifier in a user provider or in the closure of user badge to construct your "user".

Passing your token in the UserBadge construct will instantly trigger the hard coded security test provided by this commit :

if (\strlen($userIdentifier) > self::MAX_USERNAME_LENGTH) {

            throw new BadCredentialsException('Username too long.');

        }

There aren't any explicit restriction on oauth2 token size, which can lead here to a much bigger size of 4096 chars if the token comes with a bunch of permission in it.

And I'm talking about oauth2 here as an example but it could be any other specific reason.

The fact is that, having a security test is good here, but the arbitrary 4096-character-constant-restriction which relies on "it should be more than enough for normal usages" is a bit hard, because it only relies on personal apreciation.

There should be a way, for people who know the risks, to easily override or bypass that restrictrion or to change the value of MAX_USERNAME_LENGTH, but the fact that it is a class constant accessed with the "self::" keyword makes it difficult due to the fact a constant is, by definition, made to stay untouched.

A long term solution could be to add a configuration parameter to properly override MAX_USERNAME_LENGTH if set.
A short term solution could be to, at least, use the "static::" keyword in the test. In that way, it would be possible to extends UserBadge with a custom class, and redefine MAX_USERNAME_LENGTH inside the extending class.

if (\strlen($userIdentifier) > static::MAX_USERNAME_LENGTH) {

            throw new BadCredentialsException('Username too long.');

        }

Z.

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.

6 participants