Skip to content

Commit 97eb7b7

Browse files
committed
update example to include more logical vulnerable pattern, add documentations for ql classes
1 parent 664890a commit 97eb7b7

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

java/ql/src/experimental/Security/CWE/CWE-347/Auth0NoVerifier.ql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,29 @@ module JwtAuth0 {
2727
JWTVerifierType() { this.hasQualifiedName("com.auth0.jwt", "JWTVerifier") }
2828
}
2929

30+
/**
31+
* A Method that returns a Decoded Claim of JWT
32+
*/
3033
class GetPayload extends MethodAccess {
3134
GetPayload() {
3235
this.getCallee().getDeclaringType() instanceof PayloadType and
3336
this.getCallee().hasName(["getClaim", "getIssuedAt"])
3437
}
3538
}
3639

40+
/**
41+
* A Method that Decode JWT without signature verification
42+
*/
3743
class Decode extends MethodAccess {
3844
Decode() {
3945
this.getCallee().getDeclaringType() instanceof JWTType and
4046
this.getCallee().hasName("decode")
4147
}
4248
}
4349

50+
/**
51+
* A Method that Decode JWT with signature verification
52+
*/
4453
class Verify extends MethodAccess {
4554
Verify() {
4655
this.getCallee().getDeclaringType() instanceof JWTVerifierType and

java/ql/src/experimental/Security/CWE/CWE-347/Example.java

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.*;
44
import java.security.NoSuchAlgorithmException;
5+
import java.util.Objects;
56
import java.util.Optional;
67
import javax.crypto.KeyGenerator;
78
import javax.servlet.http.*;
@@ -13,32 +14,50 @@
1314
import com.auth0.jwt.exceptions.JWTVerificationException;
1415
import com.auth0.jwt.interfaces.DecodedJWT;
1516

16-
@WebServlet(name = "Jwt", value = "/Auth")
17+
@WebServlet(name = "JwtTest1", value = "/Auth")
1718
public class auth0 extends HttpServlet {
1819

19-
public void doPost(HttpServletRequest request, HttpServletResponse response) {}
20+
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
21+
response.setContentType("text/html");
22+
PrintWriter out = response.getWriter();
23+
24+
// OK: first decode without signature verification
25+
// and then verify with signature verification
26+
String JwtToken1 = request.getParameter("JWT1");
27+
String userName = decodeToken(JwtToken1);
28+
verifyToken(JwtToken1, "A Securely generated Key");
29+
if (Objects.equals(userName, "Admin")) {
30+
out.println("<html><body>");
31+
out.println("<h1>" + "heyyy Admin" + "</h1>");
32+
out.println("</body></html>");
33+
}
2034

21-
final String JWT_KEY = "KEY";
35+
out.println("<html><body>");
36+
out.println("<h1>" + "heyyy Nobody" + "</h1>");
37+
out.println("</body></html>");
38+
}
2239

2340
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
41+
response.setContentType("text/html");
42+
PrintWriter out = response.getWriter();
2443

25-
// OK
26-
String JwtToken1 = request.getParameter("JWT1");
27-
decodeToken(JwtToken1);
28-
try {
29-
verifyToken(JwtToken1, getSecureRandomKey());
30-
} catch (NoSuchAlgorithmException e) {
31-
throw new RuntimeException(e);
44+
// NOT OK: only decode, no verification
45+
String JwtToken2 = request.getParameter("JWT2");
46+
String userName = decodeToken(JwtToken2);
47+
if (Objects.equals(userName, "Admin")) {
48+
out.println("<html><body>");
49+
out.println("<h1>" + "heyyy Admin" + "</h1>");
50+
out.println("</body></html>");
3251
}
3352

34-
// only decode, no verification
35-
String JwtToken2 = request.getParameter("JWT2");
36-
decodeToken(JwtToken2);
53+
// OK: no clue of the use of unsafe decoded JWT return value
54+
JwtToken2 = request.getParameter("JWT2");
55+
JWT.decode(JwtToken2);
3756

3857

39-
response.setContentType("text/html");
40-
PrintWriter out = response.getWriter();
41-
out.println("<html><body>heyyy</body></html>");
58+
out.println("<html><body>");
59+
out.println("<h1>" + "heyyy Nobody" + "</h1>");
60+
out.println("</body></html>");
4261
}
4362

4463
public static boolean verifyToken(final String token, final String key) {
@@ -52,8 +71,10 @@ public static boolean verifyToken(final String token, final String key) {
5271
return false;
5372
}
5473

74+
5575
public static String decodeToken(final String token) {
5676
DecodedJWT jwt = JWT.decode(token);
5777
return Optional.of(jwt).map(item -> item.getClaim("userName").asString()).orElse("");
5878
}
79+
5980
}

0 commit comments

Comments
 (0)