From 0b3ac53cdd8c8f8a2f5d359f5193b20af74b9be6 Mon Sep 17 00:00:00 2001 From: sewn Date: Tue, 30 Jan 2024 21:27:34 +0300 Subject: [PATCH] config(env): move util.Sanitize env, ensure reproducible test --- config/config.go | 5 ++--- config/config_test.go | 3 +++ config/env.go | 40 ++++++++++++++++++++++++++++++++++++ config/env_test.go | 19 +++++++++++++++++ util/env.go | 48 ------------------------------------------- 5 files changed, 64 insertions(+), 51 deletions(-) delete mode 100644 util/env.go diff --git a/config/config.go b/config/config.go index d880b515..62c8aa60 100644 --- a/config/config.go +++ b/config/config.go @@ -13,7 +13,6 @@ import ( "github.com/vinegarhq/vinegar/roblox" "github.com/vinegarhq/vinegar/roblox/bootstrapper" "github.com/vinegarhq/vinegar/splash" - "github.com/vinegarhq/vinegar/util" "github.com/vinegarhq/vinegar/wine" ) @@ -154,7 +153,7 @@ func (b *Binary) setup() error { func (c *Config) setup() error { if c.SanitizeEnv { - util.SanitizeEnv() + SanitizeEnv() } if c.WineRoot != "" { @@ -164,7 +163,7 @@ func (c *Config) setup() error { return ErrWineRootAbs } - c.Env["PATH"] = bin + ":" + os.Getenv("PATH") + os.Setenv("PATH", bin+":"+os.Getenv("PATH")) os.Unsetenv("WINEDLLPATH") } diff --git a/config/config_test.go b/config/config_test.go index 4f8bcabb..d65226c8 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -54,6 +54,9 @@ func TestSetup(t *testing.T) { c := Default() c.WineRoot = wr + // Required to not conflict with system environment + os.Setenv("PATH", "") + if err := c.setup(); !errors.Is(err, ErrWineRootInvalid) { t.Error("expected wine root wine check") } diff --git a/config/env.go b/config/env.go index 1a20cdb4..62d4d542 100644 --- a/config/env.go +++ b/config/env.go @@ -2,6 +2,7 @@ package config import ( "os" + "strings" ) // Environment is a map representation of a operating environment @@ -24,3 +25,42 @@ func (e Environment) Setenv() { os.Setenv(name, value) } } + +var AllowedEnv = []string{ + "PATH", + "HOME", "USER", "LOGNAME", + "TZ", + "LANG", "LC_ALL", + "EDITOR", + "XDG_CACHE_HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_DATA_DIRS", + "XDG_RUNTIME_DIR", // Required by Wayland and Pipewire + "PULSE_SERVER", "PULSE_CLIENTCONFIG", + "DISPLAY", "WAYLAND_DISPLAY", "XAUTHORITY", + "WINEDLLPATH", + "SDL_GAMECONTROLLERCONFIG", + "__EGL_EXTERNAL_PLATFORM_CONFIG_DIRS", // Flatpak +} + +// SanitizeEnv modifies the global environment by removing +// all environment variables that are not present in [AllowedEnv]. +func SanitizeEnv() { + for _, env := range os.Environ() { + parts := strings.SplitN(env, "=", 2) + + if len(parts) != 2 { + continue + } + + allowed := false + + for _, aenv := range AllowedEnv { + if aenv == parts[0] { + allowed = true + } + } + + if !allowed { + os.Unsetenv(parts[0]) + } + } +} diff --git a/config/env_test.go b/config/env_test.go index 114a0b40..d9cbbfd8 100644 --- a/config/env_test.go +++ b/config/env_test.go @@ -18,3 +18,22 @@ func TestEnv(t *testing.T) { t.Fatal("expected Setenv set global environment") } } + +func TestSanitizeEnv(t *testing.T) { + AllowedEnv = []string{"ALLOWED"} + e := Environment{ + "ALLOWED": "im not impostor", + "IMPOSTOR": "im impostor", + } + + e.Setenv() + SanitizeEnv() + + if os.Getenv("ALLOWED") != e["ALLOWED"] { + t.Fatal("want allowed var, got sanitized") + } + + if os.Getenv("IMPOSTOR") != "" { + t.Fatal("want sanitized impostor var, got value") + } +} diff --git a/util/env.go b/util/env.go deleted file mode 100644 index aa4c8258..00000000 --- a/util/env.go +++ /dev/null @@ -1,48 +0,0 @@ -package util - -import ( - "log" - "os" - "strings" -) - -var AllowedEnv = []string{ - "PATH", - "HOME", "USER", "LOGNAME", - "TZ", - "LANG", "LC_ALL", - "EDITOR", - "XDG_CACHE_HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_DATA_DIRS", - "XDG_RUNTIME_DIR", // Required by Wayland and Pipewire - "PULSE_SERVER", "PULSE_CLIENTCONFIG", - "DISPLAY", "WAYLAND_DISPLAY", "XAUTHORITY", - "WINEDLLPATH", - "SDL_GAMECONTROLLERCONFIG", - "__EGL_EXTERNAL_PLATFORM_CONFIG_DIRS", // Flatpak -} - -// SanitizeEnv modifies the global environment by removing -// all environment variables that arent in AllowedEnv. -func SanitizeEnv() { - log.Println("Sanitizing environment") - - for _, env := range os.Environ() { - parts := strings.SplitN(env, "=", 2) - - if len(parts) != 2 { - continue - } - - allowed := false - - for _, aenv := range AllowedEnv { - if aenv == parts[0] { - allowed = true - } - } - - if !allowed { - os.Unsetenv(parts[0]) - } - } -}