diff --git a/Cargo.lock b/Cargo.lock index b1917f45..ebee16bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10461,6 +10461,7 @@ dependencies = [ "dyn-clone", "ed25519-dalek-bip32", "futures", + "getrandom 0.2.15", "hcl-edit", "hex", "highway", diff --git a/crates/txtx-addon-kit/Cargo.toml b/crates/txtx-addon-kit/Cargo.toml index 59c2e370..64420fbb 100644 --- a/crates/txtx-addon-kit/Cargo.toml +++ b/crates/txtx-addon-kit/Cargo.toml @@ -35,15 +35,18 @@ hmac = "0.12.0" pbkdf2 = { version = "0.12.2", features = ["simple"], default-features = false } libsecp256k1 = { version = "0.7.0" } keccak-hash = "0.11.0" -dirs = "5.0.1" +dirs = { version = "5.0.1", optional = true } dyn-clone = "1" [dev-dependencies] test-case = "3.3" hiro-system-kit = "0.3.4" +[target.'cfg(target_arch = "wasm32")'.dependencies] +getrandom = { version = "0.2", features = ["js"] } + [features] -default=[] +default = ["dirs"] wasm = [] [lib] diff --git a/crates/txtx-addon-kit/src/types/mod.rs b/crates/txtx-addon-kit/src/types/mod.rs index ebd92bc5..f774ba2b 100644 --- a/crates/txtx-addon-kit/src/types/mod.rs +++ b/crates/txtx-addon-kit/src/types/mod.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +#[cfg(feature = "dirs")] use std::env; use std::fmt::Display; use std::path::PathBuf; @@ -323,8 +324,18 @@ impl AuthorizationContext { let path_str = input.to_string_lossy(); let loc = if let Some(stripped) = path_str.strip_prefix("~/") { - let home = PathBuf::from(get_home_dir()); - FileLocation::from_path(home.join(stripped)) + #[cfg(feature = "dirs")] + { + let home = PathBuf::from(get_home_dir()); + FileLocation::from_path(home.join(stripped)) + } + #[cfg(not(feature = "dirs"))] + { + let _ = stripped; + return Err( + "Home directory expansion (~/) is not supported in this build".to_string(), + ); + } } // If absolute, use as-is else if input.is_absolute() { @@ -349,6 +360,7 @@ impl AuthorizationContext { /// We set out snap build to set this environment variable to the real home directory, /// because by default, snaps run in a confined environment where the home directory is not /// the user's actual home directory. +#[cfg(feature = "dirs")] fn get_home_dir() -> String { if let Ok(real_home) = env::var("SNAP_REAL_HOME") { let path_buf = PathBuf::from(real_home); diff --git a/crates/txtx-addon-kit/src/types/tests/mod.rs b/crates/txtx-addon-kit/src/types/tests/mod.rs index a357fbd5..e9f8f793 100644 --- a/crates/txtx-addon-kit/src/types/tests/mod.rs +++ b/crates/txtx-addon-kit/src/types/tests/mod.rs @@ -61,7 +61,16 @@ fn it_rejects_invalid_keys() { } } +#[cfg(feature = "dirs")] #[test_case("~/home/path", dirs::home_dir().unwrap().join("home/path").to_str().unwrap())] +fn test_auth_context_get_path_from_str_home(path_str: &str, expected: &str) { + let auth_context = AuthorizationContext::new(FileLocation::from_path( + Path::new("/workspace/txtx.yml").to_path_buf(), + )); + let result = auth_context.get_file_location_from_path_buf(&PathBuf::from(path_str)).unwrap(); + assert_eq!(result.to_string(), expected); +} + #[test_case("/absolute/path", "/absolute/path")] #[test_case("./relative/path", "/workspace/./relative/path"; "current directory")] #[test_case("../relative/path", "/workspace/../relative/path"; "parent directory")]