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)] hits : Array[(@scene.Rect, @polkit.Focus)]
/// When it opened (the spinner's clock). /// When it opened (the spinner's clock).
opened : UInt64 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. ///| The dialog that is up, if any.
@@ -125,6 +129,12 @@ fn ChandraCompositor::auth_tick(self : ChandraCompositor) -> Unit {
self.close_auth(None) self.close_auth(None)
} else { } else {
self.auth_helper_events(ui) 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 => () None => ()
} }
@@ -157,6 +167,8 @@ fn ChandraCompositor::open_auth(self : ChandraCompositor, req : @polkit.AuthRequ
icon, icon,
hits: [], hits: [],
opened: @sys.monotonic_ms(), opened: @sys.monotonic_ms(),
quick_failures: 0,
retry_at: 0UL,
} }
self.auth = Some(ui) self.auth = Some(ui)
// Nothing else gets the keyboard or the pointer while it is up. // 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) => { Some(h) => {
let evs = h.read() let evs = h.read()
for ev in evs { 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())) self.auth_effect(ui, ui.dialog.helper(ev, @sys.monotonic_ms()))
if self.auth is None { if self.auth is None {
return return
@@ -215,7 +248,27 @@ fn ChandraCompositor::auth_effect(self : ChandraCompositor, ui : AuthUi, e : @po
} }
@polkit.zero(b) @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"))) Cancelled => self.close_auth(Some(Err("The user dismissed the authentication dialog")))
Authorized => self.close_auth(Some(Ok(()))) 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 mods = self.mods | self.mods_latched | self.mods_locked
let ch = self.auth_keymap().char_for(code, mods, self.mods_group) let ch = self.auth_keymap().char_for(code, mods, self.mods_group)
self.auth_effect(ui, ui.dialog.key(code, self.mods, ch)) 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 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). ///| Whether the dialog animates (a failed attempt shakes it; a spinner while PAM checks).
fn ChandraCompositor::auth_animating(self : ChandraCompositor) -> Bool { fn ChandraCompositor::auth_animating(self : ChandraCompositor) -> Bool {
match self.auth { 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 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) assert_false(comp.shell.native_auth_agent)
comp.close() 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> };
#|};
#|
#|};
+22
View File
@@ -91,6 +91,10 @@ pub(all) enum DialogEffect {
Answer(FixedArray[Byte]) Answer(FixedArray[Byte])
/// Run the helper again (another user picked, or the last attempt failed). /// Run the helper again (another user picked, or the last attempt failed).
Restart Restart
/// The helper ended before anything was answered (it couldn't run, PAM refused at once, the
/// account is locked): run it again later, not at once, or a helper that fails straight away
/// restarts in a loop and wipes the field under the user's fingers.
RetryLater
/// The user dismissed the dialog. /// The user dismissed the dialog.
Cancelled Cancelled
/// The helper said SUCCESS. /// The helper said SUCCESS.
@@ -161,6 +165,8 @@ pub(all) struct AuthDialog {
mut focus : Focus mut focus : Focus
/// Enter came before PAM's prompt: answer as soon as it does. /// Enter came before PAM's prompt: answer as soon as it does.
mut submit_queued : Bool mut submit_queued : Bool
/// An answer went to the helper in this attempt (a failure then means a wrong answer).
mut answered : Bool
/// Face unlock in this attempt (NoFace when no face module spoke). /// Face unlock in this attempt (NoFace when no face module spoke).
mut face : FaceState mut face : FaceState
/// When face unlock started looking (monotonic ms; the scan animation's clock). /// When face unlock started looking (monotonic ms; the scan animation's clock).
@@ -186,6 +192,7 @@ pub fn AuthDialog::new(req : AuthRequest, desc : Description) -> AuthDialog {
shake_at: 0UL, shake_at: 0UL,
focus: Field, focus: Field,
submit_queued: false, submit_queued: false,
answered: false,
face: NoFace, face: NoFace,
face_at: 0UL, face_at: 0UL,
} }
@@ -261,6 +268,7 @@ fn AuthDialog::submit(self : AuthDialog) -> DialogEffect {
} else { } else {
self.phase = Checking self.phase = Checking
self.error = "" self.error = ""
self.answered = true
Answer(self.take_answer()) Answer(self.take_answer())
} }
Starting | Waiting => { Starting | Waiting => {
@@ -318,7 +326,20 @@ pub fn AuthDialog::helper(self : AuthDialog, ev : HelperEvent, now : UInt64) ->
Nothing Nothing
} }
Success => Authorized Success => Authorized
Failure if !self.answered => {
// Nothing was asked or answered: not a wrong password. Keep what was typed.
if self.error == "" {
self.error = "The authentication service stopped before asking for a password."
}
self.phase = Starting
self.info = ""
if self.face == Scanning {
self.face = NoFace
}
RetryLater
}
Failure => { Failure => {
self.answered = false
self.failures = self.failures + 1 self.failures = self.failures + 1
self.shake_at = if now == 0UL { 1UL } else { now } self.shake_at = if now == 0UL { 1UL } else { now }
if self.error == "" { if self.error == "" {
@@ -373,6 +394,7 @@ fn AuthDialog::cycle_user(self : AuthDialog, delta : Int) -> DialogEffect {
self.face = NoFace self.face = NoFace
self.phase = Starting self.phase = Starting
self.submit_queued = false self.submit_queued = false
self.answered = false
Restart Restart
} }
+13 -2
View File
@@ -101,6 +101,14 @@ pub(all) enum HelperRoute {
Program(String) Program(String)
} derive(Eq, Debug) } derive(Eq, Debug)
///| A route, for the log.
fn route_text(r : HelperRoute) -> String {
match r {
Socket(p) => "socket " + p
Program(p) => p
}
}
///| One run of the helper: the connection and the bytes read but not yet a whole line. ///| One run of the helper: the connection and the bytes read but not yet a whole line.
pub struct HelperSession { pub struct HelperSession {
mut fd : Int mut fd : Int
@@ -114,8 +122,11 @@ pub struct HelperSession {
pub fn HelperSession::start_any(routes : Array[HelperRoute], user : String, cookie : String) -> HelperSession? { pub fn HelperSession::start_any(routes : Array[HelperRoute], user : String, cookie : String) -> HelperSession? {
for r in routes { for r in routes {
match HelperSession::start(r, user, cookie) { match HelperSession::start(r, user, cookie) {
Some(s) => return Some(s) Some(s) => {
None => () @sys.log("chandra: polkit helper for " + user + " via " + route_text(r))
return Some(s)
}
None => @sys.log("chandra: polkit helper not reachable via " + route_text(r))
} }
} }
None None