Security sweep: fix open redirect, XFF rate-limit bypass, SSRF; harden image and compose

- Reject control chars and backslashes in redirect targets
- Use proxy-added X-Forwarded-For entry, cap limiter keys
- Block private/loopback/link-local addresses on image fetch
- HSTS over HTTPS, generic error on custom CSS
- x/crypto 0.59.0, pin golang:1.27.4-alpine3.24 and alpine:3.24.4
- Compose: cap_drop ALL, no-new-privileges, read_only, limits, labels
- Add SECURITY.md, CHANGELOG.md, TODO.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cdnutterandClaude Opus 5.5 committed 2026-11-23 13:16:31 -08:00
1 parent cb2604bd05
commit 796384420b
15 files changed
+220 -17

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
package server
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/cdnutter/canon/internal/config"
)
func TestSafeNextBlocksOffsite(t *testing.T) {
bad := []string{"", "x", "https://evil.example", "//evil.example", "/\\evil.example",
"/\t/evil.example", "/\n/evil.example", "/\r/evil.example", "/%09/evil.example\x00",
"/a\\b", "\\\\evil.example", "/\x7f/evil.example"}
for _, in := range bad {
if got := safeNext(in); got != "/admin" {
t.Errorf("safeNext(%q) = %q, want /admin", in, got)
}
if got := safePublicNext(in); got != "/" {
t.Errorf("safePublicNext(%q) = %q, want /", in, got)
}
}
for _, in := range []string{"/", "/admin", "/l/movies?x=1", "/admin/lists/3#top"} {
if got := safeNext(in); got != in {
t.Errorf("safeNext(%q) = %q, want unchanged", in, got)
}
}
// The redirect a browser actually sees must stay on this site.
for _, in := range bad {
w := httptest.NewRecorder()
http.Redirect(w, httptest.NewRequest("GET", "/login", nil), safeNext(in), http.StatusSeeOther)
if loc := w.Header().Get("Location"); loc != "/admin" {
t.Errorf("next=%q redirected to %q", in, loc)
}
}
}
func TestClientIPUsesProxyAddedEntry(t *testing.T) {
s := &Server{cfg: config.Config{TrustProxy: true}}
r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("X-Forwarded-For", "6.6.6.6, 203.0.113.9")
if got := s.clientIP(r); got != "203.0.113.9" {
t.Errorf("clientIP = %q, want the proxy-added 203.0.113.9", got)
}
s.cfg.TrustProxy = false
if got := s.clientIP(r); got != "192.0.2.1" {
t.Errorf("clientIP without proxy trust = %q, want RemoteAddr host", got)
}
}