From e3951368fb6c6c1b9aeba079278003387af84d56 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Mon, 20 May 2019 12:23:28 -0700 Subject: [PATCH 1/2] forbid empty multiaddrs fixes #104 --- codec.go | 10 ++++++++++ multiaddr.go | 4 ++++ multiaddr_test.go | 16 ++++++++-------- util_test.go | 1 - 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/codec.go b/codec.go index baa9918..35b50c1 100644 --- a/codec.go +++ b/codec.go @@ -20,6 +20,10 @@ func stringToBytes(s string) ([]byte, error) { // consume first empty elem sp = sp[1:] + if len(sp) == 0 { + return nil, fmt.Errorf("failed to parse multiaddr %q: empty multiaddr", s) + } + for len(sp) > 0 { name := sp[0] p := ProtocolWithName(name) @@ -58,6 +62,9 @@ func stringToBytes(s string) ([]byte, error) { } func validateBytes(b []byte) (err error) { + if len(b) == 0 { + return fmt.Errorf("empty multiaddr") + } for len(b) > 0 { code, n, err := ReadVarintCode(b) if err != nil { @@ -136,6 +143,9 @@ func readComponent(b []byte) (int, Component, error) { } func bytesToString(b []byte) (ret string, err error) { + if len(b) == 0 { + return "", fmt.Errorf("empty multiaddr") + } var buf strings.Builder for len(b) > 0 { diff --git a/multiaddr.go b/multiaddr.go index 6794b92..58fe8ce 100644 --- a/multiaddr.go +++ b/multiaddr.go @@ -159,6 +159,10 @@ func (m *multiaddr) Decapsulate(o Multiaddr) Multiaddr { return &multiaddr{bytes: cpy} } + if i == 0 { + return nil + } + ma, err := NewMultiaddr(s1[:i]) if err != nil { panic("Multiaddr.Decapsulate incorrect byte boundaries.") diff --git a/multiaddr_test.go b/multiaddr_test.go index aef676a..fe6fd82 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -377,8 +377,8 @@ func TestEncapsulate(t *testing.T) { m4, _ := NewMultiaddr("/ip4/127.0.0.1") d := c.Decapsulate(m4) - if s := d.String(); s != "" { - t.Error("decapsulate /ip4 failed.", "/", s) + if d != nil { + t.Error("decapsulate /ip4 failed: ", d) } } @@ -582,11 +582,11 @@ func TestBinaryMarshaler(t *testing.T) { t.Fatal(err) } - addr2 := newMultiaddr(t, "") + var addr2 multiaddr if err = addr2.UnmarshalBinary(b); err != nil { t.Fatal(err) } - if !addr.Equal(addr2) { + if !addr.Equal(&addr2) { t.Error("expected equal addresses in circular marshaling test") } } @@ -598,11 +598,11 @@ func TestTextMarshaler(t *testing.T) { t.Fatal(err) } - addr2 := newMultiaddr(t, "") + var addr2 multiaddr if err = addr2.UnmarshalText(b); err != nil { t.Fatal(err) } - if !addr.Equal(addr2) { + if !addr.Equal(&addr2) { t.Error("expected equal addresses in circular marshaling test") } } @@ -614,11 +614,11 @@ func TestJSONMarshaler(t *testing.T) { t.Fatal(err) } - addr2 := newMultiaddr(t, "") + var addr2 multiaddr if err = addr2.UnmarshalJSON(b); err != nil { t.Fatal(err) } - if !addr.Equal(addr2) { + if !addr.Equal(&addr2) { t.Error("expected equal addresses in circular marshaling test") } } diff --git a/util_test.go b/util_test.go index a0ee55c..3793a31 100644 --- a/util_test.go +++ b/util_test.go @@ -16,7 +16,6 @@ func TestSplitFirstLast(t *testing.T) { []string{ipStr, tcpStr, ipfsStr}, []string{ipStr, tcpStr}, []string{ipStr}, - []string{}, } { addr := StringCast(strings.Join(x, "")) head, tail := SplitFirst(addr) From 020c4d17415d5d4f4d4ffe8343a51326ed17ecb6 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Mon, 20 May 2019 12:25:15 -0700 Subject: [PATCH 2/2] test: empty multiaddr --- multiaddr_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/multiaddr_test.go b/multiaddr_test.go index fe6fd82..ffac3bb 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -74,6 +74,8 @@ func TestConstructFails(t *testing.T) { "/unix", "/ip4/1.2.3.4/tcp/80/unix", "/ip4/127.0.0.1/tcp/9090/http/p2p-webcrt-direct", + "/", + "", } for _, a := range cases { @@ -83,6 +85,13 @@ func TestConstructFails(t *testing.T) { } } +func TestEmptyMultiaddr(t *testing.T) { + _, err := NewMultiaddrBytes([]byte{}) + if err == nil { + t.Fatal("should have failed to parse empty multiaddr") + } +} + func TestConstructSucceeds(t *testing.T) { cases := []string{ "/ip4/1.2.3.4",