Skip to content

Commit 0c29f84

Browse files
Merge pull request #123385 from HirazawaUi/allow-special-characters
Allow almost all printable ASCII characters in environment variables Kubernetes-commit: 87f9b3891e7566aa085645aac4e5e3b4379b4efd
2 parents 60d24f2 + d4f2d34 commit 0c29f84

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

pkg/util/validation/validation.go

+22
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"math"
2222
"regexp"
2323
"strings"
24+
"unicode"
2425

2526
"k8s.io/apimachinery/pkg/util/validation/field"
2627
netutils "k8s.io/utils/net"
@@ -418,6 +419,9 @@ func IsHTTPHeaderName(value string) []string {
418419
const envVarNameFmt = "[-._a-zA-Z][-._a-zA-Z0-9]*"
419420
const envVarNameFmtErrMsg string = "a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.', and must not start with a digit"
420421

422+
// TODO(hirazawaui): Rename this when the RelaxedEnvironmentVariableValidation gate is removed.
423+
const relaxedEnvVarNameFmtErrMsg string = "a valid environment variable names must be printable ASCII characters other than '=' character"
424+
421425
var envVarNameRegexp = regexp.MustCompile("^" + envVarNameFmt + "$")
422426

423427
// IsEnvVarName tests if a string is a valid environment variable name.
@@ -431,6 +435,24 @@ func IsEnvVarName(value string) []string {
431435
return errs
432436
}
433437

438+
// IsRelaxedEnvVarName tests if a string is a valid environment variable name.
439+
func IsRelaxedEnvVarName(value string) []string {
440+
var errs []string
441+
442+
if len(value) == 0 {
443+
errs = append(errs, "environment variable name"+EmptyError())
444+
}
445+
446+
for _, r := range value {
447+
if r > unicode.MaxASCII || !unicode.IsPrint(r) || r == '=' {
448+
errs = append(errs, relaxedEnvVarNameFmtErrMsg)
449+
break
450+
}
451+
}
452+
453+
return errs
454+
}
455+
434456
const configMapKeyFmt = `[-._a-zA-Z0-9]+`
435457
const configMapKeyErrMsg string = "a valid config key must consist of alphanumeric characters, '-', '_' or '.'"
436458

pkg/util/validation/validation_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,30 @@ func TestIsDomainPrefixedPath(t *testing.T) {
904904
}
905905
}
906906
}
907+
908+
func TestIsRelaxedEnvVarName(t *testing.T) {
909+
goodValues := []string{
910+
"-", ":", "_", "+a", ">a", "<a",
911+
"a.", "a..", "*a", "%a", "?a",
912+
"a:a", "a_a", "aAz", "~a", "|a",
913+
"a0a", "a9", "/a", "a ", "#a",
914+
"0a", "0 a", "'a", "(a", "@a",
915+
}
916+
for _, val := range goodValues {
917+
if msgs := IsRelaxedEnvVarName(val); len(msgs) != 0 {
918+
t.Errorf("expected true for '%s': %v", val, msgs)
919+
}
920+
}
921+
922+
badValues := []string{
923+
"", "=", "a=", "1=a", "a=b", "#%=&&",
924+
string(rune(1)) + "abc", string(rune(130)) + "abc",
925+
"Ç ç", "Ä ä", "Ñ ñ", "Ø ø",
926+
}
927+
928+
for _, val := range badValues {
929+
if msgs := IsRelaxedEnvVarName(val); len(msgs) == 0 {
930+
t.Errorf("expected false for '%s'", val)
931+
}
932+
}
933+
}

0 commit comments

Comments
 (0)