@@ -39,9 +39,7 @@ use crate::metadata::{
3939} ;
4040use crate :: safari:: { SAFARI_NAME , SAFARIDRIVER_NAME , SafariManager } ;
4141use crate :: safaritp:: { SAFARITP_NAMES , SafariTPManager } ;
42- use crate :: shell:: {
43- Command , run_shell_command, run_shell_command_by_os, run_shell_command_with_log,
44- } ;
42+ use crate :: shell:: { Command , run_shell_command, run_shell_command_with_log} ;
4543use crate :: stats:: { Props , send_stats_to_plausible} ;
4644use anyhow:: Error ;
4745use anyhow:: anyhow;
@@ -85,19 +83,9 @@ pub const CANARY: &str = "canary";
8583pub const NIGHTLY : & str = "nightly" ;
8684pub const ESR : & str = "esr" ;
8785pub const REG_VERSION_ARG : & str = "version" ;
88- pub const REG_CURRENT_VERSION_ARG : & str = "CurrentVersion" ;
8986pub const REG_PV_ARG : & str = "pv" ;
90- pub const PLIST_COMMAND : & str =
91- r#"/usr/libexec/PlistBuddy -c "print :CFBundleShortVersionString" {}/Contents/Info.plist"# ;
92- pub const HDIUTIL_ATTACH_COMMAND : & str = "hdiutil attach {}" ;
93- pub const HDIUTIL_DETACH_COMMAND : & str = "hdiutil detach /Volumes/{}" ;
94- pub const CP_VOLUME_COMMAND : & str = "cp -R /Volumes/{}/{}.app {}" ;
95- pub const MSIEXEC_INSTALL_COMMAND : & str = "start /wait msiexec /i {} /qn ALLOWDOWNGRADE=1" ;
96- pub const WINDOWS_CHECK_ADMIN_COMMAND : & str = "net session" ;
97- pub const DASH_VERSION : & str = "{}{}{} -v" ;
98- pub const DASH_DASH_VERSION : & str = "{}{}{} --version" ;
99- pub const DOUBLE_QUOTE : & str = r#"""# ;
100- pub const SINGLE_QUOTE : & str = "'" ;
87+ pub const DASH_VERSION : & str = "-v" ;
88+ pub const DASH_DASH_VERSION : & str = "--version" ;
10189pub const ENV_PROGRAM_FILES : & str = "PROGRAMFILES" ;
10290pub const ENV_PROGRAM_FILES_X86 : & str = "PROGRAMFILES(X86)" ;
10391pub const ENV_LOCALAPPDATA : & str = "LOCALAPPDATA" ;
@@ -109,8 +97,6 @@ pub const ARCH_ARM64: &str = "arm64";
10997pub const ARCH_ARM7L : & str = "arm7l" ;
11098pub const ARCH_OTHER : & str = "other" ;
11199pub const TTL_SEC : u64 = 3600 ;
112- pub const UNAME_COMMAND : & str = "uname -{}" ;
113- pub const ESCAPE_COMMAND : & str = r#"printf %q "{}""# ;
114100pub const SNAPSHOT : & str = "SNAPSHOT" ;
115101pub const OFFLINE_REQUEST_ERR_MSG : & str = "Unable to discover proper {} version in offline mode" ;
116102pub const OFFLINE_DOWNLOAD_ERR_MSG : & str = "Unable to download {} in offline mode" ;
@@ -478,11 +464,8 @@ pub trait SeleniumManager {
478464 ) ) ;
479465 let mut browser_version: Option < String > = None ;
480466 for driver_version_command in commands. into_iter ( ) {
481- let output = match run_shell_command_with_log (
482- self . get_logger ( ) ,
483- self . get_os ( ) ,
484- driver_version_command,
485- ) {
467+ let output = match run_shell_command_with_log ( self . get_logger ( ) , driver_version_command)
468+ {
486469 Ok ( out) => out,
487470 Err ( _) => continue ,
488471 } ;
@@ -676,13 +659,9 @@ pub trait SeleniumManager {
676659 }
677660
678661 fn find_driver_in_path ( & self ) -> ( Option < String > , Option < String > ) {
679- let driver_version_command = Command :: new_single ( format_three_args (
680- DASH_DASH_VERSION ,
681- self . get_driver_name ( ) ,
682- "" ,
683- "" ,
684- ) ) ;
685- match run_shell_command_by_os ( self . get_os ( ) , driver_version_command) {
662+ let driver_version_command =
663+ Command :: new ( self . get_driver_name ( ) , vec ! [ String :: from( "--version" ) ] ) ;
664+ match run_shell_command ( driver_version_command) {
686665 Ok ( output) => {
687666 let parsed_version = parse_version ( output, self . get_logger ( ) ) . unwrap_or_default ( ) ;
688667 if !parsed_version. is_empty ( ) {
@@ -713,8 +692,8 @@ pub trait SeleniumManager {
713692 fn is_windows_admin ( & self ) -> bool {
714693 let os = self . get_os ( ) ;
715694 if WINDOWS . is ( os) {
716- let command = Command :: new_single ( WINDOWS_CHECK_ADMIN_COMMAND . to_string ( ) ) ;
717- let output = run_shell_command_by_os ( os , command) . unwrap_or_default ( ) ;
695+ let command = Command :: new ( "net" , vec ! [ String :: from ( "session" ) ] ) ;
696+ let output = run_shell_command ( command) . unwrap_or_default ( ) ;
718697 !output. is_empty ( ) && !output. contains ( "error" ) && !output. contains ( "not recognized" )
719698 } else {
720699 false
@@ -973,7 +952,7 @@ pub trait SeleniumManager {
973952 is_driver_in_path : & bool ,
974953 err : Error ,
975954 ) -> Result < ( ) , Error > {
976- if * is_driver_in_path {
955+ if * is_driver_in_path && self . is_fallback_driver_from_cache ( ) {
977956 self . get_logger ( ) . debug_or_warn (
978957 format ! ( "Exception managing {}: {}" , self . get_browser_name( ) , err) ,
979958 self . is_offline ( ) ,
@@ -1213,35 +1192,22 @@ pub trait SeleniumManager {
12131192 return Ok ( get_win_file_version ( & escaped_browser_path) ) ;
12141193 }
12151194 if !self . is_browser_version_unstable ( ) {
1216- let reg_command =
1217- Command :: new_multiple ( vec ! [ "REG" , "QUERY" , reg_key, "/v" , reg_version_arg] ) ;
1195+ let reg_command = Command :: new (
1196+ "REG" ,
1197+ vec ! [
1198+ String :: from( "QUERY" ) ,
1199+ reg_key. to_string( ) ,
1200+ String :: from( "/v" ) ,
1201+ reg_version_arg. to_string( ) ,
1202+ ] ,
1203+ ) ;
12181204 commands. push ( reg_command) ;
12191205 }
12201206 } else if !escaped_browser_path. is_empty ( ) {
1221- commands. push ( Command :: new_single ( format_three_args (
1222- cmd_version_arg,
1223- "" ,
1224- & escaped_browser_path,
1225- "" ,
1226- ) ) ) ;
1227- commands. push ( Command :: new_single ( format_three_args (
1228- cmd_version_arg,
1229- DOUBLE_QUOTE ,
1230- & browser_path,
1231- DOUBLE_QUOTE ,
1232- ) ) ) ;
1233- commands. push ( Command :: new_single ( format_three_args (
1234- cmd_version_arg,
1235- SINGLE_QUOTE ,
1236- & browser_path,
1237- SINGLE_QUOTE ,
1238- ) ) ) ;
1239- commands. push ( Command :: new_single ( format_three_args (
1240- cmd_version_arg,
1241- "" ,
1242- & browser_path,
1243- "" ,
1244- ) ) ) ;
1207+ commands. push ( Command :: new (
1208+ escaped_browser_path,
1209+ vec ! [ cmd_version_arg. to_string( ) ] ,
1210+ ) ) ;
12451211 }
12461212
12471213 Ok ( self . detect_browser_version ( commands) )
@@ -1259,7 +1225,14 @@ pub trait SeleniumManager {
12591225 }
12601226 }
12611227 if MACOS . is ( self . get_os ( ) ) {
1262- let plist_command = Command :: new_single ( format_one_arg ( PLIST_COMMAND , & browser_path) ) ;
1228+ let plist_command = Command :: new (
1229+ "/usr/libexec/PlistBuddy" ,
1230+ vec ! [
1231+ String :: from( "-c" ) ,
1232+ String :: from( "print :CFBundleShortVersionString" ) ,
1233+ format!( "{}/Contents/Info.plist" , browser_path) ,
1234+ ] ,
1235+ ) ;
12631236 commands. push ( plist_command) ;
12641237 } else {
12651238 return Ok ( None ) ;
@@ -1492,16 +1465,6 @@ pub trait SeleniumManager {
14921465
14931466 if path. exists ( ) {
14941467 escaped_path = self . canonicalize_path ( path. to_path_buf ( ) ) ;
1495- if WINDOWS . is ( self . get_os ( ) ) {
1496- escaped_path = escaped_path. replace ( '\\' , "\\ \\ " ) ;
1497- } else {
1498- let escape_command =
1499- Command :: new_single ( format_one_arg ( ESCAPE_COMMAND , escaped_path. as_str ( ) ) ) ;
1500- escaped_path = run_shell_command ( "bash" , "-c" , escape_command) . unwrap_or_default ( ) ;
1501- if escaped_path. is_empty ( ) {
1502- escaped_path = string_path. clone ( ) ;
1503- }
1504- }
15051468 }
15061469 if !string_path. eq ( & escaped_path) {
15071470 self . get_logger ( ) . trace ( format ! (
@@ -1735,6 +1698,23 @@ pub fn create_http_client(timeout: u64, proxy: &str) -> Result<Client, Error> {
17351698 Ok ( client_builder. build ( ) . unwrap_or_default ( ) )
17361699}
17371700
1701+ #[ cfg( test) ]
1702+ mod tests {
1703+ use super :: * ;
1704+
1705+ #[ test]
1706+ fn get_escaped_path_returns_canonical_path_without_shell_escaping ( ) {
1707+ let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
1708+ let folder = tmp. path ( ) . join ( "space dir" ) ;
1709+ std:: fs:: create_dir ( & folder) . unwrap ( ) ;
1710+
1711+ let manager = crate :: chrome:: ChromeManager :: new ( ) . unwrap ( ) ;
1712+ let escaped = manager. get_escaped_path ( folder. to_string_lossy ( ) . to_string ( ) ) ;
1713+
1714+ assert_eq ! ( escaped, manager. canonicalize_path( folder) ) ;
1715+ }
1716+ }
1717+
17381718pub fn format_one_arg ( string : & str , arg1 : & str ) -> String {
17391719 string. replacen ( "{}" , arg1, 1 )
17401720}
0 commit comments