spring-projects / spring-projects/spring-security

CookieTheftException thrown when can't connect to LDAP

Open
#3,801 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Summary

I'm using LDAP auth and Remember me functionality. When LDAP connection error occurs, CookieTheftException is being thrown.

Actual Behavior
  1. I login correctly, remember me is checked so token is stored in database.
  2. I restart the server (or wait till session expire), changing LDAP url to not existing one (to simulate connection failure)
  3. I launch application
  4. I'm getting two exceptions: first CommunicationException and then CookieTheftException
Expected Behavior

I expect CommunicationException to be handled correctly by ErrorPageFilter.

Version

Spring Boot: 1.3.1
Spring Security: 4.0.3
Spring: 4.2.4

Sample

Security configuration:

 .and()
            .rememberMe()
            .tokenRepository(jpaPersistentTokenRepository)
            .userDetailsService(ldapUserDetailsService) //org.springframework.security.ldap.userdetails.LdapUserDetailsService
            .rememberMeParameter("_spring_security_remember_me")
            .key("irrelevant")
            .tokenValiditySeconds(123456)

Error pages conf:

 @Override
    public void customize(final ConfigurableEmbeddedServletContainer container) {
        container.addErrorPages(new ErrorPage(CookieTheftException.class, "/login"), new ErrorPage("/errors/other"));

    }

So I debug and came to this conclusion:
Failure in LDAP connection causes org.springframework.ldap.CommunicationException (1)(which is runtime exception) from last line of PersistentTokenBasedRememberMeServices#processAutoLoginCookie method. This means that persistent token (2) was updated in database, new cookie was set in response but was not sent to the client. Exception is handled by ErrorPageFilter which forwards request to error page.
RememberMeAuthenticationFilter is launched and PersistentTokenBasedRememberMeServices compares token from request cookie (which is same as before (1)) with token from db (2) which are different which causes CookieTheftException.
Any RuntimeException thrown from last line of processAutoLoginCookie will cause this behavior. Maybe it should be moved to try-catch block above?

This may be connected slightly to #2970.

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 by reading PersistentTokenBasedRememberMeServices#processAutoLoginCookie and the interaction with RememberMeAuthenticationFilter and ErrorPageFilter, using the LDAP remember-me configuration in the report as context. Trace the failure after the persistent token is updated and verify that a CommunicationException is handled without producing a subsequent CookieTheftException.

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.