Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 105 additions & 22 deletions crates/phishkit-core/src/kit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,108 @@ 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<PathBuf> {
if let Ok(p) = std::env::var("PHISHKIT_ROOT") {
let root = PathBuf::from(p);
validate_kit_root(&root)?;
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<PathBuf> {
let mut out: Vec<PathBuf> = 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/<bin>; 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<PathBuf>, 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<PathBuf> {
let mut v: Vec<PathBuf> = 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<PathBuf> {
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!(
Expand Down Expand Up @@ -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:?}"
);
}
}
Loading