Files
Reims-Go/internal/host/elevate_test.go
NwflowerandClaude Opus 5.5 44e2174fff fix: harden guest driver host APIs
- run elevated actions as fixed System32 tools instead of a hidden subcommand
- require a SHA-256 for plugin downloads
- keep records of secret files that could not be deleted, and never overwrite
  files the host did not create
- stop two concurrent requests from starting the same VM
- drop invalid endpoints instead of failing the whole VM list, and reject ssh
  users and IPv6 zones that could inject command-line text
- accept driver screenshots only at the requested path
- do not treat an unreadable guest phase as normal

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 09:07:46 -07:00

47 lines
1.7 KiB
Go

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)
}
}
}