Skip to content

Commit

Permalink
Fixed an NPE on null map and list claims
Browse files Browse the repository at this point in the history
  • Loading branch information
DanilaVaratyntsev authored and lbalmaceda committed Apr 23, 2020
1 parent 720ba88 commit a81f307
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/src/main/java/com/auth0/jwt/JWTCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public Builder withArrayClaim(String name, Long[] items) throws IllegalArgumentE
public Builder withClaim(String name, Map<String, ?> map) throws IllegalArgumentException {
assertNonNull(name);
// validate map contents
if (!validateClaim(map)) {
if (map != null && !validateClaim(map)) {
throw new IllegalArgumentException("Expected map containing Map, List, Boolean, Integer, Long, Double, String and Date");
}
addClaim(name, map);
Expand All @@ -345,7 +345,7 @@ public Builder withClaim(String name, Map<String, ?> map) throws IllegalArgument
public Builder withClaim(String name, List<?> list) throws IllegalArgumentException {
assertNonNull(name);
// validate list contents
if (!validateClaim(list)) {
if (list != null && !validateClaim(list)) {
throw new IllegalArgumentException("Expected list containing Map, List, Boolean, Integer, Long, Double, String and Date");
}
addClaim(name, list);
Expand Down
35 changes: 35 additions & 0 deletions lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.security.interfaces.RSAPrivateKey;
import java.util.*;

import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -574,6 +575,40 @@ public void shouldAcceptCustomClaimForNullListItem() throws Exception {
.sign(Algorithm.HMAC256("secret"));
}

@Test
@SuppressWarnings("unchecked")
public void shouldAcceptCustomClaimWithNullMapAndRemoveClaim() throws Exception {
String jwt = JWTCreator.init()
.withClaim("map", "stubValue")
.withClaim("map", (Map<String, ?>) null)
.sign(Algorithm.HMAC256("secret"));

assertThat(jwt, is(notNullValue()));
String[] parts = jwt.split("\\.");

String body = new String(Base64.decodeBase64(parts[1]), StandardCharsets.UTF_8);
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = (Map<String, Object>) mapper.readValue(body, Map.class);
assertThat(map, anEmptyMap());
}

@Test
@SuppressWarnings("unchecked")
public void shouldAcceptCustomClaimWithNullListAndRemoveClaim() throws Exception {
String jwt = JWTCreator.init()
.withClaim("list", "stubValue")
.withClaim("list", (List<String>) null)
.sign(Algorithm.HMAC256("secret"));

assertThat(jwt, is(notNullValue()));
String[] parts = jwt.split("\\.");

String body = new String(Base64.decodeBase64(parts[1]), StandardCharsets.UTF_8);
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = (Map<String, Object>) mapper.readValue(body, Map.class);
assertThat(map, anEmptyMap());
}

@Test
public void shouldRefuseCustomClaimForNullMapValue() throws Exception {
Map<String, Object> data = new HashMap<>();
Expand Down

0 comments on commit a81f307

Please sign in to comment.