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

Problem: missing validation on nft-transfer message fields #1019

Merged
merged 10 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [#1004](https://github.com/crypto-org-chain/chain-main/pull/1004) Update rocksdb dependency to 8.1.1.
- [#1009](https://github.com/crypto-org-chain/chain-main/pull/1009) Update memiavl to c575f4797ca4, update cosmos-sdk to v0.46.15.
- [#1010](https://github.com/crypto-org-chain/chain-main/pull/1010) Bump librocksdb to 8.5.3
- [#1019](https://github.com/crypto-org-chain/chain-main/pull/1019) Limit the length of NFTTransfer fields.

### Bug Fixes

Expand Down
52 changes: 52 additions & 0 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"github.com/cosmos/cosmos-sdk/x/auth/ante"
ibcante "github.com/cosmos/ibc-go/v5/modules/core/ante"
"github.com/cosmos/ibc-go/v5/modules/core/keeper"
nfttypes "github.com/crypto-org-chain/chain-main/v4/x/nft-transfer/types"
)

// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
Expand Down Expand Up @@ -39,6 +40,7 @@
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
NewValidateMsgTransferDecorator(),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
// SetPubKeyDecorator must be called before all signature verification decorators
Expand All @@ -52,3 +54,53 @@

return sdk.ChainAnteDecorators(anteDecorators...), nil
}

const (
// values chosen arbitrarily
MaxClassIDLength = 2048
MaxTokenIds = 2048
yihuang marked this conversation as resolved.
Show resolved Hide resolved
MaxTokenIDLength = 2048
MaximumReceiverLength = 2048
)

// ValidateMsgTransferDecorator is a temporary decorator that limit the field length of MsgTransfer message.
type ValidateMsgTransferDecorator struct{}

func NewValidateMsgTransferDecorator() ValidateMsgTransferDecorator {
return ValidateMsgTransferDecorator{}
}

func (vtd ValidateMsgTransferDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
// avoid breaking consensus

Check failure on line 74 in app/ante.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofmt`-ed with `-s` (gofmt)
if !ctx.IsCheckTx() {
yihuang marked this conversation as resolved.
Show resolved Hide resolved
return next(ctx, tx, simulate)
}

msgs := tx.GetMsgs()
for _, msg := range msgs {
transfer, ok := msg.(*nfttypes.MsgTransfer)
if !ok {
continue
}

if len(transfer.ClassId) > MaxClassIDLength {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "class id length must be less than %d", MaxClassIDLength)

Check failure on line 87 in app/ante.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA1019: sdkerrors.Wrapf is deprecated: functionality of this package has been moved to it's own module: (staticcheck)
}

Check warning on line 88 in app/ante.go

View check run for this annotation

Codecov / codecov/patch

app/ante.go#L87-L88

Added lines #L87 - L88 were not covered by tests

if len(transfer.TokenIds) > MaxTokenIds {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "token id length must be less than %d", MaxTokenIds)

Check failure on line 91 in app/ante.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA1019: sdkerrors.Wrapf is deprecated: functionality of this package has been moved to it's own module: (staticcheck)
}

Check warning on line 92 in app/ante.go

View check run for this annotation

Codecov / codecov/patch

app/ante.go#L91-L92

Added lines #L91 - L92 were not covered by tests

for _, tokenID := range transfer.TokenIds {
if len(tokenID) > MaxTokenIDLength {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "token id length must be less than %d", MaxTokenIDLength)

Check failure on line 96 in app/ante.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA1019: sdkerrors.Wrapf is deprecated: functionality of this package has been moved to it's own module: (staticcheck)
}

Check warning on line 97 in app/ante.go

View check run for this annotation

Codecov / codecov/patch

app/ante.go#L96-L97

Added lines #L96 - L97 were not covered by tests
}

if len(transfer.Receiver) > MaximumReceiverLength {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "receiver length must be less than %d", MaximumReceiverLength)
}

Check warning on line 102 in app/ante.go

View check run for this annotation

Codecov / codecov/patch

app/ante.go#L101-L102

Added lines #L101 - L102 were not covered by tests
}

return next(ctx, tx, simulate)
}
Loading