Skip to content

Commit

Permalink
When reconstructing block always verify the result.
Browse files Browse the repository at this point in the history
Towards #875.

License: MIT
Signed-off-by: Kevin Atkinson <[email protected]>
  • Loading branch information
kevina committed Apr 27, 2016
1 parent 2b8704e commit f26c2df
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
20 changes: 17 additions & 3 deletions filestore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import (

ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore/query"
k "github.com/ipfs/go-ipfs/blocks/key"
//mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
)

type datastore struct {
ds ds.Datastore
ds ds.Datastore
alwaysVerify bool
}

func New(d ds.Datastore, fileStorePath string) (ds.Datastore, error) {
return &datastore{d}, nil
return &datastore{d, true}, nil
}

func (d *datastore) Put(key ds.Key, value interface{}) (err error) {
Expand Down Expand Up @@ -81,7 +85,17 @@ func (d *datastore) Get(key ds.Key) (value interface{}, err error) {
if err != nil {
return nil, err
}
return reconstruct(val.Data, buf)
data, err := reconstruct(val.Data, buf)
if err != nil {
return nil, err
}
if d.alwaysVerify {
newKey := k.Key(u.Hash(data)).DsKey()
if newKey != key {
return nil, errors.New("Filestore: Block Verification Failed")
}
}
return data, nil
} else {
return val.Data, nil
}
Expand Down
2 changes: 1 addition & 1 deletion filestore/reconstruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
)

func reconstruct(data []byte, blockData []byte) (interface{}, error) {
func reconstruct(data []byte, blockData []byte) ([]byte, error) {
// Decode data to merkledag protobuffer
var pbn dag.PBNode
err := pbn.Unmarshal(data)
Expand Down
2 changes: 1 addition & 1 deletion test/sharness/t0046-add-no-copy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test_add_cat_file() {
test_must_fail ipfs cat "$HASH" >cat.output
'

test_expect_failure "fail after file change, same size" '
test_expect_success "fail after file change, same size" '
# note: filesize does not change
echo "HELLO WORLDS!" >mountdir/hello.txt &&
test_must_fail ipfs cat "$HASH" >cat.output
Expand Down

0 comments on commit f26c2df

Please sign in to comment.