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: 更改配置项路径校验规则 #3562

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 8 additions & 32 deletions bcs-services/bcs-bscp/pkg/criteria/validator/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,25 @@ import (
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/kit"
)

// validUnixFileSubPathRegexp sub path support character:
// chinese, english, number, '-', '_', '#', '%', ',', '@', '^', '+', '=', '[', ']', '{', '}, '.'
var validUnixFileSubPathRegexp = regexp.MustCompile("^[\u4e00-\u9fa5A-Za-z0-9-_#%,.@^+=\\[\\]{}]+$")

// ValidateUnixFilePath validate unix os file path.
func ValidateUnixFilePath(kit *kit.Kit, path string) error {
if len(path) < 1 {
return errf.Errorf(errf.InvalidArgument, i18n.T(kit, "invalid path, length should >= 1"))
return errf.Errorf(errf.InvalidArgument, i18n.T(kit, "invalid path %s, length should >= 1", path))
}

if len(path) > 1024 {
return errf.Errorf(errf.InvalidArgument, i18n.T(kit, "invalid path, length should <= 1024"))
return errf.Errorf(errf.InvalidArgument, i18n.T(kit, "invalid path %s, length should <= 1024", path))
}

// 1. should start with '/'
// 1. 检查是否以 '/' 开头
if !strings.HasPrefix(path, "/") {
return errf.Errorf(errf.InvalidArgument, i18n.T(kit, "invalid path, should start with '/'"))
}

// Split the path into parts
parts := strings.Split(path, "/")[1:] // Ignore the first empty part due to the leading '/'

if strings.HasSuffix(path, "/") {
parts = parts[:len(parts)-1] // Ignore the last empty part due to the trailing '/'
return errf.Errorf(errf.InvalidArgument, i18n.T(kit, "invalid path %s, the path must start with '/'", path))
}

// Iterate over each part to validate
for _, part := range parts {

// 2. the verification path cannot all be '{'. '}'
if dotsRegexp.MatchString(part) {
return errf.Errorf(errf.InvalidArgument, i18n.T(kit, "invalid path %s, path cannot all be '.' ", part))
}

// 3. each sub path support character:
// chinese, english, number, '-', '_', '#', '%', ',', '@', '^', '+', '=', '[', ']', '{', '}'
if !validUnixFileSubPathRegexp.MatchString(part) {
return errf.Errorf(errf.InvalidArgument, i18n.T(kit, fmt.Sprintf(`invalid path, each sub path should only
contain chinese, english, number, '-', '_', '#', '%%', ',', '@', '^', '+', '=', '[', ']', '{', '}', '{'. '}`)))
}

// 4. each sub path should be separated by '/'
// (handled by strings.Split above)
// 2. 检查是否包含连续的 '/'
if strings.Contains(path, "//") {
return errf.Errorf(errf.InvalidArgument, i18n.T(kit, "invalid path %s, the path"+
"cannot contain consecutive '/'", path))
}

return nil
Expand Down
8 changes: 3 additions & 5 deletions bcs-services/bcs-bscp/pkg/criteria/validator/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ func ValidateReleaseName(kit *kit.Kit, name string) error {
}

// qualifiedFileNameRegexp file name regexp.
// support character: chinese, english, number,
// '-', '_', '#', '%', ',', '@', '^', '+', '=', '[', ']', '{', '}, '.'
var qualifiedFileNameRegexp = regexp.MustCompile("^[\u4e00-\u9fa5A-Za-z0-9-_#%,.@^+=\\[\\]\\{\\}]+$")
// 文件名不能包含/
var qualifiedFileNameRegexp = regexp.MustCompile("^[^/]+$")

var dotsRegexp = regexp.MustCompile(`^\.+$`)

Expand All @@ -202,8 +201,7 @@ func ValidateFileName(kit *kit.Kit, name string) error {
}

if !qualifiedFileNameRegexp.MatchString(name) {
return errf.Errorf(errf.InvalidArgument, i18n.T(kit, fmt.Sprintf(`invalid name %s, should only contains chinese,
english, number, '-', '_', '#', '%%', ',', '@', '^', '+', '=', '[', ']', '{', '}', '.'`, name)))
return errf.Errorf(errf.InvalidArgument, i18n.T(kit, "invalid name %s, there can't be any /", name))
}

return nil
Expand Down
Loading
Loading