Skip to content

Commit a81f307

Browse files
DanilaVaratyntsevlbalmaceda
authored andcommitted
Fixed an NPE on null map and list claims
1 parent 720ba88 commit a81f307

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/src/main/java/com/auth0/jwt/JWTCreator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public Builder withArrayClaim(String name, Long[] items) throws IllegalArgumentE
321321
public Builder withClaim(String name, Map<String, ?> map) throws IllegalArgumentException {
322322
assertNonNull(name);
323323
// validate map contents
324-
if (!validateClaim(map)) {
324+
if (map != null && !validateClaim(map)) {
325325
throw new IllegalArgumentException("Expected map containing Map, List, Boolean, Integer, Long, Double, String and Date");
326326
}
327327
addClaim(name, map);
@@ -345,7 +345,7 @@ public Builder withClaim(String name, Map<String, ?> map) throws IllegalArgument
345345
public Builder withClaim(String name, List<?> list) throws IllegalArgumentException {
346346
assertNonNull(name);
347347
// validate list contents
348-
if (!validateClaim(list)) {
348+
if (list != null && !validateClaim(list)) {
349349
throw new IllegalArgumentException("Expected list containing Map, List, Boolean, Integer, Long, Double, String and Date");
350350
}
351351
addClaim(name, list);

lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.security.interfaces.RSAPrivateKey;
1616
import java.util.*;
1717

18+
import static org.hamcrest.Matchers.anEmptyMap;
1819
import static org.hamcrest.Matchers.is;
1920
import static org.hamcrest.Matchers.notNullValue;
2021
import static org.junit.Assert.assertThat;
@@ -574,6 +575,40 @@ public void shouldAcceptCustomClaimForNullListItem() throws Exception {
574575
.sign(Algorithm.HMAC256("secret"));
575576
}
576577

578+
@Test
579+
@SuppressWarnings("unchecked")
580+
public void shouldAcceptCustomClaimWithNullMapAndRemoveClaim() throws Exception {
581+
String jwt = JWTCreator.init()
582+
.withClaim("map", "stubValue")
583+
.withClaim("map", (Map<String, ?>) null)
584+
.sign(Algorithm.HMAC256("secret"));
585+
586+
assertThat(jwt, is(notNullValue()));
587+
String[] parts = jwt.split("\\.");
588+
589+
String body = new String(Base64.decodeBase64(parts[1]), StandardCharsets.UTF_8);
590+
ObjectMapper mapper = new ObjectMapper();
591+
Map<String, Object> map = (Map<String, Object>) mapper.readValue(body, Map.class);
592+
assertThat(map, anEmptyMap());
593+
}
594+
595+
@Test
596+
@SuppressWarnings("unchecked")
597+
public void shouldAcceptCustomClaimWithNullListAndRemoveClaim() throws Exception {
598+
String jwt = JWTCreator.init()
599+
.withClaim("list", "stubValue")
600+
.withClaim("list", (List<String>) null)
601+
.sign(Algorithm.HMAC256("secret"));
602+
603+
assertThat(jwt, is(notNullValue()));
604+
String[] parts = jwt.split("\\.");
605+
606+
String body = new String(Base64.decodeBase64(parts[1]), StandardCharsets.UTF_8);
607+
ObjectMapper mapper = new ObjectMapper();
608+
Map<String, Object> map = (Map<String, Object>) mapper.readValue(body, Map.class);
609+
assertThat(map, anEmptyMap());
610+
}
611+
577612
@Test
578613
public void shouldRefuseCustomClaimForNullMapValue() throws Exception {
579614
Map<String, Object> data = new HashMap<>();

0 commit comments

Comments
 (0)