// Check for CSRF token on /rest/ URLs. If a correct one is not given, reject // the request with 403. For / and /index.html, set a new CSRF cookie if none // is currently set. func csrfMiddleware(unique string, prefix string, cfg config.GUIConfiguration, next http.Handler) http.Handler { loadCsrfTokens() return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Allow requests carrying a valid API key if cfg.IsValidAPIKey(r.Header.Get("X-API-Key")) { next.ServeHTTP(w, r) return } // Allow requests for the front page, and set a CSRF cookie if there isn't already a valid one. if !strings.HasPrefix(r.URL.Path, prefix) { cookie, err := r.Cookie("CSRF-Token-" + unique) if err != nil || !validCsrfToken(cookie.Value) { httpl.Debugln("new CSRF cookie in response to request for", r.URL) cookie = &http.Cookie{ Name: "CSRF-Token-" + unique, Value: newCsrfToken(), } http.SetCookie(w, cookie) } next.ServeHTTP(w, r) return } if r.Method == "GET" { // Allow GET requests unconditionally, but if we got the CSRF // token cookie do the verification anyway so we keep the // csrfTokens list sorted by recent usage. We don't care about the // outcome of the validity check. if cookie, err := r.Cookie("CSRF-Token-" + unique); err == nil { validCsrfToken(cookie.Value) } next.ServeHTTP(w, r) return } // Verify the CSRF token token := r.Header.Get("X-CSRF-Token-" + unique) if !validCsrfToken(token) { http.Error(w, "CSRF Error", 403) return } next.ServeHTTP(w, r) }) }
func basicAuthAndSessionMiddleware(cookieName string, cfg config.GUIConfiguration, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if cfg.IsValidAPIKey(r.Header.Get("X-API-Key")) { next.ServeHTTP(w, r) return } cookie, err := r.Cookie(cookieName) if err == nil && cookie != nil { sessionsMut.Lock() _, ok := sessions[cookie.Value] sessionsMut.Unlock() if ok { next.ServeHTTP(w, r) return } } httpl.Debugln("Sessionless HTTP request with authentication; this is expensive.") error := func() { time.Sleep(time.Duration(rand.Intn(100)+100) * time.Millisecond) w.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"") http.Error(w, "Not Authorized", http.StatusUnauthorized) } hdr := r.Header.Get("Authorization") if !strings.HasPrefix(hdr, "Basic ") { error() return } hdr = hdr[6:] bs, err := base64.StdEncoding.DecodeString(hdr) if err != nil { error() return } fields := bytes.SplitN(bs, []byte(":"), 2) if len(fields) != 2 { error() return } username := string(fields[0]) if username != cfg.User { emitLoginAttempt(false, username) error() return } if err := bcrypt.CompareHashAndPassword([]byte(cfg.Password), fields[1]); err != nil { emitLoginAttempt(false, username) error() return } sessionid := randomString(32) sessionsMut.Lock() sessions[sessionid] = true sessionsMut.Unlock() http.SetCookie(w, &http.Cookie{ Name: cookieName, Value: sessionid, MaxAge: 0, }) emitLoginAttempt(true, username) next.ServeHTTP(w, r) }) }