2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-04 06:31:38 +00:00
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
package api
|
2014-06-04 19:20:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2014-07-06 13:00:44 +00:00
|
|
|
"strings"
|
2024-01-04 10:07:12 +00:00
|
|
|
"time"
|
2014-06-04 19:20:07 +00:00
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2014-06-04 19:20:07 +00:00
|
|
|
)
|
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
const (
|
|
|
|
maxCSRFTokenLifetime = time.Hour
|
|
|
|
maxActiveCSRFTokens = 25
|
|
|
|
)
|
2016-01-03 20:56:19 +00:00
|
|
|
|
2019-09-05 11:35:51 +00:00
|
|
|
type csrfManager struct {
|
|
|
|
unique string
|
|
|
|
prefix string
|
|
|
|
apiKeyValidator apiKeyValidator
|
|
|
|
next http.Handler
|
2024-01-04 10:07:12 +00:00
|
|
|
tokens *tokenManager
|
2019-09-05 11:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type apiKeyValidator interface {
|
|
|
|
IsValidAPIKey(key string) bool
|
|
|
|
}
|
|
|
|
|
2014-06-04 19:20:07 +00:00
|
|
|
// 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.
|
2024-01-04 10:07:12 +00:00
|
|
|
func newCsrfManager(unique string, prefix string, apiKeyValidator apiKeyValidator, next http.Handler, miscDB *db.NamespacedKV) *csrfManager {
|
2019-09-05 11:35:51 +00:00
|
|
|
m := &csrfManager{
|
|
|
|
unique: unique,
|
|
|
|
prefix: prefix,
|
|
|
|
apiKeyValidator: apiKeyValidator,
|
|
|
|
next: next,
|
2024-01-04 10:07:12 +00:00
|
|
|
tokens: newTokenManager("csrfTokens", miscDB, maxCSRFTokenLifetime, maxActiveCSRFTokens),
|
2019-09-05 11:35:51 +00:00
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
2014-07-05 19:40:29 +00:00
|
|
|
|
2019-09-05 11:35:51 +00:00
|
|
|
func (m *csrfManager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Allow requests carrying a valid API key
|
2023-07-26 11:13:06 +00:00
|
|
|
if hasValidAPIKeyHeader(r, m.apiKeyValidator) {
|
2019-09-05 11:35:51 +00:00
|
|
|
// Set the access-control-allow-origin header for CORS requests
|
|
|
|
// since a valid API key has been provided
|
|
|
|
w.Header().Add("Access-Control-Allow-Origin", "*")
|
|
|
|
m.next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2016-08-02 11:06:45 +00:00
|
|
|
|
2019-09-05 11:35:51 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/rest/debug") {
|
|
|
|
// Debugging functions are only available when explicitly
|
|
|
|
// enabled, and can be accessed without a CSRF token
|
|
|
|
m.next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow requests for anything not under the protected path prefix,
|
|
|
|
// and set a CSRF cookie if there isn't already a valid one.
|
|
|
|
if !strings.HasPrefix(r.URL.Path, m.prefix) {
|
|
|
|
cookie, err := r.Cookie("CSRF-Token-" + m.unique)
|
2024-01-04 10:07:12 +00:00
|
|
|
if err != nil || !m.tokens.Check(cookie.Value) {
|
2019-09-05 11:35:51 +00:00
|
|
|
l.Debugln("new CSRF cookie in response to request for", r.URL)
|
|
|
|
cookie = &http.Cookie{
|
|
|
|
Name: "CSRF-Token-" + m.unique,
|
2024-01-04 10:07:12 +00:00
|
|
|
Value: m.tokens.New(),
|
2014-07-05 19:40:29 +00:00
|
|
|
}
|
2019-09-05 11:35:51 +00:00
|
|
|
http.SetCookie(w, cookie)
|
2014-07-05 19:40:29 +00:00
|
|
|
}
|
2019-09-05 11:35:51 +00:00
|
|
|
m.next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2014-06-05 07:16:12 +00:00
|
|
|
|
2023-10-06 11:00:58 +00:00
|
|
|
if isNoAuthPath(r.URL.Path) {
|
|
|
|
// REST calls that don't require authentication also do not
|
|
|
|
// need a CSRF token.
|
|
|
|
m.next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-05 11:35:51 +00:00
|
|
|
// Verify the CSRF token
|
|
|
|
token := r.Header.Get("X-CSRF-Token-" + m.unique)
|
2024-01-04 10:07:12 +00:00
|
|
|
if !m.tokens.Check(token) {
|
2019-09-05 11:35:51 +00:00
|
|
|
http.Error(w, "CSRF Error", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2014-07-05 19:40:29 +00:00
|
|
|
|
2019-09-05 11:35:51 +00:00
|
|
|
m.next.ServeHTTP(w, r)
|
2014-06-04 19:20:07 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 11:13:06 +00:00
|
|
|
func hasValidAPIKeyHeader(r *http.Request, validator apiKeyValidator) bool {
|
|
|
|
if key := r.Header.Get("X-API-Key"); validator.IsValidAPIKey(key) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if auth := r.Header.Get("Authorization"); strings.HasPrefix(strings.ToLower(auth), "bearer ") {
|
|
|
|
bearerToken := auth[len("bearer "):]
|
|
|
|
return validator.IsValidAPIKey(bearerToken)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|