From 38d4814a564c0d826b09592c93b0e2345373b514 Mon Sep 17 00:00:00 2001 From: Apulupie <167634780+Frun1na@users.noreply.github.com> Date: Tue, 22 Sep 2026 18:28:48 +0800 Subject: [PATCH] fix(rmqctl): allow plain HTTP for intranet DNS names that resolve to private addresses --- rmqctl/internal/studio/auth.go | 42 ++++++++++++++++++++----- rmqctl/internal/studio/client_test.go | 45 +++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/rmqctl/internal/studio/auth.go b/rmqctl/internal/studio/auth.go index fa234324f4..a4bb6e1b95 100644 --- a/rmqctl/internal/studio/auth.go +++ b/rmqctl/internal/studio/auth.go @@ -7,6 +7,7 @@ package studio import ( + "context" "crypto/hmac" "crypto/sha256" "encoding/hex" @@ -119,18 +120,43 @@ func isLoopbackHost(host string) bool { return ip != nil && ip.IsLoopback() } -// isPrivateHost reports whether host is a loopback, RFC1918 private-network, -// or link-local address. Plain HTTP is permitted for these non-routable, -// trusted networks (local development and same-VPC/intranet deployments); -// public endpoints must still use HTTPS so that HMAC credentials are never -// transmitted in the clear over the Internet. +// lookupHostIPAddrs resolves a hostname so private-network support covers intranet DNS names, +// not just literal addresses. Package-level so tests can stub resolution. +var lookupHostIPAddrs = func(host string) ([]net.IP, error) { + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + addrs, err := net.DefaultResolver.LookupIPAddr(ctx, host) + if err != nil { + return nil, err + } + ips := make([]net.IP, 0, len(addrs)) + for _, addr := range addrs { + ips = append(ips, addr.IP) + } + return ips, nil +} + +// isPrivateHost reports whether host is a loopback, RFC1918 private-network, or link-local +// address, whether given as a literal IP or as a hostname that resolves exclusively to such +// addresses. Plain HTTP is permitted for these non-routable, trusted networks (local +// development and same-VPC/intranet deployments); public endpoints must still use HTTPS so +// that HMAC credentials are never transmitted in the clear over the Internet. Unresolvable +// hostnames and mixed public/private resolution stay on HTTPS. func isPrivateHost(host string) bool { if isLoopbackHost(host) { return true } - ip := net.ParseIP(host) - if ip == nil { + if ip := net.ParseIP(host); ip != nil { + return ip.IsPrivate() || ip.IsLinkLocalUnicast() + } + ips, err := lookupHostIPAddrs(host) + if err != nil || len(ips) == 0 { return false } - return ip.IsPrivate() || ip.IsLinkLocalUnicast() + for _, ip := range ips { + if !(ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast()) { + return false + } + } + return true } diff --git a/rmqctl/internal/studio/client_test.go b/rmqctl/internal/studio/client_test.go index 7bc6fd4640..54feeb9603 100644 --- a/rmqctl/internal/studio/client_test.go +++ b/rmqctl/internal/studio/client_test.go @@ -16,12 +16,41 @@ */ package studio -import "testing" +import ( + "fmt" + "net" + "testing" +) + +// stubHostResolution replaces real DNS with a deterministic table so isPrivateHost tests do not +// depend on the network. Hosts absent from the table fail resolution (stays on HTTPS). +func stubHostResolution(t *testing.T, table map[string][]string) { + t.Helper() + original := lookupHostIPAddrs + lookupHostIPAddrs = func(host string) ([]net.IP, error) { + if addrs, ok := table[host]; ok { + ips := make([]net.IP, 0, len(addrs)) + for _, a := range addrs { + ips = append(ips, net.ParseIP(a)) + } + return ips, nil + } + return nil, fmt.Errorf("no such host %q", host) + } + t.Cleanup(func() { lookupHostIPAddrs = original }) +} // TestValidateServerSchemeAdaptiveHTTP verifies the adaptive scheme policy: // HTTPS is always accepted, plain HTTP is accepted for loopback / RFC1918 -// private / link-local hosts, and rejected for public hosts. +// private / link-local hosts — literal or resolved exclusively to such +// addresses — and rejected for public or unresolvable hosts. func TestValidateServerSchemeAdaptiveHTTP(t *testing.T) { + stubHostResolution(t, map[string][]string{ + "studio.internal": {"10.0.3.104"}, + "mixed.example.com": {"10.0.3.104", "47.98.43.243"}, + "studio.example.com": {"47.98.43.243"}, + "unresolvable.internal": nil, + }) tests := []struct { name string server string @@ -35,8 +64,11 @@ func TestValidateServerSchemeAdaptiveHTTP(t *testing.T) { {"http private 172.16/12", "http://172.16.5.10:6789", false}, {"http private 192.168/16", "http://192.168.1.20:6789", false}, {"http link-local 169.254/16", "http://169.254.1.5:6789", false}, + {"http private dns name", "http://studio.internal:6789", false}, {"http public ipv4 rejected", "http://47.98.43.243:6789", true}, {"http public domain rejected", "http://studio.example.com", true}, + {"http mixed resolution rejected", "http://mixed.example.com:6789", true}, + {"http unresolvable host rejected", "http://unresolvable.internal:6789", true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -57,9 +89,17 @@ func TestValidateServerSchemeAdaptiveHTTP(t *testing.T) { } func TestIsPrivateHost(t *testing.T) { + stubHostResolution(t, map[string][]string{ + "studio.internal": {"10.0.3.104"}, + "corp.local": {"192.168.1.1", "192.168.1.2"}, + "mixed.example.com": {"10.0.3.104", "47.98.43.243"}, + "studio.example.com": {"47.98.43.243"}, + "unresolvable.internal": nil, + }) private := []string{ "localhost", "127.0.0.1", "::1", "10.1.2.3", "172.16.0.1", "172.31.255.255", "192.168.0.1", "169.254.1.1", + "studio.internal", "corp.local", } for _, host := range private { if !isPrivateHost(host) { @@ -69,6 +109,7 @@ func TestIsPrivateHost(t *testing.T) { public := []string{ "studio.example.com", "47.98.43.243", "8.8.8.8", "172.15.0.1", "172.32.0.1", "11.0.0.1", + "mixed.example.com", "unresolvable.internal", "absent.example.org", } for _, host := range public { if isPrivateHost(host) {