mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-19 06:26:14 -05:00
refactor: align GP profile and packaging flow
Main changes: - Refactor connect/auth flows around a single OS profile and host identity model, including gateway fallback and cookie-on-stdin handling. - Align request parameters, HIP host-id propagation, SAML webview user-agent behavior, and portal default-browser handling with observed official client behavior. - Add opt-in cookie cache support and Microsoft Defender HIP reporting while keeping host-id validation explicit. - Add BSD build, packaging, release artifact, and provenance support with FreeBSD/OpenBSD path and dependency cleanup. - Update NixOS install documentation and dependency compatibility fixes.
This commit is contained in:
92 files changed
+9847
-2868
No files matched your search
+10
-3
@@ -1,7 +1,7 @@
|
||||
use anyhow::bail;
|
||||
use gpapi::{
|
||||
gp_params::GpParams,
|
||||
portal::{Prelogin, prelogin},
|
||||
portal::{Prelogin, PreloginOptions, prelogin},
|
||||
};
|
||||
|
||||
#[cfg(feature = "browser-auth")]
|
||||
@@ -15,8 +15,15 @@ mod webview;
|
||||
#[cfg(feature = "webview-auth")]
|
||||
pub use webview::*;
|
||||
|
||||
pub async fn auth_prelogin(server: &str, gp_params: &GpParams) -> anyhow::Result<String> {
|
||||
match prelogin(server, gp_params).await? {
|
||||
pub async fn auth_prelogin(
|
||||
server: &str,
|
||||
gp_params: &GpParams,
|
||||
external_browser_requested: bool,
|
||||
) -> anyhow::Result<String> {
|
||||
// gpauth only performs the initial portal/gateway prelogin. Portal policy is
|
||||
// not available here, so gateway prelogin stays on the embedded value.
|
||||
let options = PreloginOptions::default().external_browser_requested(external_browser_requested);
|
||||
match prelogin(server, gp_params, options).await? {
|
||||
Prelogin::Saml(prelogin) => Ok(prelogin.saml_request().to_string()),
|
||||
Prelogin::Standard(_) => bail!("Received non-SAML prelogin response"),
|
||||
}
|
||||
|
||||
@@ -1,8 +1,20 @@
|
||||
mod auth_messenger;
|
||||
mod webview_auth;
|
||||
|
||||
#[cfg_attr(not(target_os = "macos"), path = "webview/unix.rs")]
|
||||
#[cfg_attr(target_os = "macos", path = "webview/macos.rs")]
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
|
||||
#[path = "webview/gtk.rs"]
|
||||
mod platform_impl;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
#[path = "webview/macos.rs"]
|
||||
mod platform_impl;
|
||||
|
||||
#[cfg(not(any(
|
||||
target_os = "macos",
|
||||
target_os = "linux",
|
||||
target_os = "freebsd",
|
||||
target_os = "openbsd"
|
||||
)))]
|
||||
compile_error!("webview-auth is only supported on macOS and GTK/WebKitGTK Unix targets");
|
||||
|
||||
pub use webview_auth::WebviewAuthenticator;
|
||||
@@ -7,7 +7,7 @@ use tokio_util::sync::CancellationToken;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum AuthDataLocation {
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
|
||||
Headers,
|
||||
Body,
|
||||
}
|
||||
@@ -15,7 +15,7 @@ pub(crate) enum AuthDataLocation {
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum AuthError {
|
||||
/// Failed to load page due to TLS error
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
|
||||
TlsError,
|
||||
/// 1. Found auth data in headers/body but it's invalid
|
||||
/// 2. Loaded an empty page, failed to load page. etc.
|
||||
@@ -34,7 +34,7 @@ impl AuthError {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
|
||||
impl AuthError {
|
||||
pub fn not_found_in_headers() -> Self {
|
||||
Self::NotFound(AuthDataLocation::Headers)
|
||||
@@ -155,7 +155,7 @@ impl AuthMessenger {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
|
||||
pub fn read_from_response(&self, auth_response: &impl super::webview_auth::GetHeader) {
|
||||
use log::warn;
|
||||
|
||||
|
||||
@@ -5,8 +5,8 @@ use gpapi::utils::redact::redact_uri;
|
||||
use log::warn;
|
||||
use tauri::webview::PlatformWebview;
|
||||
use webkit2gtk::{
|
||||
LoadEvent, TLSErrorsPolicy, URIResponseExt, WebResource, WebResourceExt, WebViewExt, WebsiteDataManagerExt,
|
||||
gio::Cancellable, glib::GString,
|
||||
LoadEvent, SettingsExt, TLSErrorsPolicy, URIResponseExt, WebResource, WebResourceExt, WebViewExt,
|
||||
WebsiteDataManagerExt, gio::Cancellable, glib::GString,
|
||||
};
|
||||
|
||||
use super::{
|
||||
@@ -33,6 +33,26 @@ impl PlatformWebviewExt for PlatformWebview {
|
||||
bail!("Failed to get website data manager");
|
||||
}
|
||||
|
||||
fn user_agent(&self) -> anyhow::Result<String> {
|
||||
let Some(settings) = self.inner().settings() else {
|
||||
bail!("Failed to get webview settings");
|
||||
};
|
||||
|
||||
settings
|
||||
.user_agent()
|
||||
.map(GString::into)
|
||||
.ok_or_else(|| anyhow::anyhow!("Failed to get webview user agent"))
|
||||
}
|
||||
|
||||
fn set_user_agent(&self, user_agent: &str) -> anyhow::Result<()> {
|
||||
let Some(settings) = self.inner().settings() else {
|
||||
bail!("Failed to get webview settings");
|
||||
};
|
||||
|
||||
settings.set_user_agent(Some(user_agent));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_url(&self, url: &str) -> anyhow::Result<()> {
|
||||
self.inner().load_uri(url);
|
||||
Ok(())
|
||||
@@ -1,7 +1,8 @@
|
||||
use anyhow::bail;
|
||||
use block2::RcBlock;
|
||||
use log::warn;
|
||||
use objc2::runtime::AnyObject;
|
||||
use objc2_foundation::{NSError, NSString, NSURL, NSURLRequest};
|
||||
use objc2_foundation::{NSError, NSObject, NSObjectNSKeyValueCoding, NSString, NSURL, NSURLRequest};
|
||||
use objc2_web_kit::WKWebView;
|
||||
use tauri::webview::PlatformWebview;
|
||||
|
||||
@@ -13,6 +14,29 @@ impl PlatformWebviewExt for PlatformWebview {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn user_agent(&self) -> anyhow::Result<String> {
|
||||
unsafe {
|
||||
let wv: &NSObject = &*self.inner().cast();
|
||||
let Some(value) = wv.valueForKey(&NSString::from_str("userAgent")) else {
|
||||
bail!("Failed to get webview user agent");
|
||||
};
|
||||
let Some(user_agent) = value.downcast_ref::<NSString>() else {
|
||||
bail!("Webview user agent is not a string");
|
||||
};
|
||||
|
||||
Ok(user_agent.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn set_user_agent(&self, user_agent: &str) -> anyhow::Result<()> {
|
||||
unsafe {
|
||||
let wv: &WKWebView = &*self.inner().cast();
|
||||
wv.setCustomUserAgent(Some(&NSString::from_str(user_agent)));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_url(&self, url: &str) -> anyhow::Result<()> {
|
||||
unsafe {
|
||||
let wv: &WKWebView = &*self.inner().cast();
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
use anyhow::bail;
|
||||
use gpapi::{auth::SamlAuthData, gp_params::GpParams, utils::redact::redact_uri};
|
||||
use gpapi::{
|
||||
auth::SamlAuthData,
|
||||
gp_params::GpParams,
|
||||
os_profile::{WebviewUserAgent, WebviewUserAgentTransform},
|
||||
utils::redact::redact_uri,
|
||||
};
|
||||
use log::{info, warn};
|
||||
use tauri::{
|
||||
AppHandle, WebviewUrl, WebviewWindow, WindowEvent,
|
||||
@@ -16,6 +21,10 @@ use super::auth_messenger::{AuthError, AuthEvent, AuthMessenger};
|
||||
pub trait PlatformWebviewExt {
|
||||
fn ignore_tls_errors(&self) -> anyhow::Result<()>;
|
||||
|
||||
fn user_agent(&self) -> anyhow::Result<String>;
|
||||
|
||||
fn set_user_agent(&self, user_agent: &str) -> anyhow::Result<()>;
|
||||
|
||||
fn load_url(&self, url: &str) -> anyhow::Result<()>;
|
||||
|
||||
fn load_html(&self, html: &str) -> anyhow::Result<()>;
|
||||
@@ -33,7 +42,7 @@ pub trait PlatformWebviewExt {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
|
||||
pub trait GetHeader {
|
||||
fn get_header(&self, key: &str) -> Option<String>;
|
||||
}
|
||||
@@ -42,6 +51,7 @@ pub struct WebviewAuthenticator<'a> {
|
||||
server: &'a str,
|
||||
gp_params: &'a GpParams,
|
||||
auth_request: Option<&'a str>,
|
||||
webview_user_agent: Option<WebviewUserAgent>,
|
||||
clean: bool,
|
||||
|
||||
is_retrying: tokio::sync::RwLock<bool>,
|
||||
@@ -53,6 +63,7 @@ impl<'a> WebviewAuthenticator<'a> {
|
||||
server,
|
||||
gp_params,
|
||||
auth_request: None,
|
||||
webview_user_agent: None,
|
||||
clean: false,
|
||||
is_retrying: Default::default(),
|
||||
}
|
||||
@@ -63,6 +74,11 @@ impl<'a> WebviewAuthenticator<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_webview_user_agent(mut self, user_agent: WebviewUserAgent) -> Self {
|
||||
self.webview_user_agent = Some(user_agent);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_clean(mut self, clean: bool) -> Self {
|
||||
self.clean = clean;
|
||||
self
|
||||
@@ -116,7 +132,7 @@ impl<'a> WebviewAuthenticator<'a> {
|
||||
match auth_messenger.subscribe().await? {
|
||||
AuthEvent::Close => bail!("Authentication cancelled"),
|
||||
AuthEvent::RaiseWindow => self.raise_window(&auth_window),
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
|
||||
AuthEvent::Error(AuthError::TlsError) => bail!(gpapi::error::PortalError::TlsError),
|
||||
AuthEvent::Error(AuthError::NotFound(location)) => {
|
||||
info!(
|
||||
@@ -168,15 +184,16 @@ impl<'a> WebviewAuthenticator<'a> {
|
||||
|
||||
let auth_request = match self.auth_request {
|
||||
Some(auth_request) => auth_request.to_string(),
|
||||
None => auth_prelogin(&self.server, &self.gp_params).await?,
|
||||
None => auth_prelogin(&self.server, &self.gp_params, false).await?,
|
||||
};
|
||||
|
||||
let (tx, rx) = oneshot::channel::<anyhow::Result<()>>();
|
||||
let ignore_tls_errors = self.gp_params.ignore_tls_errors();
|
||||
let webview_user_agent = self.webview_user_agent.clone();
|
||||
|
||||
// Set up webview
|
||||
auth_window.with_webview(move |wv| {
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
|
||||
{
|
||||
use super::platform_impl::PlatformWebviewOnResponse;
|
||||
wv.on_response(Box::new(move |response| match response {
|
||||
@@ -190,7 +207,7 @@ impl<'a> WebviewAuthenticator<'a> {
|
||||
wv.ignore_tls_errors()?;
|
||||
}
|
||||
|
||||
wv.load_auth_request(&auth_request)
|
||||
load_auth_request_with_user_agent(&wv, webview_user_agent.as_ref(), &auth_request)
|
||||
}();
|
||||
|
||||
if let Err(result) = tx.send(result) {
|
||||
@@ -240,10 +257,15 @@ impl<'a> WebviewAuthenticator<'a> {
|
||||
document.body.appendChild(loading);
|
||||
"#)?;
|
||||
|
||||
let auth_request = auth_prelogin(&self.server, &self.gp_params).await?;
|
||||
let auth_request = auth_prelogin(&self.server, &self.gp_params, false).await?;
|
||||
|
||||
let (tx, rx) = oneshot::channel::<anyhow::Result<()>>();
|
||||
let webview_user_agent = self.webview_user_agent.clone();
|
||||
auth_window.with_webview(move |wv| {
|
||||
let result = wv.load_auth_request(&auth_request);
|
||||
let result = || -> anyhow::Result<()> {
|
||||
load_auth_request_with_user_agent(&wv, webview_user_agent.as_ref(), &auth_request)
|
||||
}();
|
||||
|
||||
if let Err(result) = tx.send(result) {
|
||||
warn!("Failed to send retry auth result: {:?}", result);
|
||||
}
|
||||
@@ -265,7 +287,7 @@ impl<'a> WebviewAuthenticator<'a> {
|
||||
#[cfg(target_os = "macos")]
|
||||
let result = auth_window.show();
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
|
||||
let result = {
|
||||
use gpapi::utils::window::WindowExt;
|
||||
auth_window.raise()
|
||||
@@ -276,3 +298,175 @@ impl<'a> WebviewAuthenticator<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn load_auth_request_with_user_agent(
|
||||
wv: &impl PlatformWebviewExt,
|
||||
plan: Option<&WebviewUserAgent>,
|
||||
auth_request: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
if let Some(plan) = plan {
|
||||
let user_agent = compose_webview_user_agent(plan, || wv.user_agent())?;
|
||||
wv.set_user_agent(&user_agent)?;
|
||||
}
|
||||
|
||||
wv.load_auth_request(auth_request)
|
||||
}
|
||||
|
||||
fn compose_webview_user_agent(
|
||||
plan: &WebviewUserAgent,
|
||||
native_user_agent: impl FnOnce() -> anyhow::Result<String>,
|
||||
) -> anyhow::Result<String> {
|
||||
let (prefix, default_user_agent, transform) = match plan {
|
||||
WebviewUserAgent::Native { prefix, transform } => (prefix.as_str(), native_user_agent()?, *transform),
|
||||
WebviewUserAgent::Projected {
|
||||
prefix,
|
||||
default_user_agent,
|
||||
} => (
|
||||
prefix.as_str(),
|
||||
default_user_agent.clone(),
|
||||
WebviewUserAgentTransform::None,
|
||||
),
|
||||
};
|
||||
|
||||
let default_user_agent = apply_webview_user_agent_transform(transform, &default_user_agent);
|
||||
prepend_webview_user_agent_prefix(prefix, &default_user_agent)
|
||||
}
|
||||
|
||||
fn apply_webview_user_agent_transform(transform: WebviewUserAgentTransform, user_agent: &str) -> String {
|
||||
match transform {
|
||||
WebviewUserAgentTransform::None => user_agent.to_string(),
|
||||
WebviewUserAgentTransform::LinuxPanGpuiVersion => linux_pan_gpui_user_agent(user_agent),
|
||||
}
|
||||
}
|
||||
|
||||
fn linux_pan_gpui_user_agent(user_agent: &str) -> String {
|
||||
if user_agent.contains("PanGPUI Version/10.0") {
|
||||
return user_agent.to_string();
|
||||
}
|
||||
|
||||
let Some(start) = user_agent.find("Version/") else {
|
||||
return user_agent.to_string();
|
||||
};
|
||||
let end = user_agent[start..]
|
||||
.find(char::is_whitespace)
|
||||
.map(|offset| start + offset)
|
||||
.unwrap_or(user_agent.len());
|
||||
|
||||
format!("{}PanGPUI Version/10.0{}", &user_agent[..start], &user_agent[end..])
|
||||
}
|
||||
|
||||
fn prepend_webview_user_agent_prefix(prefix: &str, default_user_agent: &str) -> anyhow::Result<String> {
|
||||
let prefix = prefix.trim();
|
||||
if prefix.is_empty() {
|
||||
return Ok(default_user_agent.to_string());
|
||||
}
|
||||
|
||||
let default_user_agent = default_user_agent.trim();
|
||||
if default_user_agent.is_empty() {
|
||||
bail!("Failed to read default webview user agent");
|
||||
}
|
||||
|
||||
if default_user_agent.starts_with(prefix) {
|
||||
return Ok(default_user_agent.to_string());
|
||||
}
|
||||
|
||||
Ok(format!("{} {}", prefix, default_user_agent))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn prepends_prefix_to_default_user_agent() {
|
||||
let user_agent = prepend_webview_user_agent_prefix("PAN GlobalProtect/6.3.3", "Mozilla/5.0").unwrap();
|
||||
|
||||
assert_eq!(user_agent, "PAN GlobalProtect/6.3.3 Mozilla/5.0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leaves_already_prefixed_user_agent_unchanged() {
|
||||
let user_agent =
|
||||
prepend_webview_user_agent_prefix("PAN GlobalProtect/6.3.3", "PAN GlobalProtect/6.3.3 Mozilla/5.0").unwrap();
|
||||
|
||||
assert_eq!(user_agent, "PAN GlobalProtect/6.3.3 Mozilla/5.0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blank_prefix_leaves_default_user_agent_unchanged() {
|
||||
let user_agent = prepend_webview_user_agent_prefix(" ", "Mozilla/5.0").unwrap();
|
||||
|
||||
assert_eq!(user_agent, "Mozilla/5.0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_blank_prefix_requires_default_user_agent() {
|
||||
let err = prepend_webview_user_agent_prefix("PAN GlobalProtect/6.3.3", " ").unwrap_err();
|
||||
|
||||
assert!(err.to_string().contains("default webview user agent"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_plan_reads_native_default_user_agent() {
|
||||
let plan = WebviewUserAgent::Native {
|
||||
prefix: "PAN GlobalProtect/6.3.3".to_string(),
|
||||
transform: WebviewUserAgentTransform::None,
|
||||
};
|
||||
let user_agent = compose_webview_user_agent(&plan, || Ok("Mozilla/5.0".to_string())).unwrap();
|
||||
|
||||
assert_eq!(user_agent, "PAN GlobalProtect/6.3.3 Mozilla/5.0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linux_transform_replaces_version_token_with_pan_gpui_version() {
|
||||
let user_agent = linux_pan_gpui_user_agent(
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/60.5 Safari/605.1.15",
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
user_agent,
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) PanGPUI Version/10.0 Safari/605.1.15"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linux_transform_does_not_duplicate_pan_gpui_version() {
|
||||
let user_agent = linux_pan_gpui_user_agent(
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/602.1 (KHTML, like Gecko) PanGPUI Version/10.0 Safari/602.1",
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
user_agent,
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/602.1 (KHTML, like Gecko) PanGPUI Version/10.0 Safari/602.1"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_linux_plan_applies_pan_gpui_transform_before_prefix() {
|
||||
let plan = WebviewUserAgent::Native {
|
||||
prefix: "PAN GlobalProtect/6.3.3".to_string(),
|
||||
transform: WebviewUserAgentTransform::LinuxPanGpuiVersion,
|
||||
};
|
||||
let user_agent =
|
||||
compose_webview_user_agent(&plan, || Ok("Mozilla/5.0 Version/60.5 Safari/605.1.15".to_string())).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
user_agent,
|
||||
"PAN GlobalProtect/6.3.3 Mozilla/5.0 PanGPUI Version/10.0 Safari/605.1.15"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn projected_plan_uses_profile_default_user_agent() {
|
||||
let plan = WebviewUserAgent::Projected {
|
||||
prefix: "PAN GlobalProtect/6.3.3".to_string(),
|
||||
default_user_agent: "Mozilla/5.0 projected".to_string(),
|
||||
};
|
||||
let user_agent = compose_webview_user_agent(&plan, || {
|
||||
panic!("projected webview user agent should not read the native default")
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(user_agent, "PAN GlobalProtect/6.3.3 Mozilla/5.0 projected");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user