-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathJsonWebTokenHandlerLib.qll
More file actions
270 lines (248 loc) · 9.05 KB
/
JsonWebTokenHandlerLib.qll
File metadata and controls
270 lines (248 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import csharp
import DataFlow
/**
* A sensitive property for `TokenValidationParameters` that updates the underlying value.
*/
class TokenValidationParametersPropertySensitiveValidation extends Property {
TokenValidationParametersPropertySensitiveValidation() {
exists(Property p, Class c |
c.hasQualifiedName("Microsoft.IdentityModel.Tokens.TokenValidationParameters")
|
p = this and
c.getAProperty() = p and
p.getName() in [
"ValidateIssuer", "ValidateAudience", "ValidateLifetime", "RequireExpirationTime",
"RequireAudience"
]
)
}
}
/**
* A dataflow from a `false` value to a write sensitive property for `TokenValidationParameters`.
*/
class FalseValueFlowsToTokenValidationParametersPropertyWriteToBypassValidation extends DataFlow::Configuration {
FalseValueFlowsToTokenValidationParametersPropertyWriteToBypassValidation() {
this = "FlowsToTokenValidationResultIsValidCall"
}
override predicate isSource(DataFlow::Node source) {
source.asExpr().getValue() = "false" and
source.asExpr().getType() instanceof BoolType
}
override predicate isSink(DataFlow::Node sink) {
exists(Assignment a |
sink.asExpr() = a.getRValue() and
a.getLValue().(PropertyAccess).getProperty() instanceof TokenValidationParametersPropertySensitiveValidation
)
}
}
/**
* Holds if `assemblyName` is older than version `ver`
*/
bindingset[ver]
predicate isAssemblyOlderVersion(string assemblyName, string ver) {
exists(Assembly a |
a.getName() = assemblyName and
a.getVersion().isEarlierThan(ver)
)
}
/**
* A method `ValidateToken` for `Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler` or other Token handler that shares the same behavior characteristics
*/
class JsonWebTokenHandlerValidateTokenMethod extends Method {
JsonWebTokenHandlerValidateTokenMethod() {
this.hasQualifiedName("Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateToken") or
this.hasQualifiedName("Microsoft.AzureAD.DeviceIdentification.Common.Tokens.JwtValidator.ValidateEncryptedToken")
//// TODO: ValidateEncryptedToken has the same behavior than ValidateToken, but it will be changed in a future release
//// The line below would allow to check if the ValidateEncryptedToken version used meets the minimum requirement
//// Once we have the fixed assembly version we can uncomment the line below
// and isAssemblyOlderVersion("Microsoft.AzureAD.DeviceIdentification", "0.0.0")
}
}
/**
* A Call to `Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateToken`
*/
class JsonWebTokenHandlerValidateTokenCall extends MethodCall {
JsonWebTokenHandlerValidateTokenCall() {
this.getTarget() instanceof JsonWebTokenHandlerValidateTokenMethod
}
}
/**
* A read access for properties `IsValid` or `Exception` for `Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateToken`
*/
private class TokenValidationResultIsValidCall extends PropertyRead {
TokenValidationResultIsValidCall() {
exists(Property p | p.getAnAccess().(PropertyRead) = this |
p.hasName("IsValid") or
p.hasName("Exception")
)
}
}
/**
* Dataflow from the output of `Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateToken` call to access the `IsValid` or `Exception` property
*/
private class FlowsToTokenValidationResultIsValidCall extends DataFlow::Configuration {
FlowsToTokenValidationResultIsValidCall() { this = "FlowsToTokenValidationResultIsValidCall" }
override predicate isSource(DataFlow::Node source) {
source.asExpr() instanceof JsonWebTokenHandlerValidateTokenCall
}
override predicate isSink(DataFlow::Node sink) {
exists(TokenValidationResultIsValidCall call | sink.asExpr() = call.getQualifier())
}
}
/**
* A security-sensitive property for `Microsoft.IdentityModel.Tokens.TokenValidationParameters`
*/
class TokenValidationParametersProperty extends Property {
TokenValidationParametersProperty() {
exists(Property p, Class c |
c.hasQualifiedName("Microsoft.IdentityModel.Tokens.TokenValidationParameters")
|
p = this and
c.getAProperty() = p and
p.getName() in [
"SignatureValidator", "TokenReplayValidator", "AlgorithmValidator", "AudienceValidator",
"IssuerSigningKeyValidator", "LifetimeValidator"
]
)
}
}
/**
* Holds if the callable has a return statement and it always returns true for all such statements
*/
predicate callableHasAReturnStmtAndAlwaysReturnsTrue(Callable c) {
c.getReturnType() instanceof BoolType and
not callableMayThrowException(c) and
forall(ReturnStmt rs | rs.getEnclosingCallable() = c |
rs.getNumberOfChildren() = 1 and
isExpressionAlwaysTrue(rs.getChildExpr(0))
) and
exists(ReturnStmt rs | rs.getEnclosingCallable() = c)
}
/**
* Holds if the lambda expression `le` always returns true
*/
predicate lambdaExprReturnsOnlyLiteralTrue(AnonymousFunctionExpr le) {
le.getExpressionBody().(BoolLiteral).getBoolValue() = true
or
// special scenarios where the expression is not a `BoolLiteral`, but it will evaluatue to `true`
exists(Expr e | le.getExpressionBody() = e |
not e instanceof Call and
not e instanceof Literal and
e.getType() instanceof BoolType and
e.getValue() = "true"
)
}
class CallableAlwaysReturnsTrue extends Callable {
CallableAlwaysReturnsTrue() {
callableHasAReturnStmtAndAlwaysReturnsTrue(this)
or
lambdaExprReturnsOnlyLiteralTrue(this)
or
exists(AnonymousFunctionExpr le, Call call, Callable callable |
this = le
|
callable.getACall() = call and
call = le.getExpressionBody() and
callableHasAReturnStmtAndAlwaysReturnsTrue(callable)
)
}
}
/**
* Holds if any exception being thrown by the callable is of type `System.ArgumentNullException`
* It will also hold if no exceptions are thrown by the callable
*/
predicate callableOnlyThrowsArgumentNullException(Callable c) {
forall(ThrowElement thre | c = thre.getEnclosingCallable() |
thre.getThrownExceptionType().hasQualifiedName("System.ArgumentNullException")
)
}
/**
* A specialization of `CallableAlwaysReturnsTrue` that takes into consideration exceptions being thrown for higher precision.
*/
class CallableAlwaysReturnsTrueHigherPrecision extends CallableAlwaysReturnsTrue {
CallableAlwaysReturnsTrueHigherPrecision() {
callableOnlyThrowsArgumentNullException(this) and
(
forall(Call call, Callable callable | call.getEnclosingCallable() = this |
callable.getACall() = call and
callable instanceof CallableAlwaysReturnsTrueHigherPrecision
)
or
exists(AnonymousFunctionExpr le, Call call, CallableAlwaysReturnsTrueHigherPrecision cat |
this = le
|
le.canReturn(call) and
cat.getACall() = call
)
or
exists(LambdaExpr le | le = this |
le.getBody() instanceof CallableAlwaysReturnsTrueHigherPrecision
)
)
}
}
/**
* A callable that returns a `string` and has a `string` as 1st argument
*/
private class CallableReturnsStringAndArg0IsString extends Callable {
CallableReturnsStringAndArg0IsString() {
this.getReturnType() instanceof StringType and
this.getParameter(0).getType() instanceof StringType
}
}
/**
* A Callable that always return the 1st argument, both of `string` type
*/
class CallableAlwaysReturnsParameter0 extends CallableReturnsStringAndArg0IsString {
CallableAlwaysReturnsParameter0() {
forex(Expr ret | this.canReturn(ret) |
ret = this.getParameter(0).getAnAccess()
or
exists(CallableAlwaysReturnsParameter0 c |
ret = c.getACall() and
ret.(Call).getArgument(0) = this.getParameter(0).getAnAccess()
)
)
}
}
/**
* A Callable that always return the 1st argument, both of `string` type. Higher precision
*/
class CallableAlwaysReturnsParameter0MayThrowExceptions extends CallableReturnsStringAndArg0IsString {
CallableAlwaysReturnsParameter0MayThrowExceptions() {
forex(Expr ret | this.canReturn(ret) |
ret = this.getParameter(0).getAnAccess()
or
exists(CallableAlwaysReturnsParameter0MayThrowExceptions c |
ret = c.getACall() and
ret.(Call).getArgument(0) = this.getParameter(0).getAnAccess()
)
)
}
}
/**
* Hold if the `Expr` e is a `BoolLiteral` with value true,
* the expression has a predictable value == `true`,
* or if it is a `ConditionalExpr` where the `then` and `else` expressions meet `isExpressionAlwaysTrue` criteria
*/
predicate isExpressionAlwaysTrue(Expr e) {
e.(BoolLiteral).getBoolValue() = true
or
e.(Expr).getValue() = "true"
or
e instanceof ConditionalExpr and
isExpressionAlwaysTrue(e.(ConditionalExpr).getThen()) and
isExpressionAlwaysTrue(e.(ConditionalExpr).getElse())
or
exists(Callable callable |
callableHasAReturnStmtAndAlwaysReturnsTrue(callable) and
callable.getACall() = e
)
}
/**
* Holds if the `Callable` c throws any exception other than `ThrowsArgumentNullException`
*/
predicate callableMayThrowException(Callable c) {
exists(ThrowStmt thre | c = thre.getEnclosingCallable()) and
not callableOnlyThrowsArgumentNullException(c)
}