Files
skillshare/internal/server/request_origin.go
T
Willie ec9b3e688c fix(server): refuse dashboard changes sent from other sites (#810)
A web page can submit a form or a no-cors fetch to the local dashboard
without a CORS preflight, and the handlers accept a text/plain or empty
body. Only the MCP, Hooks and Plugins routes checked where a request
came from, so any site the user visited could add targets, install,
sync, commit, push or upgrade, whenever the browser let the request
reach localhost.

Every POST, PUT, PATCH and DELETE now needs Fetch Metadata, when sent,
to say same-origin, and Origin, when sent, to match the Host. A client
that sends neither, such as curl, is unchanged, because a page cannot
make a browser omit both on a cross-origin request. The update stream
runs updates over GET, as EventSource only opens GET, so it gets the
same check.

The nginx example now forwards $http_host, which keeps the port, since
a proxy that drops it would now fail every change, not only MCP.
2026-11-09 11:08:43 +08:00

56 lines
2.0 KiB
Go

package server
import (
"net/http"
"net/url"
)
// rejectCrossSiteWrites refuses a request that changes state when a browser sent it from
// another site. A page can submit a form or a no-cors fetch to the dashboard without a CORS
// preflight, and the handlers accept a text/plain or empty body, so without this any site the
// user visits could install, sync or push. Browsers label such requests with Origin or
// Sec-Fetch-Site; a client that sends neither, such as curl, is not driven by a web page.
func rejectCrossSiteWrites(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet, http.MethodHead, http.MethodOptions:
default:
if !sameOriginRequest(r) {
writeError(w, http.StatusForbidden, "Request from another site refused: open the dashboard directly to make changes")
return
}
}
next.ServeHTTP(w, r)
})
}
// requireSameOrigin applies the same check to a GET route that changes state, such as an
// update stream that EventSource can only open with GET.
func requireSameOrigin(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !sameOriginRequest(r) {
writeError(w, http.StatusForbidden, "Request from another site refused: open the dashboard directly to make changes")
return
}
next(w, r)
}
}
// sameOriginRequest reports whether a browser request came from the page it was sent to:
// Fetch Metadata, when present, must say same-origin, and Origin, when present, must name
// the host and port the request was sent to.
func sameOriginRequest(r *http.Request) bool {
if site := r.Header.Get("Sec-Fetch-Site"); site != "" && site != "same-origin" {
return false
}
origin := r.Header.Get("Origin")
if origin == "" {
return true
}
u, err := url.Parse(origin)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" || u.User != nil || u.Path != "" || u.RawQuery != "" || u.Fragment != "" {
return false
}
return sameHost(u.Host, r.Host)
}