aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middleware/csrf.go
blob: 74736b57354a7375441a2df5ba7ac9e6a9476f94 (plain)
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
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package middleware

import (
	"context"
	"github.com/miniflux/miniflux2/helper"
	"log"
	"net/http"
)

func Csrf(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var csrfToken string

		csrfCookie, err := r.Cookie("csrfToken")
		if err == http.ErrNoCookie || csrfCookie.Value == "" {
			csrfToken = helper.GenerateRandomString(64)
			cookie := &http.Cookie{
				Name:     "csrfToken",
				Value:    csrfToken,
				Path:     "/",
				Secure:   r.URL.Scheme == "https",
				HttpOnly: true,
			}

			http.SetCookie(w, cookie)
		} else {
			csrfToken = csrfCookie.Value
		}

		ctx := r.Context()
		ctx = context.WithValue(ctx, "CsrfToken", csrfToken)

		w.Header().Add("Vary", "Cookie")
		isTokenValid := csrfToken == r.FormValue("csrf") || csrfToken == r.Header.Get("X-Csrf-Token")

		if r.Method == "POST" && !isTokenValid {
			log.Println("[Middleware:CSRF] Invalid or missing CSRF token!")
			w.WriteHeader(http.StatusBadRequest)
			w.Write([]byte("Invalid or missing CSRF token!"))
		} else {
			next.ServeHTTP(w, r.WithContext(ctx))
		}
	})
}