Every VM action had two bindings: one by name and one by driver and name. The window used only the second, so the first is gone and the WithDriver suffix with it. Unused bindings and frontend exports are removed too. Tests now reject bindings the window never calls, stale generated bindings and unused frontend exports. TypeScript rejects unused locals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
210 lines
6.0 KiB
Go
210 lines
6.0 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os/user"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
// elevatePromptTimeout is how long a confirmation waits for the person.
|
|
var elevatePromptTimeout = 2 * time.Minute
|
|
|
|
// ElevationPrompt is what the desktop window shows before a plugin's
|
|
// elevation request goes on to the Windows UAC prompt.
|
|
type ElevationPrompt struct {
|
|
ID string `json:"id"`
|
|
PluginID string `json:"plugin_id"`
|
|
PluginName string `json:"plugin_name"`
|
|
Publisher string `json:"publisher,omitempty"`
|
|
Trust string `json:"trust,omitempty"`
|
|
Action string `json:"action"`
|
|
NeedsReboot bool `json:"needs_reboot,omitempty"`
|
|
NeedsLogoff bool `json:"needs_logoff,omitempty"`
|
|
}
|
|
|
|
type elevationRunner func(ctx context.Context, action, targetUser string) (host.ElevationResult, error)
|
|
|
|
func (a *App) elevateHostHandlers(mgr *plugin.Manager, pluginID string) map[string]plugin.RPCRequestHandler {
|
|
if mgr == nil || mgr.Broker() == nil {
|
|
return nil
|
|
}
|
|
broker := mgr.Broker()
|
|
return map[string]plugin.RPCRequestHandler{
|
|
"host/elevate.request": func(ctx context.Context, raw json.RawMessage) (any, error) {
|
|
res, err := a.requestElevation(ctx, broker, mgr.Registry(), pluginID, raw)
|
|
if err != nil {
|
|
broker.Audit(pluginID, "HOST_API_ERROR", "host/elevate.request")
|
|
return nil, err
|
|
}
|
|
broker.Audit(pluginID, "HOST_API", "host/elevate.request")
|
|
return res, nil
|
|
},
|
|
}
|
|
}
|
|
|
|
// requestElevation runs one plugin elevation request: check the permission and
|
|
// the closed action list, ask the person in the desktop window, and only after
|
|
// a yes run the action (which raises the Windows UAC prompt). A no,
|
|
// a timeout or a missing window ends the request before anything runs.
|
|
func (a *App) requestElevation(ctx context.Context, broker *plugin.Broker, reg *plugin.Registry, pluginID string, raw json.RawMessage) (any, error) {
|
|
if err := broker.Authorize(pluginID, "driver:vm"); err != nil {
|
|
return nil, err
|
|
}
|
|
var req struct {
|
|
Action string `json:"action"`
|
|
}
|
|
if err := json.Unmarshal(raw, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
spec, ok := host.LookupElevationAction(req.Action)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unsupported elevation action %q", req.Action)
|
|
}
|
|
|
|
a.mu.Lock()
|
|
emit := a.elevateEmit
|
|
dismiss := a.elevateDismiss
|
|
runner := a.elevateRunner
|
|
winCtx := a.ctx
|
|
hasWindow := winCtx != nil
|
|
locale := i18n.EffectiveLocale(i18n.Auto)
|
|
if a.cfg != nil {
|
|
locale = i18n.EffectiveLocale(a.cfg.Locale)
|
|
}
|
|
a.mu.Unlock()
|
|
if emit == nil && !hasWindow {
|
|
broker.Audit(pluginID, "ELEVATE_UNAVAILABLE", spec.Action)
|
|
return nil, errors.New("elevation_unavailable: the desktop window is required to confirm elevation")
|
|
}
|
|
|
|
// One confirmation and one servicing run at a time.
|
|
a.elevatePromptMu.Lock()
|
|
defer a.elevatePromptMu.Unlock()
|
|
|
|
prompt := ElevationPrompt{
|
|
PluginID: pluginID, PluginName: pluginID, Action: spec.Action,
|
|
NeedsReboot: spec.NeedsReboot, NeedsLogoff: spec.NeedsLogoff,
|
|
}
|
|
if info, found := reg.Info(pluginID); found {
|
|
if name := strings.TrimSpace(info.Name.Get(string(locale))); name != "" {
|
|
prompt.PluginName = name
|
|
}
|
|
prompt.Publisher = info.Publisher
|
|
prompt.Trust = string(info.Trust)
|
|
}
|
|
|
|
ch := make(chan bool, 1)
|
|
a.elevateMu.Lock()
|
|
if a.elevatePending == nil {
|
|
a.elevatePending = map[string]chan bool{}
|
|
}
|
|
a.elevateSeq++
|
|
prompt.ID = fmt.Sprintf("elevate-%d", a.elevateSeq)
|
|
a.elevatePending[prompt.ID] = ch
|
|
a.elevateMu.Unlock()
|
|
dismissed := false
|
|
clearPrompt := func() {
|
|
a.elevateMu.Lock()
|
|
delete(a.elevatePending, prompt.ID)
|
|
a.elevateMu.Unlock()
|
|
if dismissed {
|
|
return
|
|
}
|
|
dismissed = true
|
|
if dismiss != nil {
|
|
dismiss(prompt.ID)
|
|
} else if emit == nil {
|
|
a.emit("reims:elevate-resolved", prompt.ID)
|
|
}
|
|
}
|
|
defer clearPrompt()
|
|
|
|
if emit != nil {
|
|
emit(prompt)
|
|
} else {
|
|
a.showMainWindow()
|
|
a.emit("reims:elevate-request", prompt)
|
|
}
|
|
|
|
timer := time.NewTimer(elevatePromptTimeout)
|
|
defer timer.Stop()
|
|
select {
|
|
case approved, open := <-ch:
|
|
clearPrompt()
|
|
if !open || !approved {
|
|
broker.Audit(pluginID, "ELEVATE_DENIED", spec.Action)
|
|
return nil, errors.New("elevation_denied: the user declined the elevation request")
|
|
}
|
|
case <-ctx.Done():
|
|
clearPrompt()
|
|
broker.Audit(pluginID, "ELEVATE_CANCELED", spec.Action)
|
|
return nil, fmt.Errorf("elevation_denied: %w", ctx.Err())
|
|
case <-timer.C:
|
|
clearPrompt()
|
|
broker.Audit(pluginID, "ELEVATE_TIMEOUT", spec.Action)
|
|
return nil, errors.New("elevation_denied: the confirmation timed out")
|
|
}
|
|
broker.Audit(pluginID, "ELEVATE_APPROVED", spec.Action)
|
|
|
|
targetUser := ""
|
|
if spec.Action == host.ElevateHyperVAddAdminGroup {
|
|
cur, err := user.Current()
|
|
if err != nil {
|
|
broker.Audit(pluginID, "ELEVATE_ERROR", spec.Action)
|
|
return nil, fmt.Errorf("find the current account: %w", err)
|
|
}
|
|
targetUser = cur.Username
|
|
}
|
|
if runner == nil {
|
|
runner = host.RunElevatedAction
|
|
}
|
|
res, err := runner(ctx, spec.Action, targetUser)
|
|
if err != nil {
|
|
if errors.Is(err, host.ErrElevationDeclined) {
|
|
broker.Audit(pluginID, "ELEVATE_UAC_DECLINED", spec.Action)
|
|
return nil, errors.New("elevation_denied: the Windows UAC prompt was declined")
|
|
}
|
|
broker.Audit(pluginID, "ELEVATE_ERROR", spec.Action)
|
|
return nil, err
|
|
}
|
|
broker.Audit(pluginID, "ELEVATE_OK", spec.Action)
|
|
return res, nil
|
|
}
|
|
|
|
// ResolveElevationRequest delivers the person's answer to a pending plugin
|
|
// elevation request. An expired or answered request is ignored.
|
|
func (a *App) ResolveElevationRequest(id string, approved bool) error {
|
|
a.elevateMu.Lock()
|
|
ch, ok := a.elevatePending[id]
|
|
delete(a.elevatePending, id)
|
|
a.elevateMu.Unlock()
|
|
if !ok {
|
|
return nil
|
|
}
|
|
select {
|
|
case ch <- approved:
|
|
default:
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// cancelPendingElevations answers every waiting request with a no, so
|
|
// shutting down never leaves a plugin waiting on a dialog.
|
|
func (a *App) cancelPendingElevations() {
|
|
a.elevateMu.Lock()
|
|
pending := a.elevatePending
|
|
a.elevatePending = nil
|
|
a.elevateMu.Unlock()
|
|
for _, ch := range pending {
|
|
close(ch)
|
|
}
|
|
}
|