diff --git a/CHANGELOG.md b/CHANGELOG.md index 65cebf1935..708d0b1ece 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -104,6 +104,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (global) [\#694](https://github.com/line/lbm-sdk/pull/694) replace deprecated functions since go 1.16 or 1.17 * (x/bankplus) [\#705](https://github.com/line/lbm-sdk/pull/705) add missing blockedAddr checking in bankplus * (x/foundation) [\#712](https://github.com/line/lbm-sdk/pull/712) fix x/foundation EndBlocker +* (x/feegrant) [\#720](https://github.com/line/lbm-sdk/pull/720) remove potential runtime panic in x/feegrant * (baseapp) [\#724](https://github.com/line/lbm-sdk/pull/724) add checking pubkey type from validator params * (x/staking) [\#726](https://github.com/line/lbm-sdk/pull/726) check allowedList size in StakeAuthorization.Accept() * (x/staking) [\#728](https://github.com/line/lbm-sdk/pull/728) fix typo in unbondingToUnbonded() panic diff --git a/x/feegrant/filtered_fee.go b/x/feegrant/filtered_fee.go index 290e111914..378c8f7bd7 100644 --- a/x/feegrant/filtered_fee.go +++ b/x/feegrant/filtered_fee.go @@ -53,11 +53,14 @@ func (a *AllowedMsgAllowance) GetAllowance() (FeeAllowanceI, error) { // SetAllowance sets allowed fee allowance. func (a *AllowedMsgAllowance) SetAllowance(allowance FeeAllowanceI) error { var err error - a.Allowance, err = types.NewAnyWithValue(allowance.(proto.Message)) - if err != nil { + protoAllowance, ok := allowance.(proto.Message) + if !ok { return sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", allowance) } - + a.Allowance, err = types.NewAnyWithValue(protoAllowance) + if err != nil { + return sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", protoAllowance) + } return nil } diff --git a/x/feegrant/filtered_fee_test.go b/x/feegrant/filtered_fee_test.go index 6aee743a44..bffce51a1c 100644 --- a/x/feegrant/filtered_fee_test.go +++ b/x/feegrant/filtered_fee_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + proto "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -188,3 +189,73 @@ func TestFilteredFeeValidAllow(t *testing.T) { }) } } + +// invalidInterfaceAllowance does not implement proto.Message +type invalidInterfaceAllowance struct { +} + +// compilation time interface implementation check +var _ feegrant.FeeAllowanceI = (*invalidInterfaceAllowance)(nil) + +func (i invalidInterfaceAllowance) Accept(ctx sdk.Context, fee sdk.Coins, msgs []sdk.Msg) (remove bool, err error) { + return false, nil +} + +func (i invalidInterfaceAllowance) ValidateBasic() error { + return nil +} + +// invalidProtoAllowance can not run proto.Marshal +type invalidProtoAllowance struct { + invalidInterfaceAllowance +} + +// compilation time interface implementation check +var _ feegrant.FeeAllowanceI = (*invalidProtoAllowance)(nil) +var _ proto.Message = (*invalidProtoAllowance)(nil) + +func (i invalidProtoAllowance) Reset() { +} + +func (i invalidProtoAllowance) String() string { + return "" +} + +func (i invalidProtoAllowance) ProtoMessage() { +} + +func TestSetAllowance(t *testing.T) { + cases := map[string]struct { + allowance feegrant.FeeAllowanceI + valid bool + }{ + "valid allowance": { + allowance: &feegrant.BasicAllowance{}, + valid: true, + }, + "invalid interface allowance": { + allowance: &invalidInterfaceAllowance{}, + valid: false, + }, + "empty allowance": { + allowance: (*invalidProtoAllowance)(nil), + valid: false, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + allowance := &feegrant.BasicAllowance{} + msgs := []string{sdk.MsgTypeURL(&banktypes.MsgSend{})} + allowed, err := feegrant.NewAllowedMsgAllowance(allowance, msgs) + require.NoError(t, err) + require.NotNil(t, allowed) + err = allowed.SetAllowance(tc.allowance) + if tc.valid { + require.NoError(t, err) + } else { + require.Error(t, err) + } + }) + } +}