Skip to content

Commit

Permalink
Make ValidatePB stricter.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Jun 12, 2018
1 parent 77149fd commit 2c59305
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion unixfs/io/pbdagreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var _ DagReader = (*PBDagReader)(nil)

// NewPBFileReader constructs a new PBFileReader.
func NewPBFileReader(ctx context.Context, n *mdag.ProtoNode, pb *ftpb.Data, serv ipld.NodeGetter) (*PBDagReader, error) {
err := ft.ValidatePB(n, pb)
err := ft.ValidatePB(n.Links(), pb)
if err != nil {
return nil, err
}
Expand Down
10 changes: 7 additions & 3 deletions unixfs/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"

dag "github.com/ipfs/go-ipfs/merkledag"
pb "github.com/ipfs/go-ipfs/unixfs/pb"
Expand Down Expand Up @@ -261,11 +262,14 @@ func EmptyDirNode() *dag.ProtoNode {
}

// ValidatePB validates a unixfs protonode.
func ValidatePB(n *dag.ProtoNode, pb *pb.Data) error {
if len(pb.Blocksizes) == 0 { // special case
func ValidatePB(links []*ipld.Link, pb *pb.Data) error {
if (pb.Filesize == nil) {
return errors.New("unixfs ill-formed, filesize is not defined")
}
if len(pb.Blocksizes) == 0 && len(links) > 0 { // special case links but no blocksize
return nil
}
if len(n.Links()) != len(pb.Blocksizes) {
if len(links) != len(pb.Blocksizes) {
return errors.New("unixfs ill-formed, number of links does not match blocksize count")
}
total := uint64(len(pb.GetData()))
Expand Down
38 changes: 33 additions & 5 deletions unixfs/unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,26 +175,54 @@ func TestValidatePB(t *testing.T) {
if err != nil {
t.Fatal(err)
}
links := nd.Links()
fd, err := FromBytes(nd.Data())
if err != nil {
t.Fatal(err)
}
err = ValidatePB(nd, fd)
err = ValidatePB(links, fd)
if err != nil {
t.Fatalf("valid node (with no blocksizes) failed to validate: %v", err)
}
// create no with no blocksize of filesize
invalid := *fd
invalid.Filesize = nil
err = ValidatePB(links, &invalid)
if err == nil {
t.Fatalf("invalid node with no blocksize or filesize validated")
}
// give node blocksizes
fd.Blocksizes = []uint64{3, 3}
// should be ok
err = ValidatePB(nd, fd)
err = ValidatePB(links, fd)
if err != nil {
t.Fatalf("valid node failed to validate: %v", err)
}
// give node incorrect filesize
var filesize uint64 = 8
fd.Filesize = &filesize
err = ValidatePB(nd, fd)
invalid = *fd
invalid.Filesize = proto.Uint64(8)
err = ValidatePB(links, &invalid)
if err == nil {
t.Fatal("invalid unixfs node (with incorrect filesize) validated")
}

// construct a leaf node, copied from WrapData
leaf := new(pb.Data)
typ := pb.Data_Raw
leaf.Data = []byte("abc")
leaf.Type = &typ
leaf.Filesize = proto.Uint64(3)

err = ValidatePB(nil, leaf)
if err != nil {
t.Fatalf("valid leaf node failed to validate: %v", err)
}

// make filesize incorrect
invalid = *leaf
invalid.Filesize = proto.Uint64(8)
err = ValidatePB(nil, &invalid)
if err == nil {
t.Fatal("invalid leaf node (with incorrect filesize) validated")
}
}

0 comments on commit 2c59305

Please sign in to comment.