Skip to content

Commit eafcbd6

Browse files
fix(addressbook): validate address entrie to prevent nil dereference (#5534)
1 parent 743ce16 commit eafcbd6

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

pkg/addressbook/addressbook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func (s *store) Get(overlay swarm.Address) (*bzz.Address, bool, error) {
8282
}
8383
return nil, false, err
8484
}
85+
if v.Address == nil {
86+
_ = s.store.Delete(key)
87+
return nil, false, ErrNotFound
88+
}
8589
return v.Address, v.Verified, nil
8690
}
8791

pkg/addressbook/addressbook_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package addressbook_test
66

77
import (
8+
"encoding/json"
89
"errors"
910
"testing"
1011

@@ -13,6 +14,7 @@ import (
1314
"github.com/ethersphere/bee/v2/pkg/bzz"
1415
"github.com/ethersphere/bee/v2/pkg/crypto"
1516
"github.com/ethersphere/bee/v2/pkg/statestore/mock"
17+
"github.com/ethersphere/bee/v2/pkg/storage"
1618
"github.com/ethersphere/bee/v2/pkg/swarm"
1719
ma "github.com/multiformats/go-multiaddr"
1820
)
@@ -96,3 +98,44 @@ func run(t *testing.T, f bookFunc) {
9698
t.Fatalf("expected addresses len %v, got %v", 1, len(addresses))
9799
}
98100
}
101+
102+
type mockCorruptedStore struct{}
103+
104+
func (m *mockCorruptedStore) Get(key string, i any) error {
105+
corruptedJSON := []byte(`{"address": null}`)
106+
return json.Unmarshal(corruptedJSON, i)
107+
}
108+
109+
func (m *mockCorruptedStore) Put(key string, i any) error {
110+
return nil
111+
}
112+
113+
func (m *mockCorruptedStore) Delete(key string) error {
114+
return nil
115+
}
116+
117+
func (m *mockCorruptedStore) Iterate(prefix string, fn storage.StateIterFunc) error {
118+
return nil
119+
}
120+
121+
func (m *mockCorruptedStore) Close() error {
122+
return nil
123+
}
124+
125+
func TestGetCorruptedNilAddress(t *testing.T) {
126+
t.Parallel()
127+
128+
corruptedStore := &mockCorruptedStore{}
129+
book := addressbook.New(corruptedStore)
130+
131+
addr := swarm.NewAddress([]byte{0, 1, 2, 3})
132+
133+
v, _, err := book.Get(addr)
134+
if !errors.Is(err, addressbook.ErrNotFound) {
135+
t.Fatalf("expected ErrNotFound for corrupted entry, got %v", err)
136+
}
137+
138+
if v != nil {
139+
t.Fatalf("expected nil address, got %s", v)
140+
}
141+
}

0 commit comments

Comments
 (0)