-
Notifications
You must be signed in to change notification settings - Fork 0
/
csrf.go
60 lines (50 loc) · 1.75 KB
/
csrf.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
// CSRF protection
package csrf
// Using a double-submit cookie here. We cannot just double-submit the session cookie,
// because we mark it as HttpOnly to prevent an XSS attacker from stealing it.
//
// We use a stateless, random string (not deriving the CSRF token from the secret). The user
// can in theory change it from her browser, but that's ok because while an attacker could
// inject the CSRF header, the attacker cannot set a cookie. The whole security model of this
// relies on the attacker not being able to set a cookie for another domain in the forged request:
// https://stackoverflow.com/questions/6761415/how-to-set-a-cookie-for-another-domain
// https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name
//
// This is thoroughly tested by httpauth package
import (
"errors"
"net/http"
"github.com/function61/gokit/crypto/cryptoutil"
)
const (
csrfCookieName = "csrf_token"
csrfHeaderName = "x-csrf-token"
)
var (
errHeaderMissing = errors.New("csrf: " + csrfHeaderName + " HTTP header missing")
errCookieMissing = errors.New("csrf: cookie " + csrfCookieName + " missing")
errCookieAndHeaderMismatch = errors.New("csrf: cookie does not match HTTP header")
)
func CreateCookie() *http.Cookie {
return &http.Cookie{
Name: csrfCookieName,
Value: cryptoutil.RandBase64Url(16),
Path: "/",
HttpOnly: false, // = visible to JavaScript (has to be)
// Secure: true, // TODO
}
}
func Validate(req *http.Request) error {
headerToken := req.Header.Get(csrfHeaderName)
if headerToken == "" {
return errHeaderMissing
}
cookie, err := req.Cookie(csrfCookieName)
if err != nil {
return errCookieMissing
}
if headerToken != cookie.Value {
return errCookieAndHeaderMismatch
}
return nil
}