Skip to content

Commit

Permalink
fix: fix dynamic link APIs do not panic with invalid bech32 (#57)
Browse files Browse the repository at this point in the history
* fix: fix wasmd command in init_single.sh

* fix: do error handling for invalid address in dynamic link apis

* docs: add this PR to CHANGELOG.md
  • Loading branch information
loloicci committed Jul 3, 2023
1 parent 2e0fe8b commit 8b29b5e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [\#36](https://github.com/Finschia/wasmd/pull/36) separate `x/wasm` into `x/wasmplus` module of dynamiclink

### Bug Fixes
* [\#57](https://github.com/Finschia/wasmd/pull/57) fix dynamic link APIs do not panic with invalid bech32
* [\#35](https://github.com/Finschia/wasmd/pull/35) stop wrap twice the response of handling non-plus wasm message in plus handler

### Breaking Changes
Expand Down
2 changes: 1 addition & 1 deletion init_single.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ then
mode="testnet"
fi

WASMD=${WASMD:-wasmplusd}
WASMD=${WASMD:-wasmd}

# initialize
rm -rf ~/.wasmplusd
Expand Down
12 changes: 10 additions & 2 deletions x/wasmplus/keeper/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ type cosmwasmAPIGeneratorImpl struct {
}

func (a cosmwasmAPIImpl) callCallablePoint(contractAddrStr string, name []byte, args []byte, isReadonly bool, callstack []byte, gasLimit uint64) ([]byte, uint64, error) {
contractAddr := sdk.MustAccAddressFromBech32(contractAddrStr)
contractAddr, err := sdk.AccAddressFromBech32(contractAddrStr)

if err != nil {
return nil, 0, fmt.Errorf("specified callee address is invalid: %s", err)
}

if a.keeper.IsInactiveContract(*a.ctx, contractAddr) {
return nil, 0, fmt.Errorf("called contract cannot be executed")
Expand All @@ -29,7 +33,11 @@ func (a cosmwasmAPIImpl) callCallablePoint(contractAddrStr string, name []byte,
}

func (a cosmwasmAPIImpl) validateInterface(contractAddrStr string, expectedInterface []byte) ([]byte, uint64, error) {
contractAddr := sdk.MustAccAddressFromBech32(contractAddrStr)
contractAddr, err := sdk.AccAddressFromBech32(contractAddrStr)

if err != nil {
return nil, 0, fmt.Errorf("specified contract address is invalid: %s", err)
}

if a.keeper.IsInactiveContract(*a.ctx, contractAddr) {
return nil, 0, fmt.Errorf("try to validate a contract cannot be executed")
Expand Down
20 changes: 20 additions & 0 deletions x/wasmplus/keeper/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ func TestCallCallablePoint(t *testing.T) {
// reset inactive contracts
keepers.WasmKeeper.deleteInactiveContract(ctx, contractAddr)
})

t.Run("fail with invalid callee address", func(t *testing.T) {
argsEv := [][]byte{eventsInBin}
argsEvBin, err := json.Marshal(argsEv)
require.NoError(t, err)
name := "add_events_dyn"
nameBin, err := json.Marshal(name)
require.NoError(t, err)
invalidAddr := "invalidAddr"
_, _, err = api.CallCallablePoint(invalidAddr, nameBin, argsEvBin, false, callstackBin, gasLimit)
assert.ErrorContains(t, err, "specified callee address is invalid")
})
}

func TestValidateDynamicLinkInterface(t *testing.T) {
Expand Down Expand Up @@ -288,4 +300,12 @@ func TestValidateDynamicLinkInterface(t *testing.T) {
// reset inactive contracts
keepers.WasmKeeper.deleteInactiveContract(ctx, contractAddr)
})

t.Run("fail with invalid contract address", func(t *testing.T) {
validInterface := []byte(`[{"name":"add_event_dyn","ty":{"params":["I32","I32","I32"],"results":[]}},{"name":"add_events_dyn","ty":{"params":["I32","I32"],"results":[]}},{"name":"add_attribute_dyn","ty":{"params":["I32","I32","I32"],"results":[]}},{"name":"add_attributes_dyn","ty":{"params":["I32","I32"],"results":[]}}]`)
invalidAddr := "invalidAddr"
_, _, err = api.ValidateInterface(invalidAddr, validInterface)

assert.ErrorContains(t, err, "specified contract address is invalid")
})
}

0 comments on commit 8b29b5e

Please sign in to comment.