Skip to content

Commit 1e5ef4b

Browse files
authored
fix(cli): handle browser launch correctly under sudo (#130)
Run xdg-open as the invoking user when available and avoid attempting GUI actions from root sessions.
1 parent 4a76a8f commit 1e5ef4b

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

crates/wazuh-cert-oauth2-client/src/services/get_token.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use oauth2::{
33
AuthType, AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, PkceCodeChallenge,
44
RedirectUrl, TokenResponse, TokenUrl,
55
};
6-
use std::process::Command;
6+
use std::{env, process::Command};
77
use wazuh_cert_oauth2_model::models::document::DiscoveryDocument;
88
use wazuh_cert_oauth2_model::models::errors::AppResult;
99
use wazuh_cert_oauth2_model::services::http_client::HttpClient;
@@ -105,12 +105,20 @@ fn open_in_browser(url: &str) -> bool {
105105
target_os = "openbsd"
106106
))]
107107
{
108-
// Try xdg-open; if unavailable, fall back to printing.
109-
return Command::new("xdg-open")
110-
.arg(url)
111-
.spawn()
112-
.map(|_| true)
113-
.unwrap_or(false);
108+
if let Ok(user) = env::var("SUDO_USER") {
109+
return Command::new("runuser")
110+
.arg("-u")
111+
.arg(&user)
112+
.arg("--")
113+
.arg("xdg-open")
114+
.arg(url)
115+
.spawn()
116+
.map(|_| true)
117+
.unwrap_or(false);
118+
} else {
119+
// Likely running as root without sudo
120+
return false;
121+
}
114122
}
115123

116124
// Fallback for any other targets: do nothing.

0 commit comments

Comments
 (0)