From 094baf6fbf64cc7bba6c77a2afd2df781697fb63 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Mon, 3 Nov 2014 00:28:07 +0000 Subject: [PATCH] error out if attempting connection to loopback --- net/conn/dial.go | 5 +++++ util/util.go | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/net/conn/dial.go b/net/conn/dial.go index 7b9c1dc86..dcb196bf5 100644 --- a/net/conn/dial.go +++ b/net/conn/dial.go @@ -8,6 +8,7 @@ import ( manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net" peer "github.com/jbenet/go-ipfs/peer" + "github.com/jbenet/go-ipfs/util" ) // Dial connects to a particular peer, over a given network @@ -23,6 +24,10 @@ func (d *Dialer) Dial(ctx context.Context, network string, remote peer.Peer) (Co return nil, fmt.Errorf("No remote address for network %s", network) } + if util.IsLoopbackAddr(raddr.String()) { + return nil, fmt.Errorf("Attempted to connect to loopback address: %s", raddr) + } + remote, err := d.Peerstore.Add(remote) if err != nil { log.Errorf("Error putting peer into peerstore: %s", remote) diff --git a/util/util.go b/util/util.go index 0076322e2..81b143057 100644 --- a/util/util.go +++ b/util/util.go @@ -113,7 +113,13 @@ func GetenvBool(name string) bool { } func IsLoopbackAddr(addr string) bool { - return addr == "/ip4/127.0.0.1" || addr == "/ip6/::1" + loops := []string{"/ip4/127.0.0.1", "/ip6/::1", "/ip4/0.0.0.0"} + for _, loop := range loops { + if strings.HasPrefix(addr, loop) { + return true + } + } + return false } func GetLocalAddresses() ([]ma.Multiaddr, error) {