Files
uptimesebi/internal/sso/httpclient.go
T
Sebastian UnterschützandClaude Opus 5.5 9ad5f29f1b feat(sso): add openid connect client with pkce and guarded discovery
Issuer URLs come from customers, so requests to their IdPs refuse
non-public addresses unless private networks are explicitly allowed.

Refs #4

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 00:30:43 +01:00

46 lines
1.3 KiB
Go

package sso
import (
"context"
"errors"
"net"
"net/http"
"net/netip"
"syscall"
"time"
)
// ErrForbiddenAddress is returned when an IdP resolves to an address the
// client may not contact.
var ErrForbiddenAddress = errors.New("sso: issuer resolves to a non-public address")
var cgnat = netip.MustParsePrefix("100.64.0.0/10")
// SafeHTTPClient returns a client for talking to customer-configured IdPs.
// Unless allowPrivate is set it refuses to connect to loopback, private,
// link-local and similar ranges. The check runs on the resolved address at
// dial time, so DNS rebinding and redirects cannot bypass it.
func SafeHTTPClient(allowPrivate bool) *http.Client {
d := &net.Dialer{Timeout: 5 * time.Second}
if !allowPrivate {
d.Control = func(_, address string, _ syscall.RawConn) error {
ap, err := netip.ParseAddrPort(address)
if err != nil || !isPublic(ap.Addr()) {
return ErrForbiddenAddress
}
return nil
}
}
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.Proxy = nil
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return d.DialContext(ctx, network, addr)
}
return &http.Client{Transport: tr, Timeout: 10 * time.Second}
}
func isPublic(a netip.Addr) bool {
a = a.Unmap()
return a.IsGlobalUnicast() && !a.IsPrivate() && !cgnat.Contains(a)
}