-
Notifications
You must be signed in to change notification settings - Fork 0
/
csrf.go
186 lines (161 loc) · 3.91 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"context"
"crypto/rand"
"encoding/base64"
"errors"
"log/slog"
"net/http"
"sync"
"time"
)
const (
csrfTokenLength = 32
csrfCookieName = "csrf_token"
csrfHeaderName = "X-CSRF-Token"
csrfContextKey = "CSRFToken"
cleanupInterval = 1 * time.Hour
tokenExpiryTime = 12 * time.Hour
)
var (
tokenStore = make(map[string]time.Time)
tokenStoreMu sync.RWMutex
timeNow = time.Now
csrfLogger *slog.Logger
)
func initCSRFLogger(logger *slog.Logger) {
csrfLogger = logger
}
func generateCSRFToken() string {
b := make([]byte, csrfTokenLength)
if _, err := rand.Read(b); err != nil {
if csrfLogger != nil {
csrfLogger.Error("Failed to generate CSRF token", "error", err)
}
return ""
}
return base64.StdEncoding.EncodeToString(b)
}
func setCSRFToken(w http.ResponseWriter, r *http.Request) (string, error) {
token := generateCSRFToken()
if token == "" {
return "", errors.New("Failed to generate CSRF token")
}
isSecure := r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https"
http.SetCookie(w, &http.Cookie{
Name: csrfCookieName,
Value: token,
Path: "/",
HttpOnly: true,
Secure: isSecure,
SameSite: http.SameSiteStrictMode,
MaxAge: int(tokenExpiryTime.Seconds()),
})
tokenStoreMu.Lock()
tokenStore[token] = timeNow().Add(tokenExpiryTime)
tokenStoreMu.Unlock()
return token, nil
}
func GetCSRFToken(r *http.Request) string {
if token, ok := r.Context().Value(csrfContextKey).(string); ok {
return token
}
return ""
}
func validateCSRFToken(r *http.Request) error {
cookie, err := r.Cookie(csrfCookieName)
if err != nil {
if csrfLogger != nil {
csrfLogger.Debug("CSRF cookie not found", "error", err)
}
return errors.New("CSRF cookie not found")
}
token := r.Header.Get(csrfHeaderName)
if token == "" {
token = r.FormValue("csrf_token")
if token == "" {
if csrfLogger != nil {
csrfLogger.Debug("CSRF token not found in header or form")
}
return errors.New("CSRF token not found")
}
}
if cookie.Value != token {
if csrfLogger != nil {
csrfLogger.Debug("CSRF token mismatch")
}
return errors.New("CSRF token mismatch")
}
tokenStoreMu.RLock()
expiry, exists := tokenStore[token]
tokenStoreMu.RUnlock()
if !exists {
if csrfLogger != nil {
csrfLogger.Debug("CSRF token not found in store")
}
return errors.New("CSRF token not found in store")
}
if timeNow().After(expiry) {
if csrfLogger != nil {
csrfLogger.Debug("CSRF token expired")
}
return errors.New("CSRF token expired")
}
if csrfLogger != nil {
csrfLogger.Debug("CSRF validation successful")
}
return nil
}
func CSRFMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var token string
var err error
if r.Method == http.MethodGet || r.Method == http.MethodHead {
token, err = setCSRFToken(w, r)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Set(csrfHeaderName, token)
} else {
if err := validateCSRFToken(r); err != nil {
http.Error(w, "CSRF validation failed", http.StatusForbidden)
return
}
token = r.Header.Get(csrfHeaderName)
if token == "" {
token = r.FormValue("csrf_token")
}
}
ctx := context.WithValue(r.Context(), csrfContextKey, token)
next.ServeHTTP(w, r.WithContext(ctx))
}
}
func cleanupExpiredTokens() {
tokenStoreMu.Lock()
defer tokenStoreMu.Unlock()
now := timeNow()
for token, expiry := range tokenStore {
if now.After(expiry) {
delete(tokenStore, token)
}
}
if csrfLogger != nil {
csrfLogger.Debug("Cleaned up expired CSRF tokens")
}
}
func startCleanupRoutine(ctx context.Context) {
ticker := time.NewTicker(cleanupInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
cleanupExpiredTokens()
case <-ctx.Done():
return
}
}
}
func init() {
go startCleanupRoutine(context.Background())
}