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

fix: limitation for length of package/realm path #2108

Merged
merged 24 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f284117
limit package path length to 256
May 15, 2024
7560edf
add test
May 15, 2024
9486fe6
edit convos
May 15, 2024
23029ce
Merge branch 'master' into master
thinhnx-var May 15, 2024
1b8ece6
Merge branch 'master' into master
thinhnx-var May 15, 2024
5df4024
limitations to bytes lenght const, add validate test
May 15, 2024
9d00342
go format
May 16, 2024
e152af0
refactor testcase
thinhnx-var May 16, 2024
286bd98
stop casting to bytes slice
thinhnx-var May 16, 2024
e52b61e
format
thinhnx-var May 16, 2024
4267e39
Merge branch 'master' into master
thinhnx-var May 17, 2024
e73b15b
Merge branch 'master' into master
thinhnx-var May 19, 2024
cd3409b
Merge branch 'master' into master
thinhnx-var May 23, 2024
fbb5871
Merge branch 'master' into master
thinhnx-var May 25, 2024
bdd19d0
Update tm2/pkg/std/memfile.go - format errors
thinhnx-var May 26, 2024
a97e377
update memfile_test.go - add clearly check error sw
thinhnx-var May 26, 2024
7e99abe
Merge remote-tracking branch 'refs/remotes/origin/master'
thinhnx-var May 26, 2024
c26ba41
Merge branch 'master' into master
thinhnx-var May 26, 2024
491ab8e
Merge branch 'master' into master
thinhnx-var May 27, 2024
ba8d707
Merge branch 'gnolang:master' into master
thinhnx-var May 27, 2024
5350429
Merge branch 'gnolang:master' into master
thinhnx-var May 28, 2024
d95d876
testcase runs parralel, add errContains to clearify which condition i…
thinhnx-var May 28, 2024
7bbb3d8
lint the files
thinhnx-var May 28, 2024
c05ae7e
remove redundant field
thehowl May 28, 2024
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: 7 additions & 0 deletions tm2/pkg/std/memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (mempkg *MemPackage) IsEmpty() bool {
return len(mempkg.Files) == 0
}

const pathLengthLimit = 256

var (
rePkgName = regexp.MustCompile(`^[a-z][a-z0-9_]*$`)
rePkgOrRlmPath = regexp.MustCompile(`^gno\.land\/(?:p|r)(?:\/_?[a-z]+[a-z0-9_]*)+$`)
Expand All @@ -52,9 +54,14 @@ func (mempkg *MemPackage) Validate() error {
return fmt.Errorf("no files found within package %q", mempkg.Name)
}

if len(mempkg.Path) > pathLengthLimit {
return fmt.Errorf("path length %d exceeds limit %d", len(mempkg.Path), pathLengthLimit)
}

if !rePkgName.MatchString(mempkg.Name) {
return fmt.Errorf("invalid package name %q, failed to match %q", mempkg.Name, rePkgName)
}

if !rePkgOrRlmPath.MatchString(mempkg.Path) {
return fmt.Errorf("invalid package/realm path %q, failed to match %q", mempkg.Path, rePkgOrRlmPath)
}
Expand Down
288 changes: 179 additions & 109 deletions tm2/pkg/std/memfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func TestMemPackage_Validate(t *testing.T) {
t.Parallel()
tt := []struct {
name string
mpkg *MemPackage
Expand All @@ -28,7 +29,7 @@ func TestMemPackage_Validate(t *testing.T) {
Path: "gno.land/r/demo/hey",
Files: []*MemFile{{Name: "b.gno"}, {Name: "a.gno"}},
},
`mempackage "gno.land/r/demo/hey" has unsorted files`,
"unsorted",
},
{
"Duplicate",
Expand All @@ -37,166 +38,235 @@ func TestMemPackage_Validate(t *testing.T) {
Path: "gno.land/r/demo/hey",
Files: []*MemFile{{Name: "a.gno"}, {Name: "a.gno"}},
},
`duplicate file name "a.gno"`,
"duplicate",
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
err := tc.mpkg.Validate()
if tc.errContains == "" {
assert.NoError(t, err)
} else {
assert.ErrorContains(t, err, tc.errContains)
}
})
}
}

func TestRePkgOrRlmPath(t *testing.T) {
t.Parallel()

testTable := []struct {
desc, in string
expected bool
}{
{
desc: "Valid p",
in: "gno.land/p/path/path",
expected: true,
},
{
desc: "Valid r",
in: "gno.land/r/path/path",
expected: true,
},
{
desc: "Leading Underscore",
in: "gno.land/r/path/_path",
expected: true,
"InvalidPathLength",
&MemPackage{
Name: "hey",
Path: "gno.land/r/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/long/path",
Files: []*MemFile{{Name: "a.gno"}},
},
"path length",
},
{
desc: "Trailing Underscore",
in: "gno.land/r/path/path_",
expected: true,
"valid p",
&MemPackage{
Name: "hey",
Path: "gno.land/p/path/path",
Files: []*MemFile{{Name: "a.gno"}},
},
"",
},
{
desc: "Underscore in Between",
in: "gno.land/r/path/p_ath",
expected: true,
"valid r",
&MemPackage{
Name: "hey",
Path: "gno.land/r/path/path",
Files: []*MemFile{{Name: "a.gno"}},
},
"",
},
{
desc: "Invalid With Underscore 1",
in: "gno.land/r/path/_",
expected: false,
"Leading underscore",
&MemPackage{
Name: "hey",
Path: "gno.land/r/path/_path",
Files: []*MemFile{{Name: "a.gno"}},
},
"",
},
{
desc: "Invalid With Underscore 2",
in: "gno.land/r/path/_/_",
expected: false,
"Trailing underscore",
&MemPackage{
Name: "hey",
Path: "gno.land/r/path/path_",
Files: []*MemFile{{Name: "a.gno"}},
},
"",
},
{
desc: "Invalid With Underscore 3",
in: "gno.land/r/path/__/path",
expected: false,
"Between underscore",
&MemPackage{
Name: "hey",
Path: "gno.land/r/path/p_ath",
Files: []*MemFile{{Name: "a.gno"}},
},
"",
},
{
desc: "Invalid With Hyphen",
in: "gno.land/r/path/pa-th",
expected: false,
"Invalid underscore",
&MemPackage{
Name: "hey",
Path: "gno.land/r/path/_",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Invalid x",
in: "gno.land/x/path/path",
expected: false,
"Invalid underscore 2",
&MemPackage{
Name: "hey",
Path: "gno.land/r/path/_/_",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Missing Path 1",
in: "gno.land/p",
expected: false,
"Invalid underscore 3",
&MemPackage{
Name: "hey",
Path: "gno.land/r/path/__/path",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Missing Path 2",
in: "gno.land/p/",
expected: false,
"Invalid hyphen",
&MemPackage{
Name: "hey",
Path: "gno.land/r/path/pa-th",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Invalid domain",
in: "github.com/p/path/path",
expected: false,
"Invalid x",
&MemPackage{
Name: "hey",
Path: "gno.land/x/path/path",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Special Character 1",
in: "gno.land/p/p@th/abc/def",
expected: false,
"Invalid missing path 1",
&MemPackage{
Name: "hey",
Path: "gno.land/p",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Special Character 2",
in: "gno.land/p/p&th/abc/def",
expected: false,
"Invalid missing path 2",
&MemPackage{
Name: "hey",
Path: "gno.land/p/",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Special Character 3",
in: "gno.land/p/p&%$#h/abc/def",
expected: false,
"Invalid path",
&MemPackage{
Name: "hey",
Path: "github.com/p/path/path",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Leading Number",
in: "gno.land/p/1Path/abc/def",
expected: false,
"Special character",
&MemPackage{
Name: "hey",
Path: "gno.land/p/p@th/abc/def",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Uppercase Letters",
in: "gno.land/p/PaTh/abc/def",
expected: false,
"Special character 2",
&MemPackage{
Name: "hey",
Path: "gno.land/p/p&th/abc/def",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Empty Path Part",
in: "gno.land/p/path//def",
expected: false,
"Invalid number",
&MemPackage{
Name: "hey",
Path: "gno.land/p/1Path/abc/def",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Trailing Slash",
in: "gno.land/p/path/abc/def/",
expected: false,
"Invalid uppercase",
&MemPackage{
Name: "hey",
Path: "gno.land/p/PaTh/abc/def",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Extra Slash(s)",
in: "gno.land/p/path///abc/def",
expected: false,
"Invalid empty path",
&MemPackage{
Name: "hey",
Path: "gno.land/p/path//def",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Valid Long path",
in: "gno.land/r/very/very/very/long/path",
expected: true,
"Invalid trailing slash",
&MemPackage{
Name: "hey",
Path: "gno.land/p/path/abc/def/",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Long Path With Special Character 1",
in: "gno.land/r/very/very/very/long/p@th",
expected: false,
"valid long path",
&MemPackage{
Name: "hey",
Path: "gno.land/r/very/very/very/long/path",
Files: []*MemFile{{Name: "a.gno"}},
},
"",
},
{
desc: "Long Path With Special Character 2",
in: "gno.land/r/very/very/v%ry/long/path",
expected: false,
"Invalid long path with special character",
&MemPackage{
Name: "hey",
Path: "gno.land/r/very/very/very/long/p@th",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Long Path With Trailing Slash",
in: "gno.land/r/very/very/very/long/path/",
expected: false,
"Invalid long path with trailing slash",
&MemPackage{
Name: "hey",
Path: "gno.land/r/very/very/very/long/path/",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
{
desc: "Long Path With Empty Path Part",
in: "gno.land/r/very/very/very//long/path/",
expected: false,
"Invalid long path with empty",
&MemPackage{
Name: "hey",
Path: "gno.land/r/very/very/very//long/path/",
Files: []*MemFile{{Name: "a.gno"}},
},
"invalid package/realm path",
},
}

for _, tc := range testTable {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

assert.Equal(t, tc.expected, rePkgOrRlmPath.MatchString(tc.in))
err := tc.mpkg.Validate()
if tc.errContains != "" {
assert.ErrorContains(t, err, tc.errContains)
} else {
assert.NoError(t, err)
}
})
}
}
Loading