Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
95 lines
3.8 KiB
Go
95 lines
3.8 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// No cross-origin trust/bypass list: adapters and federation use explicit
|
|
// server-to-server credentials, while browser editors are same-origin proxies.
|
|
var browserOriginProtection http.CrossOriginProtection
|
|
|
|
func validBrowserMutationOrigin(r *http.Request) bool {
|
|
// Legacy semantic editor entry may allocate an empty Page. Until creation
|
|
// is moved entirely into save, subject that navigation to the mutation gate.
|
|
navigation := (r.Method == http.MethodGet || r.Method == http.MethodHead) &&
|
|
(strings.HasPrefix(r.URL.Path, "/e/") || strings.HasPrefix(r.URL.Path, "/o/e/") || r.URL.Path == "/:/new")
|
|
if navigation {
|
|
copy := r.Clone(r.Context())
|
|
copy.Method = http.MethodPost
|
|
r = copy
|
|
}
|
|
if r.Method == http.MethodGet || r.Method == http.MethodHead || r.Method == http.MethodOptions {
|
|
return true
|
|
}
|
|
if browserOriginProtection.Check(r) != nil {
|
|
return false
|
|
}
|
|
// The standard guard compares hosts for older clients without Fetch
|
|
// Metadata. Also require the same scheme, especially an HTTP-origin form
|
|
// targeting an HTTPS session. Trusted same-origin Fetch Metadata already
|
|
// accounts for TLS termination at the reverse proxy.
|
|
if origin := r.Header.Get("Origin"); origin != "" && r.Header.Get("Sec-Fetch-Site") != "same-origin" {
|
|
u, err := url.Parse(origin)
|
|
scheme := "http"
|
|
if requestIsHTTPS(r) {
|
|
scheme = "https"
|
|
}
|
|
if err != nil || u.User != nil || u.Scheme != scheme || u.Host == "" || u.Path != "" || u.RawQuery != "" || u.Fragment != "" {
|
|
return false
|
|
}
|
|
}
|
|
// Check legacy clients too when ambient browser credentials are present.
|
|
// A CLI must send an explicit Bearer credential, not reuse a browser cookie
|
|
// without supplying an Origin. Do not trust X-Forwarded-Host for this test.
|
|
_, session := r.Cookie(authCookieName)
|
|
_, challenge := r.Cookie(mfaChallengeCookieName)
|
|
if session != nil && challenge != nil || len(r.Header.Values("Authorization")) != 0 {
|
|
return true
|
|
}
|
|
if r.Header.Get("Sec-Fetch-Site") == "same-origin" || r.Header.Get("Origin") != "" {
|
|
return true // CrossOriginProtection already validated the supplied origin.
|
|
}
|
|
if navigation && r.Header.Get("Sec-Fetch-Site") == "none" && r.Header.Get("Sec-Fetch-Mode") == "navigate" {
|
|
return true // address-bar/bookmark navigation, not an external embedding
|
|
}
|
|
u, err := url.Parse(r.Referer())
|
|
return err == nil && u.User == nil && (u.Scheme == "http" || u.Scheme == "https") && strings.EqualFold(u.Host, r.Host) && (!requestIsHTTPS(r) || u.Scheme == "https")
|
|
}
|
|
|
|
func rejectBrowserMutation(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
if strings.HasPrefix(r.URL.Path, "/_/api/") {
|
|
writeEventJSON(w, http.StatusForbidden, map[string]string{"error": "cross-origin or unverified browser mutation rejected"})
|
|
return
|
|
}
|
|
http.Error(w, "forbidden: cross-origin or unverified browser mutation", http.StatusForbidden)
|
|
}
|
|
|
|
func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
if r.Method == http.MethodGet || r.Method == http.MethodHead {
|
|
s.renderAuthPage(w, r, map[string]any{"Logout": true})
|
|
return
|
|
}
|
|
if r.Method != http.MethodPost {
|
|
w.Header().Set("Allow", "GET, HEAD, POST")
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
if token := s.authTokenFromRequest(r); token != "" {
|
|
if err := s.store.RevokeAuthSession(token); err != nil {
|
|
http.Error(w, "session revocation failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
if len(r.Header.Values("Authorization")) == 0 {
|
|
http.SetCookie(w, &http.Cookie{Name: authCookieName, Path: "/", MaxAge: -1, HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteLaxMode})
|
|
} else {
|
|
writeEventJSON(w, http.StatusOK, map[string]string{"status": "signed_out"})
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/_/auth/login", http.StatusSeeOther)
|
|
}
|