spring-projects / spring-projects/spring-security

SEC-2745: BasicAuthenticationFilter reports 401 when error occurs with authentication

Open
#2,970 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Rob Winch (Migrated from SEC-2745) said:

I'm using HTTP Basic authentication with an LDAP server as a backend (using auth.ldapAuthentication().contextSource().url("ldap://ldap.example.com")). Everything is working fine, but when the LDAP server is down, Spring Security returns HTTP 401 instead of HTTP 500.

This is problematic because the client application tells the user that the username/password are incorrect, and then users will call support asking why their passwords have changed (when in fact they haven't).

I did some investigation and found that 401 is returned when any unhandled exception is thrown during authentication (For example, if UserDetailsService throws an exception because the database server is down).

To reproduce, simply use the following to configure the AuthenticationManagerBuilder:

    auth.userDetailsService(new UserDetailsService() {
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            throw new RuntimeException("Database server down");
        }
    });

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 at BasicAuthenticationFilter, the entry point named in the issue, and trace how authentication exceptions become HTTP responses. Reproduce the failure with the provided UserDetailsService and verify that an unavailable backend produces HTTP 500 while invalid credentials still produce HTTP 401.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.