diff --git a/internal/server/handlers.go b/internal/server/handlers.go index 7bd8edb..a04047f 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -125,13 +125,25 @@ func (s *Server) baseURL(r *http.Request) string { if host == "" { return s.cfg.Server.BaseURL } - proto := r.Header.Get("X-Forwarded-Proto") - if proto == "" { - proto = "http" + // X-Forwarded-Proto is client-settable, so it is only honoured behind a + // trusted proxy (same rule as X-Forwarded-For). Otherwise fall back to + // the configured base URL's scheme. + proto := "http" + if s.isHTTPS(r) { + proto = "https" } return proto + "://" + host } +// isHTTPS reports whether the request should be treated as HTTPS: either the +// trusted proxy says so, or the operator configured an https base URL. +func (s *Server) isHTTPS(r *http.Request) bool { + if s.cfg.Server.TrustProxy && r.Header.Get("X-Forwarded-Proto") == "https" { + return true + } + return strings.HasPrefix(s.cfg.Server.BaseURL, "https://") +} + func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) { renderTemplate(w, "index.html", pageData{BaseURL: s.baseURL(r), LoggedIn: s.isLoggedIn(r), CSRFToken: csrfToken(w, r)}) } @@ -316,7 +328,7 @@ func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) { return } - secure := r.Header.Get("X-Forwarded-Proto") == "https" + secure := s.isHTTPS(r) http.SetCookie(w, &http.Cookie{ Name: "session", Value: token, @@ -664,6 +676,10 @@ func (s *Server) handleAdminBinSave(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) return } + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } name := strings.TrimSpace(r.FormValue("name")) title := strings.TrimSpace(r.FormValue("title")) @@ -1066,6 +1082,15 @@ func (s *Server) handleAdminUploadDelete(w http.ResponseWriter, r *http.Request) } func (s *Server) handleAdminUploadResize(w http.ResponseWriter, r *http.Request) { + // The resize request arrives as multipart FormData, so CSRF is verified + // here rather than via the requireCSRF wrapper (see CLAUDE.md). The body + // is tiny — cap it before the form parse triggered by verifyCSRF. + r.Body = http.MaxBytesReader(w, r.Body, 1<<20) + if !verifyCSRF(r) { + jsonError(w, "Invalid CSRF token", http.StatusForbidden) + return + } + filename := r.PathValue("filename") if !validUploadFilename(filename) { jsonError(w, "Invalid filename", http.StatusBadRequest) diff --git a/internal/server/middleware.go b/internal/server/middleware.go index 3f73b2f..329030d 100644 --- a/internal/server/middleware.go +++ b/internal/server/middleware.go @@ -204,7 +204,9 @@ func limitConcurrency(max int, next http.Handler) http.Handler { // and are excluded. func limitBody(maxBytes int64, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method == http.MethodPost && !strings.HasPrefix(r.URL.Path, "/admin/upload") { + // Exact-match the two upload endpoints: a prefix match would also + // exempt /admin/uploads/* (delete, resize) from the body cap. + if r.Method == http.MethodPost && r.URL.Path != "/admin/upload" && r.URL.Path != "/admin/upload-file" { r.Body = http.MaxBytesReader(w, r.Body, maxBytes) } next.ServeHTTP(w, r)