Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WithTrustedCar() reader option #381

Merged
merged 4 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions v2/block_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ func (br *BlockReader) Next() (blocks.Block, error) {
return nil, err
}

hashed, err := c.Prefix().Sum(data)
if err != nil {
return nil, err
}
if !br.opts.TrustedCAR {
hashed, err := c.Prefix().Sum(data)
if err != nil {
return nil, err
}

if !hashed.Equals(c) {
return nil, fmt.Errorf("mismatch in content integrity, expected: %s, got: %s", c, hashed)
if !hashed.Equals(c) {
return nil, fmt.Errorf("mismatch in content integrity, expected: %s, got: %s", c, hashed)
}
}

ss := uint64(c.ByteLen()) + uint64(len(data))
Expand Down
34 changes: 34 additions & 0 deletions v2/block_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,40 @@ func TestMaxSectionLength(t *testing.T) {
require.True(t, bytes.Equal(block, readBlock.RawData()))
}

func TestTrustedCAR(t *testing.T) {
// headerHex is the zero-roots CARv1 header
const headerHex = "11a265726f6f7473806776657273696f6e01"
headerBytes, _ := hex.DecodeString(headerHex)
// block of zeros
block := make([]byte, 5)
// CID for that block
pfx := cid.NewPrefixV1(cid.Raw, mh.SHA2_256)
cid, err := pfx.Sum(block)
require.NoError(t, err)

// Modify the block so it won't match CID anymore
block[2] = 0xFF
// construct CAR
var buf bytes.Buffer
buf.Write(headerBytes)
buf.Write(varint.ToUvarint(uint64(len(cid.Bytes()) + len(block))))
buf.Write(cid.Bytes())
buf.Write(block)

// try to read it as trusted
car, err := carv2.NewBlockReader(bytes.NewReader(buf.Bytes()), carv2.WithTrustedCAR(true))
require.NoError(t, err)
_, err = car.Next()
require.NoError(t, err)

// Try to read it as untrusted - should fail
car, err = carv2.NewBlockReader(bytes.NewReader(buf.Bytes()), carv2.WithTrustedCAR(false))
require.NoError(t, err)
// error should occur on first section read
_, err = car.Next()
require.EqualError(t, err, "mismatch in content integrity, expected: bafkreieikviivlpbn3cxhuq6njef37ikoysaqxa2cs26zxleqxpay2bzuq, got: bafkreidgklrppelx4fxcsna7cxvo3g7ayedfojkqeuus6kz6e4hy7gukmy")
}

func TestMaxHeaderLength(t *testing.T) {
// headerHex is the is a 5 root CARv1 header
const headerHex = "de01a265726f6f747385d82a58250001711220785197229dc8bb1152945da58e2348f7e279eeded06cc2ca736d0e879858b501d82a58250001711220785197229dc8bb1152945da58e2348f7e279eeded06cc2ca736d0e879858b501d82a58250001711220785197229dc8bb1152945da58e2348f7e279eeded06cc2ca736d0e879858b501d82a58250001711220785197229dc8bb1152945da58e2348f7e279eeded06cc2ca736d0e879858b501d82a58250001711220785197229dc8bb1152945da58e2348f7e279eeded06cc2ca736d0e879858b5016776657273696f6e01"
Expand Down
9 changes: 9 additions & 0 deletions v2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Options struct {
MaxTraversalLinks uint64
WriteAsCarV1 bool
TraversalPrototypeChooser traversal.LinkTargetNodePrototypeChooser
TrustedCAR bool

MaxAllowedHeaderSize uint64
MaxAllowedSectionSize uint64
Expand Down Expand Up @@ -160,6 +161,14 @@ func WithTraversalPrototypeChooser(t traversal.LinkTargetNodePrototypeChooser) O
}
}

// WithTrustedCAR specifies whether CIDs match the block data as they are read
// from the CAR files.
func WithTrustedCAR(t bool) Option {
return func(o *Options) {
o.TrustedCAR = t
}
}

// MaxAllowedHeaderSize overrides the default maximum size (of 32 MiB) that a
// CARv1 decode (including within a CARv2 container) will allow a header to be
// without erroring.
Expand Down