Skip to content

Commit 0f07b7f

Browse files
authoredJun 28, 2023
fix: invalid environment variable (#271)
* fix: invalid environment variable Check the validity of environment variables when splitting variables by `=`. Ignore environment variables that don't include an `=` sign. Signed-off-by: Ayman Bagabas <ayman.bagabas@gmail.com> * Update env_tomap_windows.go * Update env_tomap_windows_test.go --------- Signed-off-by: Ayman Bagabas <ayman.bagabas@gmail.com>
1 parent 7a25c17 commit 0f07b7f

4 files changed

+10
-4
lines changed
 

‎env_tomap.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ func toMap(env []string) map[string]string {
88
r := map[string]string{}
99
for _, e := range env {
1010
p := strings.SplitN(e, "=", 2)
11-
r[p[0]] = p[1]
11+
if len(p) == 2 {
12+
r[p[0]] = p[1]
13+
}
1214
}
1315
return r
1416
}

‎env_tomap_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ package env
55
import "testing"
66

77
func TestUnix(t *testing.T) {
8-
envVars := []string{":=/test/unix", "PATH=:/test_val1:/test_val2", "VAR=REGULARVAR"}
8+
envVars := []string{":=/test/unix", "PATH=:/test_val1:/test_val2", "VAR=REGULARVAR", "FOO=", "BAR"}
99
result := toMap(envVars)
1010
isEqual(t, map[string]string{
1111
":": "/test/unix",
1212
"PATH": ":/test_val1:/test_val2",
1313
"VAR": "REGULARVAR",
14+
"FOO": "",
1415
}, result)
1516
}

‎env_tomap_windows.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ func toMap(env []string) map[string]string {
2121
p[0] = "=" + p[0]
2222
}
2323

24-
r[p[0]] = p[1]
24+
if len(p) == 2 {
25+
r[p[0]] = p[1]
26+
}
2527
}
2628
return r
2729
}

‎env_tomap_windows_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import "testing"
77
// On Windows, environment variables can start with '='. This test verifies this behavior without relying on a Windows environment.
88
// See env_windows.go in the Go source: https://github.com/golang/go/blob/master/src/syscall/env_windows.go#L58
99
func TestToMapWindows(t *testing.T) {
10-
envVars := []string{"=::=::\\", "=C:=C:\\test", "VAR=REGULARVAR"}
10+
envVars := []string{"=::=::\\", "=C:=C:\\test", "VAR=REGULARVAR", "FOO=", "BAR"}
1111
result := toMap(envVars)
1212
isEqual(t, map[string]string{
1313
"=::": "::\\",
1414
"=C:": "C:\\test",
1515
"VAR": "REGULARVAR",
16+
"FOO": "",
1617
}, result)
1718
}

0 commit comments

Comments
 (0)
Please sign in to comment.