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:
1 parent
cb2604bd05
commit
796384420b
15 files changed
+220
-17
No files matched your search
@@ -15,9 +15,24 @@ type loginPage struct {
|
||||
Next string
|
||||
}
|
||||
|
||||
// localPath reports whether next is a path on this site. Browsers drop tabs
|
||||
// and newlines from URLs and treat a backslash like "/", so "/\t/evil.example"
|
||||
// would become "//evil.example"; any control character or backslash fails.
|
||||
func localPath(next string) bool {
|
||||
if !strings.HasPrefix(next, "/") || strings.HasPrefix(next, "//") {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(next); i++ {
|
||||
if c := next[i]; c < 0x20 || c == 0x7f || c == '\\' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// safeNext only allows local redirect targets.
|
||||
func safeNext(next string) string {
|
||||
if !strings.HasPrefix(next, "/") || strings.HasPrefix(next, "//") || strings.HasPrefix(next, "/\\") {
|
||||
if !localPath(next) {
|
||||
return "/admin"
|
||||
}
|
||||
return next
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cdnutter/canon/internal/auth"
|
||||
@@ -90,7 +89,7 @@ func (s *Server) gatedAsset(h http.HandlerFunc) http.HandlerFunc {
|
||||
|
||||
// safePublicNext is safeNext for visitors: it falls back to the home page.
|
||||
func safePublicNext(next string) string {
|
||||
if !strings.HasPrefix(next, "/") || strings.HasPrefix(next, "//") || strings.HasPrefix(next, "/\\") {
|
||||
if !localPath(next) {
|
||||
return "/"
|
||||
}
|
||||
return next
|
||||
|
||||
@@ -86,7 +86,8 @@ func DefaultTitle(ownerName string) string {
|
||||
func (s *Server) handleCustomCSS(w http.ResponseWriter, r *http.Request) {
|
||||
set, err := s.store.Settings(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
s.log.Error("loading custom css", "err", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/css; charset=utf-8")
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -218,6 +218,9 @@ func (s *Server) securityHeaders(next http.Handler) http.Handler {
|
||||
h.Set("X-Content-Type-Options", "nosniff")
|
||||
h.Set("Referrer-Policy", "same-origin")
|
||||
h.Set("X-Frame-Options", "SAMEORIGIN")
|
||||
if s.isHTTPS(r) {
|
||||
h.Set("Strict-Transport-Security", "max-age=31536000")
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
@@ -297,9 +300,16 @@ func (s *Server) sameOrigin(r *http.Request) bool {
|
||||
|
||||
func (s *Server) clientIP(r *http.Request) string {
|
||||
if s.cfg.TrustProxy {
|
||||
// Use the last entry: it's the one the proxy added. Earlier
|
||||
// entries come from the client and can be anything, which would
|
||||
// let it dodge the login rate limit.
|
||||
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
|
||||
first, _, _ := strings.Cut(xff, ",")
|
||||
return strings.TrimSpace(first)
|
||||
if i := strings.LastIndexByte(xff, ','); i >= 0 {
|
||||
xff = xff[i+1:]
|
||||
}
|
||||
if ip := strings.TrimSpace(xff); ip != "" {
|
||||
return ip
|
||||
}
|
||||
}
|
||||
if xr := r.Header.Get("X-Real-IP"); xr != "" {
|
||||
return xr
|
||||
|
||||
Reference in new issue
Block a user