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
Revert environment regexp in opts #16608
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,18 +4,22 @@ import ( | |
"bufio" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var ( | ||
// EnvironmentVariableRegexp is a regexp to validate correct environment variables | ||
// Environment variables set by the user must have a name consisting solely of | ||
// alphabetics, numerics, and underscores - the first of which must not be numeric. | ||
EnvironmentVariableRegexp = regexp.MustCompile("^[[:alpha:]_][[:alpha:][:digit:]_]*$") | ||
) | ||
|
||
// ParseEnvFile reads a file with environment variables enumerated by lines | ||
// | ||
// ``Environment variable names used by the utilities in the Shell and | ||
// Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase | ||
// letters, digits, and the '_' (underscore) from the characters defined in | ||
// Portable Character Set and do not begin with a digit. *But*, other | ||
// characters may be permitted by an implementation; applications shall | ||
// tolerate the presence of such names.'' | ||
// -- http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html | ||
// | ||
// As of #16585, it's up to application inside docker to validate or not | ||
// environment variables, that's why we just strip leading whitespace and | ||
// nothing more. | ||
func ParseEnvFile(filename string) ([]string, error) { | ||
fh, err := os.Open(filename) | ||
if err != nil { | ||
|
@@ -31,11 +35,13 @@ func ParseEnvFile(filename string) ([]string, error) { | |
// line is not empty, and not starting with '#' | ||
if len(line) > 0 && !strings.HasPrefix(line, "#") { | ||
data := strings.SplitN(line, "=", 2) | ||
variable := data[0] | ||
|
||
if !EnvironmentVariableRegexp.MatchString(variable) { | ||
return []string{}, ErrBadEnvVariable{fmt.Sprintf("variable '%s' is not a valid environment variable", variable)} | ||
// trim the front of a variable, but nothing else | ||
variable := strings.TrimLeft(data[0], whiteSpaces) | ||
if strings.ContainsAny(variable, whiteSpaces) { | ||
return []string{}, ErrBadEnvVariable{fmt.Sprintf("variable '%s' has white spaces", variable)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why trim whitespace? It's technically allowed to have an env var with leading whitespace. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @phemmer it's how it was done before the regexp so I reverted to it. But yeah.. and in ValidateEnv we do not trim leading space so.. there is something to do.. (probably removing this trim) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's leave this here. I'd rather have the previous behavior, that seemed to work as expected, than a new different behavior. |
||
} | ||
|
||
if len(data) > 1 { | ||
|
||
// pass the value through, no trimming | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps you should add some of the discussion in this PR (and the linked issue) in a comment (and in the commit message) to explain why we're not validating that the characters are valid for environment vars. (also to prevent someone to re-introduce the regex in future)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thaJeztah true 😝
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and done :)