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

Better Json validation #199

Merged
merged 1 commit into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions x/wasm/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func GetCmdGetContractStateSmart(cdc *codec.Codec) *cobra.Command {

cmd := &cobra.Command{
Use: "smart [bech32_address] [query]",
Short: "Calls contract with given address with query data and prints the returned result",
Long: "Calls contract with given address with query data and prints the returned result",
Short: "Calls contract with given address with query data and prints the returned result",
Long: "Calls contract with given address with query data and prints the returned result",
Args: cobra.ExactArgs(2),
RunE: func(_ *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
Expand All @@ -252,6 +252,9 @@ func GetCmdGetContractStateSmart(cdc *codec.Codec) *cobra.Command {
if err != nil {
return fmt.Errorf("decode query: %s", err)
}
if !json.Valid(queryData) {
alpe marked this conversation as resolved.
Show resolved Hide resolved
return errors.New("query data must be json")
}
res, _, err := cliCtx.QueryWithData(route, queryData)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions x/wasm/internal/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func TestQueryContractState(t *testing.T) {
srcReq: abci.RequestQuery{Data: []byte(`{"raw":{"key":"config"}}`)},
expErr: types.ErrQueryFailed,
},
"query smart with invalid json": {
srcPath: []string{QueryGetContractState, addr.String(), QueryMethodContractStateSmart},
srcReq: abci.RequestQuery{Data: []byte(`not a json string`)},
expErr: types.ErrQueryFailed,
},
"query unknown raw key": {
srcPath: []string{QueryGetContractState, addr.String(), QueryMethodContractStateRaw},
srcReq: abci.RequestQuery{Data: []byte("unknown")},
Expand Down
10 changes: 10 additions & 0 deletions x/wasm/internal/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ func (msg MsgInstantiateContract) ValidateBasic() error {
return err
}
}
if !json.Valid(msg.InitMsg) {
return sdkerrors.Wrap(ErrInvalid, "init msg json")
}
return nil
}

Expand Down Expand Up @@ -136,6 +139,9 @@ func (msg MsgExecuteContract) ValidateBasic() error {
if !msg.SentFunds.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "sentFunds")
}
if !json.Valid(msg.Msg) {
return sdkerrors.Wrap(ErrInvalid, "msg json")
}
return nil
}

Expand Down Expand Up @@ -172,6 +178,10 @@ func (msg MsgMigrateContract) ValidateBasic() error {
if err := sdk.VerifyAddressFormat(msg.Contract); err != nil {
return sdkerrors.Wrap(err, "contract")
}
if !json.Valid(msg.MigrateMsg) {
return sdkerrors.Wrap(ErrInvalid, "migrate msg json")
}

return nil
}

Expand Down
59 changes: 51 additions & 8 deletions x/wasm/internal/types/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,23 @@ func TestInstantiateContractValidation(t *testing.T) {
},
valid: false,
},
"non json init msg": {
msg: MsgInstantiateContract{
Sender: goodAddress,
CodeID: 1,
Label: "foo",
InitMsg: []byte("invalid-json"),
},
valid: false,
},
"empty init msg": {
msg: MsgInstantiateContract{
Sender: goodAddress,
CodeID: 1,
Label: "foo",
},
valid: false,
},
}

for name, tc := range cases {
Expand Down Expand Up @@ -236,6 +253,7 @@ func TestExecuteContractValidation(t *testing.T) {
msg: MsgExecuteContract{
Sender: goodAddress,
Contract: goodAddress,
Msg: []byte("{}"),
},
valid: true,
},
Expand Down Expand Up @@ -296,6 +314,21 @@ func TestExecuteContractValidation(t *testing.T) {
},
valid: false,
},
"non json msg": {
msg: MsgExecuteContract{
Sender: goodAddress,
Contract: goodAddress,
Msg: []byte("invalid-json"),
},
valid: false,
},
"empty msg": {
msg: MsgExecuteContract{
Sender: goodAddress,
Contract: goodAddress,
},
valid: false,
},
}

for name, tc := range cases {
Expand Down Expand Up @@ -447,14 +480,7 @@ func TestMsgMigrateContract(t *testing.T) {
Sender: goodAddress,
Contract: anotherGoodAddress,
CodeID: 1,
MigrateMsg: []byte{1},
},
},
"MigrateMsg optional": {
src: MsgMigrateContract{
Sender: goodAddress,
Contract: anotherGoodAddress,
CodeID: 1,
MigrateMsg: []byte("{}"),
},
},
"bad sender": {
Expand Down Expand Up @@ -494,6 +520,23 @@ func TestMsgMigrateContract(t *testing.T) {
},
expErr: true,
},
"non json migrateMsg": {
src: MsgMigrateContract{
Sender: goodAddress,
Contract: anotherGoodAddress,
CodeID: 1,
MigrateMsg: []byte("invalid json"),
},
expErr: true,
},
"empty migrateMsg": {
src: MsgMigrateContract{
Sender: goodAddress,
Contract: anotherGoodAddress,
CodeID: 1,
},
expErr: true,
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
Expand Down