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

Disallow invalid identifiers as environment keys #53

Merged
merged 2 commits into from
Jul 10, 2023

Conversation

sambostock
Copy link
Contributor

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'

Copy link
Contributor Author

@sambostock sambostock left a 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.

Comment on lines +32 to +37
// 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
}
Copy link
Contributor Author

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`)
Copy link
Contributor Author

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

Suggested change
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'
@sambostock sambostock merged commit 4c2ebb7 into main Jul 10, 2023
@sambostock sambostock deleted the validate-env-var-names branch July 10, 2023 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants