|
10 | 10 | */
|
11 | 11 |
|
12 | 12 | import java
|
13 |
| -import semmle.code.java.dataflow.DataFlow |
| 13 | +import semmle.code.java.security.JWT |
14 | 14 |
|
15 |
| -/** The interface `io.jsonwebtoken.JwtParser`. */ |
16 |
| -class TypeJwtParser extends Interface { |
17 |
| - TypeJwtParser() { this.hasQualifiedName("io.jsonwebtoken", "JwtParser") } |
18 |
| -} |
19 |
| - |
20 |
| -/** The interface `io.jsonwebtoken.JwtParser` or a type derived from it. */ |
21 |
| -class TypeDerivedJwtParser extends RefType { |
22 |
| - TypeDerivedJwtParser() { this.getASourceSupertype*() instanceof TypeJwtParser } |
23 |
| -} |
24 |
| - |
25 |
| -/** The interface `io.jsonwebtoken.JwtParserBuilder`. */ |
26 |
| -class TypeJwtParserBuilder extends Interface { |
27 |
| - TypeJwtParserBuilder() { this.hasQualifiedName("io.jsonwebtoken", "JwtParserBuilder") } |
28 |
| -} |
29 |
| - |
30 |
| -/** The interface `io.jsonwebtoken.JwtHandler`. */ |
31 |
| -class TypeJwtHandler extends Interface { |
32 |
| - TypeJwtHandler() { this.hasQualifiedName("io.jsonwebtoken", "JwtHandler") } |
33 |
| -} |
34 |
| - |
35 |
| -/** The class `io.jsonwebtoken.JwtHandlerAdapter`. */ |
36 |
| -class TypeJwtHandlerAdapter extends Class { |
37 |
| - TypeJwtHandlerAdapter() { this.hasQualifiedName("io.jsonwebtoken", "JwtHandlerAdapter") } |
38 |
| -} |
39 |
| - |
40 |
| -/** The `parse(token, handler)` method defined in `JwtParser`. */ |
41 |
| -private class JwtParserParseHandlerMethod extends Method { |
42 |
| - JwtParserParseHandlerMethod() { |
43 |
| - this.hasName("parse") and |
44 |
| - this.getDeclaringType() instanceof TypeJwtParser and |
45 |
| - this.getNumberOfParameters() = 2 |
46 |
| - } |
47 |
| -} |
48 |
| - |
49 |
| -/** The `parse(token)`, `parseClaimsJwt(token)` and `parsePlaintextJwt(token)` methods defined in `JwtParser`. */ |
50 |
| -private class JwtParserInsecureParseMethod extends Method { |
51 |
| - JwtParserInsecureParseMethod() { |
52 |
| - this.hasName(["parse", "parseClaimsJwt", "parsePlaintextJwt"]) and |
53 |
| - this.getNumberOfParameters() = 1 and |
54 |
| - this.getDeclaringType() instanceof TypeJwtParser |
55 |
| - } |
56 |
| -} |
57 |
| - |
58 |
| -/** The `on(Claims|Plaintext)Jwt` methods defined in `JwtHandler`. */ |
59 |
| -private class JwtHandlerOnJwtMethod extends Method { |
60 |
| - JwtHandlerOnJwtMethod() { |
61 |
| - this.hasName(["onClaimsJwt", "onPlaintextJwt"]) and |
62 |
| - this.getNumberOfParameters() = 1 and |
63 |
| - this.getDeclaringType() instanceof TypeJwtHandler |
64 |
| - } |
65 |
| -} |
66 |
| - |
67 |
| -/** The `on(Claims|Plaintext)Jwt` methods defined in `JwtHandlerAdapter`. */ |
68 |
| -private class JwtHandlerAdapterOnJwtMethod extends Method { |
69 |
| - JwtHandlerAdapterOnJwtMethod() { |
70 |
| - this.hasName(["onClaimsJwt", "onPlaintextJwt"]) and |
71 |
| - this.getNumberOfParameters() = 1 and |
72 |
| - this.getDeclaringType() instanceof TypeJwtHandlerAdapter |
73 |
| - } |
74 |
| -} |
75 |
| - |
76 |
| -/** |
77 |
| - * Holds if `parseHandlerExpr` is an insecure `JwtHandler`. |
78 |
| - * That is, it overrides a method from `JwtHandlerOnJwtMethod` and the override is not defined on `JwtHandlerAdapter`. |
79 |
| - * `JwtHandlerAdapter`'s overrides are safe since they always throw an exception. |
80 |
| - */ |
81 |
| -private predicate isInsecureJwtHandler(Expr parseHandlerExpr) { |
82 |
| - exists(RefType t | |
83 |
| - parseHandlerExpr.getType() = t and |
84 |
| - t.getASourceSupertype*() instanceof TypeJwtHandler and |
85 |
| - exists(Method m | |
86 |
| - m = t.getAMethod() and |
87 |
| - m.getASourceOverriddenMethod+() instanceof JwtHandlerOnJwtMethod and |
88 |
| - not m.getSourceDeclaration() instanceof JwtHandlerAdapterOnJwtMethod |
89 |
| - ) |
90 |
| - ) |
91 |
| -} |
92 |
| - |
93 |
| -/** |
94 |
| - * An access to an insecure parsing method. |
95 |
| - * That is, either a call to a `parse(token)`, `parseClaimsJwt(token)` or `parsePlaintextJwt(token)` method or |
96 |
| - * a call to a `parse(token, handler)` method where the `handler` is considered insecure. |
97 |
| - */ |
98 |
| -private class JwtParserInsecureParseMethodAccess extends MethodAccess { |
99 |
| - JwtParserInsecureParseMethodAccess() { |
100 |
| - this.getMethod().getASourceOverriddenMethod*() instanceof JwtParserInsecureParseMethod |
101 |
| - or |
102 |
| - this.getMethod().getASourceOverriddenMethod*() instanceof JwtParserParseHandlerMethod and |
103 |
| - isInsecureJwtHandler(this.getArgument(1)) |
104 |
| - } |
105 |
| -} |
106 |
| - |
107 |
| -/** |
108 |
| - * Holds if `signingMa` directly or indirectly sets a signing key for `expr`, which is a `JwtParser`. |
109 |
| - * The `setSigningKey` and `setSigningKeyResolver` methods set a signing key for a `JwtParser`. |
110 |
| - * Directly means code like this: |
111 |
| - * ```java |
112 |
| - * Jwts.parser().setSigningKey(key).parse(token); |
113 |
| - * ``` |
114 |
| - * Here the signing key is set directly on a `JwtParser`. |
115 |
| - * Indirectly means code like this: |
116 |
| - * ```java |
117 |
| - * Jwts.parserBuilder().setSigningKey(key).build().parse(token); |
118 |
| - * ``` |
119 |
| - * In this case, the signing key is set on a `JwtParserBuilder` indirectly setting the key of `JwtParser` that is created by the call to `build`. |
120 |
| - */ |
121 |
| -private predicate isSigningKeySetter(Expr expr, MethodAccess signingMa) { |
122 |
| - any(SigningToInsecureMethodAccessDataFlow s) |
123 |
| - .hasFlow(DataFlow::exprNode(signingMa), DataFlow::exprNode(expr)) |
124 |
| -} |
125 |
| - |
126 |
| -/** |
127 |
| - * An expr that is a (sub-type of) `JwtParser` for which a signing key has been set and which is used as |
128 |
| - * the qualifier to a `JwtParserInsecureParseMethodAccess`. |
129 |
| - */ |
130 |
| -private class JwtParserWithSigningKeyExpr extends Expr { |
131 |
| - MethodAccess signingMa; |
132 |
| - |
133 |
| - JwtParserWithSigningKeyExpr() { |
134 |
| - this.getType() instanceof TypeDerivedJwtParser and |
135 |
| - isSigningKeySetter(this, signingMa) |
136 |
| - } |
137 |
| - |
138 |
| - /** Gets the method access that sets the signing key for this parser. */ |
139 |
| - MethodAccess getSigningMethodAccess() { result = signingMa } |
140 |
| -} |
141 |
| - |
142 |
| -/** |
143 |
| - * Models flow from `SigningKeyMethodAccess`es to qualifiers of `JwtParserInsecureParseMethodAccess`es. |
144 |
| - * This is used to determine whether a `JwtParser` has a signing key set. |
145 |
| - */ |
146 |
| -private class SigningToInsecureMethodAccessDataFlow extends DataFlow::Configuration { |
147 |
| - SigningToInsecureMethodAccessDataFlow() { this = "SigningToExprDataFlow" } |
148 |
| - |
149 |
| - override predicate isSource(DataFlow::Node source) { |
150 |
| - source.asExpr() instanceof SigningKeyMethodAccess |
151 |
| - } |
152 |
| - |
153 |
| - override predicate isSink(DataFlow::Node sink) { |
154 |
| - any(JwtParserInsecureParseMethodAccess ma).getQualifier() = sink.asExpr() |
155 |
| - } |
156 |
| - |
157 |
| - /** Models the builder style of `JwtParser` and `JwtParserBuilder`. */ |
158 |
| - override predicate isAdditionalFlowStep(DataFlow::Node pred, DataFlow::Node succ) { |
159 |
| - ( |
160 |
| - pred.asExpr().getType() instanceof TypeDerivedJwtParser or |
161 |
| - pred.asExpr().getType().(RefType).getASourceSupertype*() instanceof TypeJwtParserBuilder |
162 |
| - ) and |
163 |
| - succ.asExpr().(MethodAccess).getQualifier() = pred.asExpr() |
164 |
| - } |
165 |
| -} |
166 |
| - |
167 |
| -/** An access to the `setSigningKey` or `setSigningKeyResolver` method (or an overridden method) defined in `JwtParser` and `JwtParserBuilder`. */ |
168 |
| -private class SigningKeyMethodAccess extends MethodAccess { |
169 |
| - SigningKeyMethodAccess() { |
170 |
| - exists(Method m | |
171 |
| - m.hasName(["setSigningKey", "setSigningKeyResolver"]) and |
172 |
| - m.getNumberOfParameters() = 1 and |
173 |
| - ( |
174 |
| - m.getDeclaringType() instanceof TypeJwtParser or |
175 |
| - m.getDeclaringType() instanceof TypeJwtParserBuilder |
176 |
| - ) |
177 |
| - | |
178 |
| - m = this.getMethod().getASourceOverriddenMethod*() |
179 |
| - ) |
180 |
| - } |
181 |
| -} |
182 |
| - |
183 |
| -/** |
184 |
| - * Holds if the `MethodAccess` `ma` occurs in a test file. A test file is any file that |
185 |
| - * is a direct or indirect child of a directory named `test`, ignoring case. |
186 |
| - */ |
187 |
| -private predicate isInTestFile(MethodAccess ma) { |
188 |
| - exists(string lowerCasedAbsolutePath | |
189 |
| - lowerCasedAbsolutePath = ma.getLocation().getFile().getAbsolutePath().toLowerCase() |
190 |
| - | |
191 |
| - lowerCasedAbsolutePath.matches("%/test/%") and |
192 |
| - not lowerCasedAbsolutePath |
193 |
| - .matches("%/ql/test/query-tests/security/CWE-347/%".toLowerCase()) |
194 |
| - ) |
195 |
| -} |
196 |
| - |
197 |
| -from JwtParserInsecureParseMethodAccess ma, JwtParserWithSigningKeyExpr parserExpr |
198 |
| -where ma.getQualifier() = parserExpr and not isInTestFile(ma) |
199 |
| -select ma, "A signing key is set $@, but the signature is not verified.", |
| 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.", |
200 | 18 | parserExpr.getSigningMethodAccess(), "here"
|
0 commit comments