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