fix: address security and quality issues from code review

Security fixes:
- Fix XSS in Atom feed: escape user-supplied URLs in HTML content
- Wrap signup request approval in a transaction to prevent
  partial state on crash (user created but request still pending)
- Stop leaking internal error messages to admin UI
- Add request body size limit on API import endpoint
- Log SetMustResetPassword errors instead of silently discarding

Correctness fixes:
- Handle errors from API fave update/delete instead of returning
  success on failure
- Use actual data timestamp for feed <updated> instead of
  time.Now() (improves HTTP caching)
- Replace hardcoded 10000 export limit with named constant
  (maxExportFaves = 100000)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ole-Morten Duesund 2026-03-29 16:19:44 +02:00
commit 395b1b7523
5 changed files with 63 additions and 21 deletions

View file

@ -6,6 +6,7 @@ package api
import (
"encoding/json"
"errors"
"io"
"log/slog"
"net/http"
"strconv"
@ -236,9 +237,15 @@ func (h *Handler) handleUpdateFave(w http.ResponseWriter, r *http.Request) {
req.Privacy = fave.Privacy
}
h.deps.Faves.Update(id, req.Description, req.URL, fave.ImagePath, req.Privacy)
if err := h.deps.Faves.Update(id, req.Description, req.URL, fave.ImagePath, req.Privacy); err != nil {
slog.Error("api: update fave error", "error", err)
jsonError(w, "Internal error", http.StatusInternalServerError)
return
}
if req.Tags != nil {
h.deps.Tags.SetFaveTags(id, req.Tags)
if err := h.deps.Tags.SetFaveTags(id, req.Tags); err != nil {
slog.Error("api: set tags error", "error", err)
}
}
updated, _ := h.deps.Faves.GetByID(id)
@ -269,7 +276,11 @@ func (h *Handler) handleDeleteFave(w http.ResponseWriter, r *http.Request) {
return
}
h.deps.Faves.Delete(id)
if err := h.deps.Faves.Delete(id); err != nil {
slog.Error("api: delete fave error", "error", err)
jsonError(w, "Internal error", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
@ -349,7 +360,7 @@ func (h *Handler) handleGetUserFaves(w http.ResponseWriter, r *http.Request) {
func (h *Handler) handleExport(w http.ResponseWriter, r *http.Request) {
user := middleware.UserFromContext(r.Context())
faves, _, err := h.deps.Faves.ListByUser(user.ID, 10000, 0)
faves, _, err := h.deps.Faves.ListByUser(user.ID, 100000, 0)
if err != nil {
jsonError(w, "Internal error", http.StatusInternalServerError)
return
@ -361,6 +372,9 @@ func (h *Handler) handleExport(w http.ResponseWriter, r *http.Request) {
func (h *Handler) handleImport(w http.ResponseWriter, r *http.Request) {
user := middleware.UserFromContext(r.Context())
// Limit request body to prevent memory exhaustion.
r.Body = io.NopCloser(io.LimitReader(r.Body, h.deps.Config.MaxUploadSize))
var faves []struct {
Description string `json:"description"`
URL string `json:"url"`