CSRF Double-Submit Token Middleware

Issue a random CSRF cookie on safe requests and require a matching header on unsafe methods using constant-time comparison.

GoSecurity Boundaries

Code

gosnippet
68 lines
package main

import (
	"crypto/rand"
	"crypto/subtle"
	"encoding/base64"
	"fmt"
	"net/http"
)

const (
	csrfCookie = "csrf_token"
	csrfHeader = "X-CSRF-Token"
)

func newToken() (string, error) {
	buffer := make([]byte, 32)
	if _, err := rand.Read(buffer); err != nil {
		return "", err
	}
	return base64.RawURLEncoding.EncodeToString(buffer), nil
}

func CSRFMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		cookie, cookieErr := r.Cookie(csrfCookie)
		switch r.Method {
		case http.MethodGet, http.MethodHead, http.MethodOptions:
			if cookieErr != nil {
				token, err := newToken()
				if err != nil {
					http.Error(w, "cannot issue csrf token", http.StatusInternalServerError)
					return
				}
				http.SetCookie(w, &http.Cookie{
					Name:     csrfCookie,
					Value:    token,
					Path:     "/",
					HttpOnly: false,
					SameSite: http.SameSiteLaxMode,
					Secure:   r.TLS != nil,
				})
			}
			next.ServeHTTP(w, r)
			return
		}

		if cookieErr != nil {
			http.Error(w, "missing csrf cookie", http.StatusForbidden)
			return
		}
		header := r.Header.Get(csrfHeader)
		if subtle.ConstantTimeCompare([]byte(header), []byte(cookie.Value)) != 1 {
			http.Error(w, "invalid csrf token", http.StatusForbidden)
			return
		}
		next.ServeHTTP(w, r)
	})
}

func main() {
	handler := CSRFMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "accepted")
	}))
	http.Handle("/submit", handler)
	_ = http.ListenAndServe(":8080", nil)
}

How it works

The code’s whole job belongs to the functions newToken, CSRFMiddleware and main.

Every fallible call checks err != nil (2 sites), so an error is handled where it happens rather than bubbling invisibly.

What to watch for

  • The import surface (if any) is all standard library, all versioned with the language itself.
  • Error values are returned, not thrown — every call site must inspect err before using the other return values.

Putting it to use

The signature in play here is func CSRFMiddleware(next http.Handler) http.Handler

Treat main as the orchestrator calling CSRFMiddleware in a fixed order.

Core lines under the microscope

func newToken() (string, error) {
buffer := make([]byte, 32)
if _, err := rand.Read(buffer); err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(buffer), nil
}
if err != nil {
http.Error(w, "cannot issue csrf token", http.StatusInternalServerError)
return
}
func main() {
handler := CSRFMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "accepted")
}))
http.Handle("/submit", handler)
_ = http.ListenAndServe(":8080", nil)
}

Interactive Tools for This Topic

FAQ

Is there exception handling?
No exceptions — `error` returns checked at 2 sites, and callers branch on `err` first.
How do you run CSRF Double-Submit Token Middleware?
Just run the file; everything it needs ships with the runtime itself.