Fix security issues found in review

- Verify CSRF on POST /admin/uploads/resize (was the only state-changing
  admin route without a check) and cap its body at 1 MB
- limitBody: exact-match /admin/upload and /admin/upload-file instead of
  a prefix match that also exempted /admin/uploads/{delete,resize}
- Only honour X-Forwarded-Proto behind a trusted proxy (same rule as
  X-Forwarded-For); fall back to the configured base_url scheme for the
  session cookie Secure flag and baseURL()
- handleAdminBinSave: return 500 on non-ErrNoRows DB error instead of
  dereferencing a nil paste

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
lucioandClaude Fable 5 committed 2026-06-09 19:43:32 -07:00
1 parent cc26953d5e
commit 962f5d9e06
2 files changed
+32 -5

No files matched your search

+29 -4
View File
@@ -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)
+3 -1
View File
@@ -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)