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

+38 -1
View File
@@ -10,11 +10,14 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/netip"
"os"
"path/filepath"
"regexp"
"strings"
"syscall"
"time"
)
@@ -33,9 +36,43 @@ func New(dataDir, userAgent string) (*Cache, error) {
if err := os.MkdirAll(dir, 0o755); err != nil {
return nil, err
}
return &Cache{dir: dir, ua: userAgent, client: &http.Client{Timeout: 30 * time.Second}}, nil
// Image URLs come from the admin UI and from metadata providers, so the
// server only fetches from public addresses (no SSRF into the LAN).
dialer := &net.Dialer{Timeout: 10 * time.Second, Control: publicOnly}
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
TLSHandshakeTimeout: 10 * time.Second,
ForceAttemptHTTP2: true,
}
return &Cache{dir: dir, ua: userAgent, client: &http.Client{Timeout: 30 * time.Second, Transport: transport}}, nil
}
// errPrivateAddr is returned when an image URL points at a private address.
var errPrivateAddr = errors.New("image URL points at a private or local address")
// publicOnly refuses connections to loopback, private, link-local,
// multicast and unspecified addresses. It runs after DNS resolution, so a
// public name that resolves to a LAN address is refused too.
func publicOnly(network, address string, _ syscall.RawConn) error {
host, _, err := net.SplitHostPort(address)
if err != nil {
return err
}
ip, err := netip.ParseAddr(host)
if err != nil {
return errPrivateAddr
}
ip = ip.Unmap()
if !ip.IsGlobalUnicast() || ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() ||
cgnat.Contains(ip) {
return errPrivateAddr
}
return nil
}
var cgnat = netip.MustParsePrefix("100.64.0.0/10")
func (c *Cache) Dir() string { return c.dir }
var nameRe = regexp.MustCompile(`^[0-9a-f]{40}\.(jpg|png|webp|gif|avif)$`)