diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index c991cf6..17adde4 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1078,7 +1078,13 @@ export const App: React.FC = () => { // Closing the window while a download runs asks first (beforeClose in Go). useEffect(() => events.onConfirmCloseDownloads(() => setCloseDownloadsOpen(true)), []) // Controlled elevation request from a guest driver plugin (elevate_flow.go). - useEffect(() => events.onElevateRequest((req) => setElevationQueue((q) => [...q, req])), []) + useEffect(() => { + const offRequest = events.onElevateRequest((req) => setElevationQueue((q) => [...q, req])) + const offResolved = events.onElevateResolved((id) => setElevationQueue((q) => ( + q.some((item) => item.id === id) ? q.filter((item) => item.id !== id) : q + ))) + return () => { offRequest(); offResolved() } + }, []) // Native Wails file drag-and-drop delivery into the selected VM's shared folder. const dropTargetRef = useRef<{ name: string; ready: boolean }>({ name: '', ready: false }) @@ -1546,7 +1552,7 @@ export const App: React.FC = () => { request={elevationQueue[0]} onResolve={(approved) => { const active = elevationQueue[0] - setElevationQueue((q) => q.slice(1)) + setElevationQueue((q) => q.filter((item) => item.id !== active.id)) api.ResolveElevationRequest(active.id, approved).catch((e) => console.warn('[app] ResolveElevationRequest failed:', e)) }} /> diff --git a/frontend/src/wails.ts b/frontend/src/wails.ts index eff7047..3597729 100644 --- a/frontend/src/wails.ts +++ b/frontend/src/wails.ts @@ -211,6 +211,7 @@ export const events = { onConfirmCloseDownloads: (cb: () => void) => eventsOn('reims:confirm-close-downloads', cb), // Controlled elevation request from a guest driver plugin (internal/app/elevate_flow.go). onElevateRequest: (cb: (req: ElevationPrompt) => void) => eventsOn('reims:elevate-request', cb), + onElevateResolved: (cb: (id: string) => void) => eventsOn('reims:elevate-resolved', cb), // Renderer health watchdog & crash reporting (internal/app/ui_health.go). onUIPing: (cb: () => void) => eventsOn('reims:ui-ping', cb), emitUIPong: () => eventsEmit('reims:ui-pong'), diff --git a/internal/app/app.go b/internal/app/app.go index a983fd2..01f9660 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -159,6 +159,7 @@ type App struct { elevatePending map[string]chan bool elevateSeq uint64 elevateEmit func(ElevationPrompt) + elevateDismiss func(string) elevateRunner elevationRunner } diff --git a/internal/app/elevate_flow.go b/internal/app/elevate_flow.go index b3159ee..2b90023 100644 --- a/internal/app/elevate_flow.go +++ b/internal/app/elevate_flow.go @@ -73,8 +73,10 @@ func (a *App) requestElevation(ctx context.Context, broker *plugin.Broker, reg * a.mu.Lock() emit := a.elevateEmit + dismiss := a.elevateDismiss runner := a.elevateRunner - hasWindow := a.ctx != nil + winCtx := a.ctx + hasWindow := winCtx != nil locale := i18n.EffectiveLocale(i18n.Auto) if a.cfg != nil { locale = i18n.EffectiveLocale(a.cfg.Locale) @@ -110,31 +112,45 @@ func (a *App) requestElevation(ctx context.Context, broker *plugin.Broker, reg * prompt.ID = fmt.Sprintf("elevate-%d", a.elevateSeq) a.elevatePending[prompt.ID] = ch a.elevateMu.Unlock() - defer func() { + 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 && winCtx != nil { + wailsRuntime.EventsEmit(winCtx, "reims:elevate-resolved", prompt.ID) + } + } + defer clearPrompt() if emit != nil { emit(prompt) } else { a.ShowMainWindow() - wailsRuntime.EventsEmit(a.ctx, "reims:elevate-request", prompt) + wailsRuntime.EventsEmit(winCtx, "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") } diff --git a/internal/app/elevate_flow_test.go b/internal/app/elevate_flow_test.go index 7a6ef54..236f337 100644 --- a/internal/app/elevate_flow_test.go +++ b/internal/app/elevate_flow_test.go @@ -18,15 +18,16 @@ import ( ) type elevateTestEnv struct { - app *App - h *fakedriver.Harness - plugin string - mu sync.Mutex - prompts []ElevationPrompt - runs atomic.Int32 - runArgs []string - runErr error - runRes host.ElevationResult + app *App + h *fakedriver.Harness + plugin string + mu sync.Mutex + prompts []ElevationPrompt + dismissed []string + runs atomic.Int32 + runArgs []string + runErr error + runRes host.ElevationResult // onPrompt answers a prompt; nil leaves it pending. onPrompt func(ElevationPrompt) } @@ -46,6 +47,11 @@ func newElevateTestEnv(t *testing.T) *elevateTestEnv { go answer(p) } } + e.app.elevateDismiss = func(id string) { + e.mu.Lock() + e.dismissed = append(e.dismissed, id) + e.mu.Unlock() + } e.app.elevateRunner = func(_ context.Context, exePath, action, targetUser string) (host.ElevationResult, error) { e.runs.Add(1) e.mu.Lock() @@ -70,6 +76,12 @@ func (e *elevateTestEnv) promptCount() int { return len(e.prompts) } +func (e *elevateTestEnv) dismissedIDs() []string { + e.mu.Lock() + defer e.mu.Unlock() + return append([]string(nil), e.dismissed...) +} + func (e *elevateTestEnv) auditLog(t *testing.T) string { t.Helper() data, err := os.ReadFile(filepath.Join(e.h.DataRoot, "plugins", "logs", e.plugin+".log")) @@ -257,6 +269,12 @@ func TestCancelPendingElevationsDeniesWaitingRequests(t *testing.T) { if e.runs.Load() != 0 { t.Fatal("the runner started after shutdown") } + e.mu.Lock() + wantID := e.prompts[0].ID + e.mu.Unlock() + if got := e.dismissedIDs(); len(got) != 1 || got[0] != wantID { + t.Fatalf("dismissed = %v, want [%s]", got, wantID) + } } func TestElevationTimesOutWithoutAnAnswer(t *testing.T) { @@ -271,6 +289,41 @@ func TestElevationTimesOutWithoutAnAnswer(t *testing.T) { if e.runs.Load() != 0 || !strings.Contains(e.auditLog(t), "ELEVATE_TIMEOUT") { t.Fatalf("runs=%d, audit:\n%s", e.runs.Load(), e.auditLog(t)) } + e.mu.Lock() + wantID := e.prompts[0].ID + e.mu.Unlock() + if got := e.dismissedIDs(); len(got) != 1 || got[0] != wantID { + t.Fatalf("dismissed = %v, want [%s]", got, wantID) + } +} + +func TestElevationContextCancelDismissesTheDialog(t *testing.T) { + e := newElevateTestEnv(t) + ctx, cancel := context.WithCancel(context.Background()) + errc := make(chan error, 1) + go func() { + _, err := e.request(ctx, e.plugin, host.ElevateSandboxEnable) + errc <- err + }() + waitFor(t, "the prompt to open", func() bool { return e.promptCount() == 1 }) + cancel() + select { + case err := <-errc: + if err == nil || !strings.Contains(err.Error(), "elevation_denied") { + t.Fatalf("err = %v, want elevation_denied", err) + } + case <-time.After(5 * time.Second): + t.Fatal("the canceled request was not released") + } + if e.runs.Load() != 0 || !strings.Contains(e.auditLog(t), "ELEVATE_CANCELED") { + t.Fatalf("runs=%d, audit:\n%s", e.runs.Load(), e.auditLog(t)) + } + e.mu.Lock() + wantID := e.prompts[0].ID + e.mu.Unlock() + if got := e.dismissedIDs(); len(got) != 1 || got[0] != wantID { + t.Fatalf("dismissed = %v, want [%s]", got, wantID) + } } func TestResolveElevationRequestIgnoresUnknownIDs(t *testing.T) {