61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
// Package validators provides input validation for secret names
|
|
package validators
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
const SecretNameMaxLength = 255
|
|
|
|
var SecretNamePattern = regexp.MustCompile(`^[\p{L}0-9]([\p{L}0-9 _\-\\./]*[\p{L}0-9])?$`)
|
|
|
|
func IsValidSecretName(name string) bool {
|
|
if len(name) == 0 || len(name) > SecretNameMaxLength {
|
|
return false
|
|
}
|
|
|
|
if strings.Contains(name, "..") || strings.Contains(name, "\x00") {
|
|
return false
|
|
}
|
|
|
|
// Allow UTF-8 (incl. Cyrillic) secret names; only reject control chars.
|
|
for _, ch := range name {
|
|
if ch < 32 || ch == 127 {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return SecretNamePattern.MatchString(name)
|
|
}
|
|
|
|
func SanitizeSecretName(name string) (string, bool) {
|
|
name = strings.TrimSpace(name)
|
|
|
|
cleaned := strings.Map(func(r rune) rune {
|
|
if r >= 32 && r <= 126 {
|
|
return r
|
|
}
|
|
return -1
|
|
}, name)
|
|
|
|
if IsValidSecretName(cleaned) {
|
|
return cleaned, true
|
|
}
|
|
|
|
return "", false
|
|
}
|
|
|
|
func IsValidFilterQueryValue(s string) bool {
|
|
s = strings.TrimSpace(s)
|
|
if s == "" || len(s) > SecretNameMaxLength {
|
|
return false
|
|
}
|
|
for _, r := range s {
|
|
if r < 32 || r == 127 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|