Skip to content

Commit 0ade7e4

Browse files
authored
fix: remove potential runtime panic in x/feegrant (#720)
* Remove potential runtime panic * Update `CHANGELOG.md` * Fix for codecov * Add test * Fix for reviews * Add test case * Fix names
1 parent b13ab12 commit 0ade7e4

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
105105
* (global) [\#694](https://github.com/line/lbm-sdk/pull/694) replace deprecated functions since go 1.16 or 1.17
106106
* (x/bankplus) [\#705](https://github.com/line/lbm-sdk/pull/705) add missing blockedAddr checking in bankplus
107107
* (x/foundation) [\#712](https://github.com/line/lbm-sdk/pull/712) fix x/foundation EndBlocker
108+
* (x/feegrant) [\#720](https://github.com/line/lbm-sdk/pull/720) remove potential runtime panic in x/feegrant
108109
* (baseapp) [\#724](https://github.com/line/lbm-sdk/pull/724) add checking pubkey type from validator params
109110
* (x/staking) [\#726](https://github.com/line/lbm-sdk/pull/726) check allowedList size in StakeAuthorization.Accept()
110111
* (x/staking) [\#728](https://github.com/line/lbm-sdk/pull/728) fix typo in unbondingToUnbonded() panic

x/feegrant/filtered_fee.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ func (a *AllowedMsgAllowance) GetAllowance() (FeeAllowanceI, error) {
5353
// SetAllowance sets allowed fee allowance.
5454
func (a *AllowedMsgAllowance) SetAllowance(allowance FeeAllowanceI) error {
5555
var err error
56-
a.Allowance, err = types.NewAnyWithValue(allowance.(proto.Message))
57-
if err != nil {
56+
protoAllowance, ok := allowance.(proto.Message)
57+
if !ok {
5858
return sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", allowance)
5959
}
60-
60+
a.Allowance, err = types.NewAnyWithValue(protoAllowance)
61+
if err != nil {
62+
return sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", protoAllowance)
63+
}
6164
return nil
6265
}
6366

x/feegrant/filtered_fee_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55
"time"
66

7+
proto "github.com/gogo/protobuf/proto"
78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910

@@ -188,3 +189,73 @@ func TestFilteredFeeValidAllow(t *testing.T) {
188189
})
189190
}
190191
}
192+
193+
// invalidInterfaceAllowance does not implement proto.Message
194+
type invalidInterfaceAllowance struct {
195+
}
196+
197+
// compilation time interface implementation check
198+
var _ feegrant.FeeAllowanceI = (*invalidInterfaceAllowance)(nil)
199+
200+
func (i invalidInterfaceAllowance) Accept(ctx sdk.Context, fee sdk.Coins, msgs []sdk.Msg) (remove bool, err error) {
201+
return false, nil
202+
}
203+
204+
func (i invalidInterfaceAllowance) ValidateBasic() error {
205+
return nil
206+
}
207+
208+
// invalidProtoAllowance can not run proto.Marshal
209+
type invalidProtoAllowance struct {
210+
invalidInterfaceAllowance
211+
}
212+
213+
// compilation time interface implementation check
214+
var _ feegrant.FeeAllowanceI = (*invalidProtoAllowance)(nil)
215+
var _ proto.Message = (*invalidProtoAllowance)(nil)
216+
217+
func (i invalidProtoAllowance) Reset() {
218+
}
219+
220+
func (i invalidProtoAllowance) String() string {
221+
return ""
222+
}
223+
224+
func (i invalidProtoAllowance) ProtoMessage() {
225+
}
226+
227+
func TestSetAllowance(t *testing.T) {
228+
cases := map[string]struct {
229+
allowance feegrant.FeeAllowanceI
230+
valid bool
231+
}{
232+
"valid allowance": {
233+
allowance: &feegrant.BasicAllowance{},
234+
valid: true,
235+
},
236+
"invalid interface allowance": {
237+
allowance: &invalidInterfaceAllowance{},
238+
valid: false,
239+
},
240+
"empty allowance": {
241+
allowance: (*invalidProtoAllowance)(nil),
242+
valid: false,
243+
},
244+
}
245+
246+
for name, tc := range cases {
247+
t.Run(name, func(t *testing.T) {
248+
allowance := &feegrant.BasicAllowance{}
249+
msgs := []string{sdk.MsgTypeURL(&banktypes.MsgSend{})}
250+
allowed, err := feegrant.NewAllowedMsgAllowance(allowance, msgs)
251+
require.NoError(t, err)
252+
require.NotNil(t, allowed)
253+
err = allowed.SetAllowance(tc.allowance)
254+
if tc.valid {
255+
require.NoError(t, err)
256+
} else {
257+
require.Error(t, err)
258+
}
259+
})
260+
}
261+
}

0 commit comments

Comments
 (0)