Skip to main content

sqlite_graphrag/
paths.rs

1//! XDG/cwd path resolution and traversal-safe overrides.
2//!
3//! Resolves data directories via [`directories::ProjectDirs`] and validates
4//! that user-supplied paths cannot escape the project root.
5
6use crate::errors::AppError;
7use crate::i18n::validation;
8use directories::ProjectDirs;
9use std::path::{Component, Path, PathBuf};
10
11/// Resolved filesystem paths used by the CLI at runtime.
12///
13/// Constructed via [`AppPaths::resolve`], which applies the three-layer precedence:
14/// CLI flag → `SQLITE_GRAPHRAG_DB_PATH` env var → `SQLITE_GRAPHRAG_HOME` env var → cwd.
15#[derive(Debug, Clone)]
16pub struct AppPaths {
17    /// Absolute path to the SQLite database file.
18    pub db: PathBuf,
19    /// Directory where embedding model files are cached.
20    pub models: PathBuf,
21}
22
23impl AppPaths {
24    pub fn resolve(db_override: Option<&str>) -> Result<Self, AppError> {
25        let proj = ProjectDirs::from("", "", "sqlite-graphrag").ok_or_else(|| {
26            AppError::Io(std::io::Error::other("could not determine home directory"))
27        })?;
28
29        let cache_root = if let Some(override_dir) = std::env::var_os("SQLITE_GRAPHRAG_CACHE_DIR") {
30            PathBuf::from(override_dir)
31        } else {
32            proj.cache_dir().to_path_buf()
33        };
34
35        let db = if let Some(p) = db_override {
36            validate_path(p)?;
37            PathBuf::from(p)
38        } else if let Ok(env_path) = std::env::var("SQLITE_GRAPHRAG_DB_PATH") {
39            validate_path(&env_path)?;
40            PathBuf::from(env_path)
41        } else if let Some(home_dir) = home_env_dir()? {
42            home_dir.join("graphrag.sqlite")
43        } else {
44            std::env::current_dir()
45                .map_err(AppError::Io)?
46                .join("graphrag.sqlite")
47        };
48
49        Ok(Self {
50            db,
51            models: cache_root.join("models"),
52        })
53    }
54
55    pub fn ensure_dirs(&self) -> Result<(), AppError> {
56        for dir in [parent_or_err(&self.db)?, self.models.as_path()] {
57            std::fs::create_dir_all(dir)?;
58        }
59        Ok(())
60    }
61}
62
63fn validate_path(p: &str) -> Result<(), AppError> {
64    if Path::new(p).components().any(|c| c == Component::ParentDir) {
65        return Err(AppError::Validation(validation::path_traversal(p)));
66    }
67    Ok(())
68}
69
70/// Resolves `SQLITE_GRAPHRAG_HOME` as the root directory for the default database.
71///
72/// Returns `Ok(Some(dir))` when the env var is set and valid,
73/// `Ok(None)` when absent or empty (falls back to `current_dir`),
74/// and `Err(...)` when the value contains traversal components.
75fn home_env_dir() -> Result<Option<PathBuf>, AppError> {
76    let raw = match std::env::var("SQLITE_GRAPHRAG_HOME") {
77        Ok(v) => v,
78        Err(_) => return Ok(None),
79    };
80    if raw.is_empty() {
81        return Ok(None);
82    }
83    validate_path(&raw)?;
84    Ok(Some(PathBuf::from(raw)))
85}
86
87/// Returns the XDG config directory for the application.
88pub fn config_dir() -> Result<PathBuf, AppError> {
89    let proj = ProjectDirs::from("", "", "sqlite-graphrag").ok_or_else(|| {
90        AppError::Io(std::io::Error::other(
91            "could not determine home directory for config",
92        ))
93    })?;
94    Ok(proj.config_dir().to_path_buf())
95}
96
97pub(crate) fn parent_or_err(path: &Path) -> Result<&Path, AppError> {
98    path.parent().ok_or_else(|| {
99        AppError::Validation(format!(
100            "path '{}' has no valid parent component",
101            path.display()
102        ))
103    })
104}
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109    use serial_test::serial;
110    use tempfile::TempDir;
111
112    /// Clears all variables that affect `AppPaths::resolve` to isolate the
113    /// test from the developer/CI environment.
114    fn clean_env_paths() {
115        // SAFETY: tests are annotated with #[serial], guaranteeing single-threaded execution.
116        unsafe {
117            std::env::remove_var("SQLITE_GRAPHRAG_HOME");
118            std::env::remove_var("SQLITE_GRAPHRAG_DB_PATH");
119            std::env::remove_var("SQLITE_GRAPHRAG_CACHE_DIR");
120        }
121    }
122
123    #[test]
124    #[serial]
125    fn home_env_resolves_db_in_subdir() {
126        clean_env_paths();
127        let tmp = TempDir::new().expect("tempdir");
128        // SAFETY: tests are annotated with #[serial], guaranteeing single-threaded execution.
129        unsafe {
130            std::env::set_var("SQLITE_GRAPHRAG_HOME", tmp.path());
131        }
132
133        let paths = AppPaths::resolve(None).expect("resolve with valid HOME");
134        assert_eq!(paths.db, tmp.path().join("graphrag.sqlite"));
135
136        clean_env_paths();
137    }
138
139    #[test]
140    #[serial]
141    fn home_env_traversal_rejected() {
142        clean_env_paths();
143        // SAFETY: tests are annotated with #[serial], guaranteeing single-threaded execution.
144        unsafe {
145            std::env::set_var("SQLITE_GRAPHRAG_HOME", "/tmp/../etc");
146        }
147
148        let result = AppPaths::resolve(None);
149        assert!(
150            matches!(result, Err(AppError::Validation(_))),
151            "traversal in SQLITE_GRAPHRAG_HOME must fail as Validation, got {result:?}"
152        );
153
154        clean_env_paths();
155    }
156
157    #[test]
158    #[serial]
159    fn db_path_overrides_home() {
160        clean_env_paths();
161        let tmp_home = TempDir::new().expect("tempdir home");
162        let tmp_db = TempDir::new().expect("tempdir db");
163        let explicit_db = tmp_db.path().join("explicit.sqlite");
164        // SAFETY: tests are annotated with #[serial], guaranteeing single-threaded execution.
165        unsafe {
166            std::env::set_var("SQLITE_GRAPHRAG_HOME", tmp_home.path());
167            std::env::set_var("SQLITE_GRAPHRAG_DB_PATH", &explicit_db);
168        }
169
170        let paths = AppPaths::resolve(None).expect("resolve with DB_PATH and HOME");
171        assert_eq!(paths.db, explicit_db);
172
173        clean_env_paths();
174    }
175
176    #[test]
177    #[serial]
178    fn flag_overrides_home() {
179        clean_env_paths();
180        let tmp_home = TempDir::new().expect("tempdir home");
181        let tmp_flag = TempDir::new().expect("tempdir flag");
182        let db_flag = tmp_flag.path().join("via-flag.sqlite");
183        // SAFETY: tests are annotated with #[serial], guaranteeing single-threaded execution.
184        unsafe {
185            std::env::set_var("SQLITE_GRAPHRAG_HOME", tmp_home.path());
186        }
187
188        let paths = AppPaths::resolve(Some(db_flag.to_str().expect("utf8")))
189            .expect("resolve with flag and HOME");
190        assert_eq!(paths.db, db_flag);
191
192        clean_env_paths();
193    }
194
195    #[test]
196    #[serial]
197    fn home_env_empty_falls_back_to_cwd() {
198        clean_env_paths();
199        // SAFETY: tests are annotated with #[serial], guaranteeing single-threaded execution.
200        unsafe {
201            std::env::set_var("SQLITE_GRAPHRAG_HOME", "");
202        }
203
204        let paths = AppPaths::resolve(None).expect("resolve with empty HOME");
205        let expected = std::env::current_dir()
206            .expect("cwd")
207            .join("graphrag.sqlite");
208        assert_eq!(paths.db, expected);
209
210        clean_env_paths();
211    }
212
213    #[test]
214    fn parent_or_err_accepts_normal_path() {
215        let p = PathBuf::from("/home/user/db.sqlite");
216        let parent = parent_or_err(&p).expect("valid parent");
217        assert_eq!(parent, Path::new("/home/user"));
218    }
219
220    #[test]
221    fn parent_or_err_accepts_relative_path() {
222        let p = PathBuf::from("subdir/file.sqlite");
223        let parent = parent_or_err(&p).expect("relative parent");
224        assert_eq!(parent, Path::new("subdir"));
225    }
226
227    #[test]
228    fn parent_or_err_rejects_unix_root() {
229        let p = PathBuf::from("/");
230        let result = parent_or_err(&p);
231        assert!(matches!(result, Err(AppError::Validation(_))));
232    }
233
234    #[test]
235    fn parent_or_err_rejects_empty_path() {
236        let p = PathBuf::from("");
237        let result = parent_or_err(&p);
238        assert!(matches!(result, Err(AppError::Validation(_))));
239    }
240}