Skip to content

Commit

Permalink
fix(sdk/vm): coerce MsgRun pkgpath to gno.land/r/$addr/run (#1645)
Browse files Browse the repository at this point in the history
This is the path that is reserved, but right now the handler for MsgRun
allows any path to be used. This PR enforces all run calls to happen
here exclusively.
  • Loading branch information
thehowl authored Feb 9, 2024
1 parent 2b15731 commit 59d8787
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gno.land/pkg/gnoclient/client_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (c *Client) Run(cfg RunCfg) (*ctypes.ResultBroadcastTxCommit, error) {
return nil, errors.Wrap(err, "precompile and check")
}
memPkg.Name = "main"
memPkg.Path = "gno.land/r/" + caller.String() + "/run"
memPkg.Path = ""

// Construct message & transaction and marshal.
msg := vm.MsgRun{
Expand Down
3 changes: 2 additions & 1 deletion gno.land/pkg/keyscli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ func execMakeRun(cfg *MakeRunCfg, args []string, cmdio commands.IO) error {
panic(err)
}
memPkg.Name = "main"
memPkg.Path = "gno.land/r/" + caller.String() + "/run"
// Set to empty; this will be automatically set by the VM keeper.
memPkg.Path = ""

// construct msg & tx and marshal.
msg := vm.MsgRun{
Expand Down
9 changes: 7 additions & 2 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (vm *VMKeeper) getGnoStore(ctx sdk.Context) gno.Store {
}
}

var reReservedPath = regexp.MustCompile(`gno\.land/r/g[a-z0-9]+/run`)
var reRunPath = regexp.MustCompile(`gno\.land/r/g[a-z0-9]+/run`)

// AddPackage adds a package with given fileset.
func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) error {
Expand All @@ -156,7 +156,7 @@ func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) error {
return ErrInvalidPkgPath("package already exists: " + pkgPath)
}

if reReservedPath.MatchString(pkgPath) {
if reRunPath.MatchString(pkgPath) {
return ErrInvalidPkgPath("reserved package name: " + pkgPath)
}

Expand Down Expand Up @@ -303,6 +303,11 @@ func (vm *VMKeeper) Run(ctx sdk.Context, msg MsgRun) (res string, err error) {
send := msg.Send
memPkg := msg.Package

// coerce path to right one.
// the path in the message must be "" or the following path.
// this is already checked in MsgRun.ValidateBasic
memPkg.Path = "gno.land/r/" + msg.Caller.String() + "/run"

// Validate arguments.
callerAcc := vm.acck.GetAccount(ctx, caller)
if callerAcc == nil {
Expand Down
11 changes: 8 additions & 3 deletions gno.land/pkg/sdk/vm/msgs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vm

import (
"fmt"
"strings"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
Expand Down Expand Up @@ -162,7 +163,7 @@ func NewMsgRun(caller crypto.Address, send std.Coins, files []*std.MemFile) MsgR
Send: send,
Package: &std.MemPackage{
Name: "main",
Path: "gno.land/r/" + caller.String() + "/run",
Path: "", // auto set by the handler
Files: files,
},
}
Expand All @@ -179,9 +180,13 @@ func (msg MsgRun) ValidateBasic() error {
if msg.Caller.IsZero() {
return std.ErrInvalidAddress("missing caller address")
}
if msg.Package.Path == "" { // XXX
return ErrInvalidPkgPath("missing package path")

// Force memPkg path to the reserved run path.
wantPath := "gno.land/r/" + msg.Caller.String() + "/run"
if path := msg.Package.Path; path != "" && path != wantPath {
return ErrInvalidPkgPath(fmt.Sprintf("invalid pkgpath for MsgRun: %q", path))
}

return nil
}

Expand Down

0 comments on commit 59d8787

Please sign in to comment.