-
Notifications
You must be signed in to change notification settings - Fork 0
/
csrf_test.go
139 lines (111 loc) · 2.83 KB
/
csrf_test.go
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
package csrf
import (
"log"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
"net/url"
"testing"
)
func TestCSRFTokenGeneration(t *testing.T) {
csrf := New()
token1 := csrf.GenerateToken()
token2 := csrf.GenerateToken()
if token1 == token2 {
t.Errorf("Generated tokens should not be the same")
}
}
func TestFlowInjectCookie(t *testing.T) {
csrf := New()
token := csrf.GenerateToken()
mux := http.NewServeMux()
mux.Handle("/", csrf.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
csrf.SetCookie(w, token)
})))
mux.Handle("/post", csrf.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
})))
req, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatal(err)
}
res := httptest.NewRecorder()
mux.ServeHTTP(res, req)
if res.Code != http.StatusOK {
t.Errorf("unable to do GET request")
}
if len(res.Result().Cookies()) == 0 {
t.Fatal("no cookie is set")
}
if res.Result().Cookies()[0].Value != token {
t.Error("cookie mismatch")
}
req, err = http.NewRequest(http.MethodPost, "/post", nil)
if err != nil {
t.Fatal(err)
}
// inject cookie from previous response in the new request
req.AddCookie(res.Result().Cookies()[0])
// set X-CSRF-Token in request
req.Header.Set("X-CSRF-Token", token)
mux.ServeHTTP(res, req)
if res.Code != http.StatusOK {
t.Fatal("csrf failed but should pass")
}
}
func TestFlowCookieJar(t *testing.T) {
csrf := New()
token := csrf.GenerateToken()
mux := http.NewServeMux()
mux.Handle("/", csrf.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
csrf.SetCookie(w, token)
})))
mux.Handle("/post", csrf.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
})))
ts := httptest.NewServer(mux)
defer ts.Close()
jar, err := cookiejar.New(nil)
if err != nil {
log.Fatal(err)
}
client := &http.Client{
Jar: jar,
}
u, err := url.Parse(ts.URL)
if err != nil {
log.Fatal(err)
}
if _, err = client.Get(u.String()); err != nil {
log.Fatal(err)
}
req, err := http.NewRequest(http.MethodPost, u.String()+"/post", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("X-CSRF-Token", token)
res, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != http.StatusOK {
t.Fatal("csrf failed but should pass")
}
}
func TestCSRFMiddleware(t *testing.T) {
csrf := New()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, world!"))
})
mux := http.NewServeMux()
mux.Handle("/", csrf.Middleware(handler))
req, err := http.NewRequest("POST", "/", nil)
if err != nil {
t.Fatal(err)
}
res := httptest.NewRecorder()
mux.ServeHTTP(res, req)
if res.Code != http.StatusForbidden {
t.Errorf("CSRF middleware did not reject request")
}
}