tor_config_path/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3// @@ begin lint list maintained by maint/add_warning @@
4#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6#![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![warn(clippy::cognitive_complexity)]
15#![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![deny(clippy::print_stderr)]
28#![deny(clippy::print_stdout)]
29#![warn(clippy::rc_buffer)]
30#![deny(clippy::ref_option_ref)]
31#![warn(clippy::semicolon_if_nothing_returned)]
32#![warn(clippy::trait_duplication_in_bounds)]
33#![deny(clippy::unchecked_time_subtraction)]
34#![deny(clippy::unnecessary_wraps)]
35#![warn(clippy::unseparated_literal_suffix)]
36#![deny(clippy::unwrap_used)]
37#![deny(clippy::mod_module_files)]
38#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
39#![allow(clippy::uninlined_format_args)]
40#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43#![allow(clippy::needless_lifetimes)] // See arti#1765
44#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
45//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
46
47use std::collections::HashMap;
48use std::path::{Path, PathBuf};
49
50use serde::{Deserialize, Serialize};
51use std::borrow::Cow;
52#[cfg(feature = "expand-paths")]
53use {directories::BaseDirs, std::sync::LazyLock};
54
55use tor_error::{ErrorKind, HasKind};
56
57#[cfg(all(test, feature = "expand-paths"))]
58use std::ffi::OsStr;
59
60#[cfg(feature = "address")]
61pub mod addr;
62
63#[cfg(feature = "arti-client")]
64mod arti_client_paths;
65
66#[cfg(feature = "arti-client")]
67pub use arti_client_paths::arti_client_base_resolver;
68
69/// A path in a configuration file: tilde expansion is performed, along
70/// with expansion of variables provided by a [`CfgPathResolver`].
71///
72/// The tilde expansion is performed using the home directory given by the
73/// `directories` crate, which may be based on an environment variable. For more
74/// information, see [`BaseDirs::home_dir`](directories::BaseDirs::home_dir).
75///
76/// Alternatively, a `CfgPath` can contain literal `PathBuf`, which will not be expanded.
77#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
78#[serde(transparent)]
79pub struct CfgPath(PathInner);
80
81/// Inner implementation of CfgPath
82///
83/// `PathInner` exists to avoid making the variants part of the public Rust API
84#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
85#[serde(untagged)]
86enum PathInner {
87    /// A path that should be used literally, with no expansion.
88    Literal(LiteralPath),
89    /// A path that should be expanded from a string using ShellExpand.
90    Shell(String),
91}
92
93#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
94/// Inner implementation of PathInner:Literal
95///
96/// `LiteralPath` exists to arrange that `PathInner::Literal`'s (de)serialization
97/// does not overlap with `PathInner::Shell`'s.
98struct LiteralPath {
99    /// The underlying `PathBuf`.
100    literal: PathBuf,
101}
102
103/// An error that has occurred while expanding a path.
104#[derive(thiserror::Error, Debug, Clone)]
105#[non_exhaustive]
106#[cfg_attr(test, derive(PartialEq))]
107pub enum CfgPathError {
108    /// The path contained a variable we didn't recognize.
109    #[error("Unrecognized variable {0} in path")]
110    UnknownVar(String),
111    /// We couldn't construct a ProjectDirs object.
112    #[error(
113        "Couldn't determine XDG Project Directories, needed to resolve a path; probably, unable to determine HOME directory"
114    )]
115    NoProjectDirs,
116    /// We couldn't construct a BaseDirs object.
117    #[error("Can't construct base directories to resolve a path element")]
118    NoBaseDirs,
119    /// We couldn't find our current binary path.
120    #[error("Can't find the path to the current binary")]
121    NoProgramPath,
122    /// We couldn't find the directory path containing the current binary.
123    #[error("Can't find the directory of the current binary")]
124    NoProgramDir,
125    /// We couldn't convert a string to a valid path on the OS.
126    //
127    // NOTE: This is not currently generated. Shall we remove it?
128    #[error("Invalid path string: {0:?}")]
129    InvalidString(String),
130    /// Variable interpolation (`$`) attempted, but not compiled in
131    #[error(
132        "Variable interpolation $ is not supported (tor-config/expand-paths feature disabled)); $ must still be doubled"
133    )]
134    VariableInterpolationNotSupported(String),
135    /// Home dir interpolation (`~`) attempted, but not compiled in
136    #[error("Home dir ~/ is not supported (tor-config/expand-paths feature disabled)")]
137    HomeDirInterpolationNotSupported(String),
138}
139
140impl HasKind for CfgPathError {
141    fn kind(&self) -> ErrorKind {
142        use CfgPathError as E;
143        use ErrorKind as EK;
144        match self {
145            E::UnknownVar(_) | E::InvalidString(_) => EK::InvalidConfig,
146            E::NoProjectDirs | E::NoBaseDirs => EK::NoHomeDirectory,
147            E::NoProgramPath | E::NoProgramDir => EK::InvalidConfig,
148            E::VariableInterpolationNotSupported(_) | E::HomeDirInterpolationNotSupported(_) => {
149                EK::FeatureDisabled
150            }
151        }
152    }
153}
154
155/// A variable resolver for paths in a configuration file.
156///
157/// Typically there should be one resolver per application, and the application should share the
158/// resolver throughout the application to have consistent path variable expansions. Typically the
159/// application would create its own resolver with its application-specific variables, but note that
160/// `TorClientConfig` is an exception which does not accept a resolver from the application and
161/// instead generates its own. This is done for backwards compatibility reasons.
162///
163/// Once constructed, they are used during calls to [`CfgPath::path`] to expand variables in the
164/// path.
165#[derive(Clone, Debug, Default)]
166pub struct CfgPathResolver {
167    /// The variables and their values. The values can be an `Err` if the variable is expected but
168    /// can't be expanded.
169    vars: HashMap<String, Result<Cow<'static, Path>, CfgPathError>>,
170}
171
172impl CfgPathResolver {
173    /// Get the value for a given variable name.
174    #[cfg(feature = "expand-paths")]
175    fn get_var(&self, var: &str) -> Result<Cow<'static, Path>, CfgPathError> {
176        match self.vars.get(var) {
177            Some(val) => val.clone(),
178            None => Err(CfgPathError::UnknownVar(var.to_owned())),
179        }
180    }
181
182    /// Set a variable `var` that will be replaced with `val` when a [`CfgPath`] is expanded.
183    ///
184    /// Setting an `Err` is useful when a variable is supported, but for whatever reason it can't be
185    /// expanded, and you'd like to return a more-specific error. An example might be a `USER_HOME`
186    /// variable for a user that doesn't have a `HOME` environment variable set.
187    ///
188    /// ```
189    /// use std::path::Path;
190    /// use tor_config_path::{CfgPath, CfgPathResolver};
191    ///
192    /// let mut path_resolver = CfgPathResolver::default();
193    /// path_resolver.set_var("FOO", Ok(Path::new("/foo").to_owned().into()));
194    ///
195    /// let path = CfgPath::new("${FOO}/bar".into());
196    ///
197    /// #[cfg(feature = "expand-paths")]
198    /// assert_eq!(path.path(&path_resolver).unwrap(), Path::new("/foo/bar"));
199    /// #[cfg(not(feature = "expand-paths"))]
200    /// assert!(path.path(&path_resolver).is_err());
201    /// ```
202    pub fn set_var(
203        &mut self,
204        var: impl Into<String>,
205        val: Result<Cow<'static, Path>, CfgPathError>,
206    ) {
207        self.vars.insert(var.into(), val);
208    }
209
210    /// Helper to create a `CfgPathResolver` from str `(name, value)` pairs.
211    #[cfg(all(test, feature = "expand-paths"))]
212    fn from_pairs<K, V>(vars: impl IntoIterator<Item = (K, V)>) -> CfgPathResolver
213    where
214        K: Into<String>,
215        V: AsRef<OsStr>,
216    {
217        let mut path_resolver = CfgPathResolver::default();
218        for (name, val) in vars.into_iter() {
219            let val = Path::new(val.as_ref()).to_owned();
220            path_resolver.set_var(name, Ok(val.into()));
221        }
222        path_resolver
223    }
224}
225
226impl CfgPath {
227    /// Create a new configuration path
228    pub fn new(s: String) -> Self {
229        CfgPath(PathInner::Shell(s))
230    }
231
232    /// Construct a new `CfgPath` designating a literal not-to-be-expanded `PathBuf`
233    pub fn new_literal<P: Into<PathBuf>>(path: P) -> Self {
234        CfgPath(PathInner::Literal(LiteralPath {
235            literal: path.into(),
236        }))
237    }
238
239    /// Return the path on disk designated by this `CfgPath`.
240    ///
241    /// Variables may or may not be resolved using `path_resolver`, depending on whether the
242    /// `expand-paths` feature is enabled or not.
243    pub fn path(&self, path_resolver: &CfgPathResolver) -> Result<PathBuf, CfgPathError> {
244        match &self.0 {
245            PathInner::Shell(s) => expand(s, path_resolver),
246            PathInner::Literal(LiteralPath { literal }) => Ok(literal.clone()),
247        }
248    }
249
250    /// If the `CfgPath` is a string that should be expanded, return the (unexpanded) string,
251    ///
252    /// Before use, this string would have be to expanded.  So if you want a path to actually use,
253    /// call `path` instead.
254    ///
255    /// Returns `None` if the `CfgPath` is a literal `PathBuf` not intended for expansion.
256    pub fn as_unexpanded_str(&self) -> Option<&str> {
257        match &self.0 {
258            PathInner::Shell(s) => Some(s),
259            PathInner::Literal(_) => None,
260        }
261    }
262
263    /// If the `CfgPath` designates a literal not-to-be-expanded `Path`, return a reference to it
264    ///
265    /// Returns `None` if the `CfgPath` is a string which should be expanded, which is the
266    /// usual case.
267    pub fn as_literal_path(&self) -> Option<&Path> {
268        match &self.0 {
269            PathInner::Shell(_) => None,
270            PathInner::Literal(LiteralPath { literal }) => Some(literal),
271        }
272    }
273}
274
275impl std::fmt::Display for CfgPath {
276    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
277        match &self.0 {
278            PathInner::Literal(LiteralPath { literal }) => write!(fmt, "{:?} [exactly]", literal),
279            PathInner::Shell(s) => s.fmt(fmt),
280        }
281    }
282}
283
284/// Return the user's home directory used when expanding paths.
285// This is public so that applications which want to support for example a `USER_HOME` variable can
286// use the same home directory expansion that we use in this crate for `~` expansion.
287#[cfg(feature = "expand-paths")]
288pub fn home() -> Result<&'static Path, CfgPathError> {
289    /// Lazy lock holding the home directory.
290    static HOME_DIR: LazyLock<Option<PathBuf>> =
291        LazyLock::new(|| Some(BaseDirs::new()?.home_dir().to_owned()));
292    HOME_DIR
293        .as_ref()
294        .map(PathBuf::as_path)
295        .ok_or(CfgPathError::NoBaseDirs)
296}
297
298/// Helper: expand a directory given as a string.
299#[cfg(feature = "expand-paths")]
300fn expand(s: &str, path_resolver: &CfgPathResolver) -> Result<PathBuf, CfgPathError> {
301    let path = shellexpand::path::full_with_context(
302        s,
303        || home().ok(),
304        |x| path_resolver.get_var(x).map(Some),
305    );
306    Ok(path.map_err(|e| e.cause)?.into_owned())
307}
308
309/// Helper: convert a string to a path without expansion.
310#[cfg(not(feature = "expand-paths"))]
311fn expand(input: &str, _: &CfgPathResolver) -> Result<PathBuf, CfgPathError> {
312    // We must still de-duplicate `$` and reject `~/`,, so that the behaviour is a superset
313    if input.starts_with('~') {
314        return Err(CfgPathError::HomeDirInterpolationNotSupported(input.into()));
315    }
316
317    let mut out = String::with_capacity(input.len());
318    let mut s = input;
319    while let Some((lhs, rhs)) = s.split_once('$') {
320        if let Some(rhs) = rhs.strip_prefix('$') {
321            // deduplicate the $
322            out += lhs;
323            out += "$";
324            s = rhs;
325        } else {
326            return Err(CfgPathError::VariableInterpolationNotSupported(
327                input.into(),
328            ));
329        }
330    }
331    out += s;
332    Ok(out.into())
333}
334
335#[cfg(all(test, feature = "expand-paths"))]
336mod test {
337    #![allow(clippy::unwrap_used)]
338    use super::*;
339
340    #[test]
341    fn expand_no_op() {
342        let r = CfgPathResolver::from_pairs([("FOO", "foo")]);
343
344        let p = CfgPath::new("Hello/world".to_string());
345        assert_eq!(p.to_string(), "Hello/world".to_string());
346        assert_eq!(p.path(&r).unwrap().to_str(), Some("Hello/world"));
347
348        let p = CfgPath::new("/usr/local/foo".to_string());
349        assert_eq!(p.to_string(), "/usr/local/foo".to_string());
350        assert_eq!(p.path(&r).unwrap().to_str(), Some("/usr/local/foo"));
351    }
352
353    #[cfg(not(target_family = "windows"))]
354    #[test]
355    fn expand_home() {
356        let r = CfgPathResolver::from_pairs([("USER_HOME", home().unwrap())]);
357
358        let p = CfgPath::new("~/.arti/config".to_string());
359        assert_eq!(p.to_string(), "~/.arti/config".to_string());
360
361        let expected = dirs::home_dir().unwrap().join(".arti/config");
362        assert_eq!(p.path(&r).unwrap().to_str(), expected.to_str());
363
364        let p = CfgPath::new("${USER_HOME}/.arti/config".to_string());
365        assert_eq!(p.to_string(), "${USER_HOME}/.arti/config".to_string());
366        assert_eq!(p.path(&r).unwrap().to_str(), expected.to_str());
367    }
368
369    #[cfg(target_family = "windows")]
370    #[test]
371    fn expand_home() {
372        let r = CfgPathResolver::from_pairs([("USER_HOME", home().unwrap())]);
373
374        let p = CfgPath::new("~\\.arti\\config".to_string());
375        assert_eq!(p.to_string(), "~\\.arti\\config".to_string());
376
377        let expected = dirs::home_dir().unwrap().join(".arti\\config");
378        assert_eq!(p.path(&r).unwrap().to_str(), expected.to_str());
379
380        let p = CfgPath::new("${USER_HOME}\\.arti\\config".to_string());
381        assert_eq!(p.to_string(), "${USER_HOME}\\.arti\\config".to_string());
382        assert_eq!(p.path(&r).unwrap().to_str(), expected.to_str());
383    }
384
385    #[test]
386    fn expand_bogus() {
387        let r = CfgPathResolver::from_pairs([("FOO", "foo")]);
388
389        let p = CfgPath::new("${ARTI_WOMBAT}/example".to_string());
390        assert_eq!(p.to_string(), "${ARTI_WOMBAT}/example".to_string());
391
392        assert!(matches!(p.path(&r), Err(CfgPathError::UnknownVar(_))));
393        assert_eq!(
394            &p.path(&r).unwrap_err().to_string(),
395            "Unrecognized variable ARTI_WOMBAT in path"
396        );
397    }
398
399    #[test]
400    fn literal() {
401        let r = CfgPathResolver::from_pairs([("ARTI_CACHE", "foo")]);
402
403        let p = CfgPath::new_literal(PathBuf::from("${ARTI_CACHE}/literally"));
404        // This doesn't get expanded, since we're using a literal path.
405        assert_eq!(
406            p.path(&r).unwrap().to_str().unwrap(),
407            "${ARTI_CACHE}/literally"
408        );
409        assert_eq!(p.to_string(), "\"${ARTI_CACHE}/literally\" [exactly]");
410    }
411
412    #[test]
413    #[cfg(feature = "expand-paths")]
414    fn program_dir() {
415        let current_exe = std::env::current_exe().unwrap();
416        let r = CfgPathResolver::from_pairs([("PROGRAM_DIR", current_exe.parent().unwrap())]);
417
418        let p = CfgPath::new("${PROGRAM_DIR}/foo".to_string());
419
420        let mut this_binary = current_exe;
421        this_binary.pop();
422        this_binary.push("foo");
423        let expanded = p.path(&r).unwrap();
424        assert_eq!(expanded, this_binary);
425    }
426
427    #[test]
428    #[cfg(not(feature = "expand-paths"))]
429    fn rejections() {
430        let r = CfgPathResolver::from_pairs([("PROGRAM_DIR", std::env::current_exe().unwrap())]);
431
432        let chk_err = |s: &str, mke: &dyn Fn(String) -> CfgPathError| {
433            let p = CfgPath::new(s.to_string());
434            assert_eq!(p.path(&r).unwrap_err(), mke(s.to_string()));
435        };
436
437        let chk_ok = |s: &str, exp| {
438            let p = CfgPath::new(s.to_string());
439            assert_eq!(p.path(&r), Ok(PathBuf::from(exp)));
440        };
441
442        chk_err(
443            "some/${PROGRAM_DIR}/foo",
444            &CfgPathError::VariableInterpolationNotSupported,
445        );
446        chk_err("~some", &CfgPathError::HomeDirInterpolationNotSupported);
447
448        chk_ok("some$$foo$$bar", "some$foo$bar");
449        chk_ok("no dollars", "no dollars");
450    }
451}
452
453#[cfg(test)]
454mod test_serde {
455    // @@ begin test lint list maintained by maint/add_warning @@
456    #![allow(clippy::bool_assert_comparison)]
457    #![allow(clippy::clone_on_copy)]
458    #![allow(clippy::dbg_macro)]
459    #![allow(clippy::mixed_attributes_style)]
460    #![allow(clippy::print_stderr)]
461    #![allow(clippy::print_stdout)]
462    #![allow(clippy::single_char_pattern)]
463    #![allow(clippy::unwrap_used)]
464    #![allow(clippy::unchecked_time_subtraction)]
465    #![allow(clippy::useless_vec)]
466    #![allow(clippy::needless_pass_by_value)]
467    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
468
469    use super::*;
470
471    use std::ffi::OsString;
472    use std::fmt::Debug;
473
474    use derive_builder::Builder;
475    use tor_config::load::TopLevel;
476    use tor_config::{ConfigBuildError, impl_standard_builder};
477
478    #[derive(Serialize, Deserialize, Builder, Eq, PartialEq, Debug)]
479    #[builder(derive(Serialize, Deserialize, Debug))]
480    #[builder(build_fn(error = "ConfigBuildError"))]
481    struct TestConfigFile {
482        p: CfgPath,
483    }
484
485    impl_standard_builder! { TestConfigFile: !Default }
486
487    impl TopLevel for TestConfigFile {
488        type Builder = TestConfigFileBuilder;
489    }
490
491    fn deser_json(json: &str) -> CfgPath {
492        dbg!(json);
493        let TestConfigFile { p } = serde_json::from_str(json).expect("deser json failed");
494        p
495    }
496    fn deser_toml(toml: &str) -> CfgPath {
497        dbg!(toml);
498        let TestConfigFile { p } = toml::from_str(toml).expect("deser toml failed");
499        p
500    }
501    fn deser_toml_cfg(toml: &str) -> CfgPath {
502        dbg!(toml);
503        let mut sources = tor_config::ConfigurationSources::new_empty();
504        sources.push_source(
505            tor_config::ConfigurationSource::from_verbatim(toml.to_string()),
506            tor_config::sources::MustRead::MustRead,
507        );
508        let cfg = sources.load().unwrap();
509
510        dbg!(&cfg);
511        let TestConfigFile { p } = tor_config::load::resolve(cfg).expect("cfg resolution failed");
512        p
513    }
514
515    #[test]
516    fn test_parse() {
517        fn desers(toml: &str, json: &str) -> Vec<CfgPath> {
518            vec![deser_toml(toml), deser_toml_cfg(toml), deser_json(json)]
519        }
520
521        for cp in desers(r#"p = "string""#, r#"{ "p": "string" }"#) {
522            assert_eq!(cp.as_unexpanded_str(), Some("string"));
523            assert_eq!(cp.as_literal_path(), None);
524        }
525
526        for cp in desers(
527            r#"p = { literal = "lit" }"#,
528            r#"{ "p": {"literal": "lit"} }"#,
529        ) {
530            assert_eq!(cp.as_unexpanded_str(), None);
531            assert_eq!(cp.as_literal_path(), Some(&*PathBuf::from("lit")));
532        }
533    }
534
535    fn non_string_path() -> PathBuf {
536        #[cfg(target_family = "unix")]
537        {
538            use std::os::unix::ffi::OsStringExt;
539            return PathBuf::from(OsString::from_vec(vec![0x80_u8]));
540        }
541
542        #[cfg(target_family = "windows")]
543        {
544            use std::os::windows::ffi::OsStringExt;
545            return PathBuf::from(OsString::from_wide(&[0xD800_u16]));
546        }
547
548        #[allow(unreachable_code)]
549        // Cannot test non-Stringy Paths on this platform
550        PathBuf::default()
551    }
552
553    fn test_roundtrip_cases<SER, S, DESER, E, F>(ser: SER, deser: DESER)
554    where
555        SER: Fn(&TestConfigFile) -> Result<S, E>,
556        DESER: Fn(&S) -> Result<TestConfigFile, F>,
557        S: Debug,
558        E: Debug,
559        F: Debug,
560    {
561        let case = |easy, p| {
562            let input = TestConfigFile { p };
563            let s = match ser(&input) {
564                Ok(s) => s,
565                Err(e) if easy => panic!("ser failed {:?} e={:?}", &input, &e),
566                Err(_) => return,
567            };
568            dbg!(&input, &s);
569            let output = deser(&s).expect("deser failed");
570            assert_eq!(&input, &output, "s={:?}", &s);
571        };
572
573        case(true, CfgPath::new("string".into()));
574        case(true, CfgPath::new_literal(PathBuf::from("nice path")));
575        case(true, CfgPath::new_literal(PathBuf::from("path with ✓")));
576
577        // Non-UTF-8 paths are really hard to serialize.  We allow the serializsaton
578        // to fail, and if it does, we skip the rest of the round trip test.
579        // But, if they did serialise, we want to make sure that we can deserialize.
580        // Hence this test case.
581        case(false, CfgPath::new_literal(non_string_path()));
582    }
583
584    #[test]
585    fn roundtrip_json() {
586        test_roundtrip_cases(
587            |input| serde_json::to_string(&input),
588            |json| serde_json::from_str(json),
589        );
590    }
591
592    #[test]
593    fn roundtrip_toml() {
594        test_roundtrip_cases(|input| toml::to_string(&input), |toml| toml::from_str(toml));
595    }
596
597    #[test]
598    fn roundtrip_mpack() {
599        test_roundtrip_cases(
600            |input| rmp_serde::to_vec(&input),
601            |mpack| rmp_serde::from_slice(mpack),
602        );
603    }
604}