simplegit

web: compare CSRF Origin against base_url, not r.Host

The same-origin POST check compared the Origin header's host to
r.Host. Behind a reverse proxy that rewrites Host (or a CDN in front
of it) r.Host is not the public hostname, so a legitimate browser POST
was rejected with "cross-origin request rejected". Compare against the
configured base_url host (the same source the Secure cookie flag
trusts), keeping the exact r.Host match as a fallback.

Also log the Origin/Host/base_url on rejection so a remaining mismatch
is diagnosable from the service journal.

dbd235aed7c6ff66fd437cfb278bb9ba7f749fd1
cjosie <administrator@josie-c.com> · 2026-10-16T18:30 · browse files at this commit

parents: 0bdbffc

diff --git a/internal/web/web.go b/internal/web/web.go
index c44d625..a8afbbc 100644
--- a/internal/web/web.go
+++ b/internal/web/web.go
@@ -193,7 +193,7 @@ func (s *Server) Handler() http.Handler {
 			staticPrefix.ServeHTTP(w, r)
 			return
 		}
-		if !sameOrigin(r) {
+		if !s.sameOrigin(r) {
 			http.Error(w, "cross-origin request rejected", http.StatusForbidden)
 			return
 		}
@@ -235,7 +235,12 @@ func (s *Server) legacyRedirect(w http.ResponseWriter, r *http.Request) bool {
 // header — the belt to the session cookie's SameSite=Lax braces. Browsers
 // send Origin on every form/AJAX POST; non-browser clients (git, curl)
 // send none and pass, relying on authentication instead.
-func sameOrigin(r *http.Request) bool {
+//
+// The comparison is against the configured base URL, not r.Host: TLS
+// terminates at the reverse proxy and the Host header the app sees may be
+// rewritten (the same reason the session cookie's Secure flag follows
+// base_url). r.Host is still accepted as an exact fallback.
+func (s *Server) sameOrigin(r *http.Request) bool {
 	switch r.Method {
 	case http.MethodGet, http.MethodHead, http.MethodOptions:
 		return true
@@ -248,7 +253,14 @@ func sameOrigin(r *http.Request) bool {
 	if err != nil {
 		return false
 	}
-	return u.Host == r.Host
+	if u.Host == r.Host {
+		return true
+	}
+	if base, err := url.Parse(s.cfg.BaseURL); err == nil && base.Host != "" && u.Hostname() == base.Hostname() {
+		return true
+	}
+	log.Printf("web: rejected cross-origin %s %s: Origin=%q Host=%q base_url=%q", r.Method, r.URL.Path, origin, r.Host, s.cfg.BaseURL)
+	return false
 }
 
 // Listen serves the web UI on the configured address. Read and write
diff --git a/internal/web/web_test.go b/internal/web/web_test.go
index 20bf59d..54da615 100644
--- a/internal/web/web_test.go
+++ b/internal/web/web_test.go
@@ -160,6 +160,34 @@ func TestReservedRepoNameRejected(t *testing.T) {
 	}
 }
 
+func TestSameOrigin(t *testing.T) {
+	s := &Server{cfg: config.Config{BaseURL: "https://git.example.com"}}
+	req := func(method, origin, host string) *http.Request {
+		r := httptest.NewRequest(method, "https://git.example.com/login", nil)
+		r.Host = host
+		if origin != "" {
+			r.Header.Set("Origin", origin)
+		}
+		return r
+	}
+	cases := []struct {
+		name, method, origin, host string
+		want                       bool
+	}{
+		{"no origin (git/curl)", "POST", "", "git.example.com", true},
+		{"exact host", "POST", "https://git.example.com", "git.example.com", true},
+		{"base_url host despite rewritten Host", "POST", "https://git.example.com", "10.0.0.5:8080", true},
+		{"port ignored vs base_url", "POST", "https://git.example.com:443", "10.0.0.5:8080", true},
+		{"foreign origin", "POST", "https://evil.example", "git.example.com", false},
+		{"GET is always allowed", "GET", "https://evil.example", "git.example.com", true},
+	}
+	for _, c := range cases {
+		if got := s.sameOrigin(req(c.method, c.origin, c.host)); got != c.want {
+			t.Errorf("%s: sameOrigin = %v, want %v", c.name, got, c.want)
+		}
+	}
+}
+
 func TestLoginRejectsBadPassword(t *testing.T) {
 	httpServer, _, _ := newTestServer(t)
 	resp, err := httpServer.Client().PostForm(httpServer.URL+"/login",