@@ -14,6 +14,7 @@ import (
14
14
blockstore "github.com/ipfs/go-ipfs-blockstore"
15
15
exchange "github.com/ipfs/go-ipfs-exchange-interface"
16
16
logging "github.com/ipfs/go-log"
17
+ "github.com/ipfs/go-verifcid"
17
18
)
18
19
19
20
var log = logging .Logger ("blockservice" )
@@ -130,6 +131,11 @@ func NewSession(ctx context.Context, bs BlockService) *Session {
130
131
// TODO pass a context into this if the remote.HasBlock is going to remain here.
131
132
func (s * blockService ) AddBlock (o blocks.Block ) error {
132
133
c := o .Cid ()
134
+ // hash security
135
+ err := verifcid .ValidateCid (c )
136
+ if err != nil {
137
+ return err
138
+ }
133
139
if s .checkFirst {
134
140
if has , err := s .blockstore .Has (c ); has || err != nil {
135
141
return err
@@ -150,6 +156,13 @@ func (s *blockService) AddBlock(o blocks.Block) error {
150
156
}
151
157
152
158
func (s * blockService ) AddBlocks (bs []blocks.Block ) error {
159
+ // hash security
160
+ for _ , b := range bs {
161
+ err := verifcid .ValidateCid (b .Cid ())
162
+ if err != nil {
163
+ return err
164
+ }
165
+ }
153
166
var toput []blocks.Block
154
167
if s .checkFirst {
155
168
toput = make ([]blocks.Block , 0 , len (bs ))
@@ -198,6 +211,11 @@ func (s *blockService) getExchange() exchange.Fetcher {
198
211
}
199
212
200
213
func getBlock (ctx context.Context , c cid.Cid , bs blockstore.Blockstore , fget func () exchange.Fetcher ) (blocks.Block , error ) {
214
+ err := verifcid .ValidateCid (c ) // hash security
215
+ if err != nil {
216
+ return nil , err
217
+ }
218
+
201
219
block , err := bs .Get (c )
202
220
if err == nil {
203
221
return block , nil
@@ -241,6 +259,18 @@ func getBlocks(ctx context.Context, ks []cid.Cid, bs blockstore.Blockstore, fget
241
259
go func () {
242
260
defer close (out )
243
261
262
+ k := 0
263
+ for _ , c := range ks {
264
+ // hash security
265
+ if err := verifcid .ValidateCid (c ); err == nil {
266
+ ks [k ] = c
267
+ k ++
268
+ } else {
269
+ log .Errorf ("unsafe CID (%s) passed to blockService.GetBlocks: %s" , c , err )
270
+ }
271
+ }
272
+ ks = ks [:k ]
273
+
244
274
var misses []cid.Cid
245
275
for _ , c := range ks {
246
276
hit , err := bs .Get (c )
0 commit comments