mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-20 06:56:17 -05:00
fix: quote bundled macOS VPN script path
This commit is contained in:
2 files changed
+58
-2
No files matched your search
@@ -53,8 +53,13 @@ impl VpnTaskContext {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
let allow_extend_session = args.allow_extend_session();
|
let allow_extend_session = args.allow_extend_session();
|
||||||
let vpn = match Vpn::builder(req.gateway().server(), args.cookie())
|
let mut vpn_builder = Vpn::builder(req.gateway().server(), args.cookie());
|
||||||
.script(args.vpnc_script())
|
vpn_builder = if self.brokered_macos {
|
||||||
|
vpn_builder.script_path(args.vpnc_script())
|
||||||
|
} else {
|
||||||
|
vpn_builder.script(args.vpnc_script())
|
||||||
|
};
|
||||||
|
let vpn = match vpn_builder
|
||||||
.user_agent(args.user_agent())
|
.user_agent(args.user_agent())
|
||||||
.os(args.openconnect_os())
|
.os(args.openconnect_os())
|
||||||
.os_version(args.os_version())
|
.os_version(args.os_version())
|
||||||
|
|||||||
@@ -184,6 +184,7 @@ pub struct VpnBuilder {
|
|||||||
server: String,
|
server: String,
|
||||||
cookie: String,
|
cookie: String,
|
||||||
script: Option<String>,
|
script: Option<String>,
|
||||||
|
script_is_path: bool,
|
||||||
interface: Option<String>,
|
interface: Option<String>,
|
||||||
script_tun: bool,
|
script_tun: bool,
|
||||||
|
|
||||||
@@ -217,6 +218,7 @@ impl VpnBuilder {
|
|||||||
server: server.to_string(),
|
server: server.to_string(),
|
||||||
cookie: cookie.to_string(),
|
cookie: cookie.to_string(),
|
||||||
script: None,
|
script: None,
|
||||||
|
script_is_path: false,
|
||||||
interface: None,
|
interface: None,
|
||||||
script_tun: false,
|
script_tun: false,
|
||||||
|
|
||||||
@@ -246,6 +248,13 @@ impl VpnBuilder {
|
|||||||
|
|
||||||
pub fn script<T: Into<Option<String>>>(mut self, script: T) -> Self {
|
pub fn script<T: Into<Option<String>>>(mut self, script: T) -> Self {
|
||||||
self.script = script.into();
|
self.script = script.into();
|
||||||
|
self.script_is_path = false;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn script_path<T: Into<Option<String>>>(mut self, script: T) -> Self {
|
||||||
|
self.script = script.into();
|
||||||
|
self.script_is_path = true;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,6 +361,9 @@ impl VpnBuilder {
|
|||||||
fn determine_script(&self) -> Result<&str, VpnError> {
|
fn determine_script(&self) -> Result<&str, VpnError> {
|
||||||
match &self.script {
|
match &self.script {
|
||||||
Some(script) => {
|
Some(script) => {
|
||||||
|
if self.script_is_path && !std::path::Path::new(script).exists() {
|
||||||
|
return Err(VpnError::new(format!("VPN script does not exist: {script}")));
|
||||||
|
}
|
||||||
check_executable(script).map_err(|e| VpnError::new(e.to_string()))?;
|
check_executable(script).map_err(|e| VpnError::new(e.to_string()))?;
|
||||||
Ok(script)
|
Ok(script)
|
||||||
}
|
}
|
||||||
@@ -378,6 +390,11 @@ impl VpnBuilder {
|
|||||||
|
|
||||||
pub fn build(self) -> Result<Vpn, VpnError> {
|
pub fn build(self) -> Result<Vpn, VpnError> {
|
||||||
let script = self.determine_script()?.to_owned();
|
let script = self.determine_script()?.to_owned();
|
||||||
|
let script = if self.script_is_path {
|
||||||
|
shell_quote_path(&script)
|
||||||
|
} else {
|
||||||
|
script
|
||||||
|
};
|
||||||
let csd_wrapper = self.determine_csd_wrapper()?.map(|s| s.to_owned());
|
let csd_wrapper = self.determine_csd_wrapper()?.map(|s| s.to_owned());
|
||||||
|
|
||||||
let user_agent = self.user_agent.unwrap_or_default();
|
let user_agent = self.user_agent.unwrap_or_default();
|
||||||
@@ -422,11 +439,45 @@ impl VpnBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn shell_quote_path(path: &str) -> String {
|
||||||
|
format!("'{}'", path.replace('\'', "'\\''"))
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn quotes_vpnc_script_path_for_the_shell() {
|
||||||
|
assert_eq!(
|
||||||
|
shell_quote_path("/Applications/GP Connect.app/Contents/Helpers/vpnc-script"),
|
||||||
|
"'/Applications/GP Connect.app/Contents/Helpers/vpnc-script'"
|
||||||
|
);
|
||||||
|
assert_eq!(shell_quote_path("/tmp/user's script"), "'/tmp/user'\\''s script'");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn preserves_vpnc_script_commands() {
|
||||||
|
let command = "\"/tmp/script with spaces\" --option";
|
||||||
|
let vpn = Vpn::builder("vpn.example.com", "cookie")
|
||||||
|
.script(command.to_string())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(vpn.script.to_str().unwrap(), command);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn quotes_explicit_vpnc_script_paths() {
|
||||||
|
let vpn = Vpn::builder("vpn.example.com", "cookie")
|
||||||
|
.script_path("/bin/sh".to_string())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(vpn.script.to_str().unwrap(), "'/bin/sh'");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn maps_session_info_from_callback_payload() {
|
fn maps_session_info_from_callback_payload() {
|
||||||
let message = CString::new("Session expires soon").unwrap();
|
let message = CString::new("Session expires soon").unwrap();
|
||||||
|
|||||||
Reference in new issue
Block a user