security: fix open redirect and add small hardening (#1227)
## What this does Fixes an open redirect plus a handful of small, mostly-invisible hardening changes around it. ## What's in it - **The fix**: trailing-slash redirects no longer let `//evil.com/` redirect off-host. - **SSRF block** on the DNS/HTTP namespace verification — refuses to dial loopback, RFC1918, link-local, multicast, CGNAT, or IP-literal/single-label \"domains\". - **Slow-client guard**: `ReadTimeout` and `IdleTimeout` on the HTTP server (no `WriteTimeout` — would risk cutting off legitimate multi-package publishes). - **Defence-in-depth headers** on the UI (`/`) and 404 responses. - **5xx responses** stop leaking raw DB error text to clients (logged server-side instead). - **Search filter** stops treating `%` and `_` as wildcards. - **Validators**: PyPI/NuGet identifiers URL-escaped before fetching; MCPB no longer follows redirects. - **Misc**: `Version` field bounded to 255 chars; logs quote user input; `BlockedNamespaces` denylist now catches subdomain claimants. Each change is its own commit so they can be reviewed (or reverted) independently. ## Test plan - [ ] CI green - [ ] Synthetic publish in staging for each package type (npm, pypi, nuget, oci, mcpb) — confirms validator changes don't regress - [ ] Quick browser check of `/` after deploy to confirm UI still loads with the new CSP - [ ] `curl https://<host>//evil.com/` — confirm the open redirect is closed 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
f7663fe91a
commit
1201cbd82b
1 changed files with 330 additions and 48 deletions
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -34,6 +35,9 @@ type DefaultHTTPKeyFetcher struct {
|
|||
|
||||
// NewDefaultHTTPKeyFetcher creates a new HTTP key fetcher with timeout
|
||||
func NewDefaultHTTPKeyFetcher() *DefaultHTTPKeyFetcher {
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
transport.DialContext = safeDialContext
|
||||
|
||||
return &DefaultHTTPKeyFetcher{
|
||||
client: &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
|
|
@ -42,10 +46,92 @@ func NewDefaultHTTPKeyFetcher() *DefaultHTTPKeyFetcher {
|
|||
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
Transport: transport,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// safeDialContext resolves the target hostname and refuses to dial loopback,
|
||||
// private (RFC1918, ULA), link-local, or unspecified addresses. Combined with
|
||||
// IsValidDomain rejecting IP literals, this neutralises SSRF abuse of the
|
||||
// well-known fetcher: an attacker cannot reach internal HTTPS services
|
||||
// (Kubernetes API server, internal admin panels, internal DNS-resolved hosts)
|
||||
// even if they control DNS for an attacker domain.
|
||||
//
|
||||
// The hostname is resolved once here; we then dial the resolved IP directly,
|
||||
// which pins the connection against DNS rebinding (a TOCTOU where the resolver
|
||||
// returns a public IP to a pre-flight check and an internal IP to the actual
|
||||
// dial). TLS SNI and the Host header continue to use the original hostname
|
||||
// since they are set by http.Transport from the request URL, not the dial
|
||||
// address.
|
||||
func safeDialContext(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
host, port, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resolver net.Resolver
|
||||
ips, err := resolver.LookupIPAddr(ctx, host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Try each non-blocked address in order, falling through on dial failure.
|
||||
// Without this, a stale public AAAA record that no longer routes (or any
|
||||
// individually-unreachable IP) breaks auth where the default transport
|
||||
// would have recovered by trying the next answer.
|
||||
//
|
||||
// Each attempt is bounded by perIPDialTimeout so that a single hanging
|
||||
// address can't consume the whole http.Client budget. This is a
|
||||
// simpler substitute for Happy Eyeballs (parallel A/AAAA racing) — we
|
||||
// fail fast and try the next answer instead of racing them.
|
||||
const perIPDialTimeout = 3 * time.Second
|
||||
|
||||
var lastErr error
|
||||
allBlocked := true
|
||||
for _, ip := range ips {
|
||||
if isBlockedIP(ip.IP) {
|
||||
continue
|
||||
}
|
||||
allBlocked = false
|
||||
dialCtx, cancel := context.WithTimeout(ctx, perIPDialTimeout)
|
||||
var d net.Dialer
|
||||
conn, dialErr := d.DialContext(dialCtx, network, net.JoinHostPort(ip.IP.String(), port))
|
||||
cancel()
|
||||
if dialErr == nil {
|
||||
return conn, nil
|
||||
}
|
||||
lastErr = dialErr
|
||||
}
|
||||
if allBlocked {
|
||||
return nil, fmt.Errorf("dial %s: refusing to connect to private or loopback address", host)
|
||||
}
|
||||
return nil, fmt.Errorf("dial %s: all resolved public addresses failed: %w", host, lastErr)
|
||||
}
|
||||
|
||||
// cgnatRange covers RFC 6598 Carrier-Grade NAT (100.64.0.0/10), which the
|
||||
// stdlib does not classify via any Is* helper but is reachable on some
|
||||
// cloud / mobile networks where it shadows internal infrastructure.
|
||||
var cgnatRange = func() *net.IPNet {
|
||||
_, n, _ := net.ParseCIDR("100.64.0.0/10")
|
||||
return n
|
||||
}()
|
||||
|
||||
// isBlockedIP reports whether an IP must not be dialled by the namespace
|
||||
// verification fetcher. Covers loopback (127/8, ::1), RFC1918 + ULA via
|
||||
// IsPrivate, link-local (169.254/16, fe80::/10 — includes cloud metadata
|
||||
// 169.254.169.254), unspecified (0.0.0.0, ::), all multicast (admin-scoped
|
||||
// 239/8 and ff00::/8 in addition to link-local-multicast), and CGNAT.
|
||||
func isBlockedIP(ip net.IP) bool {
|
||||
if ip == nil {
|
||||
return true
|
||||
}
|
||||
return ip.IsLoopback() || ip.IsPrivate() ||
|
||||
ip.IsLinkLocalUnicast() || ip.IsMulticast() ||
|
||||
ip.IsUnspecified() ||
|
||||
cgnatRange.Contains(ip)
|
||||
}
|
||||
|
||||
// NewDefaultHTTPKeyFetcherWithClient creates a new HTTP key fetcher with a custom HTTP client.
|
||||
// This is primarily useful in tests to inject transports or TLS settings.
|
||||
func NewDefaultHTTPKeyFetcherWithClient(client *http.Client) *DefaultHTTPKeyFetcher {
|
||||
|
|
|
|||
Loading…
Reference in a new issue