-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathJWT.qll
More file actions
269 lines (238 loc) · 8.07 KB
/
JWT.qll
File metadata and controls
269 lines (238 loc) · 8.07 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
import go
/**
* A abstract class which responsible for parsing a JWT token.
*/
abstract class JwtParseBase extends Function {
/**
* Gets argument number that responsible for JWT
*
* `-1` means the receiver is a argument node that responsible for JWT.
* In this case, we must declare some additional taint steps.
*/
abstract int getTokenArgNum();
/**
* Gets Argument as DataFlow node that responsible for JWT
*/
DataFlow::Node getTokenArg() {
this.getTokenArgNum() != -1 and result = this.getACall().getArgument(this.getTokenArgNum())
or
this.getTokenArgNum() = -1 and result = this.getACall().getReceiver()
}
}
/**
* A abstract class which responsible for parsing a JWT token which the key parameter is a function type.
*
* Extends this class for Jwt parsing methods that accepts a function type as key.
*/
abstract class JwtParseWithKeyFunction extends JwtParseBase {
/**
* Gets argument number that responsible for a function returning the secret key
*/
abstract int getKeyFuncArgNum();
/**
* Gets Argument as DataFlow node that responsible for a function returning the secret key
*/
DataFlow::Node getKeyFuncArg() { result = this.getACall().getArgument(this.getKeyFuncArgNum()) }
}
/**
* A abstract class which responsible for parsing a JWT token which the key parameter can be a string or byte type.
*
* Extends this class for Jwt parsing methods that accepts a non-function type as key.
*/
abstract class JwtParse extends JwtParseBase {
/**
* Gets argument number that responsible for secret key
*/
abstract int getKeyArgNum();
/**
* Gets Argument as DataFlow node that responsible for secret key
*/
DataFlow::Node getKeyArg() { result = this.getACall().getArgument(this.getKeyArgNum()) }
}
/**
* A abstract class which responsible for parsing a JWT without verifying it
*
* Extends this class for Jwt parsing methods that don't verify JWT signature
*/
abstract class JwtUnverifiedParse extends JwtParseBase { }
/**
* Gets `github.com/golang-jwt/jwt` and `github.com/dgrijalva/jwt-go`(previous name of `golang-jwt`) JWT packages
*/
string golangJwtPackage() {
result = package(["github.com/golang-jwt/jwt", "github.com/dgrijalva/jwt-go"], "")
}
/**
* A class that contains the following function and method:
*
* func (p *Parser) Parse(tokenString string, keyFunc Keyfunc)
*
* func Parse(tokenString string, keyFunc Keyfunc)
*/
class GolangJwtParse extends JwtParseWithKeyFunction {
GolangJwtParse() {
exists(Function f | f.hasQualifiedName(golangJwtPackage(), "Parse") | this = f)
or
exists(Method f | f.hasQualifiedName(golangJwtPackage(), "Parser", "Parse") | this = f)
}
override int getKeyFuncArgNum() { result = 1 }
override int getTokenArgNum() { result = 0 }
}
/**
* A class that contains the following function and method:
*
* func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc)
*
* func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc)
*/
class GolangJwtParseWithClaims extends JwtParseWithKeyFunction {
GolangJwtParseWithClaims() {
exists(Function f | f.hasQualifiedName(golangJwtPackage(), "ParseWithClaims") | this = f)
or
exists(Method f | f.hasQualifiedName(golangJwtPackage(), "Parser", "ParseWithClaims") |
this = f
)
}
override int getKeyFuncArgNum() { result = 2 }
override int getTokenArgNum() { result = 0 }
}
/**
* A class that contains the following method:
*
* func (p *Parser) ParseUnverified(tokenString string, claims Claims)
*/
class GolangJwtParseUnverified extends JwtUnverifiedParse {
GolangJwtParseUnverified() {
exists(Method f | f.hasQualifiedName(golangJwtPackage(), "Parser", "ParseUnverified") |
this = f
)
}
override int getTokenArgNum() { result = 0 }
}
/**
* Gets `github.com/golang-jwt/jwt` and `github.com/dgrijalva/jwt-go`(previous name of `golang-jwt`) JWT packages
*/
string golangJwtRequestPackage() {
result = package(["github.com/golang-jwt/jwt", "github.com/dgrijalva/jwt-go"], "request")
}
/**
* A class that contains the following function:
*
* func ParseFromRequest(req *http.Request, extractor Extractor, keyFunc jwt.Keyfunc, options ...ParseFromRequestOption)
*/
class GolangJwtParseFromRequest extends JwtParseWithKeyFunction {
GolangJwtParseFromRequest() {
exists(Function f | f.hasQualifiedName(golangJwtRequestPackage(), "ParseFromRequest") |
this = f
)
}
override int getKeyFuncArgNum() { result = 2 }
override int getTokenArgNum() { result = 0 }
}
/**
* A class that contains the following function:
*
* func ParseFromRequestWithClaims(req *http.Request, extractor Extractor, claims jwt.Claims, keyFunc jwt.Keyfunc)
*/
class GolangJwtParseFromRequestWithClaims extends JwtParseWithKeyFunction {
GolangJwtParseFromRequestWithClaims() {
exists(Function f |
f.hasQualifiedName(golangJwtRequestPackage(), "ParseFromRequestWithClaims")
|
this = f
)
}
override int getKeyFuncArgNum() { result = 3 }
override int getTokenArgNum() { result = 0 }
}
/**
* Gets `gopkg.in/square/go-jose` and `github.com/go-jose/go-jose` jwt package
*/
string goJoseJwtPackage() {
result =
package([
"gopkg.in/square/go-jose", "gopkg.in/go-jose/go-jose", "github.com/square/go-jose",
"github.com/go-jose/go-jose"
], "jwt")
}
/**
* A class that contains the following method:
*
* func (t *JSONWebToken) Claims(key interface{}, dest ...interface{})
*/
class GoJoseParseWithClaims extends JwtParse {
GoJoseParseWithClaims() {
exists(Method f | f.hasQualifiedName(goJoseJwtPackage(), "JSONWebToken", "Claims") | this = f)
}
override int getKeyArgNum() { result = 0 }
override int getTokenArgNum() { result = -1 }
}
/**
* A class that contains the following method:
*
* func (t *JSONWebToken) UnsafeClaimsWithoutVerification(dest ...interface{})
*/
class GoJoseUnsafeClaims extends JwtUnverifiedParse {
GoJoseUnsafeClaims() {
exists(Method f |
f.hasQualifiedName(goJoseJwtPackage(), "JSONWebToken", "UnsafeClaimsWithoutVerification")
|
this = f
)
}
override int getTokenArgNum() { result = -1 }
}
/**
* Holds for general additional steps related to parsing the secret keys in `golang-jwt/jwt`,`dgrijalva/jwt-go` packages
*/
predicate golangJwtIsAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
exists(Function f, DataFlow::CallNode call |
(
f.hasQualifiedName(package("github.com/golang-jwt/jwt", ""),
[
"ParseECPrivateKeyFromPEM", "ParseECPublicKeyFromPEM", "ParseEdPrivateKeyFromPEM",
"ParseEdPublicKeyFromPEM", "ParseRSAPrivateKeyFromPEM", "ParseRSAPublicKeyFromPEM",
"RegisterSigningMethod"
]) or
f.hasQualifiedName(package("github.com/dgrijalva/jwt-go", ""),
[
"ParseECPrivateKeyFromPEM", "ParseECPublicKeyFromPEM", "ParseRSAPrivateKeyFromPEM",
"ParseRSAPrivateKeyFromPEMWithPassword", "ParseRSAPublicKeyFromPEM"
])
) and
call = f.getACall() and
nodeFrom = call.getArgument(0) and
nodeTo = call.getResult(0)
or
(
f instanceof GolangJwtParse
or
f instanceof GolangJwtParseWithClaims
) and
call = f.getACall() and
nodeFrom = call.getArgument(0) and
nodeTo = call.getResult(0)
)
}
/**
* Holds for general additioanl steps related to parsing the secret keys in `go-jose` package
*/
predicate goJoseIsAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
exists(Function f, DataFlow::CallNode call |
f.hasQualifiedName(goJoseJwtPackage(), ["ParseEncrypted", "ParseSigned"]) and
call = f.getACall() and
nodeFrom = call.getArgument(0) and
nodeTo = call.getResult(0)
)
or
exists(Method m, DataFlow::CallNode call |
m.hasQualifiedName(goJoseJwtPackage(), "NestedJSONWebToken", "ParseSignedAndEncrypted") and
call = m.getACall() and
nodeFrom = call.getArgument(0) and
nodeTo = call.getResult(0)
or
m.hasQualifiedName(goJoseJwtPackage(), "NestedJSONWebToken", "Decrypt") and
call = m.getACall() and
nodeFrom = call.getReceiver() and
nodeTo = call.getResult(0)
)
}