-
Notifications
You must be signed in to change notification settings - Fork 15
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
Disallow invalid identifiers as environment
keys
#53
Conversation
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.
I included changes to the fixture names, as I felt it made it clearer what was going on.
// Reject keys that would be invalid environment variable identifiers | ||
if !validIdentifierPattern.MatchString(key) { | ||
err := fmt.Errorf("invalid identifier as key in environment: %q", key) | ||
|
||
return nil, err | ||
} |
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.
According to the comment below, we seem to ignore when the values don't convert to strings properly, rather than return an error. IMO we should be loud about these failures (I think non-strings should also be errors, or at least print something to stderr), however if we really think it's better to silently continue, then we could change this to "only export keys that are valid identifiers" (and simply ignore invalid ones).
Separately, I may open a PR to add said stderr message (since making it an error would be a breaking change that could break production deployments).
) | ||
|
||
var errNoEnv = errors.New("environment is not set in ejson") | ||
var errEnvNotMap = errors.New("environment is not a map[string]interface{}") | ||
|
||
var validIdentifierPattern = regexp.MustCompile(`\A[a-zA-Z_][a-zA-Z0-9_]*\z`) |
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.
This would also work, and would be shorter
var validIdentifierPattern = regexp.MustCompile(`\A[a-zA-Z_][a-zA-Z0-9_]*\z`) | |
var validIdentifierPattern = regexp.MustCompile(`\A[a-zA-Z_]\w*\z`) |
but after thinking about it, I think the long form is clearer, as it explicitly shows how the first character can't be a digit, but that otherwise it's all the same.
Since the keys under `environment` will end up as the identifiers in `export` lines, we should ensure they are valid identifiers. export not valid=value export worse; echo dangerous; _='uh oh'
3c6e08d
to
8de5507
Compare
Since the keys under
environment
will end up as the identifiers inexport
lines, we should ensure they are valid identifiers.