package host import ( "strings" "testing" ) func TestLookupElevationActionIsAClosedWhitelist(t *testing.T) { want := map[string]ElevationActionSpec{ "hyperv.enable": {Action: "hyperv.enable", NeedsReboot: true}, "hyperv.addAdminGroup": {Action: "hyperv.addAdminGroup", NeedsLogoff: true}, "sandbox.enable": {Action: "sandbox.enable", NeedsReboot: true}, } for action, spec := range want { got, ok := LookupElevationAction(action) if !ok || got != spec { t.Errorf("LookupElevationAction(%q) = %+v, %v; want %+v", action, got, ok, spec) } } for _, bad := range []string{"", "HYPERV.ENABLE", "hyperv.enable ", "shell.exec", "hyperv.enable; calc", "dism.exe /?", "../hyperv.enable"} { if _, ok := LookupElevationAction(bad); ok { t.Errorf("LookupElevationAction(%q) accepted an action outside the whitelist", bad) } } } func TestValidateElevationTargetUser(t *testing.T) { for _, ok := range []string{`DOMAIN\alice`, `DESKTOP-1\bob`, "alice@contoso.com", "alice", "Alice Smith"} { if err := ValidateElevationTargetUser(ok); err != nil { t.Errorf("ValidateElevationTargetUser(%q) = %v, want nil", ok, err) } } for _, bad := range []string{"", " ", "-flag", "/add", "alice & calc", "alice;whoami", "alice\nbob", "a|b", "a>b", "a`b", "a%PATH%", strings.Repeat("a", 129)} { if err := ValidateElevationTargetUser(bad); err == nil { t.Errorf("ValidateElevationTargetUser(%q) accepted the account name", bad) } } } func TestRunElevatedActionRejectsUnknownActions(t *testing.T) { for _, action := range []string{"shell.exec", ""} { if res, err := RunElevatedAction(t.Context(), action, ""); err == nil || res.OK { t.Fatalf("action %q outside the whitelist ran: %+v, %v", action, res, err) } } }