Skip to content

Commit 119d1dd

Browse files
authored
Merge pull request #3885 from zyfy29/feat-warning-with-both-allow-block
feat: add warning message for both blocklist and allowlist are set
2 parents cd685e5 + db733b7 commit 119d1dd

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

pkg/envutil/envutil.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,27 @@ var defaultBlockList = []string{
4242
"_*", // Variables starting with underscore are typically internal
4343
}
4444

45-
func getBlockList() []string {
45+
// getBlockList returns the list of environment variable patterns to be blocked.
46+
// The second return value indicates whether the list was explicitly set via LIMA_SHELLENV_BLOCK.
47+
func getBlockList() ([]string, bool) {
4648
blockEnv := os.Getenv("LIMA_SHELLENV_BLOCK")
4749
if blockEnv == "" {
48-
return defaultBlockList
50+
return defaultBlockList, false
4951
}
5052
after, found := strings.CutPrefix(blockEnv, "+")
5153
if !found {
52-
return parseEnvList(blockEnv)
54+
return parseEnvList(blockEnv), true
5355
}
54-
return slices.Concat(defaultBlockList, parseEnvList(after))
56+
return slices.Concat(defaultBlockList, parseEnvList(after)), true
5557
}
5658

57-
func getAllowList() []string {
59+
// getAllowList returns the list of environment variable patterns to be allowed.
60+
// The second return value indicates whether the list was explicitly set via LIMA_SHELLENV_ALLOW.
61+
func getAllowList() ([]string, bool) {
5862
if allowEnv := os.Getenv("LIMA_SHELLENV_ALLOW"); allowEnv != "" {
59-
return parseEnvList(allowEnv)
63+
return parseEnvList(allowEnv), true
6064
}
61-
return nil
65+
return nil, false
6266
}
6367

6468
func parseEnvList(envList string) []string {
@@ -92,10 +96,17 @@ func matchesAnyPattern(name string, patterns []string) bool {
9296
// It returns a slice of environment variables that are not blocked by the current configuration.
9397
// The filtering is controlled by LIMA_SHELLENV_BLOCK and LIMA_SHELLENV_ALLOW environment variables.
9498
func FilterEnvironment() []string {
99+
allowList, isAllowListSet := getAllowList()
100+
blockList, isBlockListSet := getBlockList()
101+
102+
if isBlockListSet && isAllowListSet {
103+
logrus.Warn("Both LIMA_SHELLENV_BLOCK and LIMA_SHELLENV_ALLOW are set. Block list will be ignored.")
104+
blockList = nil
105+
}
95106
return filterEnvironmentWithLists(
96107
os.Environ(),
97-
getAllowList(),
98-
getBlockList(),
108+
allowList,
109+
blockList,
99110
)
100111
}
101112

pkg/envutil/envutil_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ func TestGetBlockAndAllowLists(t *testing.T) {
8888
t.Setenv("LIMA_SHELLENV_BLOCK", "")
8989
t.Setenv("LIMA_SHELLENV_ALLOW", "")
9090

91-
blockList := getBlockList()
92-
allowList := getAllowList()
91+
blockList, isBlockListSet := getBlockList()
92+
allowList, isAllowListSet := getAllowList()
9393

94+
assert.Assert(t, !isBlockListSet)
95+
assert.Assert(t, !isAllowListSet)
9496
assert.Assert(t, isUsingDefaultBlockList())
9597
assert.DeepEqual(t, blockList, defaultBlockList)
9698
assert.Equal(t, len(allowList), 0)
@@ -99,7 +101,8 @@ func TestGetBlockAndAllowLists(t *testing.T) {
99101
t.Run("custom blocklist", func(t *testing.T) {
100102
t.Setenv("LIMA_SHELLENV_BLOCK", "PATH,HOME")
101103

102-
blockList := getBlockList()
104+
blockList, isSet := getBlockList()
105+
assert.Assert(t, isSet)
103106
assert.Assert(t, !isUsingDefaultBlockList())
104107
expected := []string{"PATH", "HOME"}
105108
assert.DeepEqual(t, blockList, expected)
@@ -108,7 +111,8 @@ func TestGetBlockAndAllowLists(t *testing.T) {
108111
t.Run("additive blocklist", func(t *testing.T) {
109112
t.Setenv("LIMA_SHELLENV_BLOCK", "+CUSTOM_VAR")
110113

111-
blockList := getBlockList()
114+
blockList, isSet := getBlockList()
115+
assert.Assert(t, isSet)
112116
assert.Assert(t, isUsingDefaultBlockList())
113117
expected := slices.Concat(GetDefaultBlockList(), []string{"CUSTOM_VAR"})
114118
assert.DeepEqual(t, blockList, expected)
@@ -117,7 +121,8 @@ func TestGetBlockAndAllowLists(t *testing.T) {
117121
t.Run("allowlist", func(t *testing.T) {
118122
t.Setenv("LIMA_SHELLENV_ALLOW", "FOO,BAR")
119123

120-
allowList := getAllowList()
124+
allowList, isSet := getAllowList()
125+
assert.Assert(t, isSet)
121126
expected := []string{"FOO", "BAR"}
122127
assert.DeepEqual(t, allowList, expected)
123128
})

0 commit comments

Comments
 (0)