Skip to content

Commit 00fd23d

Browse files
authored
Merge pull request github#12396 from porcupineyhairs/GoJwtSignImprovements
Go: Add more JWT sinks
2 parents 86fd2d5 + e9615c5 commit 00fd23d

File tree

11 files changed

+814
-81
lines changed

11 files changed

+814
-81
lines changed

go/ql/src/experimental/CWE-321/HardcodedKeysLib.qll

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,69 @@ module HardcodedKeys {
7979
}
8080
}
8181

82+
private class KatarasJwt extends Sink {
83+
KatarasJwt() {
84+
exists(string pkg |
85+
pkg = package("github.com/kataras/jwt", "") and
86+
(
87+
exists(DataFlow::MethodCallNode m |
88+
// Model the `Register` method of the type `Keys`
89+
// func (keys Keys) Register(alg Alg, kid string, pubKey PublicKey, privKey PrivateKey)
90+
m.getTarget().hasQualifiedName(pkg, "Keys", "Register")
91+
|
92+
this = m.getArgument(3)
93+
)
94+
or
95+
exists(DataFlow::CallNode m, string names |
96+
// Model the `Sign` method of the `SigningMethod` interface
97+
// func Sign(alg Alg, key PrivateKey, claims interface{}, opts ...SignOption) ([]byte, error)
98+
// func SignEncrypted(alg Alg, key PrivateKey, encrypt InjectFunc, claims interface{}, ...) ([]byte, error)
99+
// func SignEncryptedWithHeader(alg Alg, key PrivateKey, encrypt InjectFunc, claims interface{}, ...) ([]byte, error)
100+
// func SignWithHeader(alg Alg, key PrivateKey, claims interface{}, customHeader interface{}, ...) ([]byte, error)
101+
m.getTarget().hasQualifiedName(pkg, names) and
102+
names = ["Sign", "SignEncrypted", "SignEncryptedWithHeader", "SignWithHeader"]
103+
|
104+
this = m.getArgument(1)
105+
)
106+
)
107+
)
108+
}
109+
}
110+
111+
private class IrisJwt extends Sink {
112+
IrisJwt() {
113+
exists(string pkg |
114+
pkg = "github.com/kataras/iris/v12/middleware/jwt" and
115+
(
116+
exists(DataFlow::CallNode m |
117+
//func NewSigner(signatureAlg Alg, signatureKey interface{}, maxAge time.Duration) *Signer
118+
m.getTarget().hasQualifiedName(pkg, "NewSigner")
119+
|
120+
this = m.getArgument(1)
121+
)
122+
or
123+
exists(Field f |
124+
// Models the `key` field of the `Signer` type
125+
// https://github.com/kataras/iris/blob/dccd57263617f5ca95d7621acfadf9dd37752dd6/middleware/jwt/signer.go#L17
126+
f.hasQualifiedName(pkg, "Signer", "Key") and
127+
f.getAWrite().getRhs() = this
128+
)
129+
)
130+
)
131+
}
132+
}
133+
134+
private class GogfJwtSign extends Sink {
135+
GogfJwtSign() {
136+
exists(Field f, string pkg |
137+
pkg = package("github.com/gogf/gf-jwt", "") and
138+
// https://github.com/gogf/gf-jwt/blob/40503f05bc0a2bcd7aeba550163112afbb5c221f/auth_jwt.go#L27
139+
f.hasQualifiedName(pkg, "GfJWTMiddleware", "Key") and
140+
f.getAWrite().getRhs() = this
141+
)
142+
}
143+
}
144+
82145
private class GinJwtSign extends Sink {
83146
GinJwtSign() {
84147
exists(Field f |

go/ql/test/experimental/CWE-321/HardcodedKeys.expected

Lines changed: 110 additions & 58 deletions
Large diffs are not rendered by default.

go/ql/test/experimental/CWE-321/go.mod

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,92 @@ require (
66
github.com/appleboy/gin-jwt/v2 v2.8.0
77
github.com/cristalhq/jwt/v3 v3.1.0
88
github.com/go-kit/kit v0.12.0
9-
github.com/golang-jwt/jwt/v4 v4.4.1
9+
github.com/gogf/gf-jwt/v2 v2.0.1
10+
github.com/golang-jwt/jwt/v4 v4.5.0
11+
github.com/iris-contrib/middleware/jwt v0.0.0-20230311205048-b568fe9b470f
12+
github.com/kataras/iris/v12 v12.2.0
13+
github.com/kataras/jwt v0.1.8
1014
github.com/lestrrat/go-jwx v0.9.1
1115
github.com/square/go-jose/v3 v3.0.0-20200630053402-0a67ce9b0693
1216
gopkg.in/square/go-jose.v2 v2.6.0
1317
)
1418

1519
require (
20+
github.com/BurntSushi/toml v1.2.1 // indirect
21+
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect
22+
github.com/CloudyKit/jet/v6 v6.2.0 // indirect
23+
github.com/Joker/jade v1.1.3 // indirect
24+
github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06 // indirect
25+
github.com/andybalholm/brotli v1.0.5 // indirect
26+
github.com/aymerick/douceur v0.2.0 // indirect
27+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
28+
github.com/clbanning/mxj/v2 v2.5.5 // indirect
1629
github.com/davecgh/go-spew v1.1.1 // indirect
30+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
31+
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect
32+
github.com/fatih/color v1.13.0 // indirect
33+
github.com/fatih/structs v1.1.0 // indirect
34+
github.com/flosch/pongo2/v4 v4.0.2 // indirect
35+
github.com/fsnotify/fsnotify v1.5.4 // indirect
1736
github.com/gin-contrib/sse v0.1.0 // indirect
1837
github.com/gin-gonic/gin v1.7.7 // indirect
1938
github.com/go-kit/log v0.2.0 // indirect
2039
github.com/go-logfmt/logfmt v0.5.1 // indirect
2140
github.com/go-playground/locales v0.13.0 // indirect
2241
github.com/go-playground/universal-translator v0.17.0 // indirect
2342
github.com/go-playground/validator/v10 v10.4.1 // indirect
43+
github.com/go-redis/redis/v8 v8.11.5 // indirect
44+
github.com/go-sql-driver/mysql v1.6.0 // indirect
45+
github.com/gogf/gf/v2 v2.0.0-rc3 // indirect
2446
github.com/golang/protobuf v1.5.2 // indirect
47+
github.com/golang/snappy v0.0.4 // indirect
48+
github.com/google/uuid v1.3.0 // indirect
49+
github.com/gorilla/css v1.0.0 // indirect
50+
github.com/gorilla/websocket v1.5.0 // indirect
51+
github.com/grokify/html-strip-tags-go v0.0.1 // indirect
52+
github.com/iris-contrib/schema v0.0.6 // indirect
53+
github.com/josharian/intern v1.0.0 // indirect
2554
github.com/json-iterator/go v1.1.12 // indirect
55+
github.com/kataras/blocks v0.0.7 // indirect
56+
github.com/kataras/golog v0.1.8 // indirect
57+
github.com/kataras/pio v0.0.11 // indirect
58+
github.com/kataras/sitemap v0.0.6 // indirect
59+
github.com/kataras/tunnel v0.0.4 // indirect
60+
github.com/klauspost/compress v1.16.0 // indirect
2661
github.com/leodido/go-urn v1.2.0 // indirect
2762
github.com/lestrrat/go-pdebug v0.0.0-20180220043741-569c97477ae8 // indirect
28-
github.com/mattn/go-isatty v0.0.14 // indirect
63+
github.com/mailgun/raymond/v2 v2.0.48 // indirect
64+
github.com/mailru/easyjson v0.7.7 // indirect
65+
github.com/mattn/go-colorable v0.1.9 // indirect
66+
github.com/mattn/go-isatty v0.0.17 // indirect
67+
github.com/mattn/go-runewidth v0.0.9 // indirect
68+
github.com/microcosm-cc/bluemonday v1.0.23 // indirect
2969
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3070
github.com/modern-go/reflect2 v1.0.2 // indirect
71+
github.com/olekukonko/tablewriter v0.0.5 // indirect
3172
github.com/pkg/errors v0.9.1 // indirect
73+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
74+
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
75+
github.com/sirupsen/logrus v1.8.1 // indirect
76+
github.com/tdewolff/minify/v2 v2.12.4 // indirect
77+
github.com/tdewolff/parse/v2 v2.6.4 // indirect
3278
github.com/ugorji/go/codec v1.1.7 // indirect
33-
golang.org/x/crypto v0.0.0-20210915214749-c084706c2272 // indirect
34-
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf // indirect
35-
golang.org/x/sys v0.0.0-20210917161153-d61c044b1678 // indirect
36-
golang.org/x/text v0.3.7 // indirect
79+
github.com/valyala/bytebufferpool v1.0.0 // indirect
80+
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
81+
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
82+
github.com/yosssi/ace v0.0.5 // indirect
83+
go.opentelemetry.io/otel v1.0.0 // indirect
84+
go.opentelemetry.io/otel/sdk v1.0.0 // indirect
85+
go.opentelemetry.io/otel/trace v1.0.0 // indirect
86+
golang.org/x/crypto v0.7.0 // indirect
87+
golang.org/x/net v0.8.0 // indirect
88+
golang.org/x/sys v0.6.0 // indirect
89+
golang.org/x/text v0.8.0 // indirect
90+
golang.org/x/time v0.3.0 // indirect
3791
google.golang.org/genproto v0.0.0-20210917145530-b395a37504d4 // indirect
3892
google.golang.org/grpc v1.40.0 // indirect
39-
google.golang.org/protobuf v1.27.1 // indirect
40-
gopkg.in/yaml.v2 v2.2.8 // indirect
93+
google.golang.org/protobuf v1.29.0 // indirect
94+
gopkg.in/ini.v1 v1.67.0 // indirect
95+
gopkg.in/yaml.v2 v2.4.0 // indirect
96+
gopkg.in/yaml.v3 v3.0.1 // indirect
4197
)

go/ql/test/experimental/CWE-321/main.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
package main
22

33
//go:generate depstubber -vendor github.com/appleboy/gin-jwt/v2 GinJWTMiddleware New
4-
//go:generate depstubber -vendor github.com/golang-jwt/jwt/v4 MapClaims,RegisteredClaims,SigningMethodRSA,SigningMethodHMAC,Token NewNumericDate,NewWithClaims
4+
//go:generate depstubber -vendor github.com/golang-jwt/jwt/v4 MapClaims,RegisteredClaims,SigningMethodRSA,SigningMethodHMAC,Token NewNumericDate,NewWithClaims,New
55
//go:generate depstubber -vendor github.com/gin-gonic/gin Context New
66
//go:generate depstubber -vendor github.com/go-kit/kit/auth/jwt "" NewSigner
77
//go:generate depstubber -vendor github.com/lestrrat/go-jwx/jwk "" New
88
//go:generate depstubber -vendor github.com/square/go-jose/v3 Recipient NewEncrypter,NewSigner
99
//go:generate depstubber -vendor gopkg.in/square/go-jose.v2 Recipient NewEncrypter,NewSigner
1010
//go:generate depstubber -vendor github.com/cristalhq/jwt/v3 Signer NewSignerHS,HS256
11+
//go:generate depstubber -vendor github.com/iris-contrib/middleware/jwt "" NewToken,NewTokenWithClaims
12+
//go:generate depstubber -vendor github.com/kataras/iris/v12/middleware/jwt Signer,Verifier NewSigner,NewVerifier
13+
//go:generate depstubber -vendor github.com/kataras/jwt Keys,Alg Sign,SignEncrypted,SignEncryptedWithHeader,SignWithHeader
14+
//go:generate depstubber -vendor github.com/gogf/gf-jwt/v2 GfJWTMiddleware
1115

1216
import (
1317
"time"
1418

1519
jwt "github.com/appleboy/gin-jwt/v2"
1620
cristal "github.com/cristalhq/jwt/v3"
1721
gokit "github.com/go-kit/kit/auth/jwt"
22+
gogf "github.com/gogf/gf-jwt/v2"
1823
gjwt "github.com/golang-jwt/jwt/v4"
24+
iris "github.com/iris-contrib/middleware/jwt"
25+
iris12 "github.com/kataras/iris/v12/middleware/jwt"
26+
kataras "github.com/kataras/jwt"
1927
le "github.com/lestrrat/go-jwx/jwk"
2028
jose_v3 "github.com/square/go-jose/v3"
2129
jose_v2 "gopkg.in/square/go-jose.v2"
@@ -113,6 +121,78 @@ func lejwt2() (interface{}, error) {
113121
return le.New(sharedKeyglobal) // BAD
114122
}
115123

124+
func gogfjwt() interface{} {
125+
return &gogf.GfJWTMiddleware{
126+
Realm: "test zone",
127+
Key: []byte("key11"),
128+
Timeout: time.Minute * 5,
129+
MaxRefresh: time.Minute * 5,
130+
IdentityKey: "id",
131+
TokenLookup: "header: Authorization, query: token, cookie: jwt",
132+
TokenHeadName: "Bearer",
133+
TimeFunc: time.Now,
134+
Authenticator: nil,
135+
Unauthorized: nil,
136+
PayloadFunc: nil,
137+
IdentityHandler: nil,
138+
}
139+
}
140+
141+
func irisjwt() interface{} {
142+
mySecret := []byte("key12")
143+
token := iris.NewTokenWithClaims(nil, nil)
144+
tokenString, _ := token.SignedString(mySecret)
145+
return tokenString
146+
}
147+
148+
func iris12jwt2() interface{} {
149+
mySecret := []byte("key13")
150+
151+
s := &iris12.Signer{
152+
Alg: nil,
153+
Key: mySecret,
154+
MaxAge: 3 * time.Second,
155+
}
156+
return s
157+
}
158+
159+
func irisjwt3() interface{} {
160+
secret := []byte("key14")
161+
signer := iris12.NewSigner(nil, secret, 3*time.Second)
162+
return signer
163+
}
164+
165+
func katarasJwt() interface{} {
166+
secret := []byte("key15")
167+
token, _ := kataras.Sign(nil, secret, nil, nil)
168+
return token
169+
}
170+
171+
func katarasJwt2() interface{} {
172+
secret := []byte("key16")
173+
token, _ := kataras.SignEncrypted(nil, secret, nil, nil)
174+
return token
175+
}
176+
177+
func katarasJwt3() interface{} {
178+
secret := []byte("key17")
179+
token, _ := kataras.SignEncryptedWithHeader(nil, secret, nil, nil, nil)
180+
return token
181+
}
182+
183+
func katarasJwt4() interface{} {
184+
secret := []byte("key18")
185+
token, _ := kataras.SignWithHeader(nil, secret, nil, nil)
186+
return token
187+
}
188+
189+
func katarasJwt5() {
190+
secret := []byte("key19")
191+
var keys kataras.Keys
192+
var alg kataras.Alg
193+
keys.Register(alg, "api", nil, secret)
194+
}
195+
116196
func main() {
117197
return
118198
}

go/ql/test/experimental/CWE-321/vendor/github.com/gin-gonic/gin/stub.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/ql/test/experimental/CWE-321/vendor/github.com/gogf/gf-jwt/v2/stub.go

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)