auth dialog: keep typed text when the polkit helper ends before asking
CI / test (push) Successful in 2m1s

If polkit-agent-helper-1 exited (or the socket service refused) before
sending a PAM prompt, the dialog counted it as a wrong password, wiped the
field and restarted the helper immediately, in a tight loop, so typing
seemed to do nothing. Now such an early end keeps what was typed, shows
why, and retries after 1s/2s/4s (then on Enter). Every helper event and
route is logged as "chandra: polkit helper ..." (never the typed text).

Adds an end-to-end test that types through handle_input with a real
xkbcommon keymap, including Shift, across a helper that dies at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hgkRPrAzXECBUGmNEE
This commit is contained in:
Claude committed 2026-11-26 01:31:59 +00:00
1 parent ac2d0fa205
commit fa634956cc
4 files changed
+237 -4

No files matched your search

+69 -2
View File
@@ -22,6 +22,10 @@ pub(all) struct AuthUi {
hits : Array[(@scene.Rect, @polkit.Focus)]
/// When it opened (the spinner's clock).
opened : UInt64
/// Helper runs in a row that ended before anything was answered, and when to try again
/// (monotonic ms, 0: not waiting).
mut quick_failures : Int
mut retry_at : UInt64
}
///| The dialog that is up, if any.
@@ -125,6 +129,12 @@ fn ChandraCompositor::auth_tick(self : ChandraCompositor) -> Unit {
self.close_auth(None)
} else {
self.auth_helper_events(ui)
// A helper that ended early is tried again after a pause.
if ui.helper is None && ui.retry_at != 0UL && @sys.monotonic_ms() >= ui.retry_at {
ui.retry_at = 0UL
self.auth_start_helper(ui)
self.damage.mark_full()
}
}
None => ()
}
@@ -157,6 +167,8 @@ fn ChandraCompositor::open_auth(self : ChandraCompositor, req : @polkit.AuthRequ
icon,
hits: [],
opened: @sys.monotonic_ms(),
quick_failures: 0,
retry_at: 0UL,
}
self.auth = Some(ui)
// Nothing else gets the keyboard or the pointer while it is up.
@@ -191,6 +203,27 @@ fn ChandraCompositor::auth_helper_events(self : ChandraCompositor, ui : AuthUi)
Some(h) => {
let evs = h.read()
for ev in evs {
// What PAM said, for the session log (never what was typed: that only goes out).
@sys.log(
"chandra: polkit helper: " +
(match ev {
PromptEchoOff(t) => "password prompt \"" + t + "\""
PromptEchoOn(t) => "prompt \"" + t + "\""
ErrorMsg(t) => "error \"" + t + "\""
TextInfo(t) => "info \"" + t + "\""
Success => "SUCCESS"
Failure => "FAILURE"
}),
)
match ev {
PromptEchoOff(_) | PromptEchoOn(_) => {
ui.quick_failures = 0
if ui.dialog.info == "Press Enter to try again." {
ui.dialog.info = ""
}
}
_ => ()
}
self.auth_effect(ui, ui.dialog.helper(ev, @sys.monotonic_ms()))
if self.auth is None {
return
@@ -215,7 +248,27 @@ fn ChandraCompositor::auth_effect(self : ChandraCompositor, ui : AuthUi, e : @po
}
@polkit.zero(b)
}
Restart => self.auth_start_helper(ui)
Restart => {
ui.quick_failures = 0
ui.retry_at = 0UL
self.auth_start_helper(ui)
}
RetryLater => {
match ui.helper {
Some(h) => h.close()
None => ()
}
ui.helper = None
ui.quick_failures = ui.quick_failures + 1
@sys.log("chandra: polkit helper ended before asking anything (" + ui.quick_failures.to_string() + " in a row): " + ui.dialog.error)
// Try again after 1, 2, 4 s; then wait for Enter.
if ui.quick_failures <= 3 {
ui.retry_at = @sys.monotonic_ms() + (1000UL << (ui.quick_failures - 1))
} else {
ui.retry_at = 0UL
ui.dialog.info = "Press Enter to try again."
}
}
Cancelled => self.close_auth(Some(Err("The user dismissed the authentication dialog")))
Authorized => self.close_auth(Some(Ok(())))
}
@@ -283,6 +336,16 @@ fn ChandraCompositor::auth_input(self : ChandraCompositor, ev : @nested.InputEve
let mods = self.mods | self.mods_latched | self.mods_locked
let ch = self.auth_keymap().char_for(code, mods, self.mods_group)
self.auth_effect(ui, ui.dialog.key(code, self.mods, ch))
// Enter with no helper running (it ended, or couldn't start): try again now.
if (code == @polkit.key_enter || code == @polkit.key_kp_enter) && self.auth is Some(_) &&
ui.helper is None {
ui.retry_at = 0UL
ui.quick_failures = 0
if ui.dialog.info == "Press Enter to try again." {
ui.dialog.info = ""
}
self.auth_start_helper(ui)
}
}
true
}
@@ -335,7 +398,11 @@ fn ChandraCompositor::auth_cursor(self : ChandraCompositor, ui : AuthUi) -> UInt
///| Whether the dialog animates (a failed attempt shakes it; a spinner while PAM checks).
fn ChandraCompositor::auth_animating(self : ChandraCompositor) -> Bool {
match self.auth {
Some(ui) => ui.dialog.shaking(@sys.monotonic_ms()) || ui.dialog.phase == Checking || ui.dialog.face == Scanning
Some(ui) =>
ui.dialog.shaking(@sys.monotonic_ms()) ||
ui.dialog.phase == Checking ||
ui.dialog.face == Scanning ||
ui.retry_at != 0UL
None => false
}
}
+133
View File
@@ -430,3 +430,136 @@ test "Settings > Session lists the password dialog switch, saved as :auth-agent
assert_false(comp.shell.native_auth_agent)
comp.close()
}
///|
test "typing reaches the dialog through handle_input, and a helper that dies at once doesn't wipe it in a loop" {
let bus = match auth_bus("typing") {
Some(b) => b
None => return
}
let a = MockAuthority::new(bus)
let hpath = "/tmp/chandra-auth-helper-typing-" + @sys.getpid().to_string()
let listen = @sys.unix_listen(hpath)
let comp = auth_comp("typing", bus, hpath)
// The keymap the real session hands clients (xkbcommon's text format), not the US fallback.
comp.server.set_keymap(@utf8.encode(real_keymap))
assert_true(settle(comp, a, fn() { comp.polkit.map(p => p.registered()).unwrap_or(false) }))
a.begin("t1", run0_details)
assert_true(settle(comp, a, fn() { comp.auth_dialog() is Some(_) }))
let d = comp.auth_dialog().unwrap()
// The helper goes away without a word (socket helper refusing, account locked, ...).
let (h, _) = helper_accept(listen, comp, a)
@sys.close_fd(h)
assert_true(settle(comp, a, fn() { d.error != "" }))
assert_eq(d.failures, 0) // not a wrong password
// Typing still lands in the field and stays there.
auth_type(comp, "Secret9")
for i = 0; i < 20; i = i + 1 {
a.bus.pump()
comp.step()
}
assert_eq(d.typed(), 7)
// Shift+digit through the real keymap: '(' on 9.
comp.handle_input(@nested.InputEvent::Modifiers(1U, 0U, 0U, 0U), 1U)
auth_press(comp, 10U)
comp.handle_input(@nested.InputEvent::Modifiers(0U, 0U, 0U, 0U), 1U)
assert_eq(d.typed(), 8)
auth_press(comp, 28U) // Enter: kept until PAM asks
// A second later the helper is started again; this time it asks.
let (h2, hello) = helper_accept(listen, comp, a)
assert_eq(hello, "nandi|t1")
helper_say(h2, "PAM_PROMPT_ECHO_OFF Password: ")
assert_true(settle(comp, a, fn() { d.phase == Checking }))
assert_eq(helper_lines(h2, 1), "Secret9(")
// The pointer works too: a click on Cancel dismisses it.
helper_say(h2, "FAILURE")
assert_true(settle(comp, a, fn() { d.failures == 1 }))
ignore(comp.compose_frame())
let cancel = comp.auth.unwrap().hits.filter(x => x.1 == Cancel)[0].0
comp.handle_input(@nested.InputEvent::PointerMove((cancel.x + 4).to_double(), (cancel.y + 4).to_double()), 1U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 3U)
assert_true(settle(comp, a, fn() { a.answers.length() == 1 }))
@sys.close_fd(h2)
@sys.close_fd(listen)
@sys.unlink(hpath)
comp.close()
}
///| An xkbcommon 1.6 keymap (xkbcli compile-keymap --layout us), cut down to what typing needs.
let real_keymap : String =
#|xkb_keymap {
#|xkb_keycodes "(unnamed)" {
#| minimum = 8;
#| maximum = 708;
#| <ESC> = 9;
#| <AE01> = 10;
#| <AE02> = 11;
#| <AE09> = 18;
#| <AE10> = 19;
#| <AD01> = 24;
#| <AD02> = 25;
#| <AD03> = 26;
#| <AD04> = 27;
#| <AD05> = 28;
#| <AC01> = 38;
#| <AC02> = 39;
#| <AB03> = 54;
#| <AB07> = 58;
#| <AB06> = 57;
#| <SPCE> = 65;
#| indicator 1 = "Caps Lock";
#| alias <AC12> = <BKSL>;
#|};
#|
#|xkb_types "(unnamed)" {
#| virtual_modifiers NumLock,Alt,LevelThree,LevelFive,Meta,Super,Hyper,ScrollLock;
#| type "ONE_LEVEL" {
#| modifiers= none;
#| level_name[1]= "Any";
#| };
#| type "ALPHABETIC" {
#| modifiers= Shift+Lock;
#| map[Shift]= 2;
#| map[Lock]= 2;
#| level_name[1]= "Base";
#| level_name[2]= "Caps";
#| };
#|};
#|
#|xkb_compatibility "(unnamed)" {
#| virtual_modifiers NumLock,Alt,LevelThree,LevelFive,Meta,Super,Hyper,ScrollLock;
#| interpret.useModMapMods= AnyLevel;
#| interpret Shift_L+AnyOf(all) {
#| action= SetMods(modifiers=Shift,clearLocks);
#| };
#| indicator "Caps Lock" {
#| whichModState= locked;
#| modifiers= Lock;
#| };
#|};
#|
#|xkb_symbols "(unnamed)" {
#| name[Group1]="English (US)";
#|
#| key <ESC> { [ Escape ] };
#| key <AE01> { [ 1, exclam ] };
#| key <AE02> { [ 2, at ] };
#| key <AE09> { [ 9, parenleft ] };
#| key <AE10> { [ 0, parenright ] };
#| key <AD01> { [ q, Q ] };
#| key <AD02> { [ w, W ] };
#| key <AD03> { [ e, E ] };
#| key <AD04> { [ r, R ] };
#| key <AD05> { [ t, T ] };
#| key <AC01> { [ a, A ] };
#| key <AC02> { [ s, S ] };
#| key <AB03> { [ c, C ] };
#| key <AB06> { [ n, N ] };
#| key <AB07> { [ m, M ] };
#| key <SPCE> { [ space ] };
#| modifier_map Shift { <LFSH>, <RTSH> };
#| modifier_map Lock { <CAPS> };
#|};
#|
#|};