Skip to content

Commit 9fe2858

Browse files
committed
Fix NPE when nameAttributeValue is null #15338
- fix name attribute value check method - add test case when nameAttributeValue is null
1 parent beff600 commit 9fe2858

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/DefaultOAuth2User.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@
4444
*
4545
* @author Joe Grandja
4646
* @author Eddú Meléndez
47+
* @author Park Hyojong
4748
* @since 5.0
4849
* @see OAuth2User
4950
*/
@@ -69,8 +70,10 @@ public DefaultOAuth2User(Collection<? extends GrantedAuthority> authorities, Map
6970
Assert.notEmpty(attributes, "attributes cannot be empty");
7071
Assert.hasText(nameAttributeKey, "nameAttributeKey cannot be empty");
7172
if (!attributes.containsKey(nameAttributeKey)) {
72-
throw new IllegalArgumentException("Missing attribute '" + nameAttributeKey + "' in attributes");
73+
throw new IllegalArgumentException("Missing nameAttributeKey '" + nameAttributeKey + "' in attributes");
7374
}
75+
Assert.notNull(attributes.get(nameAttributeKey), "Name attribute '" + nameAttributeKey + "' cannot be null");
76+
7477
this.authorities = (authorities != null)
7578
? Collections.unmodifiableSet(new LinkedHashSet<>(this.sortAuthorities(authorities)))
7679
: Collections.unmodifiableSet(new LinkedHashSet<>(AuthorityUtils.NO_AUTHORITIES));

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/user/DefaultOAuth2UserTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
*
3535
* @author Vedran Pavic
3636
* @author Joe Grandja
37+
* @author Park Hyojong
3738
*/
3839
public class DefaultOAuth2UserTests {
3940

@@ -59,6 +60,12 @@ public void constructorWhenAttributesIsEmptyThenThrowIllegalArgumentException()
5960
.isThrownBy(() -> new DefaultOAuth2User(AUTHORITIES, Collections.emptyMap(), ATTRIBUTE_NAME_KEY));
6061
}
6162

63+
@Test
64+
public void constructorWhenAttributeValueIsNullThenThrowIllegalArgumentException() {
65+
assertThatIllegalArgumentException()
66+
.isThrownBy(() -> new DefaultOAuth2User(AUTHORITIES, Collections.singletonMap(ATTRIBUTE_NAME_KEY, null), ATTRIBUTE_NAME_KEY));
67+
}
68+
6269
@Test
6370
public void constructorWhenNameAttributeKeyIsNullThenThrowIllegalArgumentException() {
6471
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultOAuth2User(AUTHORITIES, ATTRIBUTES, null));

0 commit comments

Comments
 (0)