🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
test(plugins): stub DNS in the host SSRF guard tests (#6208)
Deluan Quintão101145742f47
diff --git a/plugins/host_httpclient.go b/plugins/host_httpclient.goindex 3265ac4b7..6a9a4d2d6 100644--- a/plugins/host_httpclient.go+++ b/plugins/host_httpclient.go@@ -54,6 +54,7 @@ func newHTTPService(pluginName string, permission *HTTPPermission) *httpServiceITimeout: 30 * time.Second,KeepAlive: 30 * time.Second,Control: svc.dialControl,+ Resolver: dialResolver,}).DialContext// No client timeout: it is set per-request via context deadline.svc.client = &http.Client{Transport: httpclient.NewTransport(svc.transport)}diff --git a/plugins/host_httpclient_test.go b/plugins/host_httpclient_test.goindex 6edf75a97..81e3192fd 100644--- a/plugins/host_httpclient_test.go+++ b/plugins/host_httpclient_test.go@@ -20,6 +20,10 @@ var _ = Describe("httpServiceImpl", func() {ts *httptest.Server)+ BeforeEach(func() {+ stubLocalhostDNS()+ })+AfterEach(func() {if ts != nil {ts.Close()diff --git a/plugins/host_netguard.go b/plugins/host_netguard.goindex f78aaf6a8..ab092009a 100644--- a/plugins/host_netguard.go+++ b/plugins/host_netguard.go@@ -8,6 +8,9 @@ import ("github.com/navidrome/navidrome/utils/netguard")+// dialResolver is nil in production (the system resolver); tests swap in a stub to avoid real DNS.+var dialResolver *net.Resolver+// checkPrivateDial runs at dial time on the resolved IP, so hostnames can't reach private addresses unless a// literal IP/CIDR entry or a bare "*" (plugins targeting user-configured LAN services) allows it.func checkPrivateDial(requiredHosts []string, address string) error {diff --git a/plugins/host_websocket.go b/plugins/host_websocket.goindex 933e53144..d9d82665c 100644--- a/plugins/host_websocket.go+++ b/plugins/host_websocket.go@@ -114,7 +114,7 @@ func (s *webSocketServiceImpl) Connect(ctx context.Context, urlStr string, heade// Establish WebSocket connectiondialer := websocket.Dialer{HandshakeTimeout: 30 * time.Second,- NetDialContext: (&net.Dialer{Control: s.dialControl}).DialContext,+ NetDialContext: (&net.Dialer{Control: s.dialControl, Resolver: dialResolver}).DialContext,}conn, resp, err := dialer.DialContext(ctx, urlStr, httpHeaders)diff --git a/plugins/host_websocket_test.go b/plugins/host_websocket_test.goindex 772d83cc9..9b94e4b70 100644--- a/plugins/host_websocket_test.go+++ b/plugins/host_websocket_test.go@@ -505,6 +505,7 @@ var _ = Describe("WebSocketService", Ordered, func() {var savedHosts []stringBeforeEach(func() {+ stubLocalhostDNS()savedHosts = testService.requiredHostsupgrader := websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }}wsServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {diff --git a/plugins/plugins_suite_test.go b/plugins/plugins_suite_test.goindex cc1b45f97..aa07eae32 100644--- a/plugins/plugins_suite_test.go+++ b/plugins/plugins_suite_test.go@@ -46,6 +46,12 @@ func TestPlugins(t *testing.T) {RunSpecs(t, "Plugins Suite")}+// stubLocalhostDNS resolves "localhost." without real DNS: the trailing dot never matches /etc/hosts.+func stubLocalhostDNS() {+ dialResolver = tests.StubResolver(map[string]string{"localhost.": "127.0.0.1"})+ DeferCleanup(func() { dialResolver = nil })+}+// createTestManager creates a new plugin Manager with the given plugin config.// It creates a temp directory, copies the test-metadata-agent plugin, and starts the manager.// Returns the manager, temp directory path, and a cleanup function.diff --git a/tests/dns_stub.go b/tests/dns_stub.gonew file mode 100644index 000000000..7cd7faf6d--- /dev/null+++ b/tests/dns_stub.go@@ -0,0 +1,54 @@+package tests++import (+ "context"+ "encoding/binary"+ "io"+ "net"+ "net/netip"++ "golang.org/x/net/dns/dnsmessage"+)++// StubResolver returns a net.Resolver that answers A queries from records (absolute name to IPv4,+// e.g. "localhost." to "127.0.0.1") instead of the system DNS. Other names do not resolve.+func StubResolver(records map[string]string) *net.Resolver {+ return &net.Resolver{+ PreferGo: true,+ Dial: func(context.Context, string, string) (net.Conn, error) {+ client, server := net.Pipe()+ go serveStubDNS(server, records)+ return client, nil+ },+ }+}++// net.Pipe is not a PacketConn, so the resolver sends one query framed with a 2-byte length prefix.+func serveStubDNS(conn net.Conn, records map[string]string) {+ defer conn.Close()+ var size [2]byte+ if _, err := io.ReadFull(conn, size[:]); err != nil {+ return+ }+ buf := make([]byte, binary.BigEndian.Uint16(size[:]))+ if _, err := io.ReadFull(conn, buf); err != nil {+ return+ }+ var msg dnsmessage.Message+ if err := msg.Unpack(buf); err != nil || len(msg.Questions) != 1 {+ return+ }+ msg.Response = true+ q := msg.Questions[0]+ if ip, err := netip.ParseAddr(records[q.Name.String()]); err == nil && q.Type == dnsmessage.TypeA {+ msg.Answers = []dnsmessage.Resource{{+ Header: dnsmessage.ResourceHeader{Name: q.Name, Type: q.Type, Class: q.Class},+ Body: &dnsmessage.AResource{A: ip.As4()},+ }}+ }+ resp, err := msg.Pack()+ if err != nil {+ return+ }+ _, _ = conn.Write(append(binary.BigEndian.AppendUint16(nil, uint16(len(resp))), resp...))+}