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 2 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
20 changes: 17 additions & 3 deletions tm2/pkg/std/memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@ func (mempkg *MemPackage) IsEmpty() bool {
}

var (
rePkgName = regexp.MustCompile(`^[a-z][a-z0-9_]*$`)
rePkgOrRlmPath = regexp.MustCompile(`^gno\.land\/(?:p|r)(?:\/_?[a-z]+[a-z0-9_]*)+$`)
reFileName = regexp.MustCompile(`^([a-zA-Z0-9_]*\.[a-z0-9_\.]*|LICENSE|README)$`)
rePkgName = regexp.MustCompile(`^[a-z][a-z0-9_]*$`)
/*
in case we use RE2 external lib (e.g. dlclark/regexp2), we can do this
^(?=.{0,256}$)gno\.land\/(?:p|r)(?:\/_?[a-z]+[a-z0-9_]*)+$
with lookahead operation to limit lenght
but for now we just check the pkgPath's len instead of.
*/
rePkgOrRlmPath = regexp.MustCompile(`^gno\.land\/(?:p|r)(?:\/_?[a-z]+[a-z0-9_]*)+$`)
reFileName = regexp.MustCompile(`^([a-zA-Z0-9_]*\.[a-z0-9_\.]*|LICENSE|README)$`)
rePkgOrRlmLenLimit = 256
thinhnx-var marked this conversation as resolved.
Show resolved Hide resolved
)

// path must not contain any dots after the first domain component.
Expand All @@ -58,6 +65,9 @@ func (mempkg *MemPackage) Validate() error {
if !rePkgOrRlmPath.MatchString(mempkg.Path) {
return fmt.Errorf("invalid package/realm path %q, failed to match %q", mempkg.Path, rePkgOrRlmPath)
}
if !isValidLength(mempkg.Path, rePkgOrRlmLenLimit) {
return fmt.Errorf("invalid length of package/realm path: %v which limitation is %v", len(mempkg.Path), rePkgOrRlmLenLimit)
thinhnx-var marked this conversation as resolved.
Show resolved Hide resolved
}
// enforce sorting files based on Go conventions for predictability
sorted := sort.SliceIsSorted(
mempkg.Files,
Expand Down Expand Up @@ -98,3 +108,7 @@ func SplitFilepath(filepath string) (dirpath string, filename string) {
return strings.Join(parts, "/"), ""
}
}

func isValidLength(pkgPath string, limitation int) bool {
return len(pkgPath) <= limitation
}
thinhnx-var marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 17 additions & 1 deletion tm2/pkg/std/memfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,16 @@ func TestRePkgOrRlmPath(t *testing.T) {
expected: false,
},
}

testLengthTable := []struct{
thinhnx-var marked this conversation as resolved.
Show resolved Hide resolved
desc, in string
expected bool
}{
{
desc: "Longer Than Limit",
in: "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/very/very",
expected: false,
},
}
for _, tc := range testTable {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
Expand All @@ -199,4 +208,11 @@ func TestRePkgOrRlmPath(t *testing.T) {
assert.Equal(t, tc.expected, rePkgOrRlmPath.MatchString(tc.in))
})
}
for _, tc := range testLengthTable {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.expected, isValidLength(tc.in, rePkgOrRlmLenLimit))
})
}
}
Loading