diff --git a/pkg/addon/source.go b/pkg/addon/source.go index 1a5e0a3e5..d2cb68167 100644 --- a/pkg/addon/source.go +++ b/pkg/addon/source.go @@ -317,10 +317,10 @@ func NewAsyncReader(baseURL, bucket, repo, subPath, token string, rdType ReaderT return nil, errors.New("addon registry invalid") } _, content, err := utils.ParseGitlab(u.String(), repo) - content.GitlabContent.Path = subPath if err != nil { return nil, err } + content.GitlabContent.Path = subPath gitlabHelper, err := createGitlabHelper(content, token) if err != nil { return nil, errors.New("addon registry connect fail") diff --git a/pkg/utils/parse.go b/pkg/utils/parse.go index 7cfe91855..fbaf38008 100644 --- a/pkg/utils/parse.go +++ b/pkg/utils/parse.go @@ -232,6 +232,9 @@ func ParseGitlab(addr, repo string) (string, *Content, error) { arr := strings.Split(addr, repo) owner := strings.Split(arr[0], URL.Host+"/") + if len(owner) < 2 || len(owner[1]) == 0 { + return "", nil, errors.New(errInvalidFormatMsg + addr) + } if !strings.Contains(arr[1], "/") { // https://example.gitlab.com// return TypeGitlab, &Content{ @@ -246,6 +249,9 @@ func ParseGitlab(addr, repo string) (string, *Content, error) { // https://example.gitlab.com///tree/ l := strings.Split(arr[1], "/") + if len(l) < 3 { + return "", nil, errors.New(errInvalidFormatMsg + addr) + } return TypeGitlab, &Content{ GitlabContent: GitlabContent{ diff --git a/pkg/utils/parse_test.go b/pkg/utils/parse_test.go index e9193a50c..4204dcd8d 100644 --- a/pkg/utils/parse_test.go +++ b/pkg/utils/parse_test.go @@ -174,6 +174,24 @@ func TestParseGitlab(t *testing.T) { repo: "repo", wantErr: true, }, + { + name: "invalid gitlab url repo at path root without owner", + addr: "https://gitlab.com/catalog", + repo: "catalog", + wantErr: true, + }, + { + name: "invalid gitlab url tree branch missing", + addr: "https://gitlab.com/kubevela/catalog/tree", + repo: "catalog", + wantErr: true, + }, + { + name: "invalid gitlab url repo only in host", + addr: "https://catalog.gitlab.com/kubevela/foo", + repo: "catalog", + wantErr: true, + }, } for _, tc := range testCases {