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

feat(cmd): gno mod module name validations #3526

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions gnovm/cmd/gno/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"path/filepath"
"strings"
"unicode"
"unicode/utf8"

"github.com/gnolang/gno/gnovm/cmd/gno/internal/pkgdownload"
"github.com/gnolang/gno/gnovm/cmd/gno/internal/pkgdownload/rpcpkgfetcher"
Expand Down Expand Up @@ -213,6 +215,10 @@ func execModInit(args []string) error {
var modPath string
if len(args) == 1 {
modPath = args[0]

if err := validateModulePath(modPath); err != nil {
return fmt.Errorf("invalid module path: %w", err)
}
}
dir, err := os.Getwd()
if err != nil {
Expand All @@ -225,6 +231,47 @@ func execModInit(args []string) error {
return nil
}

var invalidChars = map[rune]bool{
'`': true,
'"': true,
'\\': true,
'?': true,
'*': true,
':': true,
'<': true,
'>': true,
'|': true,
'[': true,
']': true,
}

// validateModulePath checks if the given module path contains only valid characters.
// It returns an error if the path is empty or contains invalid characters such as:
// non-printable ASCII characters, Unicode characters, spaces, or special characters
// (`, ", \, ?, *, :, <, >, |, [, ]).
func validateModulePath(path string) error {
if path == "" {
return errors.New("module path cannot be empty")
}

if i := strings.IndexFunc(path, func(r rune) bool {
if r >= utf8.RuneSelf || !unicode.IsPrint(r) {
return true
}
if invalidChars[r] {
return true
}
if unicode.IsSpace(r) {
return true
}
return false
}); i != -1 {
return fmt.Errorf("invalid character '%c' in module path at position %d", path[i], i)
}

return nil
}

type modTidyCfg struct {
verbose bool
recursive bool
Expand Down
132 changes: 132 additions & 0 deletions gnovm/cmd/gno/mod_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"regexp"
"testing"
)

Expand Down Expand Up @@ -216,3 +217,134 @@ valid.gno

testMainCaseRun(t, tc)
}

func TestExecModInit(t *testing.T) {
tc := []testMainCase{
{
args: []string{"mod", "init", "gno.land/p/demo/foo"},
testDir: "../../tests/integ/empty_dir",
simulateExternalRepo: true,
},
{
args: []string{"mod", "init", string([]byte{0xff, 0xfe, 0xfd}) + "xample.com/myproject"},
testDir: "../../tests/integ/empty_dir",
simulateExternalRepo: true,
errShouldContain: "invalid character",
},
{
args: []string{"mod", "init", "example com/myproject"},
testDir: "../../tests/integ/empty_dir",
simulateExternalRepo: true,
errShouldContain: "invalid character ' ' in module path",
},
{
args: []string{"mod", "init", "example?.com/myproject"},
testDir: "../../tests/integ/empty_dir",
simulateExternalRepo: true,
errShouldContain: "invalid character '?' in module path",
},
{
args: []string{"mod", "init", ""},
testDir: "../../tests/integ/empty_dir",
simulateExternalRepo: true,
errShouldContain: "module path cannot be empty",
},
{
args: []string{"mod", "init", "gno.land/p/demo/foo"},
testDir: "../../tests/integ/empty_gnomod",
simulateExternalRepo: true,
errShouldContain: "gno.mod file already exists",
},
{
args: []string{"mod", "init", "gno.land/p/demo/foo", "extra"},
testDir: "../../tests/integ/empty_dir",
simulateExternalRepo: true,
errShouldContain: "flag: help requested",
},
}

testMainCaseRun(t, tc)
}

func TestValidateModulePath(t *testing.T) {
tests := []struct {
name string
path string
wantErrRegex string
}{
{
name: "valid path",
path: "example.com/myproject",
},
{
name: "valid gno.land path",
path: "gno.land/p/demo/avl",
},
{
name: "empty path",
path: "",
wantErrRegex: `^module path cannot be empty$`,
},
{
name: "invalid UTF8 character",
path: string([]byte{0xFF, 0xFE, 0xFD}),
wantErrRegex: `^invalid character '.*' in module path at position 0$`,
},
{
name: "space in path",
path: "example com/myproject",
wantErrRegex: `^invalid character ' ' in module path at position 7$`,
},
{
name: "question mark in path",
path: "example?.com/myproject",
wantErrRegex: `^invalid character '\?' in module path at position 7$`,
},
{
name: "asterisk in path",
path: "example*.com/myproject",
wantErrRegex: `^invalid character '\*' in module path at position 7$`,
},
{
name: "backslash in path",
path: "example\\com/myproject",
wantErrRegex: `^invalid character '\\' in module path at position 7$`,
},
{
name: "quote in path",
path: "example\"com/myproject",
wantErrRegex: `^invalid character '"' in module path at position 7$`,
},
{
name: "backtick in path",
path: "example`com/myproject",
wantErrRegex: "^invalid character '`' in module path at position 7$",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateModulePath(tt.path)
if tt.wantErrRegex == "" {
if err != nil {
t.Errorf("validateModulePath() error = %v, expected no error", err)
}
return
}

if err == nil {
t.Errorf("validateModulePath() expected error matching %q, got nil", tt.wantErrRegex)
return
}

// using a regex due to avoid encoding issues.
re, regexErr := regexp.Compile(tt.wantErrRegex)
if regexErr != nil {
t.Fatalf("invalid regex pattern: %v", regexErr)
}
if !re.MatchString(err.Error()) {
t.Errorf("validateModulePath() error = %q, want error matching %q", err.Error(), tt.wantErrRegex)
}
})
}
}
Loading