Skip to content

Commit 0788a90

Browse files
committed
Convert RequestForgery test to inline expectations
1 parent d10b9e6 commit 0788a90

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

go/ql/test/query-tests/Security/CWE-918/RequestForgery.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
)
66

77
func handler(w http.ResponseWriter, req *http.Request) {
8-
target := req.FormValue("target")
8+
target := req.FormValue("target") // $ Source
99

1010
// BAD: `target` is controlled by the attacker
11-
resp, err := http.Get("https://" + target + ".example.com/data/")
11+
resp, err := http.Get("https://" + target + ".example.com/data/") // $ Alert
1212
if err != nil {
1313
// error handling
1414
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
query: Security/CWE-918/RequestForgery.ql
2-
postprocess: utils/test/PrettyPrintModels.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql

go/ql/test/query-tests/Security/CWE-918/tst.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@ import (
77
)
88

99
func handler2(w http.ResponseWriter, req *http.Request) {
10-
tainted := req.FormValue("target")
10+
tainted := req.FormValue("target") // $ Source
1111

1212
http.Get("example.com") // OK
1313

14-
http.Get(tainted) // Not OK
14+
http.Get(tainted) // $ Alert
1515

1616
http.Head(tainted) // OK
1717

18-
http.Post(tainted, "text/basic", nil) // Not OK
18+
http.Post(tainted, "text/basic", nil) // $ Alert
1919

2020
client := &http.Client{}
21-
rq, _ := http.NewRequest("GET", tainted, nil)
22-
client.Do(rq) // Not OK
21+
rq, _ := http.NewRequest("GET", tainted, nil) // $ Sink
22+
client.Do(rq) // $ Alert
2323

24-
rq, _ = http.NewRequestWithContext(context.Background(), "GET", tainted, nil)
25-
client.Do(rq) // Not OK
24+
rq, _ = http.NewRequestWithContext(context.Background(), "GET", tainted, nil) // $ Sink
25+
client.Do(rq) // $ Alert
2626

27-
http.Get("http://" + tainted) // Not OK
27+
http.Get("http://" + tainted) // $ Alert
2828

29-
http.Get("http://example.com" + tainted) // Not OK
29+
http.Get("http://example.com" + tainted) // $ Alert
3030

3131
http.Get("http://example.com/" + tainted) // OK
3232

3333
http.Get("http://example.com/?" + tainted) // OK
3434

3535
u, _ := url.Parse("http://example.com/relative-path")
3636
u.Host = tainted
37-
http.Get(u.String()) // Not OK
37+
http.Get(u.String()) // $ Alert
3838
}
3939

4040
func main() {

go/ql/test/query-tests/Security/CWE-918/websocket.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ func test() {
5757

5858
// x net websocket dial bad
5959
http.HandleFunc("/ex2", func(w http.ResponseWriter, r *http.Request) {
60-
untrustedInput := r.Referer()
60+
untrustedInput := r.Referer() // $ Source
6161

6262
origin := "http://localhost/"
6363

6464
// bad as input is directly passed to dial function
65-
ws, _ := websocket.Dial(untrustedInput, "", origin)
65+
ws, _ := websocket.Dial(untrustedInput, "", origin) // $ Alert
6666
var msg = make([]byte, 512)
6767
var n int
6868
n, _ = ws.Read(msg)
@@ -71,12 +71,12 @@ func test() {
7171

7272
// x net websocket dialConfig bad
7373
http.HandleFunc("/ex3", func(w http.ResponseWriter, r *http.Request) {
74-
untrustedInput := r.Referer()
74+
untrustedInput := r.Referer() // $ Source
7575

7676
origin := "http://localhost/"
7777
// bad as input is directly used
78-
config, _ := websocket.NewConfig(untrustedInput, origin) // good
79-
ws2, _ := websocket.DialConfig(config)
78+
config, _ := websocket.NewConfig(untrustedInput, origin) // $ Sink
79+
ws2, _ := websocket.DialConfig(config) // $ Alert
8080
var msg = make([]byte, 512)
8181
var n int
8282
n, _ = ws2.Read(msg)
@@ -85,10 +85,10 @@ func test() {
8585

8686
// nhooyr websocket dial bad
8787
http.HandleFunc("/ex4", func(w http.ResponseWriter, r *http.Request) {
88-
untrustedInput := r.Referer()
88+
untrustedInput := r.Referer() // $ Source
8989

9090
// bad as input is used directly
91-
nhooyr.Dial(context.TODO(), untrustedInput, nil)
91+
nhooyr.Dial(context.TODO(), untrustedInput, nil) // $ Alert
9292
w.WriteHeader(500)
9393
})
9494

@@ -104,10 +104,10 @@ func test() {
104104

105105
// gorilla websocket Dialer.Dial bad
106106
http.HandleFunc("/ex6", func(w http.ResponseWriter, r *http.Request) {
107-
untrustedInput := r.Referer()
107+
untrustedInput := r.Referer() // $ Source
108108

109109
dialer := gorilla.Dialer{}
110-
dialer.Dial(untrustedInput, r.Header)
110+
dialer.Dial(untrustedInput, r.Header) // $ Alert
111111
})
112112

113113
// gorilla websocket Dialer.Dial good
@@ -123,10 +123,10 @@ func test() {
123123

124124
// gorilla websocket Dialer.DialContext bad
125125
http.HandleFunc("/ex8", func(w http.ResponseWriter, r *http.Request) {
126-
untrustedInput := r.Referer()
126+
untrustedInput := r.Referer() // $ Source
127127

128128
dialer := gorilla.Dialer{}
129-
dialer.DialContext(context.TODO(), untrustedInput, r.Header)
129+
dialer.DialContext(context.TODO(), untrustedInput, r.Header) // $ Alert
130130
})
131131

132132
// gorilla websocket Dialer.DialContext good
@@ -151,15 +151,15 @@ func test() {
151151

152152
// gobwas websocket Dial bad
153153
http.HandleFunc("/ex11", func(w http.ResponseWriter, r *http.Request) {
154-
untrustedInput := r.Referer()
155-
gobwas.Dial(context.TODO(), untrustedInput)
154+
untrustedInput := r.Referer() // $ Source
155+
gobwas.Dial(context.TODO(), untrustedInput) // $ Alert
156156
})
157157

158158
// gobwas websocket Dialer.Dial bad
159159
http.HandleFunc("/ex12", func(w http.ResponseWriter, r *http.Request) {
160-
untrustedInput := r.Referer()
160+
untrustedInput := r.Referer() // $ Source
161161
dialer := gobwas.Dialer{}
162-
dialer.Dial(context.TODO(), untrustedInput)
162+
dialer.Dial(context.TODO(), untrustedInput) // $ Alert
163163
})
164164

165165
// gobwas websocket Dialer.Dial good
@@ -192,16 +192,16 @@ func test() {
192192

193193
// sac007 websocket BuildProxy bad
194194
http.HandleFunc("/ex15", func(w http.ResponseWriter, r *http.Request) {
195-
untrustedInput := r.Referer()
195+
untrustedInput := r.Referer() // $ Source
196196

197-
_ = sac.BuildProxy(untrustedInput)
197+
_ = sac.BuildProxy(untrustedInput) // $ Alert
198198
})
199199

200200
// sac007 websocket New bad
201201
http.HandleFunc("/ex16", func(w http.ResponseWriter, r *http.Request) {
202-
untrustedInput := r.Referer()
202+
untrustedInput := r.Referer() // $ Source
203203

204-
_ = sac.New(untrustedInput)
204+
_ = sac.New(untrustedInput) // $ Alert
205205
})
206206

207207
log.Println(http.ListenAndServe(":80", nil))

0 commit comments

Comments
 (0)