swarm/storage: add size validator

This commit is contained in:
Anton Evangelatov 2018-08-14 11:18:08 +02:00
parent 1af15cf52f
commit c56a21b67b
3 changed files with 18 additions and 6 deletions

View file

@ -230,6 +230,11 @@ R:
for req := range d.receiveC {
processReceivedChunksCount.Inc(1)
if len(req.SData) > cp.DefaultSize+8 {
log.Warn("received chunk is bigger than expected", "len", len(req.SData))
continue R
}
// this should be has locally
chunk, err := d.db.Get(context.TODO(), req.Addr)
if err == nil {
@ -245,10 +250,6 @@ R:
continue R
default:
}
if len(req.SData) > cp.DefaultSize {
log.Warn("received chunk is bigger than expected", "len", len(req.SData))
continue R
}
chunk.SData = req.SData
d.db.Put(context.TODO(), chunk)

View file

@ -355,3 +355,12 @@ func (v *ContentAddressValidator) Validate(addr Address, data []byte) bool {
return bytes.Equal(hash, addr[:])
}
// SizeValidator provides method for validation of max chunk data size
type SizeValidator struct {
}
// Validate that the chunk has valid size
func (v *SizeValidator) Validate(_ Address, data []byte) bool {
return len(data) <= 8+chunk.DefaultSize
}

View file

@ -201,8 +201,10 @@ func NewSwarm(config *api.Config, mockStore *mock.NodeStore) (self *Swarm, err e
}
resourceHandler.SetStore(netStore)
var validators []storage.ChunkValidator
validators = append(validators, storage.NewContentAddressValidator(storage.MakeHashFunc(storage.DefaultHash)))
validators := []storage.ChunkValidator{
storage.NewContentAddressValidator(storage.MakeHashFunc(storage.DefaultHash)),
&storage.SizeValidator{},
}
if resourceHandler != nil {
validators = append(validators, resourceHandler)
}