From f6627a9b7776fc8c0e0a1a3a6fb837a163d64dfb Mon Sep 17 00:00:00 2001 From: snail8501 <0402liujun@163.com> Date: Mon, 16 Nov 2026 22:46:07 +0800 Subject: [PATCH] fix: reject undefined DealDataRoot in deal proposals DealDataRoot is part of the unsigned DealParams, so it is not covered by the client signature, and CBOR decoding leaves it at cid.Undef when the key is omitted. validateDealProposal only checked PieceCID, so such a proposal was accepted and persisted: CidFieldDef serialized cid.Undef as the one-character string "b", which cid.Parse rejects, so every subsequent Provider.Start() failed in ListActive/ListCompleted until the row was deleted by hand. - validateDealProposal: reject an undefined DealDataRoot next to the existing PieceCID guard, before signature verification. - fielddef: make CidFieldDef and CidPtrFieldDef refuse to marshal an undefined CID, so a value that cannot be read back is never written. Tests cover the acceptance rejection and the DB insert failure. --- db/deals_test.go | 27 +++++++++++++++++ db/fielddef/fielddef.go | 9 ++++++ storagemarket/deal_acceptance.go | 9 ++++++ storagemarket/provider_test.go | 50 ++++++++++++++++++++++---------- 4 files changed, 80 insertions(+), 15 deletions(-) diff --git a/db/deals_test.go b/db/deals_test.go index ec54db216..dc58660f6 100644 --- a/db/deals_test.go +++ b/db/deals_test.go @@ -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" @@ -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) +} diff --git a/db/fielddef/fielddef.go b/db/fielddef/fielddef.go index f8c7aec49..3f63f6675 100644 --- a/db/fielddef/fielddef.go +++ b/db/fielddef/fielddef.go @@ -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 } @@ -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 } diff --git a/storagemarket/deal_acceptance.go b/storagemarket/deal_acceptance.go index 6294c8d7d..82f9c01f8 100644 --- a/storagemarket/deal_acceptance.go +++ b/storagemarket/deal_acceptance.go @@ -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{ diff --git a/storagemarket/provider_test.go b/storagemarket/provider_test.go index 49b847c85..24ef0e753 100644 --- a/storagemarket/provider_test.go +++ b/storagemarket/provider_test.go @@ -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), @@ -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 @@ -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 @@ -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 { @@ -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,