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

Validate env var names before writing env var files #365

Merged
merged 4 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/environment_writer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package internal

import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)

type EnvironmentWriter struct{}
Expand All @@ -21,7 +24,15 @@ func (w EnvironmentWriter) Write(dir string, env map[string]string) error {
return err
}

// this regex checks that map keys contain valid env var name characters,
// per https://pubs.opengroup.org/onlinepubs/9699919799/
validEnvVarRegex := regexp.MustCompile(`^[a-zA-Z_]{1,}[a-zA-Z0-9_]*$`)

for key, value := range env {
parts := strings.SplitN(key, ".", 2)
if !validEnvVarRegex.MatchString(parts[0]) {
return fmt.Errorf("invalid environment variable name '%s'", parts[0])
}
err := os.WriteFile(filepath.Join(dir, key), []byte(value), 0644)
if err != nil {
return err
Expand Down
30 changes: 22 additions & 8 deletions internal/environment_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,23 @@ func testEnvironmentWriter(t *testing.T, context spec.G, it spec.S) {

it("writes the given environment to a directory", func() {
err := writer.Write(tmpDir, map[string]string{
"some-name": "some-content",
"other-name": "other-content",
"some_name": "some-content",
"OTHER_NAME": "other-content",
"ANOTHER.override": "more-content",
})
Expect(err).NotTo(HaveOccurred())

content, err := os.ReadFile(filepath.Join(tmpDir, "some-name"))
content, err := os.ReadFile(filepath.Join(tmpDir, "some_name"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("some-content"))

content, err = os.ReadFile(filepath.Join(tmpDir, "other-name"))
content, err = os.ReadFile(filepath.Join(tmpDir, "OTHER_NAME"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("other-content"))

content, err = os.ReadFile(filepath.Join(tmpDir, "ANOTHER.override"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("more-content"))
})

it("writes does not create a directory of the env map is empty", func() {
Expand All @@ -64,8 +69,8 @@ func testEnvironmentWriter(t *testing.T, context spec.G, it spec.S) {

it("returns an error", func() {
err := writer.Write(filepath.Join(tmpDir, "sub-dir"), map[string]string{
"some-name": "some-content",
"other-name": "other-content",
"some_name": "some-content",
"OTHER_NAME": "other-content",
})
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
Expand All @@ -78,11 +83,20 @@ func testEnvironmentWriter(t *testing.T, context spec.G, it spec.S) {

it("returns an error", func() {
err := writer.Write(tmpDir, map[string]string{
"some-name": "some-content",
"other-name": "other-content",
"some_name": "some-content",
"OTHER_NAME": "other-content",
})
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})

context("when env var name is invalid", func() {
it("returns an error", func() {
err := writer.Write(tmpDir, map[string]string{
"INVA=*LID.override": "more-content",
})
Expect(err).To(MatchError(ContainSubstring("invalid environment variable name 'INVA=*LID'")))
})
})
})
}