feat: safely control Shell Target
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"proxyctl/internal/profile"
|
||||
"proxyctl/internal/shell"
|
||||
)
|
||||
|
||||
type commandEnvironment struct {
|
||||
identity profile.UserIdentity
|
||||
env map[string]string
|
||||
}
|
||||
|
||||
func (e commandEnvironment) CurrentUser() (profile.UserIdentity, error) { return e.identity, nil }
|
||||
func (e commandEnvironment) LookupUser(string) (profile.UserIdentity, error) {
|
||||
return profile.UserIdentity{}, os.ErrNotExist
|
||||
}
|
||||
func (e commandEnvironment) LookupEnv(name string) (string, bool) {
|
||||
value, ok := e.env[name]
|
||||
return value, ok
|
||||
}
|
||||
|
||||
func TestShellInstallAndUninstallCommandsUseInjectedUserRoots(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
environment := commandEnvironment{identity: profile.UserIdentity{Username: "tester", HomeDir: home, UID: os.Getuid(), GID: os.Getgid()}, env: map[string]string{"SHELL": "/bin/bash", "XDG_CONFIG_HOME": filepath.Join(home, "config"), "XDG_STATE_HOME": filepath.Join(home, "state")}}
|
||||
var output bytes.Buffer
|
||||
if err := runWithEnvironment([]string{"shell", "install"}, strings.NewReader(""), &output, environment, shell.OSFileSystem{}, "/test/proxyctl"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
startup := filepath.Join(home, ".bashrc")
|
||||
data, err := os.ReadFile(startup)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(data), "command '/test/proxyctl'") || !strings.Contains(output.String(), "installed") {
|
||||
t.Fatalf("install output/startup = %q / %q", output.String(), data)
|
||||
}
|
||||
output.Reset()
|
||||
if err := runWithEnvironment([]string{"shell", "uninstall"}, strings.NewReader(""), &output, environment, shell.OSFileSystem{}, "/test/proxyctl"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err = os.ReadFile(startup)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(data), "proxyctl shell integration") || !strings.Contains(output.String(), "uninstalled") {
|
||||
t.Fatalf("uninstall output/startup = %q / %q", output.String(), data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnsupportedShellMaintenanceCommandExplainsAvailability(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
environment := commandEnvironment{identity: profile.UserIdentity{Username: "tester", HomeDir: home, UID: os.Getuid(), GID: os.Getgid()}, env: map[string]string{"SHELL": "/bin/fish"}}
|
||||
err := runWithEnvironment([]string{"shell", "install"}, strings.NewReader(""), &bytes.Buffer{}, environment, shell.OSFileSystem{}, "/test/proxyctl")
|
||||
if err == nil || !strings.Contains(err.Error(), "Bash or Zsh") {
|
||||
t.Fatalf("unsupported Shell error = %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user