From 61afe4a4ef8a3cd1a2917f54add35e047306e98f Mon Sep 17 00:00:00 2001 From: Hariom Verma Date: Wed, 10 Apr 2024 01:31:32 +0530 Subject: [PATCH] Correctly validate package/realm path --- tm2/pkg/std/memfile.go | 4 +--- tm2/pkg/std/memfile_test.go | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/tm2/pkg/std/memfile.go b/tm2/pkg/std/memfile.go index 5935428ac28..5655876c9bb 100644 --- a/tm2/pkg/std/memfile.go +++ b/tm2/pkg/std/memfile.go @@ -37,11 +37,9 @@ func (mempkg *MemPackage) IsEmpty() bool { return len(mempkg.Files) == 0 } -const rePathPart = `[a-z][a-z0-9_]*` - var ( rePkgName = regexp.MustCompile(`^[a-z][a-z0-9_]*$`) - rePkgOrRlmPath = regexp.MustCompile(`gno\.land/(?:p|r)(?:/` + rePathPart + `)+`) + rePkgOrRlmPath = regexp.MustCompile(`^gno\.land\/(?:p|r)(?:\/_?[a-z]+[a-z0-9_]*)+$`) reFileName = regexp.MustCompile(`^([a-zA-Z0-9_]*\.[a-z0-9_\.]*|LICENSE|README)$`) ) diff --git a/tm2/pkg/std/memfile_test.go b/tm2/pkg/std/memfile_test.go index 4e441bef0e7..59996b24626 100644 --- a/tm2/pkg/std/memfile_test.go +++ b/tm2/pkg/std/memfile_test.go @@ -85,22 +85,22 @@ func TestRePkgOrRlmPath(t *testing.T) { { desc: "Invalid With Underscore 1", in: "gno.land/r/path/_", - expected: false, // fails + expected: false, }, { desc: "Invalid With Underscore 2", in: "gno.land/r/path/_/_", - expected: false, // fails + expected: false, }, { desc: "Invalid With Underscore 3", in: "gno.land/r/path/__/path", - expected: false, // fails + expected: false, }, { desc: "Invalid With Hyphen", in: "gno.land/r/path/pa-th", - expected: false, // fails + expected: false, }, { desc: "Invalid x", @@ -125,17 +125,17 @@ func TestRePkgOrRlmPath(t *testing.T) { { desc: "Special Character 1", in: "gno.land/p/p@th/abc/def", - expected: false, // fails + expected: false, }, { desc: "Special Character 2", in: "gno.land/p/p&th/abc/def", - expected: false, // fails + expected: false, }, { desc: "Special Character 3", in: "gno.land/p/p&%$#h/abc/def", - expected: false, // fails + expected: false, }, { desc: "Leading Number", @@ -150,17 +150,17 @@ func TestRePkgOrRlmPath(t *testing.T) { { desc: "Empty Path Part", in: "gno.land/p/path//def", - expected: false, // fails + expected: false, }, { desc: "Trailing Slash", in: "gno.land/p/path/abc/def/", - expected: false, // fails + expected: false, }, { desc: "Extra Slash(s)", in: "gno.land/p/path///abc/def", - expected: false, // fails + expected: false, }, { desc: "Valid Long path", @@ -170,22 +170,22 @@ func TestRePkgOrRlmPath(t *testing.T) { { desc: "Long Path With Special Character 1", in: "gno.land/r/very/very/very/long/p@th", - expected: false, // fails + expected: false, }, { desc: "Long Path With Special Character 2", in: "gno.land/r/very/very/v%ry/long/path", - expected: false, // fails + expected: false, }, { desc: "Long Path With Trailing Slash", in: "gno.land/r/very/very/very/long/path/", - expected: false, // fails + expected: false, }, { desc: "Long Path With Empty Path Part", in: "gno.land/r/very/very/very//long/path/", - expected: false, // fails + expected: false, }, } { t.Run(tc.desc, func(t *testing.T) {