Skip to content

Commit 452fd9a

Browse files
committed
Refactor to path query
1 parent b586f3e commit 452fd9a

File tree

3 files changed

+13
-41
lines changed

3 files changed

+13
-41
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/**
22
* @name Missing JWT signature check
33
* @description Failing to check the JWT signature may allow an attacker to forge their own tokens.
4-
* @kind problem
4+
* @kind path-problem
55
* @problem.severity error
6+
* @security-severity 7.8
67
* @precision high
78
* @id java/missing-jwt-signature-check
89
* @tags security
@@ -11,8 +12,9 @@
1112

1213
import java
1314
import semmle.code.java.security.MissingJWTSignatureCheckQuery
15+
import DataFlow::PathGraph
1416

15-
from JwtParserWithInsecureParseSink sink, JwtParserWithSigningKeyExpr parserExpr
16-
where sink.asExpr() = parserExpr
17-
select sink.getParseMethodAccess(), "A signing key is set $@, but the signature is not verified.",
18-
parserExpr.getSigningMethodAccess(), "here"
17+
from DataFlow::PathNode source, DataFlow::PathNode sink, MissingJwtSignatureCheckConf conf
18+
where conf.hasFlowPath(source, sink)
19+
select sink.getNode(), source, sink, "A signing key is set $@, but the signature is not verified.",
20+
source.getNode(), "here"

java/ql/src/semmle/code/java/security/MissingJWTSignatureCheckQuery.qll

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,12 @@ import semmle.code.java.dataflow.DataFlow
55
import semmle.code.java.dataflow.ExternalFlow
66
import semmle.code.java.security.JWT
77

8-
/**
9-
* An expr that is a (sub-type of) `JwtParser` for which a signing key has been set.
10-
*/
11-
class JwtParserWithSigningKeyExpr extends Expr {
12-
MethodAccess signingMa;
13-
14-
JwtParserWithSigningKeyExpr() { isSigningKeySetter(this, signingMa) }
15-
16-
/** Gets the method access that sets the signing key for this parser. */
17-
MethodAccess getSigningMethodAccess() { result = signingMa }
18-
}
19-
20-
/**
21-
* Holds if `signingMa` directly or indirectly sets a signing key for `expr`, which is a `JwtParser`.
22-
* The `setSigningKey` and `setSigningKeyResolver` methods set a signing key for a `JwtParser`.
23-
*
24-
* Directly means code like this (the signing key is set directly on a `JwtParser`):
25-
* ```java
26-
* Jwts.parser().setSigningKey(key).parse(token);
27-
* ```
28-
*
29-
* Indirectly means code like this (the signing key is set on a `JwtParserBuilder` indirectly setting the key of `JwtParser` that is created by the call to `build`):
30-
* ```java
31-
* Jwts.parserBuilder().setSigningKey(key).build().parse(token);
32-
* ```
33-
*/
34-
private predicate isSigningKeySetter(Expr expr, MethodAccess signingMa) {
35-
any(SigningToInsecureMethodAccessDataFlow s)
36-
.hasFlow(DataFlow::exprNode(signingMa), DataFlow::exprNode(expr))
37-
}
38-
398
/**
409
* Models flow from signing keys assignements to qualifiers of JWT insecure parsers.
41-
* This is used to determine whether a `JwtParser` has a signing key set.
10+
* This is used to determine whether a `JwtParser` performing unsafe parsing has a signing key set.
4211
*/
43-
private class SigningToInsecureMethodAccessDataFlow extends DataFlow::Configuration {
44-
SigningToInsecureMethodAccessDataFlow() { this = "SigningToExprDataFlow" }
12+
class MissingJwtSignatureCheckConf extends DataFlow::Configuration {
13+
MissingJwtSignatureCheckConf() { this = "SigningToExprDataFlow" }
4514

4615
override predicate isSource(DataFlow::Node source) {
4716
source instanceof JwtParserWithInsecureParseSource

java/ql/test/query-tests/security/CWE-347/MissingJWTSignatureCheckTest.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ class HasMissingJwtSignatureCheckTest extends InlineExpectationsTest {
99

1010
override predicate hasActualResult(Location location, string element, string tag, string value) {
1111
tag = "hasMissingJwtSignatureCheck" and
12-
exists(JwtParserWithInsecureParseSink sink, JwtParserWithSigningKeyExpr parserExpr |
13-
sink.asExpr() = parserExpr and
12+
exists(DataFlow::Node source, DataFlow::Node sink, MissingJwtSignatureCheckConf conf |
13+
conf.hasFlow(source, sink)
14+
|
1415
sink.getLocation() = location and
1516
element = sink.toString() and
1617
value = ""

0 commit comments

Comments
 (0)