Skip to content

Commit cbe8eeb

Browse files
committed
fix NPE when missing expected array claim
1 parent 5fa3fe3 commit cbe8eeb

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public Verification withArrayClaim(String name, Integer... items) throws Illegal
168168
requireClaim(name, items);
169169
return this;
170170
}
171-
171+
172172
/**
173173
* Require a specific Array Claim to contain at least the given items.
174174
*
@@ -178,11 +178,11 @@ public Verification withArrayClaim(String name, Integer... items) throws Illegal
178178
* @throws IllegalArgumentException if the name is null.
179179
*/
180180
@Override
181-
public Verification withArrayClaim(String name, Long ... items) throws IllegalArgumentException {
181+
public Verification withArrayClaim(String name, Long... items) throws IllegalArgumentException {
182182
assertNonNull(name);
183183
requireClaim(name, items);
184184
return this;
185-
}
185+
}
186186

187187
@Override
188188
public JWTVerifier build() {
@@ -220,7 +220,7 @@ private void addLeewayToDateClaims() {
220220
if (!claims.containsKey(PublicClaims.NOT_BEFORE)) {
221221
claims.put(PublicClaims.NOT_BEFORE, defaultLeeway);
222222
}
223-
if(ignoreIssuedAt) {
223+
if (ignoreIssuedAt) {
224224
claims.remove(PublicClaims.ISSUED_AT);
225225
return;
226226
}
@@ -329,18 +329,18 @@ private void assertValidClaim(Claim claim, String claimName, Object value) {
329329
Object[] claimAsObject = claim.as(Object[].class);
330330

331331
// Jackson uses 'natural' mapping which uses Integer if value fits in 32 bits.
332-
if(value instanceof Long[]) {
332+
if (value instanceof Long[]) {
333333
// convert Integers to Longs for comparison with equals
334334
claimArr = new ArrayList<>(claimAsObject.length);
335-
for(Object cao : claimAsObject) {
336-
if(cao instanceof Integer) {
337-
claimArr.add(((Integer)cao).longValue());
335+
for (Object cao : claimAsObject) {
336+
if (cao instanceof Integer) {
337+
claimArr.add(((Integer) cao).longValue());
338338
} else {
339339
claimArr.add(cao);
340340
}
341341
}
342342
} else {
343-
claimArr = Arrays.asList(claim.as(Object[].class));
343+
claimArr = claim.isNull() ? Collections.emptyList() : Arrays.asList(claim.as(Object[].class));
344344
}
345345
List<Object> valueArr = Arrays.asList((Object[]) value);
346346
isValid = claimArr.containsAll(valueArr);

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,28 @@ public void shouldThrowOnNullCustomClaimName() throws Exception {
170170
.withClaim(null, "value");
171171
}
172172

173+
@Test
174+
public void shouldThrowWhenExpectedArrayClaimIsMissing() throws Exception {
175+
exception.expect(InvalidClaimException.class);
176+
exception.expectMessage("The Claim 'missing' value doesn't match the required one.");
177+
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcnJheSI6WzEsMiwzXX0.wKNFBcMdwIpdF9rXRxvexrzSM6umgSFqRO1WZj992YM";
178+
JWTVerifier.init(Algorithm.HMAC256("secret"))
179+
.withArrayClaim("missing", 1, 2, 3)
180+
.build()
181+
.verify(token);
182+
}
183+
184+
@Test
185+
public void shouldThrowWhenExpectedClaimIsMissing() throws Exception {
186+
exception.expect(InvalidClaimException.class);
187+
exception.expectMessage("The Claim 'missing' value doesn't match the required one.");
188+
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGFpbSI6InRleHQifQ.aZ27Ze35VvTqxpaSIK5ZcnYHr4SrvANlUbDR8fw9qsQ";
189+
JWTVerifier.init(Algorithm.HMAC256("secret"))
190+
.withClaim("missing", "text")
191+
.build()
192+
.verify(token);
193+
}
194+
173195
@Test
174196
public void shouldThrowOnInvalidCustomClaimValueOfTypeString() throws Exception {
175197
exception.expect(InvalidClaimException.class);
@@ -546,8 +568,8 @@ public void shouldThrowOnNegativeNotBeforeLeeway() throws Exception {
546568
.acceptNotBefore(-1);
547569
}
548570

549-
// Issued At with future date
550-
@Test (expected = InvalidClaimException.class)
571+
// Issued At with future date
572+
@Test(expected = InvalidClaimException.class)
551573
public void shouldThrowOnFutureIssuedAt() throws Exception {
552574
Clock clock = mock(Clock.class);
553575
when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE - 1000));

0 commit comments

Comments
 (0)