Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 3d98989

Browse files
committed
Fix run and inject when env contains invalid value
On Windows the environment can contain key-value pair that is not valid according to the standard. Run and inject used to fail on this because they try to validate every individual key in the environment. When one is incorrect, they error. E.g., I encountered the following line in os.Environ() on Windows: =::=::\ This commits fixes this problem by ignoring any invalid lines in the environment for injection purposes, but appending them to the environment of the child process in case of a run.
1 parent 046578c commit 3d98989

3 files changed

Lines changed: 28 additions & 12 deletions

File tree

internals/secrethub/inject.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ func (cmd *InjectCommand) Run() error {
9898

9999
templateVars := make(map[string]string)
100100

101-
osEnv, err := parseKeyValueStringsToMap(os.Environ())
102-
if err != nil {
103-
return err
104-
}
101+
osEnv, _ := parseKeyValueStringsToMap(os.Environ())
105102

106103
for k, v := range osEnv {
107104
if strings.HasPrefix(k, templateVarEnvVarPrefix) {

internals/secrethub/run.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (cmd *RunCommand) Run() error {
121121
}
122122
}
123123

124-
osEnv, err := parseKeyValueStringsToMap(os.Environ())
124+
osEnv, passthroughEnv := parseKeyValueStringsToMap(os.Environ())
125125
if err != nil {
126126
return err
127127
}
@@ -239,7 +239,7 @@ func (cmd *RunCommand) Run() error {
239239
maskedStderr := masker.NewMaskedWriter(os.Stderr, valuesToMask, maskString, cmd.maskingTimeout)
240240

241241
command := exec.Command(cmd.command[0], cmd.command[1:]...)
242-
command.Env = mapToKeyValueStrings(environment)
242+
command.Env = append(passthroughEnv, mapToKeyValueStrings(environment)...)
243243
command.Stdin = os.Stdin
244244
if cmd.noMasking {
245245
command.Stdout = os.Stdout
@@ -323,8 +323,9 @@ func mapToKeyValueStrings(pairs map[string]string) []string {
323323
// parseKeyValueStringsToMap converts a slice of "key=value" strings to a
324324
// map of "key":"value" pairs. When duplicate keys occur, the last value is
325325
// used.
326-
func parseKeyValueStringsToMap(values []string) (map[string]string, error) {
327-
result := make(map[string]string)
326+
func parseKeyValueStringsToMap(values []string) (map[string]string, []string) {
327+
parsedLines := make(map[string]string)
328+
var unparsableLines []string
328329
for _, kv := range values {
329330
split := strings.SplitN(kv, "=", 2)
330331
key := strings.TrimSpace(split[0])
@@ -335,13 +336,13 @@ func parseKeyValueStringsToMap(values []string) (map[string]string, error) {
335336

336337
err := validation.ValidateEnvarName(key)
337338
if err != nil {
338-
return nil, err
339+
unparsableLines = append(unparsableLines, kv)
340+
} else {
341+
parsedLines[key] = value
339342
}
340-
341-
result[key] = value
342343
}
343344

344-
return result, nil
345+
return parsedLines, unparsableLines
345346
}
346347

347348
// EnvSource defines a method of reading environment variables from a source.

internals/secrethub/run_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,21 @@ func TestTrimQuotes(t *testing.T) {
657657
})
658658
}
659659
}
660+
661+
func Test_parseKeyValueStringsToMap(t *testing.T) {
662+
input := []string{
663+
"A=B",
664+
"B",
665+
"=::=::\\",
666+
}
667+
668+
parsableValues, unparsableValues := parseKeyValueStringsToMap(input)
669+
670+
assert.Equal(t, parsableValues, map[string]string{
671+
"A": "B",
672+
"B": "",
673+
})
674+
assert.Equal(t, unparsableValues, []string{
675+
"=::=::\\",
676+
})
677+
}

0 commit comments

Comments
 (0)