Skip to content
Open
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
27 changes: 27 additions & 0 deletions db/deals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/ipfs/go-cid"
"github.com/stretchr/testify/require"

cborutil "github.com/filecoin-project/go-cbor-util"
Expand Down Expand Up @@ -309,3 +310,29 @@ func TestSqlDbBkp(t *testing.T) {
req.Len(dealList, len(deals))

}

func TestDealsDBRejectsUndefinedCID(t *testing.T) {
req := require.New(t)
ctx := context.Background()

sqldb := CreateTestTmpDB(t)
require.NoError(t, CreateAllBoostTables(ctx, sqldb, sqldb))
require.NoError(t, migrations.Migrate(sqldb))

db := NewDealsDB(sqldb)
deals, err := GenerateDeals()
req.NoError(err)

// A CID field that is undefined cannot be represented as a string that
// round-trips through cid.Parse, so the insert must fail rather than write
// a row that breaks every subsequent read of the deals table.
deal := deals[0]
deal.DealDataRoot = cid.Undef
err = db.Insert(ctx, &deal)
req.ErrorContains(err, "cannot marshal undefined cid")

// The table is left untouched.
allDeals, err := db.List(ctx, "", nil, nil, 0, 0)
req.NoError(err)
req.Empty(allDeals)
}
9 changes: 9 additions & 0 deletions db/fielddef/fielddef.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ func (fd *CidFieldDef) Marshall() (interface{}, error) {
if fd.F == nil {
return nil, nil
}
// An undefined CID encodes as the one-character string "b", which cid.Parse
// rejects with "cid too short". Refuse to write a value that cannot be read
// back, so a malformed value fails the insert instead of poisoning the row.
if !fd.F.Defined() {
return nil, fmt.Errorf("cannot marshal undefined cid")
}
return fd.F.String(), nil
}

Expand Down Expand Up @@ -84,6 +90,9 @@ func (fd *CidPtrFieldDef) Marshall() (interface{}, error) {
if (*fd.F) == nil {
return nil, nil
}
if !(*fd.F).Defined() {
return nil, fmt.Errorf("cannot marshal undefined cid")
}
return (*fd.F).String(), nil
}

Expand Down
9 changes: 9 additions & 0 deletions storagemarket/deal_acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ func (p *Provider) validateDealProposal(deal types.ProviderDealState) *validatio
return &validationError{error: fmt.Errorf("proposal PieceCID undefined")}
}

// DealDataRoot is part of the unsigned DealParams, so it is not covered by
// the client signature and CBOR decoding tolerates its absence. Reject an
// undefined value here: it cannot be round-tripped through the deals DB
// (it would be stored as the unparseable string "b"), and a single such
// row makes every subsequent Provider.Start() fail in ListActive.
if !deal.DealDataRoot.Defined() {
return &validationError{error: fmt.Errorf("deal data root undefined")}
}

if ok, err := p.validateSignature(deal); err != nil || !ok {
if err != nil {
return &validationError{
Expand Down
50 changes: 35 additions & 15 deletions storagemarket/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,15 @@ func TestDealVerification(t *testing.T) {
},
expectedErr: "proposal PieceCID undefined",
},
"deal data root undefined": {
ask: &legacytypes.StorageAsk{
Price: abi.NewTokenAmount(0),
},
dbuilder: func(t *testing.T, h *ProviderHarness) *testDeal {
return h.newDealBuilder(t, 1, withUndefinedDealDataRoot()).withNoOpMinerStub().build()
},
expectedErr: "deal data root undefined",
},
"proposal end 9 before proposal start 10": {
ask: &legacytypes.StorageAsk{
Price: abi.NewTokenAmount(0),
Expand Down Expand Up @@ -1763,20 +1772,21 @@ func (h *ProviderHarness) Stop() {
}

type dealProposalConfig struct {
normalFileSize int
offlineDeal bool
verifiedDeal bool
providerCollateral abi.TokenAmount
clientAddr address.Address
minerAddr address.Address
pieceCid cid.Cid
pieceSize abi.PaddedPieceSize
undefinedPieceCid bool
startEpoch abi.ChainEpoch
endEpoch abi.ChainEpoch
label market.DealLabel
carVersion CarVersion
sectorsStatusDealId abi.DealID
normalFileSize int
offlineDeal bool
verifiedDeal bool
providerCollateral abi.TokenAmount
clientAddr address.Address
minerAddr address.Address
pieceCid cid.Cid
pieceSize abi.PaddedPieceSize
undefinedPieceCid bool
undefinedDealDataRoot bool
startEpoch abi.ChainEpoch
endEpoch abi.ChainEpoch
label market.DealLabel
carVersion CarVersion
sectorsStatusDealId abi.DealID
}

// dealProposalOpt allows configuration of the deal proposal
Expand Down Expand Up @@ -1850,6 +1860,12 @@ func withUndefinedPieceCid() dealProposalOpt {
}
}

func withUndefinedDealDataRoot() dealProposalOpt {
return func(dc *dealProposalConfig) {
dc.undefinedDealDataRoot = true
}
}

func withEpochs(start, end abi.ChainEpoch) dealProposalOpt {
return func(dc *dealProposalConfig) {
dc.startEpoch = start
Expand Down Expand Up @@ -1888,6 +1904,10 @@ func (ph *ProviderHarness) newDealBuilder(t *testing.T, seed int, opts ...dealPr
require.NoError(tbuilder.t, err)
rootCid, carFilePath, err := testutil.CreateDenseCARv2(tbuilder.ph.TempDir, randomFilepath)
require.NoError(tbuilder.t, err)
dealDataRoot := rootCid
if dc.undefinedDealDataRoot {
dealDataRoot = cid.Undef
}

// if the file should be a version 1 car file
if dc.carVersion == CarVersion1 {
Expand Down Expand Up @@ -1958,7 +1978,7 @@ func (ph *ProviderHarness) newDealBuilder(t *testing.T, seed int, opts ...dealPr
Data: []byte("sig"),
}, // We don't do signature verification in Boost SM testing.
},
DealDataRoot: rootCid,
DealDataRoot: dealDataRoot,
Transfer: types.Transfer{
Type: "http",
Params: xferParams,
Expand Down
Loading