提交 b563ea5a 作者: Kevin Atkinson

Make ValidatePB stricter.

License: MIT
Signed-off-by: 's avatarKevin Atkinson <k@kevina.org>
上级 cb0d1f44
......@@ -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
}
......
......@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
ipld "gx/ipfs/QmWi2BYBL5gJ3CiAiQchg6rn1A8iBsrWy51EYxvHVjFvLb/go-ipld-format"
proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
dag "github.com/ipfs/go-ipfs/merkledag"
......@@ -261,18 +262,21 @@ 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()))
for _, blocksize := range pb.Blocksizes {
total += blocksize
}
if total != pb.GetFilesize() {
if total != *pb.Filesize {
return fmt.Errorf("unixfs ill-formed, actual size of %d does not match size in filesize field with value %d",
total, pb.GetFilesize())
}
......
......@@ -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")
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论