Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(config): normalize $HOME so a trailing slash cannot bypass catch-all guards

Thiago Silva516914d41ccf
diff --git a/crates/ai-memory-cli/src/config.rs b/crates/ai-memory-cli/src/config.rs
index ceec3513..14970eb3 100644
--- a/crates/ai-memory-cli/src/config.rs
+++ b/crates/ai-memory-cli/src/config.rs
@@ -530,7 +530,11 @@ impl Config {
// $HOME read once here (config-read-path invariant); threaded to the
// resolver guard and startup heal so neither reads the env directly.
- config.home_dir = std::env::var("HOME").ok();
+ // Normalized so a trailing slash can't slip a catch-all past the
+ // exact-match guards.
+ config.home_dir = std::env::var("HOME")
+ .ok()
+ .and_then(|home| normalize_home_dir(&home));
// CLI override always wins (figment doesn't see it because clap has
// already parsed the flag into `cli_data_dir`).
@@ -816,6 +820,15 @@ fn canonicalise_or_keep(p: &Path) -> PathBuf {
p.to_path_buf()
}
+/// Normalize `$HOME` for prefix-match comparisons: strip trailing separators
+/// so a stored `repo_path` of `/home/u` still equals a `$HOME` of `/home/u/`
+/// (the cwd side is trimmed the same way in `find_project_by_cwd_prefix`).
+/// All-separator or empty input yields `None` (no usable home).
+fn normalize_home_dir(home: &str) -> Option<String> {
+ let trimmed = home.trim_end_matches('/');
+ (!trimmed.is_empty()).then(|| trimmed.to_string())
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -875,9 +888,29 @@ mod tests {
let cli_dir = tmp.path().join("override");
let cfg = Config::load(None, Some(cli_dir)).unwrap();
// `home_dir` is derived from `$HOME` at load (the single config-read
- // path). Reading the env in a test is allowed; this fails if the
+ // path), normalized so a trailing slash can't bypass the catch-all
+ // guards. Reading the env in a test is allowed; this fails if the
// load-time assignment is dropped while `$HOME` is set.
- assert_eq!(cfg.home_dir, std::env::var("HOME").ok());
+ assert_eq!(
+ cfg.home_dir,
+ std::env::var("HOME")
+ .ok()
+ .and_then(|h| normalize_home_dir(&h))
+ );
+ }
+
+ #[test]
+ fn normalize_home_dir_trims_trailing_separators() {
+ assert_eq!(normalize_home_dir("/home/u/"), Some("/home/u".to_string()));
+ assert_eq!(normalize_home_dir("/home/u"), Some("/home/u".to_string()));
+ assert_eq!(
+ normalize_home_dir("/home/u///"),
+ Some("/home/u".to_string())
+ );
+ // Degenerate inputs yield no usable home rather than an empty or
+ // root-collapsing prefix key.
+ assert_eq!(normalize_home_dir("/"), None);
+ assert_eq!(normalize_home_dir(""), None);
}
#[test]