fix: improve authentication recovery and diagnostics

Recognize real GTK Wayland crash messages and keep remote
authentication prompts readable. Preserve device identifiers
without initializing OpenSSL, validate diagnostic executable
paths, and improve release automation.

Fixes #669
This commit is contained in:
Kevin Yue committed 2026-10-12 13:37:15 +08:00
1 parent d73b712dd3
commit 93504eaec2
15 files changed
+294 -15

No files matched your search

+3 -3
View File
@@ -14,7 +14,9 @@ impl AuthServer {
let server = Server::http(addr).map_err(|err| anyhow::anyhow!(err))?;
let auth_id = Uuid::new_v4().to_string();
Ok(Self { server, auth_id })
let auth_server = Self { server, auth_id };
info!("auth server started at: {}", auth_server.auth_url());
Ok(auth_server)
}
pub fn auth_url(&self) -> String {
@@ -22,8 +24,6 @@ impl AuthServer {
}
pub fn serve_request(&self, auth_request: &str) {
info!("auth server started at: {}", self.auth_url());
for req in self.server.incoming_requests() {
info!("received request, method: {}, url: {}", req.method(), req.url());
+9 -2
View File
@@ -1,4 +1,7 @@
use std::net::{IpAddr, SocketAddr};
use std::{
io::Write,
net::{IpAddr, SocketAddr},
};
use browser_launcher::Browser as LaunchBrowser;
use gpapi::auth::SamlAuthData;
@@ -34,7 +37,7 @@ impl<'a> BrowserAuthenticator<'a> {
let auth_url = auth_server.auth_url();
let auth_request = self.auth_request.to_string();
tokio::spawn(async move {
let auth_server_task = tokio::task::spawn_blocking(move || {
auth_server.serve_request(&auth_request);
});
@@ -55,6 +58,7 @@ Note that the URL is only valid for a single use.
"#,
auth_url
);
auth_server_task.await?;
return read_auth_data_from_stdin();
}
@@ -95,6 +99,9 @@ async fn detect_local_ip() -> anyhow::Result<IpAddr> {
}
fn read_auth_data_from_stdin() -> anyhow::Result<SamlAuthData> {
eprint!("\nAuthentication callback (paste and press Enter): ");
std::io::stderr().flush()?;
let mut data = String::new();
std::io::stdin().read_line(&mut data)?;
+1
View File
@@ -34,6 +34,7 @@ uzers.workspace = true
serde_urlencoded.workspace = true
md5.workspace = true
sha256.workspace = true
sha2 = "0.10"
uuid = { workspace = true, features = ["v4", "v5", "serde"] }
chrono.workspace = true
humantime.workspace = true
+28 -2
View File
@@ -1,6 +1,7 @@
//! Neutral device-anchor transport model and deterministic derivation.
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -68,7 +69,7 @@ impl DeviceAnchor {
let mut input = domain.to_vec();
input.extend_from_slice(seed.as_ref());
Self {
hardware_id: URL_SAFE_NO_PAD.encode(openssl::sha::sha256(&input)),
hardware_id: URL_SAFE_NO_PAD.encode(Sha256::digest(&input)),
source,
}
}
@@ -78,11 +79,36 @@ impl DeviceAnchor {
mod tests {
use super::*;
#[test]
fn preserves_existing_hardware_ids_for_all_sources() {
let cases: [(AnchorSource, &[u8], &str); 3] = [
(
AnchorSource::UnixMac,
&[0, 17, 34, 51, 68, 85],
"UWDOx4AVJEuCAYfJBaPioyGtRfAkJBhMYSi8LuFsE1A",
),
(
AnchorSource::UnixRandom,
&[0, 17, 34, 51, 68, 85],
"fYXFpivIFuD9Z4I6qSNxUIMTljeLvw4xuqXgg0VJAu0",
),
(
AnchorSource::MacosIokit,
b"12345678-1234-5678-9abc-def012345678",
"K1KMtAwiOiXKQvQuRmTDKPA6-ZPF5QKUWZzmvRzeP34",
),
];
for (source, seed, expected) in cases {
let anchor = DeviceAnchor::derive(source, seed);
assert_eq!(anchor.hardware_id, expected, "{source:?}");
assert!(anchor.validate());
}
}
#[test]
fn hashes_original_seed_order_with_stable_domain_separation() {
let mac = DeviceAnchor::derive(AnchorSource::UnixMac, [0, 17, 34, 51, 68, 85]);
assert!(mac.validate());
assert_eq!(mac.hardware_id, "UWDOx4AVJEuCAYfJBaPioyGtRfAkJBhMYSi8LuFsE1A");
assert_ne!(
mac.hardware_id,
DeviceAnchor::derive(AnchorSource::UnixMac, [85, 68, 51, 34, 17, 0]).hardware_id
+1 -1
View File
@@ -510,7 +510,7 @@ mod tests {
let executable = directory.path().join("auth-fixture");
std::fs::write(
&executable,
"#!/bin/sh\nprintf 'Gdk-Message: Error 71 (Protocol error) dispatching to Wayland display\\n' >&2\nexit 71\n",
"#!/bin/sh\nprintf 'Gdk-Message: Error 71 (Protocol error) dispatching to Wayland display.\\n' >&2\nexit 1\n",
)
.unwrap();
// The launcher drops root privileges in CI; its child must be able to
@@ -110,7 +110,7 @@ mod tests {
let path = fixture(
&dir,
&credential,
"printf '(gpgui-helper:42): Gdk-Message: 12:00:00: Error 71 (Protocol error) dispatching to Wayland display\\n' >&2; exit 71",
"printf '(gpgui-helper:42): Gdk-Message: 12:00:00: Error 71 (Protocol error) dispatching to Wayland display.\\n' >&2; exit 1",
);
let mut envs = HashMap::from([("WAYLAND_DISPLAY".into(), "wayland-0".into())]);
for override_value in [None, Some(""), Some("0"), Some("1")] {
+41 -4
View File
@@ -97,14 +97,15 @@ impl StderrDiagnostic {
}
fn check_line(&mut self) {
let line = self.line.as_slice();
let line = self.line.trim_ascii_end();
let message = line.strip_suffix(b".").unwrap_or(line);
if !self.overflow
&& (line.starts_with(b"Gdk-Message:")
|| line.starts_with(b"(gpgui:")
|| line.starts_with(b"(gpauth:")
|| line.starts_with(b"(gpgui-helper:"))
&& line.windows(b"Gdk-Message:".len()).any(|part| part == b"Gdk-Message:")
&& line.strip_suffix(b"\r").unwrap_or(line).ends_with(DIAGNOSTIC)
&& message.ends_with(DIAGNOSTIC)
{
self.matched = true;
}
@@ -123,13 +124,49 @@ mod tests {
#[test]
fn recognizes_split_diagnostic_without_matching_quoted_warning() {
let mut observer = StderrDiagnostic::default();
observer.push(b"warning: Gdk-Message: Error 71 (Protocol error) dispatching to Wayland display\n");
observer.push(b"warning: Gdk-Message: Error 71 (Protocol error) dispatching to Wayland display.\n");
assert!(!observer.matched());
observer.push(b"Gdk-Message: 12:00:00: Error 71 (Protocol error) dispat");
observer.push(b"ching to Wayland display\n");
observer.push(b"ching to Wayland display.\n");
assert!(observer.matched());
}
#[test]
fn recognizes_real_gtk_diagnostic_with_prefix_and_line_endings() {
for prefix in [
"Gdk-Message:",
"(gpauth:42): Gdk-Message:",
"(gpgui:42): Gdk-Message:",
"(gpgui-helper:42): Gdk-Message:",
] {
for punctuation in ["", "."] {
for ending in ["\n", "\r\n", "", " \t\n"] {
let line = format!(
"{prefix} 10:49:22.617: Error 71 (Protocol error) dispatching to Wayland display{punctuation}{ending}"
);
let mut observer = StderrDiagnostic::default();
for chunk in line.as_bytes().chunks(7) {
observer.push(chunk);
}
assert!(observer.matched(), "unrecognized GTK diagnostic: {line:?}");
}
}
}
}
#[test]
fn rejects_quoted_or_extended_gtk_diagnostic() {
for line in [
"warning: Gdk-Message: Error 71 (Protocol error) dispatching to Wayland display.\n",
"Gdk-Message: Error 71 (Protocol error) dispatching to Wayland display. Retry suggested\n",
"Gdk-Message: Error 70 (Communication error) dispatching to Wayland display.\n",
] {
let mut observer = StderrDiagnostic::default();
observer.push(line.as_bytes());
assert!(!observer.matched(), "incorrectly recognized: {line:?}");
}
}
#[test]
fn oversized_lines_do_not_grow_or_match() {
let mut observer = StderrDiagnostic::default();