-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathJsonWebTokenHandlerLib.qll
More file actions
209 lines (190 loc) · 6.6 KB
/
JsonWebTokenHandlerLib.qll
File metadata and controls
209 lines (190 loc) · 6.6 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
deprecated module;
import csharp
import DataFlow
/**
* A sensitive property for `TokenValidationParameters` that updates the underlying value.
*/
class TokenValidationParametersPropertySensitiveValidation extends Property {
TokenValidationParametersPropertySensitiveValidation() {
exists(Class c |
c.hasFullyQualifiedName("Microsoft.IdentityModel.Tokens", "TokenValidationParameters")
|
c.getAProperty() = this and
this.getName() in [
"ValidateIssuer", "ValidateAudience", "ValidateLifetime", "RequireExpirationTime",
"RequireAudience"
]
)
}
}
/**
* A dataflow configuration from a `false` value to a write sensitive property for `TokenValidationParameters`.
*/
private module FalseValueFlowsToTokenValidationParametersPropertyWriteToBypassValidationConfig
implements DataFlow::ConfigSig
{
predicate isSource(DataFlow::Node source) {
source.asExpr().getValue() = "false" and
source.asExpr().getType() instanceof BoolType
}
predicate isSink(DataFlow::Node sink) {
sink.asExpr() = any(TokenValidationParametersPropertySensitiveValidation p).getAnAssignedValue()
}
}
module FalseValueFlowsToTokenValidationParametersPropertyWriteToBypassValidation =
DataFlow::Global<FalseValueFlowsToTokenValidationParametersPropertyWriteToBypassValidationConfig>;
/**
* 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.hasFullyQualifiedName("Microsoft.IdentityModel.JsonWebTokens", "JsonWebTokenHandler",
"ValidateToken") or
this.hasFullyQualifiedName("Microsoft.AzureAD.DeviceIdentification.Common.Tokens",
"JwtValidator", "ValidateEncryptedToken")
}
}
/**
* 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() = this |
p.hasName("IsValid") or
p.hasName("Exception")
)
}
}
/**
* A security-sensitive property for `Microsoft.IdentityModel.Tokens.TokenValidationParameters`
*/
class TokenValidationParametersProperty extends Property {
TokenValidationParametersProperty() {
exists(Class c |
c.hasFullyQualifiedName("Microsoft.IdentityModel.Tokens", "TokenValidationParameters")
|
c.getAProperty() = this and
this.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
forex(ReturnStmt rs | rs.getEnclosingCallable() = c |
rs.getNumberOfChildren() = 1 and
isExpressionAlwaysTrue(rs.getChildExpr(0))
)
}
/**
* Holds if the lambda expression `le` always returns true
*/
predicate lambdaExprReturnsOnlyLiteralTrue(AnonymousFunctionExpr le) {
isExpressionAlwaysTrue(le.getExpressionBody())
}
class CallableAlwaysReturnsTrue extends Callable {
CallableAlwaysReturnsTrue() {
callableHasAReturnStmtAndAlwaysReturnsTrue(this)
or
lambdaExprReturnsOnlyLiteralTrue(this)
}
}
/**
* 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().hasFullyQualifiedName("System", "ArgumentNullException")
)
}
/**
* 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.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)
}