Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

p2p: fix infinite loop in dnsaddr resolution #5926

Merged
Merged
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
6 changes: 6 additions & 0 deletions network/p2p/dnsaddr/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ func Iterate(initial multiaddr.Multiaddr, controller ResolveController, f func(d
if resolver == nil {
return errors.New("passed controller has no resolvers Iterate")
}
const maxHops = 100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 100 - i.e. this feels more like 10 is sufficient..?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know what is actual max limit

hops := 0
var toResolve = []multiaddr.Multiaddr{initial}
for resolver != nil && len(toResolve) > 0 {
hops++
if hops > maxHops {
return errors.New("max hops reached while resolving dnsaddr " + initial.String())
}
curr := toResolve[0]
maddrs, resolveErr := resolver.Resolve(context.Background(), curr)
if resolveErr != nil {
Expand Down
40 changes: 40 additions & 0 deletions network/p2p/dnsaddr/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"net"
"testing"
"time"

"github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
Expand Down Expand Up @@ -109,3 +110,42 @@ func TestMultiaddrsFromResolverDnsFailure(t *testing.T) {
assert.Empty(t, maddrs)
assert.ErrorContains(t, err, "always errors")
}

type mockController struct {
}

func (c mockController) Resolver() Resolver {
return selfResolver{}
}

func (c mockController) NextResolver() Resolver {
return nil
}

type selfResolver struct {
}

func (r selfResolver) Resolve(ctx context.Context, maddr multiaddr.Multiaddr) ([]multiaddr.Multiaddr, error) {
return []multiaddr.Multiaddr{maddr}, nil
}

// TestIterate ensures the Iterate() does not hang in infinite loop
// when resolver returns the same dnsaddr
func TestIterate(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

dnsAddr := "/dnsaddr/foobar.com"
require.True(t, isDnsaddr(multiaddr.StringCast(dnsAddr)))
ma, err := multiaddr.NewMultiaddr(dnsAddr)
require.NoError(t, err)

require.Eventually(t, func() bool {
Iterate(
ma,
mockController{},
func(dnsaddr multiaddr.Multiaddr, entries []multiaddr.Multiaddr) error { return nil },
)
return true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would check the error returned by Iterate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? I want to check no infinite loops and not resolution errors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You made the decision to return an error when max hops are reached. In my view, it only makes sense to test that return value here, rather than ignoring it

}, 100*time.Millisecond, 50*time.Millisecond)
}
Loading