spring-projects / spring-projects/spring-security

SEC-3098: PasswordComparisonAuthenticator can return too many attributes

Open
#3,271 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: ldap type: bug type: jira
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Wendell Beckwith (Migrated from SEC-3098) said:

Here's the authenticate method for the PAC:

    public DirContextOperations authenticate(final Authentication authentication) {
        Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication,
                "Can only process UsernamePasswordAuthenticationToken objects");
        // locate the user and check the password

        DirContextOperations user = null;
        String username = authentication.getName();
        String password = (String) authentication.getCredentials();

        SpringSecurityLdapTemplate ldapTemplate = new SpringSecurityLdapTemplate(
                getContextSource());

        for (String userDn : getUserDns(username)) {
            try {
                user = ldapTemplate.retrieveEntry(userDn, getUserAttributes());
            }
            catch (NameNotFoundException ignore) {
            }
            if (user != null) {
                break;
            }
        }

        if (user == null && getUserSearch() != null) {
            user = getUserSearch().searchForUser(username);
        }

        if (user == null) {
            throw new UsernameNotFoundException("User not found: " + username);
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Performing LDAP compare of password attribute '"
                    + passwordAttributeName + "' for user '" + user.getDn() + "'");
        }

        if (usePasswordAttrCompare && isPasswordAttrCompare(user, password)) {
            return user;
        }
        else if (isLdapPasswordCompare(user, ldapTemplate, password)) {
            return user;
        }
        throw new BadCredentialsException(messages.getMessage(
                "PasswordComparisonAuthenticator.badCredentials", "Bad credentials"));
    }

If the authentication is done via userDN patterns then the resulting DirContextOperations result has only the attributes set in the getUserAttributes() method. However, if the code goes past the loop for the userDN patterns and does a search for the user, then all attributes are returned.

It appears that the following code should be inserted before the "if (usePasswordAttrCompare && isPasswordAttrCompare(user, password)) {" line:

Attributes attrs = user.getAttributes(user.getDn().toString(), getUserAttributes());
user = new DirContextAdapter(attrs, user.getDn().toString(), getContextSource().getBaseLdapPath());

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in PasswordComparisonAuthenticator.authenticate and compare the userDN-pattern path with the getUserSearch() path, paying attention to getUserAttributes() and the returned DirContextOperations. Verify that authentication results from both paths expose only the requested attributes, without changing the password comparison behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
authentication, security
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.