Skip to content
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

Ignore multiaddrs with unknown protocols #30

Merged
merged 1 commit into from
May 9, 2023
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
13 changes: 9 additions & 4 deletions announce/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package message

import (
"errors"
"strings"

"github.com/ipfs/go-cid"
"github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -38,13 +39,17 @@ func (m *Message) SetAddrs(addrs []multiaddr.Multiaddr) {
// GetAddrs reads the slice of Multiaddr that is stored in the Message as a
// slice of []byte.
func (m *Message) GetAddrs() ([]multiaddr.Multiaddr, error) {
addrs := make([]multiaddr.Multiaddr, len(m.Addrs))
for i := range m.Addrs {
var err error
addrs[i], err = multiaddr.NewMultiaddrBytes(m.Addrs[i])
addrs := make([]multiaddr.Multiaddr, 0, len(m.Addrs))
for _, addrBytes := range m.Addrs {
addr, err := multiaddr.NewMultiaddrBytes(addrBytes)
if err != nil {
if strings.Contains(err.Error(), "no protocol with code") {
Copy link
Member

Choose a reason for hiding this comment

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

is this really our best way of identifying this error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wish there was a type, but that will require a change here: https://github.com/multiformats/go-multiaddr/blob/master/protocol.go#L97

Copy link
Member

Choose a reason for hiding this comment

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

that's "not protocol with name" rather than "no protocol with code"

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

// Ignore unknown protocols.
continue
}
return nil, err
}
addrs = append(addrs, addr)
}
return addrs, nil
}
15 changes: 15 additions & 0 deletions announce/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,18 @@ func TestJSON(t *testing.T) {

require.Equal(t, msg, newMsg)
}

func TestUnknownProtocol(t *testing.T) {
// Encoded unknown protocol code 9999: /ip4/127.0.0.1/udp/1234/<proto-9999>
addrData := []byte{54, 9, 108, 111, 99, 97, 108, 104, 111, 115, 116, 145, 2, 4, 210, 143, 78}
_, err := multiaddr.NewMultiaddrBytes(addrData)
require.ErrorContains(t, err, "no protocol with code")

var msg message.Message
msg.SetAddrs([]multiaddr.Multiaddr{maddr1})
msg.Addrs = append(msg.Addrs, addrData)

addrs, err := msg.GetAddrs()
require.NoError(t, err)
require.Equal(t, 1, len(addrs))
}