1
0
mirror of synced 2026-08-04 01:07:02 +00:00

SEC-1867: Perform null check on Authentication.getCredentials() prior to calling toString()

This commit is contained in:
Rob Winch
2011-12-30 13:59:04 -06:00
parent 448a42916d
commit 1f835fec43
4 changed files with 29 additions and 2 deletions
@@ -57,7 +57,8 @@ public class RemoteAuthenticationProvider implements AuthenticationProvider, Ini
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
String username = authentication.getPrincipal().toString();
String password = authentication.getCredentials().toString();
Object credentials = authentication.getCredentials();
String password = credentials == null ? null : credentials.toString();
Collection<? extends GrantedAuthority> authorities = remoteAuthenticationManager.attemptAuthentication(username, password);
return new UsernamePasswordAuthenticationToken(username, password, authorities);
@@ -21,6 +21,7 @@ import junit.framework.TestCase;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
@@ -77,6 +78,17 @@ public class RemoteAuthenticationProviderTests extends TestCase {
assertTrue(AuthorityUtils.authorityListToSet(result.getAuthorities()).contains("foo"));
}
public void testNullCredentialsDoesNotCauseNullPointerException() {
RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(false));
try {
provider.authenticate(new UsernamePasswordAuthenticationToken("rod", null));
fail("Expected Exception");
} catch(RemoteAuthenticationException success) {}
}
public void testSupports() {
RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
assertTrue(provider.supports(UsernamePasswordAuthenticationToken.class));