Files
kubevela/pkg/utils/parse.go
Anas KhanandGitHub 6fd0e71e50 Fix: prevent panics in ParseGitlab on malformed addon registry URLs (#7206)
ParseGitlab indexed the results of splitting the address without bounds
checks, so a malformed (but repo-name-matching) GitLab registry URL crashed
the controller/CLI instead of returning an error. Three inputs panic today:

  - https://gitlab.com/catalog          -> slice bounds out of range [:-1]
  - https://gitlab.com/kubevela/catalog/tree -> index out of range [2]
  - https://catalog.gitlab.com/kubevela/foo  -> index out of range [1]

Add length/empty guards that return the existing invalid-format error
(errInvalidFormatMsg, consistent with Parse) for the empty owner slice, the
short host split, and the missing tree branch segment. Behaviour for all
currently-valid inputs is unchanged.

Also fix the caller NewAsyncReader, which dereferenced the returned content
before checking the error: on any ParseGitlab error content is nil, so the
assignment nil-panicked before the error could be returned. Move the error
check above the dereference.

Extend TestParseGitlab with the three malformed cases (wantErr).

Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
2026-07-15 11:16:48 +01:00

265 lines
6.6 KiB
Go

/*
Copyright 2021 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"fmt"
"net/url"
"strings"
"github.com/pkg/errors"
)
// TypeLocal represents github
const TypeLocal = "local"
// TypeOss represent oss
const TypeOss = "oss"
// TypeGithub represents github
const TypeGithub = "github"
// TypeGitee represents gitee
const TypeGitee = "gitee"
// TypeGitlab represents gitlab
const TypeGitlab = "gitlab"
// TypeUnknown represents parse failed
const TypeUnknown = "unknown"
// errInvalidFormatMsg with the message to be returned in case of a format error.
const errInvalidFormatMsg = "invalid format "
// Content contains different type of content needed when building Registry
type Content struct {
OssContent
GithubContent
GiteeContent
GitlabContent
LocalContent
}
// LocalContent for local registry
type LocalContent struct {
AbsDir string `json:"abs_dir"`
}
// OssContent for oss registry
type OssContent struct {
EndPoint string `json:"bucket_url"`
Bucket string `json:"bucket"`
}
// GithubContent for cap center
type GithubContent struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
Path string `json:"path"`
Ref string `json:"ref"`
}
// GiteeContent for cap center
type GiteeContent struct {
Owner string `json:"gitee_owner"`
Repo string `json:"gitee_repo"`
Path string `json:"gitee_path"`
Ref string `json:"gitee_ref"`
}
// GitlabContent for cap center
type GitlabContent struct {
Host string `json:"gitlab_host"`
Owner string `json:"gitlab_owner"`
Repo string `json:"gitlab_repo"`
Path string `json:"gitlab_path"`
Ref string `json:"gitlab_ref"`
PId int `json:"gitlab_pid"`
}
// Parse will parse config from address
func Parse(addr string) (string, *Content, error) {
URL, err := url.Parse(addr)
if err != nil {
return "", nil, err
}
l := strings.Split(strings.TrimPrefix(URL.Path, "/"), "/")
switch URL.Scheme {
case "http", "https":
switch URL.Host {
case "github.com":
// We support two valid format:
// 1. https://github.com/<owner>/<repo>/tree/<branch>/<path-to-dir>
// 2. https://github.com/<owner>/<repo>/<path-to-dir>
if len(l) < 3 {
return "", nil, errors.New(errInvalidFormatMsg + addr)
}
if l[2] == "tree" {
// https://github.com/<owner>/<repo>/tree/<branch>/<path-to-dir>
if len(l) < 5 {
return "", nil, errors.New(errInvalidFormatMsg + addr)
}
return TypeGithub, &Content{
GithubContent: GithubContent{
Owner: l[0],
Repo: l[1],
Path: strings.Join(l[4:], "/"),
Ref: l[3],
},
}, nil
}
// https://github.com/<owner>/<repo>/<path-to-dir>
return TypeGithub, &Content{
GithubContent: GithubContent{
Owner: l[0],
Repo: l[1],
Path: strings.Join(l[2:], "/"),
Ref: "", // use default branch
},
},
nil
case "api.github.com":
if len(l) != 5 {
return "", nil, errors.New(errInvalidFormatMsg + addr)
}
//https://api.github.com/repos/<owner>/<repo>/contents/<path-to-dir>
return TypeGithub, &Content{
GithubContent: GithubContent{
Owner: l[1],
Repo: l[2],
Path: l[4],
Ref: URL.Query().Get("ref"),
},
},
nil
case "gitee.com":
// We support two valid format:
// 1. https://gitee.com/<owner>/<repo>/tree/<branch>/<path-to-dir>
// 2. https://gitee.com/<owner>/<repo>/<path-to-dir>
if len(l) < 3 {
return "", nil, errors.New(errInvalidFormatMsg + addr)
}
switch l[2] {
case "tree":
// https://gitee.com/<owner>/<repo>/tree/<branch>/<path-to-dir>
if len(l) < 5 {
return "", nil, errors.New(errInvalidFormatMsg + addr)
}
return TypeGitee, &Content{
GiteeContent: GiteeContent{
Owner: l[0],
Repo: l[1],
Path: strings.Join(l[4:], "/"),
Ref: l[3],
},
}, nil
default:
// https://gitee.com/<owner>/<repo>/<path-to-dir>
return TypeGitee, &Content{
GiteeContent: GiteeContent{
Owner: l[0],
Repo: l[1],
Path: strings.Join(l[2:], "/"),
Ref: "", // use default branch
},
}, nil
}
default:
return "", nil, fmt.Errorf("git type repository only support github for now")
}
case "oss":
return TypeOss, &Content{
OssContent: OssContent{
EndPoint: URL.Host,
Bucket: URL.Path,
},
}, nil
case "file":
return TypeLocal, &Content{
LocalContent: LocalContent{
AbsDir: URL.Path,
},
}, nil
}
return TypeUnknown, nil, nil
}
// ByteCountIEC convert number of bytes into readable string
// borrowed from https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/
func ByteCountIEC(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB",
float64(b)/float64(div), "KMGTPE"[exp])
}
// ParseGitlab will parse gitlab config from address
func ParseGitlab(addr, repo string) (string, *Content, error) {
if !strings.Contains(addr, repo) {
return "", nil, errors.New("addon registry repo name invalid")
}
// We support two valid format:
// 1. https://example.gitlab.com/<owner>/<repo>
// 2. https://example.gitlab.com/<owner>/<repo>/tree/<branch>
URL, err := url.Parse(addr)
if err != nil {
return "", nil, err
}
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/<owner>/<repo>
return TypeGitlab, &Content{
GitlabContent: GitlabContent{
Host: URL.Scheme + "://" + URL.Host,
Owner: owner[1][:len(owner[1])-1],
Repo: repo,
Ref: "", // use default branch
},
}, nil
}
// https://example.gitlab.com/<owner>/<repo>/tree/<branch>
l := strings.Split(arr[1], "/")
if len(l) < 3 {
return "", nil, errors.New(errInvalidFormatMsg + addr)
}
return TypeGitlab, &Content{
GitlabContent: GitlabContent{
Host: URL.Scheme + "://" + URL.Host,
Owner: owner[1][:len(owner[1])-1],
Repo: repo,
Ref: l[2],
},
}, nil
}