Files
Reims-Go/internal/host/elevate.go
T
千川白浪andClaude 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

70 lines
2.3 KiB
Go

package host
import (
"errors"
"fmt"
"strings"
"unicode/utf8"
)
// The closed set of elevated actions a plugin may ask the host to run. The
// host runs a fixed command for each; plugins never supply command lines.
const (
ElevateHyperVEnable = "hyperv.enable"
ElevateHyperVAddAdminGroup = "hyperv.addAdminGroup"
ElevateSandboxEnable = "sandbox.enable"
)
// ElevationActionSpec describes one whitelisted elevated action and what the
// person has to do afterwards. The host never restarts Windows itself.
type ElevationActionSpec struct {
Action string
NeedsReboot bool
NeedsLogoff bool
}
// ElevationResult is what an elevated action reports back.
type ElevationResult struct {
OK bool `json:"ok"`
Action string `json:"action"`
ExitCode int `json:"exit_code,omitempty"`
RebootRequired bool `json:"reboot_required,omitempty"`
LogoffRequired bool `json:"logoff_required,omitempty"`
}
var elevationActions = map[string]ElevationActionSpec{
ElevateHyperVEnable: {Action: ElevateHyperVEnable, NeedsReboot: true},
ElevateHyperVAddAdminGroup: {Action: ElevateHyperVAddAdminGroup, NeedsLogoff: true},
ElevateSandboxEnable: {Action: ElevateSandboxEnable, NeedsReboot: true},
}
// LookupElevationAction returns the spec of a whitelisted action.
func LookupElevationAction(action string) (ElevationActionSpec, bool) {
spec, ok := elevationActions[action]
return spec, ok
}
// maxElevationUserLen bounds the account name passed to the group command.
const maxElevationUserLen = 128
// ValidateElevationTargetUser rejects account names that could be read as
// command-line switches or carry shell metacharacters.
func ValidateElevationTargetUser(user string) error {
user = strings.TrimSpace(user)
if user == "" {
return errors.New("the account name is empty")
}
if utf8.RuneCountInString(user) > maxElevationUserLen {
return errors.New("the account name is too long")
}
if strings.HasPrefix(user, "-") || strings.HasPrefix(user, "/") {
return errors.New("the account name must not start with a switch character")
}
for _, r := range user {
if r < 0x20 || r == 0x7f || strings.ContainsRune(";&|<>^\"'`%$()!", r) {
return fmt.Errorf("the account name contains the character %q", r)
}
}
return nil
}