Auto-detect scheme for AUTHGUARD_URL instead of always assuming https
A bare host:port (e.g. a LAN AuthGuard reachable only over plain HTTP) was always defaulting to https, so a TLS handshake against a plain-HTTP AuthGuard failed at the transport level. Now a loopback, RFC1918/RFC4193 private, or link-local host (or a .local name) defaults to http instead; an explicit http:// or https:// is still always honored as-is. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c29520fcd3
commit
40578f5392
8 files changed
+241
-27
No files matched your search
@@ -20,6 +20,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -77,9 +78,11 @@ func WithCookieName(name string) Option {
|
||||
|
||||
// New creates a Client for the AuthServer instance at baseURL. baseURL may
|
||||
// be given without a scheme (e.g. "auth.example.com" or "192.168.1.50:8080")
|
||||
// - "https://" is assumed in that case, so a deployment only needs to name
|
||||
// the host, not repeat the scheme; an explicit "http://" (for a plain-HTTP
|
||||
// local/LAN AuthServer) is left as-is.
|
||||
// - the scheme is then inferred from the host: a loopback, RFC1918/RFC4193
|
||||
// private, or link-local address, or a ".local" name, defaults to "http://"
|
||||
// (a plain-HTTP AuthServer on a local network is the common case for those),
|
||||
// anything else defaults to "https://". An explicit "http://" or "https://"
|
||||
// is always left as-is, overriding the inference.
|
||||
func New(baseURL string, opts ...Option) *Client {
|
||||
c := &Client{
|
||||
baseURL: normalizeBaseURL(baseURL),
|
||||
@@ -101,9 +104,37 @@ func normalizeBaseURL(baseURL string) string {
|
||||
if strings.HasPrefix(lower, "http://") || strings.HasPrefix(lower, "https://") {
|
||||
return trimmed
|
||||
}
|
||||
if isPrivateHost(trimmed) {
|
||||
return "http://" + trimmed
|
||||
}
|
||||
return "https://" + trimmed
|
||||
}
|
||||
|
||||
// isPrivateHost reports whether hostport (a bare "host" or "host:port", no
|
||||
// scheme) names a loopback, RFC1918/RFC4193 private, or link-local address,
|
||||
// or a ".local" mDNS name -- the kind of host a plain-HTTP AuthServer is
|
||||
// typically reachable at on a local network, as opposed to a public hostname
|
||||
// expected to terminate TLS itself. Never performs DNS resolution: only
|
||||
// literal IPs and the "localhost"/".local" names are recognized, so a real
|
||||
// public hostname that merely happens to resolve to a private address still
|
||||
// defaults to https.
|
||||
func isPrivateHost(hostport string) bool {
|
||||
host := hostport
|
||||
if h, _, err := net.SplitHostPort(hostport); err == nil {
|
||||
host = h
|
||||
}
|
||||
host = strings.Trim(host, "[]")
|
||||
lower := strings.ToLower(host)
|
||||
if lower == "localhost" || strings.HasSuffix(lower, ".local") {
|
||||
return true
|
||||
}
|
||||
ip := net.ParseIP(host)
|
||||
if ip == nil {
|
||||
return false
|
||||
}
|
||||
return ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast()
|
||||
}
|
||||
|
||||
// Validate calls AuthServer's GET /api/session/validate with token as a
|
||||
// Bearer credential and returns the resolved user. It returns
|
||||
// ErrInvalidSession (wrapped) when AuthServer reports the session as not
|
||||
|
||||
Reference in new issue
Block a user