From 07d3337bef1513e02b6a6580e3100ced15a41ae1 Mon Sep 17 00:00:00 2001 From: Russell Cohen Date: Fri, 24 May 2024 20:38:32 +0000 Subject: [PATCH 1/3] Escape Pattern before being passed to glob --- src/lib.rs | 4 ++-- tests/[funky-javahome]/nested/more*nested/libjvm.dll | 1 + tests/[funky-javahome]/nested/more*nested/libjvm.so | 1 + tests/ensure-escaped.rs | 11 +++++++++++ 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 tests/[funky-javahome]/nested/more*nested/libjvm.dll create mode 100644 tests/[funky-javahome]/nested/more*nested/libjvm.so create mode 100644 tests/ensure-escaped.rs diff --git a/src/lib.rs b/src/lib.rs index 7be5d31..245fae7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,7 +91,7 @@ use std::env; use std::path::PathBuf; use std::process::Command; -use glob::glob; +use glob::{glob, Pattern}; use lazy_static::lazy_static; pub mod errors; @@ -252,7 +252,7 @@ pub fn locate_file(file_name: &str) -> errors::Result { // Find the JAVA_HOME let java_home = locate_java_home()?; - let query = format!("{}/**/{}", java_home, file_name); + let query = format!("{}/**/{}", Pattern::escape(&java_home), file_name); let paths_vec: Vec = glob(&query)? .filter_map(Result::ok) diff --git a/tests/[funky-javahome]/nested/more*nested/libjvm.dll b/tests/[funky-javahome]/nested/more*nested/libjvm.dll new file mode 100644 index 0000000..3f875ac --- /dev/null +++ b/tests/[funky-javahome]/nested/more*nested/libjvm.dll @@ -0,0 +1 @@ +# test file for windows diff --git a/tests/[funky-javahome]/nested/more*nested/libjvm.so b/tests/[funky-javahome]/nested/more*nested/libjvm.so new file mode 100644 index 0000000..1dd4e4f --- /dev/null +++ b/tests/[funky-javahome]/nested/more*nested/libjvm.so @@ -0,0 +1 @@ +# Test file for unix diff --git a/tests/ensure-escaped.rs b/tests/ensure-escaped.rs new file mode 100644 index 0000000..bc2c218 --- /dev/null +++ b/tests/ensure-escaped.rs @@ -0,0 +1,11 @@ +use java_locator::locate_jvm_dyn_library; + +#[test] +fn test_javahome_can_be_escaped() { + println!("{:?}", std::env::current_dir()); + std::env::set_var("JAVA_HOME", "tests/[funky-javahome]/nested"); + assert_eq!( + locate_jvm_dyn_library().expect("failed to located jvm library"), + "tests/[funky-javahome]/nested/more*nested" + ); +} From 0f3ee87253aee7c5abd300251c3380af082c6735 Mon Sep 17 00:00:00 2001 From: Russell Cohen Date: Fri, 24 May 2024 20:41:02 +0000 Subject: [PATCH 2/3] Run cargo fmt --- src/lib.rs | 65 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 245fae7..15e8be5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,13 +162,9 @@ pub fn get_jvm_dyn_lib_file_name() -> &'static str { /// If `JAVA_HOME` is not defined, the function tries to locate it using the `java` executable. pub fn locate_java_home() -> errors::Result { match &env::var("JAVA_HOME") { - Ok(s) if s.is_empty() => { - do_locate_java_home() - } + Ok(s) if s.is_empty() => do_locate_java_home(), Ok(java_home_env_var) => Ok(java_home_env_var.clone()), - Err(_) => { - do_locate_java_home() - } + Err(_) => do_locate_java_home(), } } @@ -189,25 +185,31 @@ fn do_locate_java_home() -> errors::Result { } let output = command.output().map_err(|error| { - let message = format!("Command '{}' is not found in the system PATH ({})", command_str, error); + let message = format!( + "Command '{}' is not found in the system PATH ({})", + command_str, error + ); errors::JavaLocatorError::new(&message) })?; - let java_exec_path = String::from_utf8(output.stdout) - .map(|jp| { - let mut lines: Vec<&str> = jp.lines().collect(); - if lines.len() > 1 { - println!("WARNING: java_locator found {} possible java locations: {}. Using the last one.", - lines.len(), - lines.join(", ")); - lines.remove(lines.len() - 1).to_string() - } else { - jp - } - })?; + let java_exec_path = String::from_utf8(output.stdout).map(|jp| { + let mut lines: Vec<&str> = jp.lines().collect(); + if lines.len() > 1 { + println!( + "WARNING: java_locator found {} possible java locations: {}. Using the last one.", + lines.len(), + lines.join(", ") + ); + lines.remove(lines.len() - 1).to_string() + } else { + jp + } + })?; // Return early in case that the java executable is not found if java_exec_path.is_empty() { - Err(errors::JavaLocatorError::new("Java is not installed or not added in the system PATH"))? + Err(errors::JavaLocatorError::new( + "Java is not installed or not added in the system PATH", + ))? } let mut test_path = PathBuf::from(java_exec_path.trim()); @@ -230,17 +232,16 @@ fn do_locate_java_home() -> errors::Result { match test_path.to_str() { Some(s) => Ok(String::from(s)), - None => Err(errors::JavaLocatorError::new(&format!("Could not convert path {:?} to String", test_path))), + None => Err(errors::JavaLocatorError::new(&format!( + "Could not convert path {:?} to String", + test_path + ))), } } /// Returns the path that contains the `libjvm.so` (or `jvm.dll` in windows). pub fn locate_jvm_dyn_library() -> errors::Result { - let jvm_dyn_lib_file_name = if is_windows() { - "jvm.dll" - } else { - "libjvm.*" - }; + let jvm_dyn_lib_file_name = if is_windows() { "jvm.dll" } else { "libjvm.*" }; locate_file(jvm_dyn_lib_file_name) } @@ -265,7 +266,10 @@ pub fn locate_file(file_name: &str) -> errors::Result { .collect(); if paths_vec.is_empty() { - Err(errors::JavaLocatorError::new(&format!("Could not find the {} library in any subdirectory of {}", file_name, java_home))) + Err(errors::JavaLocatorError::new(&format!( + "Could not find the {} library in any subdirectory of {}", + file_name, java_home + ))) } else { Ok(paths_vec[0].clone()) } @@ -278,11 +282,14 @@ mod unit_tests { #[test] fn locate_java_home_test() { println!("locate_java_home: {}", locate_java_home().unwrap()); - println!("locate_jvm_dyn_library: {}", locate_jvm_dyn_library().unwrap()); + println!( + "locate_jvm_dyn_library: {}", + locate_jvm_dyn_library().unwrap() + ); } #[test] fn locate_java_from_exec_test() { println!("do_locate_java_home: {}", do_locate_java_home().unwrap()); } -} \ No newline at end of file +} From 9af3cc5fc251d770189e6b61ac33b9f0c9cf12fb Mon Sep 17 00:00:00 2001 From: Russell Cohen Date: Tue, 28 May 2024 15:01:36 +0000 Subject: [PATCH 3/3] Fix pathing for windows by cfg-gating test --- .../nested/more*nested/libjvm.dll | 1 - .../nested/more*nested/libjvm.so | 1 - tests/ensure-escaped.rs | 22 ++++++++++++++++--- 3 files changed, 19 insertions(+), 5 deletions(-) delete mode 100644 tests/[funky-javahome]/nested/more*nested/libjvm.dll delete mode 100644 tests/[funky-javahome]/nested/more*nested/libjvm.so diff --git a/tests/[funky-javahome]/nested/more*nested/libjvm.dll b/tests/[funky-javahome]/nested/more*nested/libjvm.dll deleted file mode 100644 index 3f875ac..0000000 --- a/tests/[funky-javahome]/nested/more*nested/libjvm.dll +++ /dev/null @@ -1 +0,0 @@ -# test file for windows diff --git a/tests/[funky-javahome]/nested/more*nested/libjvm.so b/tests/[funky-javahome]/nested/more*nested/libjvm.so deleted file mode 100644 index 1dd4e4f..0000000 --- a/tests/[funky-javahome]/nested/more*nested/libjvm.so +++ /dev/null @@ -1 +0,0 @@ -# Test file for unix diff --git a/tests/ensure-escaped.rs b/tests/ensure-escaped.rs index bc2c218..fc7770f 100644 --- a/tests/ensure-escaped.rs +++ b/tests/ensure-escaped.rs @@ -1,11 +1,27 @@ use java_locator::locate_jvm_dyn_library; +// Windows does not support `[`, `]`, or `*` in paths so this test does not apply +#[cfg(not(target_os = "windows"))] #[test] fn test_javahome_can_be_escaped() { - println!("{:?}", std::env::current_dir()); - std::env::set_var("JAVA_HOME", "tests/[funky-javahome]/nested"); + use std::env::temp_dir; + + let test_path = temp_dir() + .join("[funky-javahome]") + .join("nested") + .join("*dir*"); + + std::fs::create_dir_all(&test_path).expect("failed to create directory"); + std::fs::write(test_path.join("libjvm.so"), "stub-file").unwrap(); + std::env::set_var( + "JAVA_HOME", + test_path.to_str().expect("no invalid characters"), + ); assert_eq!( locate_jvm_dyn_library().expect("failed to located jvm library"), - "tests/[funky-javahome]/nested/more*nested" + format!( + "{}", + temp_dir().join("[funky-javahome]/nested/*dir*").display() + ) ); }