Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions rmqctl/internal/studio/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package studio

import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
Expand Down Expand Up @@ -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
}
45 changes: 43 additions & 2 deletions rmqctl/internal/studio/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down