Debian Codegraph @

store_legacy_test.go

raw
1 //go:build !go1.11
2 // +build !go1.11
3
4 // file for compatibility with go versions prior to 1.11
5
6 package csrf
7
8 import (
9 "errors"
10 "fmt"
11 "net/http"
12 "net/http/httptest"
13 "strings"
14 "testing"
15
16 "github.com/gorilla/securecookie"
17 )
18
19 // Check store implementations
20 var _ store = &cookieStore{}
21
22 // brokenSaveStore is a CSRF store that cannot, well, save.
23 type brokenSaveStore struct {
24 store
25 }
26
27 func (bs *brokenSaveStore) Get(*http.Request) ([]byte, error) {
28 // Generate an invalid token so we can progress to our Save method
29 return generateRandomBytes(24)
30 }
31
32 func (bs *brokenSaveStore) Save(realToken []byte, w http.ResponseWriter) error {
33 return errors.New("test error")
34 }
35
36 // Tests for failure if the middleware can't save to the Store.
37 func TestStoreCannotSave(t *testing.T) {
38 s := http.NewServeMux()
39 bs := &brokenSaveStore{}
40 s.HandleFunc("/", testHandler)
41 p := Protect(testKey, setStore(bs))(s)
42
43 r, err := http.NewRequest("GET", "/", nil)
44 if err != nil {
45 t.Fatal(err)
46 }
47
48 rr := httptest.NewRecorder()
49 p.ServeHTTP(rr, r)
50
51 if rr.Code != http.StatusForbidden {
52 t.Fatalf("broken store did not set an error status: got %v want %v",
53 rr.Code, http.StatusForbidden)
54 }
55
56 if c := rr.Header().Get("Set-Cookie"); c != "" {
57 t.Fatalf("broken store incorrectly set a cookie: got %v want %v",
58 c, "")
59 }
60
61 }
62
63 // TestCookieDecode tests that an invalid cookie store returns a decoding error.
64 func TestCookieDecode(t *testing.T) {
65 r, err := http.NewRequest("GET", "/", nil)
66 if err != nil {
67 t.Fatal(err)
68 }
69
70 var age = 3600
71
72 // Test with a nil hash key
73 sc := securecookie.New(nil, nil)
74 sc.MaxAge(age)
75 st := &cookieStore{cookieName, age, true, true, "", "", sc, SameSiteDefaultMode}
76
77 // Set a fake cookie value so r.Cookie passes.
78 r.Header.Set("Cookie", fmt.Sprintf("%s=%s", cookieName, "notacookie"))
79
80 _, err = st.Get(r)
81 if err == nil {
82 t.Fatal("cookiestore did not report an invalid hashkey on decode")
83 }
84 }
85
86 // TestCookieEncode tests that an invalid cookie store returns an encoding error.
87 func TestCookieEncode(t *testing.T) {
88 var age = 3600
89
90 // Test with a nil hash key
91 sc := securecookie.New(nil, nil)
92 sc.MaxAge(age)
93 st := &cookieStore{cookieName, age, true, true, "", "", sc, SameSiteDefaultMode}
94
95 rr := httptest.NewRecorder()
96
97 err := st.Save(nil, rr)
98 if err == nil {
99 t.Fatal("cookiestore did not report an invalid hashkey on encode")
100 }
101 }
102
103 // TestMaxAgeZero tests that setting MaxAge(0) does not set the Expires
104 // attribute on the cookie.
105 func TestMaxAgeZero(t *testing.T) {
106 var age = 0
107
108 s := http.NewServeMux()
109 s.HandleFunc("/", testHandler)
110
111 r, err := http.NewRequest("GET", "/", nil)
112 if err != nil {
113 t.Fatal(err)
114 }
115
116 rr := httptest.NewRecorder()
117 p := Protect(testKey, MaxAge(age))(s)
118 p.ServeHTTP(rr, r)
119
120 if rr.Code != http.StatusOK {
121 t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
122 rr.Code, http.StatusOK)
123 }
124
125 if rr.Header().Get("Set-Cookie") == "" {
126 t.Fatalf("cookie not set: got %q", rr.Header().Get("Set-Cookie"))
127 }
128
129 cookie := rr.Header().Get("Set-Cookie")
130 if !strings.Contains(cookie, "HttpOnly") || strings.Contains(cookie, "Expires") {
131 t.Fatalf("cookie incorrectly has the Expires attribute set: got %q", cookie)
132 }
133 }
134
135 // TestSameSizeSet tests that setting SameSite Option does not set the SameSite
136 // attribute on the cookie in legacy systems.
137 func TestSameSizeSet(t *testing.T) {
138 s := http.NewServeMux()
139 s.HandleFunc("/", testHandler)
140
141 r, err := http.NewRequest("GET", "/", nil)
142 if err != nil {
143 t.Fatal(err)
144 }
145
146 rr := httptest.NewRecorder()
147 p := Protect(testKey, SameSite(SameSiteStrictMode))(s)
148 p.ServeHTTP(rr, r)
149
150 if rr.Code != http.StatusOK {
151 t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
152 rr.Code, http.StatusOK)
153 }
154
155 if rr.Header().Get("Set-Cookie") == "" {
156 t.Fatalf("cookie not set: got %q", rr.Header().Get("Set-Cookie"))
157 }
158
159 cookie := rr.Header().Get("Set-Cookie")
160 if strings.Contains(cookie, "SameSite") {
161 t.Fatalf("cookie incorrectly has the SameSite attribute set: got %q", cookie)
162 }
163 }