Skip to content

Commit 119134d

Browse files
committed
adds more checks for pre parse jwt
1 parent b93443c commit 119134d

File tree

1 file changed

+18
-5
lines changed
  • src/main/java/io/supertokens/session/jwt

1 file changed

+18
-5
lines changed

src/main/java/io/supertokens/session/jwt/JWT.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ private static void initHeader() {
5151
}
5252
}
5353

54-
public static String createAndSignLegacyAccessToken(JsonElement jsonObj, String privateSigningKey, AccessToken.VERSION version)
54+
public static String createAndSignLegacyAccessToken(JsonElement jsonObj, String privateSigningKey,
55+
AccessToken.VERSION version)
5556
throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, SignatureException {
5657
initHeader();
5758
String payload;
5859
String header;
5960
header = version == AccessToken.VERSION.V1 ? JWT.HEADERv1 : JWT.HEADERv2;
6061
payload = Utils.convertToBase64(jsonObj.toString());
61-
String signature = Utils.signWithPrivateKey(header + "." + payload, privateSigningKey, version != AccessToken.VERSION.V1 && version != AccessToken.VERSION.V2);
62+
String signature = Utils.signWithPrivateKey(header + "." + payload, privateSigningKey,
63+
version != AccessToken.VERSION.V1 && version != AccessToken.VERSION.V2);
6264
return header + "." + payload + "." + signature;
6365
}
6466

@@ -79,11 +81,17 @@ public static JWTPreParseInfo preParseJWTInfo(String jwt) throws JWTException {
7981

8082
JsonObject parsedHeader = new JsonParser().parse(Utils.convertFromBase64(splittedInput[0])).getAsJsonObject();
8183

84+
if (parsedHeader.get("typ") == null) {
85+
throw new JWTException("JWT header missing - typ");
86+
}
8287
JsonPrimitive typ = parsedHeader.get("typ").getAsJsonPrimitive();
8388
if (!typ.isString() || !typ.getAsString().equals("JWT")) {
8489
throw new JWTException("JWT header mismatch - typ");
8590
}
8691

92+
if (parsedHeader.get("alg") == null) {
93+
throw new JWTException("JWT header missing - alg");
94+
}
8795
JsonPrimitive alg = parsedHeader.get("alg").getAsJsonPrimitive();
8896
if (!alg.isString() || !alg.getAsString().equals("RS256")) {
8997
throw new JWTException("JWT header mismatch - alg");
@@ -103,6 +111,9 @@ public static JWTPreParseInfo preParseJWTInfo(String jwt) throws JWTException {
103111
}
104112

105113
JsonPrimitive kid = parsedHeader.get("kid").getAsJsonPrimitive();
114+
if (parsedHeader.get("kid") == null) {
115+
throw new JWTException("JWT header missing - kid");
116+
}
106117
if (!kid.isString()) {
107118
throw new JWTException("JWT header mismatch - kid");
108119
}
@@ -113,7 +124,8 @@ public static JWTInfo verifyJWTAndGetPayload(JWTPreParseInfo jwt, String publicS
113124
throws InvalidKeyException, NoSuchAlgorithmException, JWTException {
114125

115126
try {
116-
if (!Utils.verifyWithPublicKey(jwt.header + "." + jwt.payload, jwt.signature, publicSigningKey, jwt.version != AccessToken.VERSION.V1 && jwt.version != AccessToken.VERSION.V2)) {
127+
if (!Utils.verifyWithPublicKey(jwt.header + "." + jwt.payload, jwt.signature, publicSigningKey,
128+
jwt.version != AccessToken.VERSION.V1 && jwt.version != AccessToken.VERSION.V2)) {
117129
throw new JWTException("JWT verification failed");
118130
}
119131
} catch (InvalidKeySpecException | SignatureException e) {
@@ -124,7 +136,8 @@ public static JWTInfo verifyJWTAndGetPayload(JWTPreParseInfo jwt, String publicS
124136

125137
public static JWTInfo getPayloadWithoutVerifying(String jwt) throws JWTException {
126138
JWTPreParseInfo jwtInfo = preParseJWTInfo(jwt);
127-
return new JWTInfo(new JsonParser().parse(Utils.convertFromBase64(jwtInfo.payload)).getAsJsonObject(), jwtInfo.version);
139+
return new JWTInfo(new JsonParser().parse(Utils.convertFromBase64(jwtInfo.payload)).getAsJsonObject(),
140+
jwtInfo.version);
128141
}
129142

130143
public static class JWTException extends Exception {
@@ -150,7 +163,7 @@ public static class JWTPreParseInfo {
150163
@Nullable
151164
public final String kid;
152165

153-
public JWTPreParseInfo(String[] splittedInput, AccessToken.VERSION version, String kid) throws JWTException{
166+
public JWTPreParseInfo(String[] splittedInput, AccessToken.VERSION version, String kid) throws JWTException {
154167
if (splittedInput.length != 3) {
155168
throw new JWTException("Invalid JWT");
156169
}

0 commit comments

Comments
 (0)