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

fix: remove potential runtime panic in x/feegrant #720

Merged
merged 11 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from 10 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 @@ -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
Expand Down
9 changes: 6 additions & 3 deletions x/feegrant/filtered_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
78 changes: 78 additions & 0 deletions x/feegrant/filtered_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

proto "github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -188,3 +189,80 @@ func TestFilteredFeeValidAllow(t *testing.T) {
})
}
}

0Tech marked this conversation as resolved.
Show resolved Hide resolved
// invalidAllowance does not implement proto.Message
type invalidAllowance struct {
}

// compilation time interface implementation check
var _ feegrant.FeeAllowanceI = (*invalidAllowance)(nil)

func (i invalidAllowance) Accept(ctx sdk.Context, fee sdk.Coins, msgs []sdk.Msg) (remove bool, err error) {
return false, nil
}

func (i invalidAllowance) ValidateBasic() error {
return nil
}

// invalidAllowance2 does not have field
type invalidAllowance2 struct {
0Tech marked this conversation as resolved.
Show resolved Hide resolved
}

// compilation time interface implementation check
var _ feegrant.FeeAllowanceI = (*invalidAllowance2)(nil)
var _ proto.Message = (*invalidAllowance2)(nil)

func (i invalidAllowance2) Accept(ctx sdk.Context, fee sdk.Coins, msgs []sdk.Msg) (remove bool, err error) {
return false, nil
}

func (i invalidAllowance2) ValidateBasic() error {
return nil
}

func (i invalidAllowance2) Reset() {
}

func (i invalidAllowance2) String() string {
return ""
}

func (i invalidAllowance2) ProtoMessage() {
}

func TestSetAllowance(t *testing.T) {
cases := map[string]struct {
allowance feegrant.FeeAllowanceI
valid bool
}{
"valid allowance": {
allowance: &feegrant.BasicAllowance{},
valid: true,
},
"allowance without proto.Message": {
allowance: &invalidAllowance{},
valid: false,
},
"allowance without fields": {
allowance: (*invalidAllowance2)(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)
}
})
}
}