Skip to content

Commit 6d02b00

Browse files
authored
Fix NPE when key null and using JWT tokens with RS256 or ES256 algorithms(#59)
thanks to @DolphFlynn
1 parent 80198fa commit 6d02b00

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/app/algorithm/AlgorithmLinker.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import app.helpers.KeyHelper;
2020
import app.helpers.Output;
2121

22+
import static org.apache.commons.lang.StringUtils.isNotEmpty;
23+
2224
public class AlgorithmLinker {
2325

2426
public static final String[] keyBeginMarkers = new String[]{"-----BEGIN PUBLIC KEY-----",
@@ -53,7 +55,7 @@ public class AlgorithmLinker {
5355

5456
private static PublicKey generatePublicKeyFromString(String key, String algorithm) {
5557
PublicKey publicKey = null;
56-
if (key.length() > 1) {
58+
if (isNotEmpty(key)) {
5759
key = cleanKey(key);
5860
byte[] keyByteArray = java.util.Base64.getDecoder().decode(key);
5961
try {

src/app/helpers/KeyHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import app.algorithm.AlgorithmLinker;
1717
import app.algorithm.AlgorithmType;
1818

19+
import static org.apache.commons.lang.StringUtils.isNotEmpty;
20+
1921
public class KeyHelper {
2022

2123
public static final String[] keyHeaderBeginMarkers = new String[]{"-----BEGIN PUBLIC KEY-----",
@@ -52,7 +54,7 @@ public static String getRandomKey(String algorithm) {
5254

5355
public static PrivateKey generatePrivateKeyFromString(String key, String algorithm) {
5456
PrivateKey privateKey = null;
55-
if (key.length() > 1) {
57+
if (isNotEmpty(key)) {
5658
key = cleanKey(key);
5759
try {
5860
byte[] keyByteArray = Base64.decodeBase64(key);

test/app/TestAlgorithmLinker.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app;
22

33
import java.io.UnsupportedEncodingException;
4+
import java.security.Key;
45

56
import org.junit.Test;
67

@@ -11,6 +12,8 @@
1112
import app.algorithm.AlgorithmLinker;
1213
import model.CustomJWToken;
1314

15+
import static org.junit.Assert.assertNull;
16+
1417
public class TestAlgorithmLinker {
1518

1619
@Test
@@ -44,4 +47,32 @@ public void testESWithFalseKey() throws IllegalArgumentException, UnsupportedEnc
4447
DecodedJWT test = verifier.verify(TestTokens.es256_token);
4548
test.getAlgorithm();
4649
}
50+
51+
@Test
52+
public void testGetKeyInstanceWithNullKeyForPublicRSA() {
53+
Key key = AlgorithmLinker.getKeyInstance(null, "RSA", false);
54+
55+
assertNull(key);
56+
}
57+
58+
@Test
59+
public void testGetKeyInstanceWithNullKeyForPublicEC() {
60+
Key key = AlgorithmLinker.getKeyInstance(null, "EC", false);
61+
62+
assertNull(key);
63+
}
64+
65+
@Test
66+
public void testGetKeyInstanceWithNullKeyForPrivateRSA() {
67+
Key key = AlgorithmLinker.getKeyInstance(null, "RSA", true);
68+
69+
assertNull(key);
70+
}
71+
72+
@Test
73+
public void testGetKeyInstanceWithNullKeyForPrivateEC() {
74+
Key key = AlgorithmLinker.getKeyInstance(null, "EC", true);
75+
76+
assertNull(key);
77+
}
4778
}

0 commit comments

Comments
 (0)