mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-05-30 08:42:13 +00:00
Catch possible NullPointerException
Some maps may throw a NullPointerException when get is called with null. This commit catches the exceptions and just leaves the delegate null. Fixes gh-4936
This commit is contained in:
parent
718052932a
commit
2b66793535
@ -116,6 +116,7 @@ import java.util.Map;
|
|||||||
* @see org.springframework.security.crypto.factory.PasswordEncoderFactories
|
* @see org.springframework.security.crypto.factory.PasswordEncoderFactories
|
||||||
*
|
*
|
||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
|
* @author Michael Simons
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
public class DelegatingPasswordEncoder implements PasswordEncoder {
|
public class DelegatingPasswordEncoder implements PasswordEncoder {
|
||||||
@ -190,7 +191,11 @@ public class DelegatingPasswordEncoder implements PasswordEncoder {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
String id = extractId(prefixEncodedPassword);
|
String id = extractId(prefixEncodedPassword);
|
||||||
PasswordEncoder delegate = this.idToPasswordEncoder.get(id);
|
PasswordEncoder delegate = null;
|
||||||
|
try {
|
||||||
|
delegate = this.idToPasswordEncoder.get(id);
|
||||||
|
} catch(NullPointerException e) {
|
||||||
|
}
|
||||||
if(delegate == null) {
|
if(delegate == null) {
|
||||||
return this.defaultPasswordEncoderForMatches
|
return this.defaultPasswordEncoderForMatches
|
||||||
.matches(rawPassword, prefixEncodedPassword);
|
.matches(rawPassword, prefixEncodedPassword);
|
||||||
|
@ -33,6 +33,7 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
|
* @author Michael Simons
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
@ -46,6 +47,9 @@ public class DelegatingPasswordEncoderTests {
|
|||||||
@Mock
|
@Mock
|
||||||
private PasswordEncoder invalidId;
|
private PasswordEncoder invalidId;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Map<String, PasswordEncoder> throwingDelegates;
|
||||||
|
|
||||||
private String bcryptId = "bcrypt";
|
private String bcryptId = "bcrypt";
|
||||||
|
|
||||||
private String rawPassword = "password";
|
private String rawPassword = "password";
|
||||||
@ -167,6 +171,21 @@ public class DelegatingPasswordEncoderTests {
|
|||||||
verifyZeroInteractions(this.bcrypt, this.noop);
|
verifyZeroInteractions(this.bcrypt, this.noop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void matchesWhenIdIsNullThenFalse() {
|
||||||
|
when(this.throwingDelegates.containsKey(this.bcryptId)).thenReturn(true);
|
||||||
|
when(this.throwingDelegates.get(this.bcryptId)).thenReturn(this.bcrypt);
|
||||||
|
when(this.throwingDelegates.get(null)).thenThrow(NullPointerException.class);
|
||||||
|
|
||||||
|
DelegatingPasswordEncoder passwordEncoder = new DelegatingPasswordEncoder(this.bcryptId, throwingDelegates);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> passwordEncoder.matches(this.rawPassword, this.rawPassword))
|
||||||
|
.isInstanceOf(IllegalArgumentException.class)
|
||||||
|
.hasMessage("There is no PasswordEncoder mapped for the id \"null\"");
|
||||||
|
|
||||||
|
verifyZeroInteractions(this.bcrypt, this.noop);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void matchesWhenNullIdThenDelegatesToInvalidId() {
|
public void matchesWhenNullIdThenDelegatesToInvalidId() {
|
||||||
this.delegates.put(null, this.invalidId);
|
this.delegates.put(null, this.invalidId);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user