Files
Reims-Go/internal/app/console_router.go
T
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

185 lines
6.0 KiB
Go

package app
import (
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/Nwflower/reims-go/internal/driver"
)
const (
consoleRouteQMP = "qmp"
consoleRouteDriver = "driver"
)
// consoleRoute says how the host reaches one VM's screen and input: through
// its own QMP console, or through the console its driver serves.
type consoleRoute struct {
kind string
vm driver.VMInfo
console driver.ConsoleDriver // set for consoleRouteDriver
}
// driverFor returns the registered driver that owns vm, or nil.
func (a *App) driverFor(vm driver.VMInfo) driver.Driver {
id := driver.CanonicalDriverID(vm.DriverID)
if id == "" {
id = driver.BuiltinReimsMacOSDriverID
}
a.mu.Lock()
router := a.driverRouter
a.mu.Unlock()
if router == nil {
return nil
}
drv, err := router.DriverByID(id)
if err != nil {
return nil
}
return drv
}
// resolveConsoleRoute picks the console of vm. A driver that declares
// `consoleEndpoint: "driver"` and the `console` capability serves its own;
// every other VM needs a QMP console, which a hosted VM does not have.
func (a *App) resolveConsoleRoute(vm driver.VMInfo) (consoleRoute, error) {
if cd, ok := driver.DriverServesConsole(a.driverFor(vm)); ok && driver.HasCapability(vm.Capabilities, driver.CapConsole) {
return consoleRoute{kind: consoleRouteDriver, vm: vm, console: cd}, nil
}
if !driver.IsHosted(vm.Capabilities) && (len(vm.Capabilities) == 0 || driver.HasCapability(vm.Capabilities, driver.CapQMP)) {
return consoleRoute{kind: consoleRouteQMP, vm: vm}, nil
}
return consoleRoute{}, fmt.Errorf("computer_use_unavailable: driver %q has no console the host can reach", vm.DriverID)
}
// consoleCapture writes one full frame of the guest screen to a fresh temp
// file and returns its path and format. cleanup removes the file. External
// agent calls use this so disabling builtin.computer-use blocks QMP access.
func (a *App) consoleCapture(ctx context.Context, route consoleRoute, maxEdge int) (path, format string, cleanup func(), err error) {
return a.consoleCaptureWithMode(ctx, route, maxEdge, false)
}
// consoleCaptureInternal captures a frame for desktop-owned features
// (thumbnails and screenshot copy) using the internal QMP fallback when
// builtin.computer-use is disabled.
func (a *App) consoleCaptureInternal(ctx context.Context, route consoleRoute, maxEdge int) (path, format string, cleanup func(), err error) {
return a.consoleCaptureWithMode(ctx, route, maxEdge, true)
}
func (a *App) consoleCaptureWithMode(ctx context.Context, route consoleRoute, maxEdge int, internal bool) (path, format string, cleanup func(), err error) {
a.mu.Lock()
if a.cfg == nil {
a.mu.Unlock()
return "", "", nil, errors.New("application configuration is unavailable")
}
tempDir := a.cfg.TempDir()
a.mu.Unlock()
if err := os.MkdirAll(tempDir, 0o755); err != nil {
return "", "", nil, err
}
file, err := os.CreateTemp(tempDir, "console-*.frame")
if err != nil {
return "", "", nil, err
}
path = file.Name()
if err := file.Close(); err != nil {
_ = os.Remove(path)
return "", "", nil, err
}
cleanup = func() { _ = os.Remove(path) }
switch route.kind {
case consoleRouteDriver:
res, err := route.console.ConsoleScreenshot(ctx, route.vm.Name, driver.ConsoleScreenshotRequest{Path: path, MaxEdge: maxEdge})
if err != nil {
cleanup()
return "", "", nil, err
}
if res.Path != "" && !samePath(res.Path, path) {
cleanup()
return "", "", nil, fmt.Errorf("driver %q did not write its frame to the requested path", route.vm.DriverID)
}
format = strings.ToLower(strings.TrimSpace(res.Format))
switch format {
case "":
format = "png"
case "png", "ppm":
default:
cleanup()
return "", "", nil, fmt.Errorf("driver %q returned an unsupported frame format %q", route.vm.DriverID, res.Format)
}
if st, err := os.Lstat(path); err != nil || !st.Mode().IsRegular() || st.Size() == 0 {
cleanup()
return "", "", nil, fmt.Errorf("driver %q wrote no frame", route.vm.DriverID)
}
return path, format, cleanup, nil
default:
service := a.computerUseInternalService()
if !internal {
var svcErr error
service, svcErr = a.computerUseService()
if svcErr != nil {
cleanup()
return "", "", nil, svcErr
}
}
if route.vm.Directory == "" {
cleanup()
return "", "", nil, fmt.Errorf("VM %q has no console directory", route.vm.Name)
}
format, err = service.Screendump(ctx, route.vm.Directory, path)
if err != nil {
cleanup()
return "", "", nil, err
}
return path, format, cleanup, nil
}
}
// consolePointer runs one pointer action on the VM's console.
func (a *App) consolePointer(ctx context.Context, route consoleRoute, req driver.ConsolePointerRequest) error {
if route.kind == consoleRouteDriver {
return route.console.ConsolePointer(ctx, route.vm.Name, req)
}
service, err := a.computerUseService()
if err != nil {
return err
}
dir := route.vm.Directory
switch strings.ToLower(strings.TrimSpace(req.Action)) {
case "move":
available, err := service.PointerAvailable(ctx, dir)
if err != nil {
return err
}
if !available {
return errors.New("VM has no active absolute pointer")
}
return service.MouseMove(ctx, dir, req.X, req.Y, req.Width, req.Height)
case "button":
if req.Down == nil {
return errors.New("button action requires down")
}
return service.MouseButton(ctx, dir, req.Button, *req.Down)
case "click":
return service.MouseClick(ctx, dir, req.Button)
case "scroll":
return service.MouseScroll(ctx, dir, req.Delta, req.Horizontal)
}
return fmt.Errorf("unsupported pointer action %q", req.Action)
}
// consoleKey presses one key on the VM's console.
func (a *App) consoleKey(ctx context.Context, route consoleRoute, key string, modifiers []string) error {
if route.kind == consoleRouteDriver {
return route.console.ConsoleKey(ctx, route.vm.Name, driver.ConsoleKeyRequest{Key: key, Modifiers: modifiers})
}
service, err := a.computerUseService()
if err != nil {
return err
}
return service.PressKey(ctx, route.vm.Directory, key, modifiers...)
}