|
| 1 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package envutil |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | +) |
| 12 | + |
| 13 | +// BuiltinBlocklist contains environment variables that should not be propagated by default. |
| 14 | +var BuiltinBlocklist = []string{ |
| 15 | + "BASH*", |
| 16 | + "DISPLAY", |
| 17 | + "DYLD_*", |
| 18 | + "EUID", |
| 19 | + "FPATH", |
| 20 | + "GID", |
| 21 | + "GROUP", |
| 22 | + "HOME", |
| 23 | + "HOSTNAME", |
| 24 | + "LD_*", |
| 25 | + "LOGNAME", |
| 26 | + "OLDPWD", |
| 27 | + "PATH", |
| 28 | + "PWD", |
| 29 | + "SHELL", |
| 30 | + "SHLVL", |
| 31 | + "SSH_*", |
| 32 | + "TERM", |
| 33 | + "TERMINFO", |
| 34 | + "TMPDIR", |
| 35 | + "UID", |
| 36 | + "USER", |
| 37 | + "XAUTHORITY", |
| 38 | + "XDG_*", |
| 39 | + "ZDOTDIR", |
| 40 | + "ZSH*", |
| 41 | + "_*", // Variables starting with underscore are typically internal |
| 42 | +} |
| 43 | + |
| 44 | +func getBlockList() []string { |
| 45 | + if blockEnv := os.Getenv("LIMA_SHELLENV_BLOCK"); blockEnv != "" { |
| 46 | + if strings.HasPrefix(blockEnv, "+") { |
| 47 | + additionalBlocks := parseEnvList(blockEnv[1:]) |
| 48 | + blockList := make([]string, len(BuiltinBlocklist)+len(additionalBlocks)) |
| 49 | + copy(blockList, BuiltinBlocklist) |
| 50 | + copy(blockList[len(BuiltinBlocklist):], additionalBlocks) |
| 51 | + return blockList |
| 52 | + } |
| 53 | + return parseEnvList(blockEnv) |
| 54 | + } |
| 55 | + return BuiltinBlocklist |
| 56 | +} |
| 57 | + |
| 58 | +func getAllowList() []string { |
| 59 | + if allowEnv := os.Getenv("LIMA_SHELLENV_ALLOW"); allowEnv != "" { |
| 60 | + return parseEnvList(allowEnv) |
| 61 | + } |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func IsUsingBuiltinBlocklist() bool { |
| 66 | + blockEnv := os.Getenv("LIMA_SHELLENV_BLOCK") |
| 67 | + return blockEnv == "" || strings.HasPrefix(blockEnv, "+") |
| 68 | +} |
| 69 | + |
| 70 | +func parseEnvList(envList string) []string { |
| 71 | + if envList == "" { |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + parts := strings.Split(envList, ",") |
| 76 | + result := make([]string, 0, len(parts)) |
| 77 | + for _, part := range parts { |
| 78 | + if trimmed := strings.TrimSpace(part); trimmed != "" { |
| 79 | + result = append(result, trimmed) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return result |
| 84 | +} |
| 85 | + |
| 86 | +func matchesPattern(name, pattern string) bool { |
| 87 | + if pattern == name { |
| 88 | + return true |
| 89 | + } |
| 90 | + |
| 91 | + if strings.HasSuffix(pattern, "*") { |
| 92 | + prefix := pattern[:len(pattern)-1] |
| 93 | + return strings.HasPrefix(name, prefix) |
| 94 | + } |
| 95 | + |
| 96 | + return false |
| 97 | +} |
| 98 | + |
| 99 | +func matchesAnyPattern(name string, patterns []string) bool { |
| 100 | + for _, pattern := range patterns { |
| 101 | + if matchesPattern(name, pattern) { |
| 102 | + return true |
| 103 | + } |
| 104 | + } |
| 105 | + return false |
| 106 | +} |
| 107 | + |
| 108 | +func FilterEnvironment() []string { |
| 109 | + return FilterEnvironmentWithLists(os.Environ(), getBlockList(), getAllowList()) |
| 110 | +} |
| 111 | + |
| 112 | +func FilterEnvironmentWithLists(env, blockList, allowList []string) []string { |
| 113 | + var filtered []string |
| 114 | + |
| 115 | + for _, envVar := range env { |
| 116 | + parts := strings.SplitN(envVar, "=", 2) |
| 117 | + if len(parts) != 2 { |
| 118 | + continue |
| 119 | + } |
| 120 | + |
| 121 | + name := parts[0] |
| 122 | + |
| 123 | + if len(allowList) > 0 { |
| 124 | + if !matchesAnyPattern(name, allowList) { |
| 125 | + continue |
| 126 | + } |
| 127 | + filtered = append(filtered, envVar) |
| 128 | + continue |
| 129 | + } |
| 130 | + |
| 131 | + if matchesAnyPattern(name, blockList) { |
| 132 | + logrus.Debugf("Blocked env variable %q", name) |
| 133 | + continue |
| 134 | + } |
| 135 | + |
| 136 | + filtered = append(filtered, envVar) |
| 137 | + } |
| 138 | + |
| 139 | + return filtered |
| 140 | +} |
| 141 | + |
| 142 | +func GetBuiltinBlocklist() []string { |
| 143 | + result := make([]string, len(BuiltinBlocklist)) |
| 144 | + copy(result, BuiltinBlocklist) |
| 145 | + return result |
| 146 | +} |
0 commit comments