mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-24 02:49:57 +01:00
test(plugins): stub DNS in the host SSRF guard tests (#6208)
The dial-time SSRF guard runs on the resolved IP, so the tests that prove a symbolic hostname cannot reach loopback used "localhost." — a trailing dot never matches /etc/hosts, so Go queries real DNS. Machines whose resolver does not answer "localhost." (a VPN DNS, for example) got "no such host" before the dial guard ever ran, failing three specs. Add tests.StubResolver, a net.Resolver backed by an in-memory DNS responder over net.Pipe, and let the plugin dialers take a resolver so tests can inject it. Name resolution in those specs no longer depends on the machine's DNS.
This commit is contained in:
parent
ee6dd1bc03
commit
101145742f
7 changed files with 70 additions and 1 deletions
|
|
@ -54,6 +54,7 @@ func newHTTPService(pluginName string, permission *HTTPPermission) *httpServiceI
|
|||
Timeout: 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)}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,10 @@ var _ = Describe("httpServiceImpl", func() {
|
|||
ts *httptest.Server
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
stubLocalhostDNS()
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
if ts != nil {
|
||||
ts.Close()
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ func (s *webSocketServiceImpl) Connect(ctx context.Context, urlStr string, heade
|
|||
// Establish WebSocket connection
|
||||
dialer := 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)
|
||||
|
|
|
|||
|
|
@ -505,6 +505,7 @@ var _ = Describe("WebSocketService", Ordered, func() {
|
|||
var savedHosts []string
|
||||
|
||||
BeforeEach(func() {
|
||||
stubLocalhostDNS()
|
||||
savedHosts = testService.requiredHosts
|
||||
upgrader := websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }}
|
||||
wsServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
54
tests/dns_stub.go
Normal file
54
tests/dns_stub.go
Normal file
|
|
@ -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...))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue