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:
Gabriel20xxandClaude Sonnet 5 committed 2026-09-03 21:46:53 +02:00
1 parent 730bf0463f
commit 449bb6fd6a
8 files changed
+241 -27

No files matched your search

+34 -3
View File
@@ -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
+14 -4
View File
@@ -14,10 +14,20 @@ func TestNewNormalizesBaseURL(t *testing.T) {
input string
want string
}{
{name: "bare host gets https", input: "auth.example.com", want: "https://auth.example.com"},
{name: "bare host:port gets https", input: "192.168.1.50:8080", want: "https://192.168.1.50:8080"},
{name: "explicit http is left alone", input: "http://auth.example.com", want: "http://auth.example.com"},
{name: "explicit https is left alone", input: "https://auth.example.com", want: "https://auth.example.com"},
{name: "bare public host gets https", input: "auth.example.com", want: "https://auth.example.com"},
{name: "bare private host:port gets http", input: "192.168.1.50:8080", want: "http://192.168.1.50:8080"},
{name: "bare public host:port gets https", input: "auth.example.com:8080", want: "https://auth.example.com:8080"},
{name: "loopback IPv4 gets http", input: "127.0.0.1:8080", want: "http://127.0.0.1:8080"},
{name: "10/8 private range gets http", input: "10.1.2.3:8080", want: "http://10.1.2.3:8080"},
{name: "172.16/12 private range gets http", input: "172.20.0.5:8080", want: "http://172.20.0.5:8080"},
{name: "172.x outside the 16-31 private range gets https", input: "172.32.0.5:8080", want: "https://172.32.0.5:8080"},
{name: "link-local IPv4 gets http", input: "169.254.1.1:8080", want: "http://169.254.1.1:8080"},
{name: "localhost name gets http", input: "localhost:8080", want: "http://localhost:8080"},
{name: ".local mDNS name gets http", input: "authguard.local:8080", want: "http://authguard.local:8080"},
{name: "loopback IPv6 gets http", input: "[::1]:8080", want: "http://[::1]:8080"},
{name: "unique-local IPv6 gets http", input: "[fd00::1]:8080", want: "http://[fd00::1]:8080"},
{name: "explicit http is left alone even for a public host", input: "http://auth.example.com", want: "http://auth.example.com"},
{name: "explicit https is left alone even for a private host", input: "https://192.168.1.50:8080", want: "https://192.168.1.50:8080"},
{name: "scheme match is case-insensitive", input: "HTTP://auth.example.com", want: "HTTP://auth.example.com"},
{name: "trailing slash stripped before scheme check", input: "auth.example.com/", want: "https://auth.example.com"},
{name: "surrounding whitespace trimmed", input: " auth.example.com ", want: "https://auth.example.com"},