spring-projects / spring-projects/spring-security

SwitchUserFilter fails with IllegalArgumentException (currentAuthentication / source cannot be null)

Open
#19,418 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

The SwitchUserFilter setup fails when upgrading Spring Boot and Spring Security.
Depending on the version, it throws either an IllegalArgumentException: currentAuthentication cannot be null or IllegalArgumentException: source cannot be null when attempting to trigger the impersonation URL.

@Bean
public SwitchUserFilter switchUserFilter() throws Exception {
    final SwitchUserFilter switchUserFilter = new SwitchUserFilter();
    switchUserFilter.setUserDetailsService(userDetailsService);
    switchUserFilter.setSwitchUserMatcher(PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.GET, SWITCH_USER_URL));
    switchUserFilter.setExitUserMatcher(PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.GET, SWITCH_USER_EXIT_URL));
    switchUserFilter.setTargetUrl("/");
    switchUserFilter.setSwitchFailureUrl(ACCESS_DENIED_URL);
    return switchUserFilter;
}

I configure the filter like that : http.addFilterAfter(switchUserFilter(), AuthorizationFilter.class);

  • spring-boot-starter-parent 4.1.0, spring-security 7.1.0 :
java.lang.IllegalArgumentException: currentAuthentication cannot be null

at org.springframework.util.Assert.notNull(Assert.java:182)

at org.springframework.security.web.authentication.switchuser.SwitchUserFilter.createSwitchUserToken(SwitchUserFilter.java:306)

at org.springframework.security.web.authentication.switchuser.SwitchUserFilter.attemptSwitchUser(SwitchUserFilter.java:243)

at org.springframework.security.web.authentication.switchuser.SwitchUserFilter.doFilter(SwitchUserFilter.java:190)

at org.springframework.security.web.authentication.switchuser.SwitchUserFilter.doFilter(SwitchUserFilter.java:181)

at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:385)
  • spring-boot-starter-parent 4.0.7, spring-security 7.0.6 :
java.lang.IllegalArgumentException: source cannot be null

at org.springframework.util.Assert.notNull(Assert.java:182)

at org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority.<init>(SwitchUserGrantedAuthority.java:45)

at org.springframework.security.web.authentication.switchuser.SwitchUserFilter.createSwitchUserToken(SwitchUserFilter.java:306)

at org.springframework.security.web.authentication.switchuser.SwitchUserFilter.attemptSwitchUser(SwitchUserFilter.java:243)

at org.springframework.security.web.authentication.switchuser.SwitchUserFilter.doFilter(SwitchUserFilter.java:190)

at org.springframework.security.web.authentication.switchuser.SwitchUserFilter.doFilter(SwitchUserFilter.java:181)

at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:385)

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

Reproduce the failure with the shown SwitchUserFilter configuration and impersonation URL, then trace SwitchUserFilter.createSwitchUserToken and SwitchUserGrantedAuthority using the stack traces. Done means the configured switch-user flow no longer throws either null-related IllegalArgumentException and the reported impersonation behavior works across the affected Spring Security versions.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
backend, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.