-
Notifications
You must be signed in to change notification settings - Fork 527
p2p: fix infinite loop in dnsaddr resolution #5926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why 100 - i.e. this feels more like 10 is sufficient..?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ import ( | |
| "fmt" | ||
| "net" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/multiformats/go-multiaddr" | ||
| madns "github.com/multiformats/go-multiaddr-dns" | ||
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally this would check the error returned by
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? I want to check no infinite loops and not resolution errors
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.