Skip to main content

tsafe_core/
profile.rs

1//! Profile management — path resolution, validation, and global config.
2//!
3//! A "profile" is a named vault file. Default paths split durable vault data,
4//! mutable app state, and config across the platform directories exposed by
5//! `ProjectDirs`. All path helpers also respect `TSAFE_VAULT_DIR` so tests can
6//! redirect I/O to a temporary directory without touching the real user paths.
7
8use std::path::PathBuf;
9
10use directories::{ProjectDirs, UserDirs};
11use serde::{Deserialize, Serialize};
12
13use crate::errors::{SafeError, SafeResult};
14
15const EXEC_CONFIG_KEY: &str = "exec";
16const EXEC_MODE_KEY: &str = "mode";
17const EXEC_CUSTOM_INHERIT_KEY: &str = "custom_inherit";
18const EXEC_CUSTOM_DENY_DANGEROUS_ENV_KEY: &str = "custom_deny_dangerous_env";
19const EXEC_AUTO_REDACT_OUTPUT_KEY: &str = "auto_redact_output";
20const EXEC_EXTRA_SENSITIVE_PARENT_VARS_KEY: &str = "extra_sensitive_parent_vars";
21const QUICK_UNLOCK_CONFIG_KEY: &str = "quick_unlock";
22const QUICK_UNLOCK_AUTO_RETRIEVE_KEY: &str = "auto_retrieve";
23const QUICK_UNLOCK_RETRY_COOLDOWN_SECS_KEY: &str = "retry_cooldown_secs";
24
25#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26pub enum ExecMode {
27    Standard,
28    Hardened,
29    Custom,
30}
31
32impl ExecMode {
33    pub fn as_str(self) -> &'static str {
34        match self {
35            Self::Standard => "standard",
36            Self::Hardened => "hardened",
37            Self::Custom => "custom",
38        }
39    }
40}
41
42#[derive(Clone, Copy, Debug, PartialEq, Eq)]
43pub enum ExecCustomInheritMode {
44    Full,
45    Minimal,
46    Clean,
47}
48
49impl ExecCustomInheritMode {
50    pub fn as_str(self) -> &'static str {
51        match self {
52            Self::Full => "full",
53            Self::Minimal => "minimal",
54            Self::Clean => "clean",
55        }
56    }
57}
58
59fn project_dirs() -> Option<ProjectDirs> {
60    ProjectDirs::from("", "", "tsafe")
61}
62
63/// Platform data root for durable vault data and snapshots (app id: `tsafe`).
64fn platform_data_root() -> PathBuf {
65    project_dirs()
66        .map(|d| d.data_dir().to_path_buf())
67        .unwrap_or_else(|| PathBuf::from(".tsafe"))
68}
69
70/// Platform config root for persisted settings such as `config.json`.
71fn platform_config_root() -> PathBuf {
72    project_dirs()
73        .map(|d| d.config_dir().to_path_buf())
74        .unwrap_or_else(platform_data_root)
75}
76
77/// Platform state root for receipts and mutable runtime state.
78fn platform_state_root() -> PathBuf {
79    project_dirs()
80        .and_then(|d| d.state_dir().map(|p| p.to_path_buf()))
81        .unwrap_or_else(platform_data_root)
82}
83
84fn vault_location_from_env() -> Option<PathBuf> {
85    std::env::var("TSAFE_VAULT_DIR").ok().map(PathBuf::from)
86}
87
88/// Parent directory of `vaults/` — durable vault data, snapshots, browser mappings.
89pub fn app_data_dir() -> PathBuf {
90    if let Some(v) = vault_location_from_env() {
91        v.parent()
92            .map(|p| p.to_path_buf())
93            .unwrap_or_else(|| PathBuf::from("."))
94    } else {
95        platform_data_root()
96    }
97}
98
99/// Parent directory for audit receipts and mutable runtime state.
100pub fn app_state_dir() -> PathBuf {
101    if let Some(v) = vault_location_from_env() {
102        v.parent()
103            .map(|p| p.join("state"))
104            .unwrap_or_else(|| PathBuf::from(".tsafe-state"))
105    } else {
106        platform_state_root()
107    }
108}
109
110/// Base dir for all vault files. Override with `TSAFE_VAULT_DIR`.
111pub fn vault_dir() -> PathBuf {
112    if let Some(v) = vault_location_from_env() {
113        return v;
114    }
115    platform_data_root().join("vaults")
116}
117
118/// Base dir for audit log files. Follows `TSAFE_VAULT_DIR` or the platform state root.
119pub fn audit_dir() -> PathBuf {
120    app_state_dir().join("audit")
121}
122
123/// Path to the global config file (`~/.config/tsafe/config.json` or similar).
124pub fn config_path() -> PathBuf {
125    if let Some(v) = vault_location_from_env() {
126        return v
127            .parent()
128            .map(|p| p.join("config.json"))
129            .unwrap_or_else(|| PathBuf::from(".tsafe/config.json"));
130    }
131    platform_config_root().join("config.json")
132}
133
134/// Return the persisted default profile name. Falls back to `"default"` if no config is set.
135pub fn get_default_profile() -> String {
136    let path = config_path();
137    if let Ok(contents) = std::fs::read_to_string(&path) {
138        if let Ok(cfg) = serde_json::from_str::<serde_json::Value>(&contents) {
139            if let Some(name) = cfg.get("default_profile").and_then(|v| v.as_str()) {
140                if !name.is_empty() {
141                    return name.to_string();
142                }
143            }
144        }
145    }
146    "default".to_string()
147}
148
149fn read_config_map() -> serde_json::Map<String, serde_json::Value> {
150    let path = config_path();
151    std::fs::read_to_string(&path)
152        .ok()
153        .and_then(|s| serde_json::from_str(&s).ok())
154        .unwrap_or_default()
155}
156
157fn write_config_map(cfg: &serde_json::Map<String, serde_json::Value>) -> SafeResult<()> {
158    let path = config_path();
159    if let Some(parent) = path.parent() {
160        std::fs::create_dir_all(parent).map_err(SafeError::Io)?;
161    }
162    let json = serde_json::to_string_pretty(&serde_json::Value::Object(cfg.clone()))?;
163    let tmp = path.with_extension("json.tmp");
164    std::fs::write(&tmp, json).map_err(SafeError::Io)?;
165    std::fs::rename(&tmp, &path).map_err(SafeError::Io)?;
166    Ok(())
167}
168
169fn ensure_object_slot<'a>(
170    cfg: &'a mut serde_json::Map<String, serde_json::Value>,
171    key: &str,
172) -> &'a mut serde_json::Map<String, serde_json::Value> {
173    if !matches!(cfg.get(key), Some(serde_json::Value::Object(_))) {
174        cfg.insert(
175            key.to_string(),
176            serde_json::Value::Object(Default::default()),
177        );
178    }
179    cfg.get_mut(key)
180        .and_then(serde_json::Value::as_object_mut)
181        .expect("object slot must exist")
182}
183
184fn exec_config(
185    cfg: &serde_json::Map<String, serde_json::Value>,
186) -> Option<&serde_json::Map<String, serde_json::Value>> {
187    cfg.get(EXEC_CONFIG_KEY)
188        .and_then(serde_json::Value::as_object)
189}
190
191fn quick_unlock_config(
192    cfg: &serde_json::Map<String, serde_json::Value>,
193) -> Option<&serde_json::Map<String, serde_json::Value>> {
194    cfg.get(QUICK_UNLOCK_CONFIG_KEY)
195        .and_then(serde_json::Value::as_object)
196}
197
198fn parse_env_toggle(name: &str) -> Option<bool> {
199    let raw = std::env::var(name).ok()?;
200    match raw.trim().to_ascii_lowercase().as_str() {
201        "1" | "true" | "yes" | "on" => Some(true),
202        "0" | "false" | "no" | "off" => Some(false),
203        _ => None,
204    }
205}
206
207/// Persist `name` as the default profile in the config file.
208pub fn set_default_profile(name: &str) -> SafeResult<()> {
209    let mut cfg = read_config_map();
210    cfg.insert(
211        "default_profile".to_string(),
212        serde_json::Value::String(name.to_string()),
213    );
214    write_config_map(&cfg)
215}
216
217/// If set, after each **new** password vault is created, its master password is copied into this
218/// profile's vault under `profile-passwords/<new-profile>` (for recovery via main-vault bridging).
219pub fn get_backup_new_profile_passwords_to() -> Option<String> {
220    let cfg = serde_json::Value::Object(read_config_map());
221    cfg.get("backup_new_profile_passwords_to")
222        .and_then(|v| v.as_str())
223        .map(str::trim)
224        .filter(|s| !s.is_empty())
225        .map(|s| s.to_string())
226}
227
228/// Set or clear the backup target profile (`main` and `default` are typical). Pass `None` to disable.
229pub fn set_backup_new_profile_passwords_to(target: Option<&str>) -> SafeResult<()> {
230    let mut cfg = read_config_map();
231    match target {
232        None | Some("") => {
233            cfg.remove("backup_new_profile_passwords_to");
234        }
235        Some(t) => {
236            validate_profile_name(t)?;
237            cfg.insert(
238                "backup_new_profile_passwords_to".to_string(),
239                serde_json::Value::String(t.to_string()),
240            );
241        }
242    }
243    write_config_map(&cfg)
244}
245
246/// Return whether the CLI should automatically try the OS credential store during normal vault opens.
247///
248/// Environment override: `TSAFE_AUTO_QUICK_UNLOCK=on|off`.
249pub fn get_auto_quick_unlock() -> bool {
250    if let Some(v) = parse_env_toggle("TSAFE_AUTO_QUICK_UNLOCK") {
251        return v;
252    }
253    let cfg = read_config_map();
254    quick_unlock_config(&cfg)
255        .and_then(|quick_unlock| quick_unlock.get(QUICK_UNLOCK_AUTO_RETRIEVE_KEY))
256        .and_then(serde_json::Value::as_bool)
257        .unwrap_or(true)
258}
259
260/// Persist whether the CLI should automatically try the OS credential store during normal vault opens.
261pub fn set_auto_quick_unlock(enabled: bool) -> SafeResult<()> {
262    let mut cfg = read_config_map();
263    let quick_unlock = ensure_object_slot(&mut cfg, QUICK_UNLOCK_CONFIG_KEY);
264    quick_unlock.insert(
265        QUICK_UNLOCK_AUTO_RETRIEVE_KEY.to_string(),
266        serde_json::Value::Bool(enabled),
267    );
268    write_config_map(&cfg)
269}
270
271/// Return the cooldown, in seconds, applied after an automatic quick-unlock failure.
272///
273/// Environment override: `TSAFE_QUICK_UNLOCK_RETRY_COOLDOWN_SECS=<n>`.
274pub fn get_quick_unlock_retry_cooldown_secs() -> u64 {
275    if let Ok(raw) = std::env::var("TSAFE_QUICK_UNLOCK_RETRY_COOLDOWN_SECS") {
276        if let Ok(secs) = raw.trim().parse::<u64>() {
277            return secs;
278        }
279    }
280    let cfg = read_config_map();
281    quick_unlock_config(&cfg)
282        .and_then(|quick_unlock| quick_unlock.get(QUICK_UNLOCK_RETRY_COOLDOWN_SECS_KEY))
283        .and_then(serde_json::Value::as_u64)
284        .unwrap_or(300)
285}
286
287/// Persist the cooldown, in seconds, applied after an automatic quick-unlock failure.
288pub fn set_quick_unlock_retry_cooldown_secs(seconds: u64) -> SafeResult<()> {
289    let mut cfg = read_config_map();
290    let quick_unlock = ensure_object_slot(&mut cfg, QUICK_UNLOCK_CONFIG_KEY);
291    quick_unlock.insert(
292        QUICK_UNLOCK_RETRY_COOLDOWN_SECS_KEY.to_string(),
293        serde_json::Value::Number(seconds.into()),
294    );
295    write_config_map(&cfg)
296}
297
298/// Return true when `tsafe exec` should redact child stdout/stderr by default.
299pub fn get_exec_auto_redact_output() -> bool {
300    let cfg = read_config_map();
301    exec_config(&cfg)
302        .and_then(|exec| exec.get(EXEC_AUTO_REDACT_OUTPUT_KEY))
303        .and_then(serde_json::Value::as_bool)
304        .unwrap_or(false)
305}
306
307/// Persist whether `tsafe exec` should redact child stdout/stderr by default.
308pub fn set_exec_auto_redact_output(enabled: bool) -> SafeResult<()> {
309    let mut cfg = read_config_map();
310    let exec = ensure_object_slot(&mut cfg, EXEC_CONFIG_KEY);
311    exec.insert(
312        EXEC_AUTO_REDACT_OUTPUT_KEY.to_string(),
313        serde_json::Value::Bool(enabled),
314    );
315    write_config_map(&cfg)
316}
317
318/// Return the persisted exec trust mode. Defaults to `custom`, which preserves the
319/// current config-driven exec behavior until stricter presets are selected.
320pub fn get_exec_mode() -> ExecMode {
321    let cfg = read_config_map();
322    match exec_config(&cfg)
323        .and_then(|exec| exec.get(EXEC_MODE_KEY))
324        .and_then(serde_json::Value::as_str)
325    {
326        Some("standard") => ExecMode::Standard,
327        Some("hardened") => ExecMode::Hardened,
328        Some("custom") => ExecMode::Custom,
329        _ => ExecMode::Custom,
330    }
331}
332
333/// Persist the exec trust mode.
334pub fn set_exec_mode(mode: ExecMode) -> SafeResult<()> {
335    let mut cfg = read_config_map();
336    let exec = ensure_object_slot(&mut cfg, EXEC_CONFIG_KEY);
337    exec.insert(
338        EXEC_MODE_KEY.to_string(),
339        serde_json::Value::String(mode.as_str().to_string()),
340    );
341    write_config_map(&cfg)
342}
343
344/// Return the inherit strategy used by exec when mode=`custom`.
345pub fn get_exec_custom_inherit_mode() -> ExecCustomInheritMode {
346    let cfg = read_config_map();
347    match exec_config(&cfg)
348        .and_then(|exec| exec.get(EXEC_CUSTOM_INHERIT_KEY))
349        .and_then(serde_json::Value::as_str)
350    {
351        Some("minimal") => ExecCustomInheritMode::Minimal,
352        Some("clean") => ExecCustomInheritMode::Clean,
353        _ => ExecCustomInheritMode::Full,
354    }
355}
356
357/// Persist the inherit strategy used by exec when mode=`custom`.
358pub fn set_exec_custom_inherit_mode(mode: ExecCustomInheritMode) -> SafeResult<()> {
359    let mut cfg = read_config_map();
360    let exec = ensure_object_slot(&mut cfg, EXEC_CONFIG_KEY);
361    exec.insert(
362        EXEC_CUSTOM_INHERIT_KEY.to_string(),
363        serde_json::Value::String(mode.as_str().to_string()),
364    );
365    write_config_map(&cfg)
366}
367
368/// Return whether exec should deny dangerous injected env names when mode=`custom`.
369pub fn get_exec_custom_deny_dangerous_env() -> bool {
370    let cfg = read_config_map();
371    exec_config(&cfg)
372        .and_then(|exec| exec.get(EXEC_CUSTOM_DENY_DANGEROUS_ENV_KEY))
373        .and_then(serde_json::Value::as_bool)
374        .unwrap_or(true)
375}
376
377/// Persist whether exec should deny dangerous injected env names when mode=`custom`.
378pub fn set_exec_custom_deny_dangerous_env(enabled: bool) -> SafeResult<()> {
379    let mut cfg = read_config_map();
380    let exec = ensure_object_slot(&mut cfg, EXEC_CONFIG_KEY);
381    exec.insert(
382        EXEC_CUSTOM_DENY_DANGEROUS_ENV_KEY.to_string(),
383        serde_json::Value::Bool(enabled),
384    );
385    write_config_map(&cfg)
386}
387
388/// Return additional parent environment variable names to strip during `tsafe exec`.
389pub fn get_exec_extra_sensitive_parent_vars() -> Vec<String> {
390    let cfg = read_config_map();
391    let mut out = Vec::new();
392    if let Some(values) = exec_config(&cfg)
393        .and_then(|exec| exec.get(EXEC_EXTRA_SENSITIVE_PARENT_VARS_KEY))
394        .and_then(serde_json::Value::as_array)
395    {
396        for value in values {
397            if let Some(name) = value.as_str() {
398                let trimmed = name.trim();
399                if validate_env_var_name(trimmed).is_ok()
400                    && !out
401                        .iter()
402                        .any(|existing: &String| existing.eq_ignore_ascii_case(trimmed))
403                {
404                    out.push(trimmed.to_string());
405                }
406            }
407        }
408    }
409    out
410}
411
412/// Add a parent environment variable name to the extra strip list for `tsafe exec`.
413pub fn add_exec_extra_sensitive_parent_var(name: &str) -> SafeResult<()> {
414    let trimmed = name.trim();
415    validate_env_var_name(trimmed)?;
416
417    let mut names = get_exec_extra_sensitive_parent_vars();
418    if !names
419        .iter()
420        .any(|existing| existing.eq_ignore_ascii_case(trimmed))
421    {
422        names.push(trimmed.to_string());
423        names.sort();
424    }
425    set_exec_extra_sensitive_parent_vars(&names)
426}
427
428/// Remove a parent environment variable name from the extra strip list for `tsafe exec`.
429pub fn remove_exec_extra_sensitive_parent_var(name: &str) -> SafeResult<bool> {
430    let trimmed = name.trim();
431    validate_env_var_name(trimmed)?;
432
433    let mut names = get_exec_extra_sensitive_parent_vars();
434    let original_len = names.len();
435    names.retain(|existing| !existing.eq_ignore_ascii_case(trimmed));
436    if names.len() == original_len {
437        return Ok(false);
438    }
439    set_exec_extra_sensitive_parent_vars(&names)?;
440    Ok(true)
441}
442
443fn set_exec_extra_sensitive_parent_vars(names: &[String]) -> SafeResult<()> {
444    let mut cfg = read_config_map();
445    let exec = ensure_object_slot(&mut cfg, EXEC_CONFIG_KEY);
446    exec.insert(
447        EXEC_EXTRA_SENSITIVE_PARENT_VARS_KEY.to_string(),
448        serde_json::Value::Array(
449            names
450                .iter()
451                .map(|name| serde_json::Value::String(name.clone()))
452                .collect(),
453        ),
454    );
455    write_config_map(&cfg)
456}
457
458/// Default age identity path for a profile: `~/.age/tsafe-<profile>.txt`.
459pub fn default_age_identity_path(profile: &str) -> PathBuf {
460    let base = UserDirs::new()
461        .map(|d| d.home_dir().join(".age"))
462        .unwrap_or_else(|| PathBuf::from(".age"));
463    base.join(format!("tsafe-{profile}.txt"))
464}
465
466/// Resolve age identity: `TSAFE_AGE_IDENTITY` if set, else [`default_age_identity_path`].
467pub fn resolve_age_identity_path(profile: &str) -> PathBuf {
468    if let Ok(p) = std::env::var("TSAFE_AGE_IDENTITY") {
469        return PathBuf::from(p);
470    }
471    default_age_identity_path(profile)
472}
473
474/// Resolve the vault file path for a named profile.
475pub fn vault_path(profile: &str) -> PathBuf {
476    vault_dir().join(format!("{profile}.vault"))
477}
478
479/// Resolve the audit log path for a named profile.
480pub fn audit_log_path(profile: &str) -> PathBuf {
481    audit_dir().join(format!("{profile}.audit.jsonl"))
482}
483
484/// Move a profile's snapshot history to a new profile name, updating both the
485/// directory and the snapshot file prefixes so future snapshot commands keep
486/// working under the destination profile.
487pub fn rename_profile_snapshot_history(from: &str, to: &str) -> SafeResult<bool> {
488    let src_dir = crate::snapshot::snapshot_dir(from);
489    if !src_dir.exists() {
490        return Ok(false);
491    }
492
493    let dst_dir = crate::snapshot::snapshot_dir(to);
494    if dst_dir.exists() {
495        return Err(SafeError::InvalidVault {
496            reason: format!("snapshot history already exists for profile '{to}'"),
497        });
498    }
499
500    if let Some(parent) = dst_dir.parent() {
501        std::fs::create_dir_all(parent)?;
502    }
503    std::fs::create_dir(&dst_dir)?;
504
505    let from_prefix = format!("{from}.vault.");
506    let to_prefix = format!("{to}.vault.");
507    for entry in std::fs::read_dir(&src_dir)? {
508        let entry = entry?;
509        let path = entry.path();
510        let name = entry.file_name();
511        let name_text = name.to_string_lossy();
512        let dest_name = if name_text.starts_with(&from_prefix) && name_text.ends_with(".snap") {
513            format!("{}{}", to_prefix, &name_text[from_prefix.len()..])
514        } else {
515            name_text.into_owned()
516        };
517        let dst_path = dst_dir.join(dest_name);
518        if dst_path.exists() {
519            return Err(SafeError::InvalidVault {
520                reason: format!(
521                    "snapshot migration target already exists at '{}'",
522                    dst_path.display()
523                ),
524            });
525        }
526        std::fs::rename(path, dst_path)?;
527    }
528
529    std::fs::remove_dir(&src_dir)?;
530    Ok(true)
531}
532
533/// Path to the browser domain -> profile mapping file.
534pub fn browser_profiles_path() -> PathBuf {
535    vault_dir().join("browser-profiles.json")
536}
537
538/// Structural validation for hostnames the browser extension sends to the native host.
539///
540/// Rejects empty, oversized, non-ASCII, malformed DNS-like, or punycode-prefixed
541/// hostnames before profile mapping or vault I/O. This is a lightweight abuse
542/// / oddity guard. Punycode labels (`xn--*`) are rejected outright because they
543/// are the only IDN-attack vector that survives the "ASCII-only" check —
544/// `xn--pyal-9ja.com` IS ASCII but encodes Cyrillic Unicode confusables.
545/// Full IDN support (decode + confusables-table) is post-v1.
546pub fn browser_hostname_fill_guard(hostname: &str) -> Result<(), &'static str> {
547    let host = hostname.trim().trim_end_matches('.');
548    if host.is_empty() {
549        return Err("empty hostname");
550    }
551    if host.len() > 253 {
552        return Err("hostname too long");
553    }
554    if !host.is_ascii() {
555        return Err("hostname must be ASCII (IDN should be sent as punycode)");
556    }
557    let lower = host.to_ascii_lowercase();
558    let labels: Vec<&str> = lower.split('.').collect();
559    if labels.len() > 12 {
560        return Err("too many hostname labels");
561    }
562    for label in &labels {
563        if label.is_empty() {
564            return Err("empty hostname label");
565        }
566        if label.len() > 63 {
567            return Err("hostname label too long");
568        }
569        if label.starts_with('-') || label.ends_with('-') {
570            return Err("hostname label has invalid hyphen placement");
571        }
572        // RFC 3490 punycode labels (`xn--*`) are how IDN homoglyph attacks
573        // bypass the "ASCII-only" check above: `xn--pyal-9ja.com` IS ASCII
574        // but encodes Cyrillic characters that look identical to a registered
575        // Latin-script domain. Until tsafe ships full IDN support (decode +
576        // confusables-table check), reject all xn-- labels at the door.
577        // Mirrored in `extension/src/content/autofill.ts`'s
578        // `browserHostnameFillGuard` — keep both copies in sync.
579        if label.starts_with("xn--") {
580            return Err("punycode/IDN labels not supported (post-v1)");
581        }
582    }
583    Ok(())
584}
585
586/// Load browser domain -> profile mappings.
587pub fn load_browser_profiles() -> SafeResult<Vec<(String, String)>> {
588    let path = browser_profiles_path();
589    if !path.exists() {
590        return Ok(Vec::new());
591    }
592
593    let value: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(&path)?)?;
594    let map = value.as_object().ok_or_else(|| SafeError::InvalidVault {
595        reason: format!(
596            "browser profile mappings at '{}' must be a JSON object",
597            path.display()
598        ),
599    })?;
600
601    Ok(map
602        .iter()
603        .filter_map(|(domain, profile)| {
604            profile
605                .as_str()
606                .map(|target| (domain.to_string(), target.to_string()))
607        })
608        .collect())
609}
610
611/// Resolve a hostname to a configured browser profile.
612///
613/// Exact matches win. Otherwise, the longest wildcard suffix (`*.corp.example`)
614/// that matches a subdomain is returned.
615pub fn resolve_browser_profile(hostname: &str) -> SafeResult<Option<String>> {
616    let host = hostname.trim().trim_end_matches('.').to_ascii_lowercase();
617    if host.is_empty() {
618        return Ok(None);
619    }
620
621    let mappings = load_browser_profiles()?;
622    if let Some((_, profile)) = mappings
623        .iter()
624        .find(|(domain, _)| !domain.starts_with("*.") && domain.eq_ignore_ascii_case(&host))
625    {
626        return Ok(Some(profile.clone()));
627    }
628
629    let mut best: Option<(usize, &str)> = None;
630    for (pattern, profile) in &mappings {
631        let Some(suffix) = pattern.strip_prefix("*.") else {
632            continue;
633        };
634        let suffix = suffix.trim_end_matches('.').to_ascii_lowercase();
635        if suffix.is_empty() || host == suffix {
636            continue;
637        }
638        if host.ends_with(&suffix) && host.as_bytes()[host.len() - suffix.len() - 1] == b'.' {
639            match best {
640                Some((best_len, _)) if best_len >= suffix.len() => {}
641                _ => best = Some((suffix.len(), profile.as_str())),
642            }
643        }
644    }
645
646    Ok(best.map(|(_, profile)| profile.to_string()))
647}
648
649/// Return all profile names that have an existing vault file.
650pub fn list_profiles() -> SafeResult<Vec<String>> {
651    let dir = vault_dir();
652    if !dir.exists() {
653        return Ok(Vec::new());
654    }
655    let mut names: Vec<String> = std::fs::read_dir(&dir)?
656        .filter_map(|e| e.ok())
657        .filter_map(|e| {
658            let name = e.file_name().to_string_lossy().into_owned();
659            name.strip_suffix(".vault").map(|s| s.to_string())
660        })
661        .collect();
662    names.sort();
663    Ok(names)
664}
665
666/// Return `true` if a vault file exists for the given profile name.
667pub fn profile_exists(profile: &str) -> bool {
668    vault_path(profile).exists()
669}
670
671/// Validate a profile name: alphanumeric, hyphens, underscores only.
672pub fn validate_profile_name(name: &str) -> SafeResult<()> {
673    if name.is_empty() {
674        return Err(SafeError::ProfileNotFound {
675            name: name.to_string(),
676        });
677    }
678    if !name
679        .chars()
680        .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
681    {
682        return Err(SafeError::InvalidVault {
683            reason: format!("profile '{name}': only alphanumeric, '-', '_' characters allowed"),
684        });
685    }
686    Ok(())
687}
688
689/// Validate an environment variable name for config-based strip rules.
690pub fn validate_env_var_name(name: &str) -> SafeResult<()> {
691    if name.is_empty() {
692        return Err(SafeError::InvalidVault {
693            reason: "environment variable name cannot be empty".to_string(),
694        });
695    }
696    if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
697        return Err(SafeError::InvalidVault {
698            reason: format!(
699                "environment variable '{name}': only ASCII letters, digits, and '_' are allowed"
700            ),
701        });
702    }
703    Ok(())
704}
705
706// ── Profile metadata and protection ──────────────────────────────────────────
707
708/// Metadata associated with a named profile, stored in a sidecar JSON file
709/// (`<vault_dir>/<profile>.meta.json`). This is separate from the vault file so
710/// it can be read without the vault password.
711#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
712pub struct ProfileMeta {
713    /// Human-readable description of the profile's purpose.
714    #[serde(default, skip_serializing_if = "Option::is_none")]
715    pub description: Option<String>,
716    /// When the vault file was first created (best-effort; set at meta creation time).
717    pub created_at: chrono::DateTime<chrono::Utc>,
718    /// When the metadata was last modified.
719    pub last_modified: chrono::DateTime<chrono::Utc>,
720    /// If `true`, deletion without `--force` must be blocked with a warning.
721    #[serde(default)]
722    pub is_protected: bool,
723}
724
725impl ProfileMeta {
726    /// Create new metadata with default values and the current timestamp.
727    pub fn new() -> Self {
728        let now = chrono::Utc::now();
729        Self {
730            description: None,
731            created_at: now,
732            last_modified: now,
733            is_protected: false,
734        }
735    }
736
737    /// Touch `last_modified` without changing any other fields.
738    pub fn touch(&mut self) {
739        self.last_modified = chrono::Utc::now();
740    }
741}
742
743impl Default for ProfileMeta {
744    fn default() -> Self {
745        Self::new()
746    }
747}
748
749/// Path to the metadata sidecar file for a named profile.
750pub fn profile_meta_path(profile: &str) -> PathBuf {
751    vault_dir().join(format!("{profile}.meta.json"))
752}
753
754/// Read profile metadata. Returns `None` if the sidecar file does not exist.
755pub fn read_profile_meta(profile: &str) -> SafeResult<Option<ProfileMeta>> {
756    let path = profile_meta_path(profile);
757    if !path.exists() {
758        return Ok(None);
759    }
760    let contents = std::fs::read_to_string(&path).map_err(SafeError::Io)?;
761    let meta: ProfileMeta = serde_json::from_str(&contents)?;
762    Ok(Some(meta))
763}
764
765/// Write profile metadata, creating the vault directory if necessary.
766pub fn write_profile_meta(profile: &str, meta: &ProfileMeta) -> SafeResult<()> {
767    let path = profile_meta_path(profile);
768    if let Some(parent) = path.parent() {
769        std::fs::create_dir_all(parent).map_err(SafeError::Io)?;
770    }
771    let json = serde_json::to_string_pretty(meta)?;
772    let tmp = path.with_extension("meta.json.tmp");
773    std::fs::write(&tmp, json).map_err(SafeError::Io)?;
774    std::fs::rename(&tmp, &path).map_err(SafeError::Io)?;
775    Ok(())
776}
777
778/// Ensure a profile's metadata sidecar exists, creating it with defaults if not.
779pub fn ensure_profile_meta(profile: &str) -> SafeResult<ProfileMeta> {
780    if let Some(existing) = read_profile_meta(profile)? {
781        return Ok(existing);
782    }
783    let meta = ProfileMeta::new();
784    write_profile_meta(profile, &meta)?;
785    Ok(meta)
786}
787
788/// Set the protection flag on a profile's metadata, creating the sidecar if needed.
789pub fn set_profile_protected(profile: &str, protected: bool) -> SafeResult<()> {
790    let mut meta = read_profile_meta(profile)?.unwrap_or_default();
791    meta.is_protected = protected;
792    meta.touch();
793    write_profile_meta(profile, &meta)
794}
795
796/// Returns `true` if the profile exists and has `is_protected = true`.
797pub fn is_profile_protected(profile: &str) -> bool {
798    read_profile_meta(profile)
799        .ok()
800        .flatten()
801        .map(|m| m.is_protected)
802        .unwrap_or(false)
803}
804
805// ── Profile portability (export / import) ─────────────────────────────────────
806
807/// A self-contained, re-encrypted export bundle for a single profile.
808///
809/// The vault file bytes are stored as base64. A separate re-encryption wrapper
810/// (PBKDF2 + XChaCha20-Poly1305) protects the payload so the export file is
811/// safe to copy across machines. The original vault password is **not** stored.
812///
813/// Bundle format version: `1`.
814#[derive(Debug, Serialize, Deserialize)]
815pub struct ProfileBundle {
816    /// Format version for forward compatibility.
817    pub version: u8,
818    /// Profile name at export time (advisory — import may use a different name).
819    pub profile: String,
820    /// Export timestamp.
821    pub exported_at: chrono::DateTime<chrono::Utc>,
822    /// Metadata sidecar at export time (optional).
823    #[serde(default, skip_serializing_if = "Option::is_none")]
824    pub meta: Option<ProfileMeta>,
825    /// PBKDF2-HMAC-SHA256 salt (base64) used to derive the bundle encryption key.
826    pub salt: String,
827    /// XChaCha20-Poly1305 nonce (base64) for the encrypted payload.
828    pub nonce: String,
829    /// Encrypted vault file bytes (base64).
830    pub ciphertext: String,
831}
832
833impl ProfileBundle {
834    const NONCE_LEN: usize = 24;
835    const SALT_LEN: usize = 32;
836    /// Argon2id parameters for bundle key derivation — intentionally lower than
837    /// the vault KDF to allow faster export/import on constrained machines.
838    const KDF_M_COST: u32 = 32_768; // 32 MiB
839    const KDF_T_COST: u32 = 2;
840    const KDF_P_COST: u32 = 1;
841
842    /// Derive a 256-bit key from `password` and `salt` using Argon2id.
843    fn derive_key(password: &[u8], salt: &[u8]) -> SafeResult<[u8; 32]> {
844        let vault_key = crate::crypto::derive_key(
845            password,
846            salt,
847            Self::KDF_M_COST,
848            Self::KDF_T_COST,
849            Self::KDF_P_COST,
850        )?;
851        Ok(*vault_key.as_bytes())
852    }
853
854    /// Encrypt `plaintext` with XChaCha20-Poly1305 under `key` and a random nonce.
855    fn seal(key: &[u8; 32], plaintext: &[u8]) -> SafeResult<([u8; Self::NONCE_LEN], Vec<u8>)> {
856        use chacha20poly1305::{
857            aead::{Aead, KeyInit},
858            XChaCha20Poly1305, XNonce,
859        };
860        use rand::RngCore;
861        let mut nonce_bytes = [0u8; Self::NONCE_LEN];
862        rand::rngs::OsRng.fill_bytes(&mut nonce_bytes);
863        let cipher = XChaCha20Poly1305::new(key.into());
864        let nonce = XNonce::from_slice(&nonce_bytes);
865        let ciphertext = cipher
866            .encrypt(nonce, plaintext)
867            .map_err(|_| SafeError::Crypto {
868                context: "bundle encryption failed".into(),
869            })?;
870        Ok((nonce_bytes, ciphertext))
871    }
872
873    /// Decrypt `ciphertext` with XChaCha20-Poly1305 under `key` and `nonce`.
874    fn open(key: &[u8; 32], nonce: &[u8], ciphertext: &[u8]) -> SafeResult<Vec<u8>> {
875        use chacha20poly1305::{
876            aead::{Aead, KeyInit},
877            XChaCha20Poly1305, XNonce,
878        };
879        let cipher = XChaCha20Poly1305::new(key.into());
880        let nonce = XNonce::from_slice(nonce);
881        cipher
882            .decrypt(nonce, ciphertext)
883            .map_err(|_| SafeError::DecryptionFailed)
884    }
885}
886
887/// Export a profile's vault file to a self-contained encrypted bundle.
888///
889/// `profile` — the profile name whose vault file will be read.
890/// `dest_path` — where to write the bundle JSON.
891/// `bundle_password` — the export password; independent of the vault's master password.
892///
893/// Returns `SafeError::VaultNotFound` if the profile has no vault file.
894pub fn export_profile(
895    profile: &str,
896    dest_path: &std::path::Path,
897    bundle_password: &[u8],
898) -> SafeResult<()> {
899    use base64::{engine::general_purpose::STANDARD as B64, Engine};
900    use rand::RngCore;
901
902    let vault_file_path = vault_path(profile);
903    if !vault_file_path.exists() {
904        return Err(SafeError::VaultNotFound {
905            path: vault_file_path.display().to_string(),
906        });
907    }
908
909    let vault_bytes = std::fs::read(&vault_file_path).map_err(SafeError::Io)?;
910    let meta = read_profile_meta(profile)?;
911
912    let mut salt = [0u8; ProfileBundle::SALT_LEN];
913    rand::rngs::OsRng.fill_bytes(&mut salt);
914
915    let key = ProfileBundle::derive_key(bundle_password, &salt)?;
916    let (nonce, ciphertext) = ProfileBundle::seal(&key, &vault_bytes)?;
917
918    let bundle = ProfileBundle {
919        version: 1,
920        profile: profile.to_string(),
921        exported_at: chrono::Utc::now(),
922        meta,
923        salt: B64.encode(salt),
924        nonce: B64.encode(nonce),
925        ciphertext: B64.encode(&ciphertext),
926    };
927
928    let json = serde_json::to_string_pretty(&bundle)?;
929    if let Some(parent) = dest_path.parent() {
930        if !parent.as_os_str().is_empty() {
931            std::fs::create_dir_all(parent).map_err(SafeError::Io)?;
932        }
933    }
934    std::fs::write(dest_path, json).map_err(SafeError::Io)?;
935    Ok(())
936}
937
938/// Import a profile bundle, registering it under `dest_profile`.
939///
940/// The vault file is decrypted from the bundle and written to the standard vault
941/// directory. If `dest_profile` is `None`, the name embedded in the bundle is used.
942/// Returns `SafeError::VaultAlreadyExists` if the target vault file already exists.
943pub fn import_profile(
944    src_path: &std::path::Path,
945    dest_profile: Option<&str>,
946    bundle_password: &[u8],
947) -> SafeResult<String> {
948    use base64::{engine::general_purpose::STANDARD as B64, Engine};
949
950    let json = std::fs::read_to_string(src_path).map_err(SafeError::Io)?;
951    let bundle: ProfileBundle = serde_json::from_str(&json)?;
952
953    if bundle.version != 1 {
954        return Err(SafeError::InvalidVault {
955            reason: format!(
956                "unsupported profile bundle version: {} (expected 1)",
957                bundle.version
958            ),
959        });
960    }
961
962    let salt = B64
963        .decode(&bundle.salt)
964        .map_err(|_| SafeError::InvalidVault {
965            reason: "bundle salt is not valid base64".into(),
966        })?;
967    let nonce = B64
968        .decode(&bundle.nonce)
969        .map_err(|_| SafeError::InvalidVault {
970            reason: "bundle nonce is not valid base64".into(),
971        })?;
972    let ciphertext = B64
973        .decode(&bundle.ciphertext)
974        .map_err(|_| SafeError::InvalidVault {
975            reason: "bundle ciphertext is not valid base64".into(),
976        })?;
977
978    if salt.len() != ProfileBundle::SALT_LEN {
979        return Err(SafeError::InvalidVault {
980            reason: format!(
981                "bundle salt has wrong length: {} (expected {})",
982                salt.len(),
983                ProfileBundle::SALT_LEN
984            ),
985        });
986    }
987
988    let key = ProfileBundle::derive_key(bundle_password, &salt)?;
989    let vault_bytes = ProfileBundle::open(&key, &nonce, &ciphertext)?;
990
991    let profile_name = dest_profile.unwrap_or(&bundle.profile);
992    validate_profile_name(profile_name)?;
993
994    let dest_vault = vault_path(profile_name);
995    if dest_vault.exists() {
996        return Err(SafeError::VaultAlreadyExists {
997            path: dest_vault.display().to_string(),
998        });
999    }
1000
1001    if let Some(parent) = dest_vault.parent() {
1002        std::fs::create_dir_all(parent).map_err(SafeError::Io)?;
1003    }
1004    std::fs::write(&dest_vault, &vault_bytes).map_err(SafeError::Io)?;
1005
1006    // Restore metadata sidecar if present in the bundle.
1007    if let Some(meta) = &bundle.meta {
1008        write_profile_meta(profile_name, meta)?;
1009    }
1010
1011    Ok(profile_name.to_string())
1012}
1013
1014// ── Phishing / lookalike guard ────────────────────────────────────────────────
1015
1016/// Space-optimised Levenshtein edit distance between two ASCII strings.
1017fn edit_distance(a: &str, b: &str) -> usize {
1018    let a: Vec<char> = a.chars().collect();
1019    let b: Vec<char> = b.chars().collect();
1020    let (m, n) = (a.len(), b.len());
1021    if m == 0 {
1022        return n;
1023    }
1024    if n == 0 {
1025        return m;
1026    }
1027    let mut row: Vec<usize> = (0..=n).collect();
1028    for i in 1..=m {
1029        let mut prev = row[0];
1030        row[0] = i;
1031        for j in 1..=n {
1032            let temp = row[j];
1033            row[j] = if a[i - 1] == b[j - 1] {
1034                prev
1035            } else {
1036                1 + prev.min(row[j]).min(row[j - 1])
1037            };
1038            prev = temp;
1039        }
1040    }
1041    row[n]
1042}
1043
1044/// Strip a leading `www.` from a hostname (ASCII, already lowercased).
1045fn strip_www_prefix(host: &str) -> &str {
1046    host.strip_prefix("www.").unwrap_or(host)
1047}
1048
1049/// A suspicious domain match returned by [`lookalike_check`].
1050pub struct LookalikeMatch {
1051    /// The registered profile domain that the candidate closely resembles.
1052    pub registered: String,
1053    /// Levenshtein edit distance between the normalised forms.
1054    pub edit_distance: usize,
1055}
1056
1057/// Check whether `hostname` is a typosquat lookalike of any registered browser-profile
1058/// domain.
1059///
1060/// Returns the closest suspicious match (edit distance ≤ 1, non-exact) or `None`.
1061///
1062/// Rules:
1063/// - Wildcard patterns (`*.`-prefixed) are skipped — they cover legitimate subdomains.
1064/// - Leading `www.` is stripped from both sides before comparison.
1065/// - Comparison is case-insensitive (ASCII lowercase).
1066/// - Exact matches return `None` (already handled by profile-mapping logic).
1067pub fn lookalike_check(hostname: &str, profiles: &[(String, String)]) -> Option<LookalikeMatch> {
1068    const THRESHOLD: usize = 1;
1069
1070    let candidate = strip_www_prefix(&hostname.to_ascii_lowercase()).to_string();
1071    let mut best: Option<LookalikeMatch> = None;
1072
1073    for (registered_pattern, _) in profiles {
1074        // Skip wildcards — they are subdomain matchers, not canonical domain names.
1075        if registered_pattern.starts_with("*.") {
1076            continue;
1077        }
1078        let registered_lower = registered_pattern.to_ascii_lowercase();
1079        let registered = strip_www_prefix(&registered_lower);
1080
1081        // Exact match → already allowed; not a phishing signal.
1082        if candidate == registered {
1083            continue;
1084        }
1085
1086        let dist = edit_distance(&candidate, registered);
1087        if dist <= THRESHOLD && best.as_ref().is_none_or(|b| dist < b.edit_distance) {
1088            best = Some(LookalikeMatch {
1089                registered: registered_pattern.clone(),
1090                edit_distance: dist,
1091            });
1092        }
1093    }
1094
1095    best
1096}
1097
1098#[cfg(test)]
1099mod tests {
1100    use std::sync::Mutex;
1101
1102    use super::*;
1103
1104    /// `TSAFE_VAULT_DIR` is process-global; serialize tests that touch it.
1105    static PROFILE_TEST_ENV_LOCK: Mutex<()> = Mutex::new(());
1106
1107    // ── Task 1.7: Profile metadata and protection ─────────────────────────────
1108
1109    #[test]
1110    fn profile_meta_roundtrip() {
1111        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1112        let dir = tempfile::tempdir().unwrap();
1113        let vaults = dir.path().join("vaults");
1114        temp_env::with_var(
1115            "TSAFE_VAULT_DIR",
1116            Some(vaults.as_os_str().to_str().unwrap()),
1117            || {
1118                std::fs::create_dir_all(&vaults).unwrap();
1119                let meta = ProfileMeta {
1120                    description: Some("my dev vault".into()),
1121                    created_at: chrono::Utc::now(),
1122                    last_modified: chrono::Utc::now(),
1123                    is_protected: true,
1124                };
1125                write_profile_meta("dev", &meta).unwrap();
1126                let loaded = read_profile_meta("dev")
1127                    .unwrap()
1128                    .expect("meta should exist");
1129                assert_eq!(loaded.description.as_deref(), Some("my dev vault"));
1130                assert!(loaded.is_protected);
1131            },
1132        );
1133    }
1134
1135    #[test]
1136    fn read_profile_meta_missing_returns_none() {
1137        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1138        let dir = tempfile::tempdir().unwrap();
1139        let vaults = dir.path().join("vaults");
1140        temp_env::with_var(
1141            "TSAFE_VAULT_DIR",
1142            Some(vaults.as_os_str().to_str().unwrap()),
1143            || {
1144                assert!(read_profile_meta("nonexistent").unwrap().is_none());
1145            },
1146        );
1147    }
1148
1149    #[test]
1150    fn set_profile_protected_blocks_deletion_signal() {
1151        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1152        let dir = tempfile::tempdir().unwrap();
1153        let vaults = dir.path().join("vaults");
1154        temp_env::with_var(
1155            "TSAFE_VAULT_DIR",
1156            Some(vaults.as_os_str().to_str().unwrap()),
1157            || {
1158                std::fs::create_dir_all(&vaults).unwrap();
1159                // Not protected by default.
1160                assert!(!is_profile_protected("prod"));
1161
1162                set_profile_protected("prod", true).unwrap();
1163                assert!(is_profile_protected("prod"));
1164
1165                // Caller must check is_profile_protected; clearing protection lifts the block.
1166                set_profile_protected("prod", false).unwrap();
1167                assert!(!is_profile_protected("prod"));
1168            },
1169        );
1170    }
1171
1172    #[test]
1173    fn ensure_profile_meta_creates_defaults_when_missing() {
1174        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1175        let dir = tempfile::tempdir().unwrap();
1176        let vaults = dir.path().join("vaults");
1177        temp_env::with_var(
1178            "TSAFE_VAULT_DIR",
1179            Some(vaults.as_os_str().to_str().unwrap()),
1180            || {
1181                std::fs::create_dir_all(&vaults).unwrap();
1182                let meta = ensure_profile_meta("newprofile").unwrap();
1183                assert!(!meta.is_protected);
1184                assert!(meta.description.is_none());
1185                // Calling again should return the persisted value.
1186                let meta2 = ensure_profile_meta("newprofile").unwrap();
1187                assert_eq!(meta.created_at, meta2.created_at);
1188            },
1189        );
1190    }
1191
1192    // ── Task 1.6: Profile portability ─────────────────────────────────────────
1193
1194    #[test]
1195    fn export_import_profile_roundtrip() {
1196        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1197        let dir = tempfile::tempdir().unwrap();
1198        let vaults = dir.path().join("vaults");
1199        temp_env::with_var(
1200            "TSAFE_VAULT_DIR",
1201            Some(vaults.as_os_str().to_str().unwrap()),
1202            || {
1203                std::fs::create_dir_all(&vaults).unwrap();
1204
1205                // Create a minimal vault file (just needs to exist for export).
1206                let vault_content = b"fake-vault-bytes-for-testing";
1207                std::fs::write(vault_path("source"), vault_content).unwrap();
1208
1209                let bundle_path = dir.path().join("source.bundle.json");
1210                let bundle_pw = b"export-password-123";
1211
1212                export_profile("source", &bundle_path, bundle_pw).unwrap();
1213                assert!(bundle_path.exists(), "bundle file should be written");
1214
1215                let imported_name =
1216                    import_profile(&bundle_path, Some("imported"), bundle_pw).unwrap();
1217                assert_eq!(imported_name, "imported");
1218
1219                let imported_vault = vault_path("imported");
1220                assert!(imported_vault.exists(), "imported vault should exist");
1221                let imported_bytes = std::fs::read(&imported_vault).unwrap();
1222                assert_eq!(imported_bytes, vault_content);
1223            },
1224        );
1225    }
1226
1227    #[test]
1228    fn export_import_uses_bundle_profile_name_when_dest_is_none() {
1229        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1230        let dir = tempfile::tempdir().unwrap();
1231        let vaults = dir.path().join("vaults");
1232        temp_env::with_var(
1233            "TSAFE_VAULT_DIR",
1234            Some(vaults.as_os_str().to_str().unwrap()),
1235            || {
1236                std::fs::create_dir_all(&vaults).unwrap();
1237                // Write a source vault under one name, export it,
1238                // then import to a different directory to avoid collision.
1239                std::fs::write(vault_path("srcprofile"), b"vault-data").unwrap();
1240
1241                let bundle_path = dir.path().join("srcprofile.bundle.json");
1242                export_profile("srcprofile", &bundle_path, b"pw").unwrap();
1243
1244                // Remove the source vault so the import-with-embedded-name doesn't collide.
1245                std::fs::remove_file(vault_path("srcprofile")).unwrap();
1246
1247                let name = import_profile(&bundle_path, None, b"pw").unwrap();
1248                assert_eq!(name, "srcprofile");
1249                assert!(vault_path("srcprofile").exists());
1250            },
1251        );
1252    }
1253
1254    #[test]
1255    fn import_with_wrong_password_fails() {
1256        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1257        let dir = tempfile::tempdir().unwrap();
1258        let vaults = dir.path().join("vaults");
1259        temp_env::with_var(
1260            "TSAFE_VAULT_DIR",
1261            Some(vaults.as_os_str().to_str().unwrap()),
1262            || {
1263                std::fs::create_dir_all(&vaults).unwrap();
1264                std::fs::write(vault_path("src"), b"vault-data").unwrap();
1265                let bundle_path = dir.path().join("src.bundle.json");
1266                export_profile("src", &bundle_path, b"correct-pw").unwrap();
1267
1268                let result = import_profile(&bundle_path, Some("dst"), b"wrong-pw");
1269                assert!(
1270                    matches!(result, Err(SafeError::DecryptionFailed)),
1271                    "wrong password should fail decryption"
1272                );
1273            },
1274        );
1275    }
1276
1277    #[test]
1278    fn export_nonexistent_profile_fails() {
1279        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1280        let dir = tempfile::tempdir().unwrap();
1281        let vaults = dir.path().join("vaults");
1282        temp_env::with_var(
1283            "TSAFE_VAULT_DIR",
1284            Some(vaults.as_os_str().to_str().unwrap()),
1285            || {
1286                let bundle_path = dir.path().join("out.json");
1287                let result = export_profile("no-such-profile", &bundle_path, b"pw");
1288                assert!(
1289                    matches!(result, Err(SafeError::VaultNotFound { .. })),
1290                    "expected VaultNotFound"
1291                );
1292            },
1293        );
1294    }
1295
1296    #[test]
1297    fn import_into_existing_profile_fails() {
1298        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1299        let dir = tempfile::tempdir().unwrap();
1300        let vaults = dir.path().join("vaults");
1301        temp_env::with_var(
1302            "TSAFE_VAULT_DIR",
1303            Some(vaults.as_os_str().to_str().unwrap()),
1304            || {
1305                std::fs::create_dir_all(&vaults).unwrap();
1306                std::fs::write(vault_path("src"), b"v1").unwrap();
1307                let bundle_path = dir.path().join("bundle.json");
1308                export_profile("src", &bundle_path, b"pw").unwrap();
1309
1310                // Pre-create the destination.
1311                std::fs::write(vault_path("dst"), b"pre-existing").unwrap();
1312                let result = import_profile(&bundle_path, Some("dst"), b"pw");
1313                assert!(
1314                    matches!(result, Err(SafeError::VaultAlreadyExists { .. })),
1315                    "expected VaultAlreadyExists"
1316                );
1317            },
1318        );
1319    }
1320
1321    #[test]
1322    fn export_import_preserves_metadata() {
1323        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1324        let dir = tempfile::tempdir().unwrap();
1325        let vaults = dir.path().join("vaults");
1326        temp_env::with_var(
1327            "TSAFE_VAULT_DIR",
1328            Some(vaults.as_os_str().to_str().unwrap()),
1329            || {
1330                std::fs::create_dir_all(&vaults).unwrap();
1331                std::fs::write(vault_path("prod"), b"vault-data").unwrap();
1332
1333                // Set up metadata with protection.
1334                set_profile_protected("prod", true).unwrap();
1335                let bundle_path = dir.path().join("prod.bundle.json");
1336                export_profile("prod", &bundle_path, b"pw").unwrap();
1337
1338                let imported = import_profile(&bundle_path, Some("prod-copy"), b"pw").unwrap();
1339                assert_eq!(imported, "prod-copy");
1340                // Metadata should be restored.
1341                let meta = read_profile_meta("prod-copy").unwrap();
1342                assert!(meta.is_some(), "metadata should be imported");
1343                assert!(
1344                    meta.unwrap().is_protected,
1345                    "protection flag should be preserved"
1346                );
1347            },
1348        );
1349    }
1350
1351    #[test]
1352    fn vault_dir_uses_env_override() {
1353        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1354        temp_env::with_var("TSAFE_VAULT_DIR", Some("/tmp/tsafe-vault-test"), || {
1355            assert_eq!(vault_dir(), PathBuf::from("/tmp/tsafe-vault-test"));
1356        });
1357    }
1358
1359    #[test]
1360    fn state_and_config_paths_follow_env_override() {
1361        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1362        temp_env::with_var("TSAFE_VAULT_DIR", Some("/tmp/tsafe/vaults"), || {
1363            assert_eq!(app_data_dir(), PathBuf::from("/tmp/tsafe"));
1364            assert_eq!(app_state_dir(), PathBuf::from("/tmp/tsafe/state"));
1365            assert_eq!(audit_dir(), PathBuf::from("/tmp/tsafe/state/audit"));
1366            assert_eq!(config_path(), PathBuf::from("/tmp/tsafe/config.json"));
1367        });
1368    }
1369
1370    #[test]
1371    fn vault_path_suffix() {
1372        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1373        temp_env::with_var("TSAFE_VAULT_DIR", Some("/tmp/ts"), || {
1374            let p = vault_path("dev");
1375            assert!(p.to_string_lossy().ends_with("dev.vault"));
1376        });
1377    }
1378
1379    #[test]
1380    fn rename_profile_snapshot_history_moves_and_reprefixes_snapshots() {
1381        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1382        let dir = tempfile::tempdir().unwrap();
1383        let vaults = dir.path().join("vaults");
1384        temp_env::with_var(
1385            "TSAFE_VAULT_DIR",
1386            Some(vaults.as_os_str().to_str().unwrap()),
1387            || {
1388                let src_dir = crate::snapshot::snapshot_dir("work");
1389                std::fs::create_dir_all(&src_dir).unwrap();
1390                std::fs::write(src_dir.join("work.vault.123.0000.snap"), b"one").unwrap();
1391                std::fs::write(src_dir.join("work.vault.124.0000.snap"), b"two").unwrap();
1392                std::fs::write(src_dir.join("keep.tmp"), b"tmp").unwrap();
1393
1394                let migrated = rename_profile_snapshot_history("work", "prod").unwrap();
1395
1396                assert!(migrated);
1397                assert!(!src_dir.exists());
1398                let dst_dir = crate::snapshot::snapshot_dir("prod");
1399                assert!(dst_dir.join("prod.vault.123.0000.snap").exists());
1400                assert!(dst_dir.join("prod.vault.124.0000.snap").exists());
1401                assert!(dst_dir.join("keep.tmp").exists());
1402                let listed = crate::snapshot::list("prod").unwrap();
1403                assert_eq!(listed.len(), 2);
1404                assert!(crate::snapshot::list("work").unwrap().is_empty());
1405            },
1406        );
1407    }
1408
1409    #[test]
1410    fn rename_profile_snapshot_history_is_noop_when_source_missing() {
1411        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1412        let dir = tempfile::tempdir().unwrap();
1413        let vaults = dir.path().join("vaults");
1414        temp_env::with_var(
1415            "TSAFE_VAULT_DIR",
1416            Some(vaults.as_os_str().to_str().unwrap()),
1417            || {
1418                let migrated = rename_profile_snapshot_history("missing", "renamed").unwrap();
1419                assert!(!migrated);
1420                assert!(!crate::snapshot::snapshot_dir("renamed").exists());
1421            },
1422        );
1423    }
1424
1425    #[test]
1426    fn rename_profile_snapshot_history_rejects_existing_destination() {
1427        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1428        let dir = tempfile::tempdir().unwrap();
1429        let vaults = dir.path().join("vaults");
1430        temp_env::with_var(
1431            "TSAFE_VAULT_DIR",
1432            Some(vaults.as_os_str().to_str().unwrap()),
1433            || {
1434                std::fs::create_dir_all(crate::snapshot::snapshot_dir("from")).unwrap();
1435                std::fs::create_dir_all(crate::snapshot::snapshot_dir("to")).unwrap();
1436
1437                let err = rename_profile_snapshot_history("from", "to").unwrap_err();
1438
1439                assert!(matches!(err, SafeError::InvalidVault { .. }));
1440            },
1441        );
1442    }
1443
1444    #[test]
1445    fn validate_valid_names() {
1446        for n in ["dev", "prod-1", "my_profile", "ABC123"] {
1447            assert!(validate_profile_name(n).is_ok(), "{n} should be valid");
1448        }
1449    }
1450
1451    #[test]
1452    fn validate_rejects_bad_names() {
1453        for n in ["", "has spaces", "has/slash", "path\\sep", "na:me"] {
1454            assert!(validate_profile_name(n).is_err(), "{n} should be invalid");
1455        }
1456    }
1457
1458    #[test]
1459    fn backup_new_profile_passwords_config_roundtrip() {
1460        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1461        let dir = tempfile::tempdir().unwrap();
1462        let vaults = dir.path().join("vaults");
1463        temp_env::with_var(
1464            "TSAFE_VAULT_DIR",
1465            Some(vaults.as_os_str().to_str().unwrap()),
1466            || {
1467                assert!(get_backup_new_profile_passwords_to().is_none());
1468                set_backup_new_profile_passwords_to(Some("main")).unwrap();
1469                assert_eq!(
1470                    get_backup_new_profile_passwords_to().as_deref(),
1471                    Some("main")
1472                );
1473                set_backup_new_profile_passwords_to(None).unwrap();
1474                assert!(get_backup_new_profile_passwords_to().is_none());
1475            },
1476        );
1477    }
1478
1479    #[test]
1480    fn exec_auto_redact_output_config_roundtrip() {
1481        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1482        let dir = tempfile::tempdir().unwrap();
1483        let vaults = dir.path().join("vaults");
1484        temp_env::with_var(
1485            "TSAFE_VAULT_DIR",
1486            Some(vaults.as_os_str().to_str().unwrap()),
1487            || {
1488                assert!(!get_exec_auto_redact_output());
1489                set_exec_auto_redact_output(true).unwrap();
1490                assert!(get_exec_auto_redact_output());
1491                set_exec_auto_redact_output(false).unwrap();
1492                assert!(!get_exec_auto_redact_output());
1493            },
1494        );
1495    }
1496
1497    #[test]
1498    fn exec_mode_config_roundtrip() {
1499        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1500        let dir = tempfile::tempdir().unwrap();
1501        let vaults = dir.path().join("vaults");
1502        temp_env::with_var(
1503            "TSAFE_VAULT_DIR",
1504            Some(vaults.as_os_str().to_str().unwrap()),
1505            || {
1506                assert_eq!(get_exec_mode(), ExecMode::Custom);
1507                set_exec_mode(ExecMode::Hardened).unwrap();
1508                assert_eq!(get_exec_mode(), ExecMode::Hardened);
1509                set_exec_mode(ExecMode::Standard).unwrap();
1510                assert_eq!(get_exec_mode(), ExecMode::Standard);
1511            },
1512        );
1513    }
1514
1515    #[test]
1516    fn exec_custom_settings_roundtrip() {
1517        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1518        let dir = tempfile::tempdir().unwrap();
1519        let vaults = dir.path().join("vaults");
1520        temp_env::with_var(
1521            "TSAFE_VAULT_DIR",
1522            Some(vaults.as_os_str().to_str().unwrap()),
1523            || {
1524                assert_eq!(get_exec_custom_inherit_mode(), ExecCustomInheritMode::Full);
1525                assert!(get_exec_custom_deny_dangerous_env()); // default is true (deny by default)
1526
1527                set_exec_custom_inherit_mode(ExecCustomInheritMode::Minimal).unwrap();
1528                set_exec_custom_deny_dangerous_env(false).unwrap();
1529
1530                assert_eq!(
1531                    get_exec_custom_inherit_mode(),
1532                    ExecCustomInheritMode::Minimal
1533                );
1534                assert!(!get_exec_custom_deny_dangerous_env()); // explicitly set to false
1535            },
1536        );
1537    }
1538
1539    #[test]
1540    fn exec_extra_sensitive_parent_vars_roundtrip() {
1541        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1542        let dir = tempfile::tempdir().unwrap();
1543        let vaults = dir.path().join("vaults");
1544        temp_env::with_var(
1545            "TSAFE_VAULT_DIR",
1546            Some(vaults.as_os_str().to_str().unwrap()),
1547            || {
1548                assert!(get_exec_extra_sensitive_parent_vars().is_empty());
1549                add_exec_extra_sensitive_parent_var("OPENAI_API_KEY").unwrap();
1550                add_exec_extra_sensitive_parent_var("openai_api_key").unwrap();
1551                add_exec_extra_sensitive_parent_var("ANTHROPIC_API_KEY").unwrap();
1552                assert_eq!(
1553                    get_exec_extra_sensitive_parent_vars(),
1554                    vec![
1555                        "ANTHROPIC_API_KEY".to_string(),
1556                        "OPENAI_API_KEY".to_string()
1557                    ]
1558                );
1559                assert!(remove_exec_extra_sensitive_parent_var("OPENAI_API_KEY").unwrap());
1560                assert_eq!(
1561                    get_exec_extra_sensitive_parent_vars(),
1562                    vec!["ANTHROPIC_API_KEY".to_string()]
1563                );
1564                assert!(!remove_exec_extra_sensitive_parent_var("OPENAI_API_KEY").unwrap());
1565            },
1566        );
1567    }
1568
1569    #[test]
1570    fn auto_quick_unlock_config_roundtrip() {
1571        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1572        let dir = tempfile::tempdir().unwrap();
1573        let vaults = dir.path().join("vaults");
1574        temp_env::with_var(
1575            "TSAFE_VAULT_DIR",
1576            Some(vaults.as_os_str().to_str().unwrap()),
1577            || {
1578                assert!(get_auto_quick_unlock());
1579                set_auto_quick_unlock(false).unwrap();
1580                assert!(!get_auto_quick_unlock());
1581                set_auto_quick_unlock(true).unwrap();
1582                assert!(get_auto_quick_unlock());
1583            },
1584        );
1585    }
1586
1587    #[test]
1588    fn quick_unlock_retry_cooldown_roundtrip() {
1589        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1590        let dir = tempfile::tempdir().unwrap();
1591        let vaults = dir.path().join("vaults");
1592        temp_env::with_var(
1593            "TSAFE_VAULT_DIR",
1594            Some(vaults.as_os_str().to_str().unwrap()),
1595            || {
1596                assert_eq!(get_quick_unlock_retry_cooldown_secs(), 300);
1597                set_quick_unlock_retry_cooldown_secs(45).unwrap();
1598                assert_eq!(get_quick_unlock_retry_cooldown_secs(), 45);
1599                set_quick_unlock_retry_cooldown_secs(0).unwrap();
1600                assert_eq!(get_quick_unlock_retry_cooldown_secs(), 0);
1601            },
1602        );
1603    }
1604
1605    #[test]
1606    fn resolve_browser_profile_prefers_exact_then_longest_wildcard() {
1607        let _guard = PROFILE_TEST_ENV_LOCK.lock().unwrap();
1608        let dir = tempfile::tempdir().unwrap();
1609        let vaults = dir.path().join("vaults");
1610        std::fs::create_dir_all(&vaults).unwrap();
1611        std::fs::write(
1612            vaults.join("browser-profiles.json"),
1613            r#"{
1614  "github.com": "work",
1615  "*.corp.example": "corp",
1616  "*.deep.corp.example": "deep"
1617}"#,
1618        )
1619        .unwrap();
1620
1621        temp_env::with_var(
1622            "TSAFE_VAULT_DIR",
1623            Some(vaults.as_os_str().to_str().unwrap()),
1624            || {
1625                assert_eq!(
1626                    resolve_browser_profile("github.com").unwrap().as_deref(),
1627                    Some("work")
1628                );
1629                assert_eq!(
1630                    resolve_browser_profile("jira.corp.example")
1631                        .unwrap()
1632                        .as_deref(),
1633                    Some("corp")
1634                );
1635                assert_eq!(
1636                    resolve_browser_profile("login.deep.corp.example")
1637                        .unwrap()
1638                        .as_deref(),
1639                    Some("deep")
1640                );
1641                assert!(resolve_browser_profile("corp.example").unwrap().is_none());
1642                assert!(resolve_browser_profile("unknown.example")
1643                    .unwrap()
1644                    .is_none());
1645            },
1646        );
1647    }
1648
1649    // ── edit_distance ──────────────────────────────────────────────────────────
1650
1651    #[test]
1652    fn edit_distance_identical_strings_is_zero() {
1653        assert_eq!(edit_distance("paypal.com", "paypal.com"), 0);
1654        assert_eq!(edit_distance("", ""), 0);
1655    }
1656
1657    #[test]
1658    fn edit_distance_single_substitution_is_one() {
1659        // '1' instead of 'l' — classic zero-to-letter swap
1660        assert_eq!(edit_distance("paypa1.com", "paypal.com"), 1);
1661        // one char off at the end
1662        assert_eq!(edit_distance("github.co", "github.com"), 1);
1663    }
1664
1665    #[test]
1666    fn edit_distance_unrelated_domains_exceeds_threshold() {
1667        assert!(edit_distance("amazon.com", "paypal.com") > 1);
1668        assert!(edit_distance("example.org", "google.com") > 1);
1669    }
1670
1671    // ── lookalike_check ────────────────────────────────────────────────────────
1672
1673    fn profiles_fixture() -> Vec<(String, String)> {
1674        vec![
1675            ("paypal.com".into(), "finance".into()),
1676            ("github.com".into(), "work".into()),
1677            ("*.corp.example".into(), "corp".into()),
1678        ]
1679    }
1680
1681    #[test]
1682    fn lookalike_check_exact_match_returns_none() {
1683        let result = lookalike_check("paypal.com", &profiles_fixture());
1684        assert!(
1685            result.is_none(),
1686            "exact match should not trigger phishing warning"
1687        );
1688    }
1689
1690    #[test]
1691    fn lookalike_check_typosquat_returns_match() {
1692        // paypa1.com is 1 edit away from paypal.com
1693        let result = lookalike_check("paypa1.com", &profiles_fixture());
1694        assert!(
1695            result.is_some(),
1696            "typosquat should trigger phishing warning"
1697        );
1698        let m = result.unwrap();
1699        assert_eq!(m.registered, "paypal.com");
1700        assert_eq!(m.edit_distance, 1);
1701    }
1702
1703    #[test]
1704    fn lookalike_check_www_prefix_stripped() {
1705        // www.paypa1.com should still match paypal.com after stripping www.
1706        let result = lookalike_check("www.paypa1.com", &profiles_fixture());
1707        assert!(result.is_some());
1708        assert_eq!(result.unwrap().registered, "paypal.com");
1709    }
1710
1711    #[test]
1712    fn lookalike_check_unrelated_domain_returns_none() {
1713        let result = lookalike_check("totally-different.io", &profiles_fixture());
1714        assert!(
1715            result.is_none(),
1716            "unrelated domain should not trigger phishing warning"
1717        );
1718    }
1719
1720    #[test]
1721    fn lookalike_check_skips_wildcard_patterns() {
1722        // A 1-edit lookalike of the wildcard suffix shouldn't be flagged
1723        // (wildcards are for subdomains, not canonical domain names)
1724        let result = lookalike_check("corp.exampl", &profiles_fixture());
1725        assert!(result.is_none(), "wildcard patterns should be skipped");
1726    }
1727
1728    #[test]
1729    fn browser_hostname_fill_guard_accepts_normal_hosts() {
1730        assert!(browser_hostname_fill_guard("github.com").is_ok());
1731        assert!(browser_hostname_fill_guard("login.deep.corp.example.").is_ok());
1732    }
1733
1734    #[test]
1735    fn browser_hostname_fill_guard_rejects_garbage() {
1736        assert_eq!(browser_hostname_fill_guard(""), Err("empty hostname"));
1737        assert_eq!(browser_hostname_fill_guard("   "), Err("empty hostname"));
1738        let long_label = format!("{}.com", "a".repeat(64));
1739        assert_eq!(
1740            browser_hostname_fill_guard(&long_label),
1741            Err("hostname label too long")
1742        );
1743        let many = (0..14)
1744            .map(|i| format!("l{i}"))
1745            .collect::<Vec<_>>()
1746            .join(".");
1747        assert_eq!(
1748            browser_hostname_fill_guard(&many),
1749            Err("too many hostname labels")
1750        );
1751        assert_eq!(
1752            browser_hostname_fill_guard("bad-.example.com"),
1753            Err("hostname label has invalid hyphen placement")
1754        );
1755    }
1756
1757    #[test]
1758    fn browser_hostname_fill_guard_rejects_punycode_labels() {
1759        // The IDN-homoglyph attack vector that survives the ASCII-only check.
1760        // `xn--pyal-9ja.com` is ASCII but encodes Cyrillic confusables of
1761        // `paypal.com`. Reject all xn-- labels until we ship full IDN support.
1762        assert_eq!(
1763            browser_hostname_fill_guard("xn--pyal-9ja.com"),
1764            Err("punycode/IDN labels not supported (post-v1)")
1765        );
1766        assert_eq!(
1767            browser_hostname_fill_guard("login.xn--anything-9ja.com"),
1768            Err("punycode/IDN labels not supported (post-v1)")
1769        );
1770        // Case-insensitive: lowercased before the prefix check.
1771        assert_eq!(
1772            browser_hostname_fill_guard("XN--PYAL-9JA.COM"),
1773            Err("punycode/IDN labels not supported (post-v1)")
1774        );
1775        // Existing ASCII edit-distance attacks (paypa1.com vs paypal.com) are
1776        // unrelated to this guard — they're caught downstream by lookalike_check.
1777        assert!(browser_hostname_fill_guard("paypa1.com").is_ok());
1778    }
1779}