* "Release: Updated versions in package to 8.3.5" * [v8.3.x] Fix for CVE-2022-21702 (#225) Fix for CVE-2022-21702 * Update yarn.lock for 8.3.5 * resolve conflicts (cherry picked from commit bb38cfcba4b4f824060ff385d858c63f50b72d74) * csrf checks for v8.3.5 (#234) * Fix lint * Cherry pick e2e test server changes Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> Co-authored-by: Kevin Minehart <kmineh0151@gmail.com> Co-authored-by: Serge Zaitsev <serge.zaitsev@grafana.com>
40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package middleware
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
func CSRF(loginCookieName string) func(http.Handler) http.Handler {
|
|
// As per RFC 7231/4.2.2 these methods are idempotent:
|
|
// (GET is excluded because it may have side effects in some APIs)
|
|
safeMethods := []string{"HEAD", "OPTIONS", "TRACE"}
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// If request has no login cookie - skip CSRF checks
|
|
if _, err := r.Cookie(loginCookieName); errors.Is(err, http.ErrNoCookie) {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
// Skip CSRF checks for "safe" methods
|
|
for _, method := range safeMethods {
|
|
if r.Method == method {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
}
|
|
// Otherwise - verify that Origin matches the server origin
|
|
host := strings.Split(r.Host, ":")[0]
|
|
origin, err := url.Parse(r.Header.Get("Origin"))
|
|
if err != nil || (origin.String() != "" && origin.Hostname() != host) {
|
|
http.Error(w, "origin not allowed", http.StatusForbidden)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
}
|