From 1a97c3d8717d92625a4dae9fd0a2de7b6d3b3bd0 Mon Sep 17 00:00:00 2001 From: Adam Siwiec <73085927+adamsiwiec1@users.noreply.github.com> Date: Mon, 26 Oct 2026 07:25:03 -0400 Subject: [PATCH] core: resolve kit root from installed layouts, not just dev checkout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A brew-installed desktop app (or CLI) has no kit/ tree on the Cargo-manifest or dev path, so kit_root() failed with "Could not find phishkit root" and the Activity log flooded. Extend resolution to also check installed layouts: - macOS .app bundle resources: /../Resources and ../Resources/kit-root (Tauri bundle.resources), and ../share/phishkit beside the binary - Homebrew/standard prefixes: $HOMEBREW_PREFIX, /opt/homebrew, /usr/local -> share/phishkit[/kit-root] - macOS Application Support (com.phishkit.phishkit / com.phishkit.app)/kit-root - ~/.phishkit PHISHKIT_ROOT still wins. Also fix validate_kit_root, which checked the modern kit/evilginx/phishlets path twice instead of falling back to the legacy evilginx/phishlets layout. Added tests (legacy layout + homebrew-share candidate). NOTE: this is the resolution half — a release must also *ship* the kit tree into one of these locations (see PR body). --- crates/phishkit-core/src/kit.rs | 127 ++++++++++++++++++++++++++------ 1 file changed, 105 insertions(+), 22 deletions(-) diff --git a/crates/phishkit-core/src/kit.rs b/crates/phishkit-core/src/kit.rs index ba2cf70..c4a4aeb 100644 --- a/crates/phishkit-core/src/kit.rs +++ b/crates/phishkit-core/src/kit.rs @@ -2,7 +2,16 @@ use std::path::{Path, PathBuf}; use crate::error::{AppError, AppResult}; -/// Resolve the phishkit kit root (repo containing kit/evilginx/, vendor/). +/// Resolve the phishkit kit root (repo/dir containing kit/evilginx/, vendor/). +/// +/// Resolution order: +/// 1. `PHISHKIT_ROOT` env var (explicit override). +/// 2. Parents of the Cargo manifest dir (development / `cargo run`). +/// 3. Parents of the running executable, plus macOS `.app` sibling dirs +/// (`../Resources`, `../share/phishkit`) for bundled layouts. +/// 4. Standard installed locations (Homebrew `share/phishkit`, the app's +/// Application Support dir, `~/.phishkit`) so a brew-installed desktop +/// app or CLI finds the kit without a manual `PHISHKIT_ROOT`. pub fn kit_root() -> AppResult { if let Ok(p) = std::env::var("PHISHKIT_ROOT") { let root = PathBuf::from(p); @@ -10,40 +19,91 @@ pub fn kit_root() -> AppResult { return Ok(root); } - // Walk parents from the Cargo manifest (apps/desktop/src-tauri → repo root). - let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - let mut cur = Some(manifest.as_path()); - for _ in 0..8 { - if let Some(dir) = cur { - if validate_kit_root(dir).is_ok() { - return Ok(dir.to_path_buf()); - } - cur = dir.parent(); + for candidate in candidate_roots() { + if validate_kit_root(&candidate).is_ok() { + return Ok(candidate); } } - // Packaged: look next to the executable, then walk parents. + Err(AppError::msg( + "Could not find phishkit root. Set PHISHKIT_ROOT to the kit directory.", + )) +} + +/// Ordered list of directories that might be a kit root, excluding the explicit +/// `PHISHKIT_ROOT` override (handled separately so its failure is surfaced). +fn candidate_roots() -> Vec { + let mut out: Vec = Vec::new(); + + // Walk parents from the Cargo manifest (apps/desktop/src-tauri -> repo root). + let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + push_ancestors(&mut out, &manifest, 8); + + // Packaged: look next to the executable and walk its parents. if let Ok(exe) = std::env::current_exe() { - let mut cur = exe.parent().map(|p| p.to_path_buf()); - for _ in 0..8 { - if let Some(ref dir) = cur { - if validate_kit_root(dir).is_ok() { - return Ok(dir.clone()); - } + if let Some(dir) = exe.parent() { + // macOS .app: exe is Contents/MacOS/; bundled resources live in + // Contents/Resources (Tauri `bundle.resources`) and a CLI cask may + // drop the kit beside the binary under share/phishkit. + out.push(dir.join("../Resources")); + out.push(dir.join("../Resources/kit-root")); + out.push(dir.join("../share/phishkit")); + push_ancestors(&mut out, dir, 8); + } + } + + // Standard installed locations. + for prefix in install_prefixes() { + out.push(prefix.join("share/phishkit")); + out.push(prefix.join("share/phishkit/kit-root")); + } + if let Some(home) = home_dir() { + // macOS Application Support (both the historic and current bundle ids). + for id in ["com.phishkit.phishkit", "com.phishkit.app"] { + out.push( + home.join("Library/Application Support") + .join(id) + .join("kit-root"), + ); + } + out.push(home.join(".phishkit")); + } + + out +} + +fn push_ancestors(out: &mut Vec, start: &Path, max: usize) { + let mut cur = Some(start.to_path_buf()); + for _ in 0..max { + match cur { + Some(ref dir) => { + out.push(dir.clone()); cur = dir.parent().map(|p| p.to_path_buf()); } + None => break, } } +} - Err(AppError::msg( - "Could not find phishkit root. Set PHISHKIT_ROOT to the kit directory.", - )) +fn install_prefixes() -> Vec { + let mut v: Vec = Vec::new(); + if let Ok(p) = std::env::var("HOMEBREW_PREFIX") { + if !p.is_empty() { + v.push(PathBuf::from(p)); + } + } + v.push(PathBuf::from("/opt/homebrew")); + v.push(PathBuf::from("/usr/local")); + v +} + +fn home_dir() -> Option { + std::env::var_os("HOME").map(PathBuf::from) } pub fn validate_kit_root(root: &Path) -> AppResult<()> { // Prefer kit/evilginx; accept legacy evilginx/ during migration. - if root.join("kit/evilginx/phishlets").is_dir() || root.join("kit/evilginx/phishlets").is_dir() - { + if root.join("kit/evilginx/phishlets").is_dir() || root.join("evilginx/phishlets").is_dir() { Ok(()) } else { Err(AppError::msg(format!( @@ -111,4 +171,27 @@ mod tests { assert!(validate_kit_root(tmp.path()).is_ok()); assert_eq!(evilginx_dir(tmp.path()), tmp.path().join("kit/evilginx")); } + + #[test] + fn validate_kit_root_accepts_legacy_layout() { + let tmp = tempfile::tempdir().unwrap(); + fs::create_dir_all(tmp.path().join("evilginx/phishlets")).unwrap(); + assert!(validate_kit_root(tmp.path()).is_ok()); + } + + #[test] + fn candidate_roots_include_homebrew_share() { + // SAFETY: single-threaded test; restores env afterwards. + let prev = std::env::var_os("HOMEBREW_PREFIX"); + unsafe { std::env::set_var("HOMEBREW_PREFIX", "/fake/brew") }; + let roots = candidate_roots(); + match prev { + Some(v) => unsafe { std::env::set_var("HOMEBREW_PREFIX", v) }, + None => unsafe { std::env::remove_var("HOMEBREW_PREFIX") }, + } + assert!( + roots.contains(&PathBuf::from("/fake/brew/share/phishkit")), + "expected homebrew share/phishkit candidate, got: {roots:?}" + ); + } }