Skip to content

Commit

Permalink
config(env): move util.Sanitize env, ensure reproducible test
Browse files Browse the repository at this point in the history
  • Loading branch information
apprehensions committed Jan 30, 2024
1 parent 28bdab4 commit 0b3ac53
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 51 deletions.
5 changes: 2 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -154,7 +153,7 @@ func (b *Binary) setup() error {

func (c *Config) setup() error {
if c.SanitizeEnv {
util.SanitizeEnv()
SanitizeEnv()
}

if c.WineRoot != "" {
Expand All @@ -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")
}

Expand Down
3 changes: 3 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
40 changes: 40 additions & 0 deletions config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"os"
"strings"
)

// Environment is a map representation of a operating environment
Expand All @@ -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])
}
}
}
19 changes: 19 additions & 0 deletions config/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
48 changes: 0 additions & 48 deletions util/env.go

This file was deleted.

0 comments on commit 0b3ac53

Please sign in to comment.