arti_client/
err.rs

1//! Declare tor client specific errors.
2
3mod hint;
4
5use std::fmt::{self, Display};
6use std::sync::Arc;
7
8use futures::task::SpawnError;
9
10#[cfg(feature = "onion-service-client")]
11use safelog::Redacted;
12use safelog::Sensitive;
13use thiserror::Error;
14use tor_circmgr::TargetPorts;
15use tor_error::{ErrorKind, HasKind};
16
17use crate::TorAddrError;
18#[cfg(feature = "onion-service-client")]
19use tor_hscrypto::pk::HsId;
20
21pub use hint::HintableError;
22
23/// Main high-level error type for the Arti Tor client
24///
25/// If you need to handle different types of errors differently, use the
26/// [`kind`](`tor_error::HasKind::kind`) trait method to check what kind of
27/// error it is.
28///
29/// Note that although this type implements that standard
30/// [`Error`](trait@std::error::Error) trait, the output of that trait's methods are
31/// not covered by semantic versioning.  Specifically: you should not rely on
32/// the specific output of `Display`, `Debug`, or `Error::source()` when run on
33/// this type; it may change between patch versions without notification.
34#[derive(Error, Clone, Debug)]
35pub struct Error {
36    /// The actual error.
37    ///
38    /// This field is exposed via the `detail()` method only if the
39    /// `error_detail` feature is enabled. Using it will void your semver
40    /// guarantee.
41    #[source]
42    detail: Box<ErrorDetail>,
43}
44
45impl From<ErrorDetail> for Error {
46    fn from(detail: ErrorDetail) -> Error {
47        Error {
48            detail: detail.into(),
49        }
50    }
51}
52
53/// Declare an enum as `pub` if `error_details` is enabled, and as `pub(crate)` otherwise.
54#[cfg(feature = "error_detail")]
55macro_rules! pub_if_error_detail {
56    {  $(#[$meta:meta])* enum $e:ident $tt:tt } => {
57        $(#[$meta])* pub enum $e $tt
58    }
59}
60
61/// Declare an enum as `pub` if `error_details` is enabled, and as `pub(crate)` otherwise.
62#[cfg(not(feature = "error_detail"))]
63macro_rules! pub_if_error_detail {
64    {  $(#[$meta:meta])* enum $e:ident $tt:tt } => {
65        $(#[$meta])* pub(crate) enum $e $tt }
66}
67
68// Hello, macro-fans!  There are some other solutions that we considered here
69// but didn't use.
70//
71// 1. For one, `pub_if_error_detail!{} enum ErrorDetail { ... }` would be neat,
72// but Rust doesn't allow macros to appear in that position.
73//
74// 2. We could also declare `ErrorDetail` here as `pub` unconditionally, and
75// rely on `mod err` being private to keep it out of the user's hands.  Then we
76// could conditionally re-export `ErrorDetail` in `lib`:
77//
78// ```
79// mod err {
80//    pub enum ErrorDetail { ... }
81// }
82//
83// #[cfg(feature = "error_detail")]
84// pub use err::ErrorDetail;
85// ```
86//
87// But if we did that, the compiler would no longer warn us if we
88// _unconditionally_ exposed the ErrorDetail type from somewhere else in this
89// crate.  That doesn't seem too safe.
90//
91// 3. At one point we had a macro more like:
92// ```
93// macro_rules! declare_error_detail { { $vis: $vis } } =>
94//  => { ... $vis enum ErrorDetail {...} }
95// ```
96// There's nothing wrong with that in principle, but it's no longer needed,
97// since we used to use $vis in several places but now it's only used in one.
98// Also, it's good to make macro declarations small, and rust-analyzer seems to
99// handle understand format a little bit better.
100
101pub_if_error_detail! {
102// We cheat with the indentation, a bit.  Happily rustfmt doesn't seem to mind.
103
104/// Represents errors that can occur while doing Tor operations.
105///
106/// This enumeration is the inner view of a
107/// [`arti_client::Error`](crate::Error): we don't expose it unless the
108/// `error_detail` feature is enabled.
109///
110/// The details of this enumeration are not stable: using the `error_detail`
111/// feature will void your semver guarantee.
112///
113/// Instead of looking at the type, you should try to use the
114/// [`kind`](`tor_error::HasKind::kind`) trait method to distinguish among
115/// different kinds of [`Error`](struct@crate::Error).  If that doesn't provide enough information
116/// for your use case, please let us know.
117#[cfg_attr(docsrs, doc(cfg(feature = "error_detail")))]
118#[cfg_attr(test, derive(strum::EnumDiscriminants))]
119#[cfg_attr(test, strum_discriminants(vis(pub(crate))))]
120#[derive(Error, Clone, Debug)]
121#[non_exhaustive]
122enum ErrorDetail {
123    /// Error setting up the memory quota tracker
124    #[error("Error setting up the memory quota tracker")]
125    MemquotaSetup(#[from] tor_memquota::StartupError),
126
127    /// Memory quota error while starting up Arti
128    #[error("Memory quota error during startup")]
129    MemquotaDuringStartup(#[from] tor_memquota::Error),
130
131    /// Error setting up the channel manager
132    // TODO: should "chanmgr setup error" be its own type in tor-chanmgr
133    #[error("Error setting up the channel manager")]
134    ChanMgrSetup(#[source] tor_chanmgr::Error),
135
136    /// Error setting up the guard manager
137    // TODO: should "guardmgr setup error" be its own type in tor-guardmgr?
138    #[error("Error setting up the guard manager")]
139    GuardMgrSetup(#[source] tor_guardmgr::GuardMgrError),
140
141    /// Error setting up the guard manager
142    // TODO: should "vanguardmgr setup error" be its own type in tor-guardmgr?
143    #[cfg(all(
144        feature = "vanguards",
145        any(feature = "onion-service-client", feature = "onion-service-service")
146    ))]
147    #[error("Error setting up the vanguard manager")]
148    VanguardMgrSetup(#[source] tor_guardmgr::VanguardMgrError),
149
150    /// Error setting up the circuit manager
151    // TODO: should "circmgr setup error" be its own type in tor-circmgr?
152    #[error("Error setting up the circuit manager")]
153    CircMgrSetup(#[source] tor_circmgr::Error),
154
155    /// Error setting up the bridge descriptor manager
156    #[error("Error setting up the bridge descriptor manager")]
157    #[cfg(feature = "bridge-client")]
158    BridgeDescMgrSetup(#[from] tor_dirmgr::bridgedesc::StartupError),
159
160    /// Error setting up the directory manager
161    // TODO: should "dirmgr setup error" be its own type in tor-dirmgr?
162    #[error("Error setting up the directory manager")]
163    DirMgrSetup(#[source] tor_dirmgr::Error),
164
165    /// Error setting up the state manager.
166    #[error("Error setting up the persistent state manager")]
167    StateMgrSetup(#[source] tor_persist::Error),
168
169    /// Error setting up the hidden service client connector.
170    #[error("Error setting up the hidden service client connector")]
171    #[cfg(feature = "onion-service-client")]
172    HsClientConnectorSetup(#[from] tor_hsclient::StartupError),
173
174    /// Failed to obtain exit circuit
175    #[error("Failed to obtain exit circuit for ports {exit_ports}")]
176    ObtainExitCircuit {
177        /// The ports that we wanted a circuit for.
178        exit_ports: Sensitive<TargetPorts>,
179
180        /// What went wrong
181        #[source]
182        cause: tor_circmgr::Error,
183    },
184
185    /// Failed to obtain hidden service circuit
186    #[cfg(feature = "onion-service-client")]
187    #[error("Failed to obtain hidden service circuit to {hsid}")]
188    ObtainHsCircuit {
189        /// The service we were trying to connect to
190        hsid: Redacted<HsId>,
191
192        /// What went wrong
193        #[source]
194        cause: tor_hsclient::ConnError,
195    },
196
197    /// Directory manager was unable to bootstrap a working directory.
198    #[error("Unable to bootstrap a working directory")]
199    DirMgrBootstrap(#[source] tor_dirmgr::Error),
200
201    /// A protocol error while launching a stream
202    #[error("Protocol error while launching a {kind} stream")]
203    StreamFailed {
204        /// What kind of stream we were trying to launch.
205        kind: &'static str,
206
207        /// The error that occurred.
208        #[source]
209        cause:  tor_proto::Error
210    },
211
212    /// An error while interfacing with the persistent data layer.
213    #[error("Error while trying to access persistent state")]
214    StateAccess(#[source] tor_persist::Error),
215
216    /// We asked an exit to do something, and waited too long for an answer.
217    #[error("Timed out while waiting for answer from exit")]
218    ExitTimeout,
219
220    /// Onion services are not compiled in, but we were asked to connect to one.
221    #[error("Rejecting .onion address; feature onion-service-client not compiled in")]
222    OnionAddressNotSupported,
223
224    /// Onion services are not enabled, but we were asked to connect to one.
225    ///
226    /// This error occurs when Arti is built with onion service support, but
227    /// onion services are disabled via our stream preferences.
228    ///
229    /// To enable onion services, set `allow_onion_addrs` to `true` in the
230    /// `address_filter` configuration section.  Alternatively, set
231    /// `connect_to_onion_services` in your `StreamPrefs` object.
232    #[cfg(feature = "onion-service-client")]
233    #[error("Rejecting .onion address; allow_onion_addrs disabled in stream preferences")]
234    OnionAddressDisabled,
235
236    /// Error when trying to find the IP address of a hidden service
237    #[error("A .onion address cannot be resolved to an IP address")]
238    OnionAddressResolveRequest,
239
240    /// Unusable target address.
241    ///
242    /// `TorAddrError::InvalidHostname` should not appear here;
243    /// use `ErrorDetail::InvalidHostname` instead.
244    // TODO this is a violation of the "make invalid states unrepresentable" principle,
245    // but maybe that doesn't matter too much here?
246    #[error("Could not parse target address")]
247    Address(crate::address::TorAddrError),
248
249    /// Hostname not valid.
250    #[error("Rejecting hostname as invalid")]
251    InvalidHostname,
252
253    /// Address was local, and we don't permit connecting to those over Tor.
254    #[error("Cannot connect to a local-only address without enabling allow_local_addrs")]
255    LocalAddress,
256
257    /// Building configuration for the client failed.
258    #[error("Problem with configuration")]
259    Configuration(#[from] tor_config::ConfigBuildError),
260
261    /// Unable to change configuration.
262    #[error("Unable to change configuration")]
263    Reconfigure(#[from] tor_config::ReconfigureError),
264
265    /// Problem creating or launching a pluggable transport.
266    #[cfg(feature="pt-client")]
267    #[error("Problem with a pluggable transport")]
268    PluggableTransport(#[from] tor_ptmgr::err::PtError),
269
270    /// We encountered a problem while inspecting or creating a directory.
271    #[error("Problem accessing filesystem")]
272    FsMistrust(#[from] fs_mistrust::Error),
273
274    /// Unable to spawn task
275    #[error("Unable to spawn {spawning}")]
276    Spawn {
277        /// What we were trying to spawn.
278        spawning: &'static str,
279        /// What happened when we tried to spawn it.
280        #[source]
281        cause: Arc<SpawnError>
282    },
283
284    /// Attempted to use an unbootstrapped `TorClient` for something that
285    /// requires bootstrapping to have completed.
286    #[error("Cannot {action} with unbootstrapped client")]
287    BootstrapRequired {
288        /// What we were trying to do that required bootstrapping.
289        action: &'static str
290    },
291
292    /// Attempted to use a `TorClient` for something when it did not
293    /// have a valid directory.
294    #[error("Tried to {action} without a valid directory")]
295    NoDir {
296        /// The underlying error.
297        #[source]
298        error: tor_netdir::Error,
299        /// What we were trying to do that needed a directory.
300        action: &'static str,
301    },
302
303    /// A key store access failed.
304    #[error("Error while trying to access a key store")]
305    Keystore(#[from] tor_keymgr::Error),
306
307    /// Attempted to use a `TorClient` for something that
308    /// requires the keystore to be enabled in the configuration.
309    #[error("Cannot {action} without enabling storage.keystore")]
310    KeystoreRequired {
311        /// What we were trying to do that required the keystore to be enabled.
312        action: &'static str
313    },
314
315    /// Encountered a malformed client specifier.
316    #[error("Bad client specifier")]
317    BadClientSpecifier(#[from] tor_keymgr::ArtiPathSyntaxError),
318
319    /// We tried to parse an onion address, but we found that it was invalid.
320    ///
321    /// This error occurs if we are asked to connect to an invalid .onion address.
322    #[cfg(feature = "onion-service-client")]
323    #[error("Invalid onion address")]
324    BadOnionAddress(#[from] tor_hscrypto::pk::HsIdParseError),
325
326    /// We were unable to launch an onion service, even though we
327    /// we are configured to be able to do so.
328    #[cfg(feature= "onion-service-service")]
329    #[error("Unable to launch onion service")]
330    LaunchOnionService(#[source] tor_hsservice::StartupError),
331
332    /// A programming problem, either in our code or the code calling it.
333    #[error("Programming problem")]
334    Bug(#[from] tor_error::Bug),
335}
336
337// End of the use of $vis to refer to visibility according to `error_detail`
338}
339
340#[cfg(feature = "error_detail")]
341impl Error {
342    /// Return the underlying error detail object for this error.
343    ///
344    /// In general, it's not a good idea to use this function.  Our
345    /// `arti_client::ErrorDetail` objects are unstable, and matching on them is
346    /// probably not the best way to achieve whatever you're trying to do.
347    /// Instead, we recommend using  the [`kind`](`tor_error::HasKind::kind`)
348    /// trait method if your program needs to distinguish among different types
349    /// of errors.
350    ///
351    /// (If the above function don't meet your needs, please let us know!)
352    ///
353    /// This function is only available when `arti-client` is built with the
354    /// `error_detail` feature.  Using this function will void your semver
355    /// guarantees.
356    pub fn detail(&self) -> &ErrorDetail {
357        &self.detail
358    }
359}
360
361impl Error {
362    /// Consume this error and return the underlying error detail object.
363    pub(crate) fn into_detail(self) -> ErrorDetail {
364        *self.detail
365    }
366}
367
368impl ErrorDetail {
369    /// Construct a new `Error` from a `SpawnError`.
370    pub(crate) fn from_spawn(spawning: &'static str, err: SpawnError) -> ErrorDetail {
371        ErrorDetail::Spawn {
372            spawning,
373            cause: Arc::new(err),
374        }
375    }
376}
377
378impl Display for Error {
379    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
380        write!(f, "tor: {}: {}", self.detail.kind(), &self.detail)
381    }
382}
383
384impl tor_error::HasKind for Error {
385    fn kind(&self) -> ErrorKind {
386        self.detail.kind()
387    }
388}
389
390impl tor_error::HasKind for ErrorDetail {
391    fn kind(&self) -> ErrorKind {
392        use ErrorDetail as E;
393        use ErrorKind as EK;
394        match self {
395            E::ObtainExitCircuit { cause, .. } => cause.kind(),
396            #[cfg(feature = "onion-service-client")]
397            E::ObtainHsCircuit { cause, .. } => cause.kind(),
398            E::ExitTimeout => EK::RemoteNetworkTimeout,
399            E::BootstrapRequired { .. } => EK::BootstrapRequired,
400            E::MemquotaSetup(e) => e.kind(),
401            E::MemquotaDuringStartup(e) => e.kind(),
402            E::GuardMgrSetup(e) => e.kind(),
403            #[cfg(all(
404                feature = "vanguards",
405                any(feature = "onion-service-client", feature = "onion-service-service")
406            ))]
407            E::VanguardMgrSetup(e) => e.kind(),
408            #[cfg(feature = "bridge-client")]
409            E::BridgeDescMgrSetup(e) => e.kind(),
410            E::CircMgrSetup(e) => e.kind(),
411            E::DirMgrSetup(e) => e.kind(),
412            E::StateMgrSetup(e) => e.kind(),
413            #[cfg(feature = "onion-service-client")]
414            E::HsClientConnectorSetup(e) => e.kind(),
415            E::DirMgrBootstrap(e) => e.kind(),
416            #[cfg(feature = "pt-client")]
417            E::PluggableTransport(e) => e.kind(),
418            E::StreamFailed { cause, .. } => cause.kind(),
419            E::StateAccess(e) => e.kind(),
420            E::Configuration(e) => e.kind(),
421            E::Reconfigure(e) => e.kind(),
422            E::Spawn { cause, .. } => cause.kind(),
423            E::OnionAddressNotSupported => EK::FeatureDisabled,
424            E::OnionAddressResolveRequest => EK::NotImplemented,
425            #[cfg(feature = "onion-service-client")]
426            E::OnionAddressDisabled => EK::ForbiddenStreamTarget,
427            #[cfg(feature = "onion-service-client")]
428            E::BadOnionAddress(_) => EK::InvalidStreamTarget,
429            #[cfg(feature = "onion-service-service")]
430            E::LaunchOnionService(e) => e.kind(),
431            // TODO Should delegate to TorAddrError EK
432            E::Address(_) | E::InvalidHostname => EK::InvalidStreamTarget,
433            E::LocalAddress => EK::ForbiddenStreamTarget,
434            E::ChanMgrSetup(e) => e.kind(),
435            E::NoDir { error, .. } => error.kind(),
436            E::Keystore(e) => e.kind(),
437            E::KeystoreRequired { .. } => EK::InvalidConfig,
438            E::BadClientSpecifier(_) => EK::InvalidConfig,
439            E::FsMistrust(_) => EK::FsPermissions,
440            E::Bug(e) => e.kind(),
441        }
442    }
443}
444
445impl From<TorAddrError> for Error {
446    fn from(e: TorAddrError) -> Error {
447        ErrorDetail::from(e).into()
448    }
449}
450
451impl From<tor_keymgr::Error> for Error {
452    fn from(e: tor_keymgr::Error) -> Error {
453        ErrorDetail::Keystore(e).into()
454    }
455}
456
457impl From<TorAddrError> for ErrorDetail {
458    fn from(e: TorAddrError) -> ErrorDetail {
459        use ErrorDetail as E;
460        use TorAddrError as TAE;
461        match e {
462            TAE::InvalidHostname => E::InvalidHostname,
463            TAE::NoPort | TAE::BadPort => E::Address(e),
464        }
465    }
466}
467
468/// Verbose information about an error, meant to provide detail or justification
469/// for user-facing errors, rather than the normal short message for
470/// developer-facing errors.
471///
472/// User-facing code may attempt to produce this by calling [`Error::hint`].
473/// Not all errors may wish to provide verbose messages. `Some(ErrorHint)` will be
474/// returned if hinting is supported for the error. Err(()) will be returned otherwise.
475/// Which errors support hinting, and the hint content, have no SemVer warranty and may
476/// change in patch versions without warning. Callers should handle both cases,
477/// falling back on the original error message in case of Err.
478///
479/// Since the internal machinery for constructing and displaying hints may change over time,
480/// no data members are currently exposed. In the future we may wish to offer an unstable
481/// API locked behind a feature, like we do with ErrorDetail.
482#[derive(Clone, Debug)]
483pub struct ErrorHint<'a> {
484    /// The pieces of the message to display to the user
485    inner: ErrorHintInner<'a>,
486}
487
488/// An inner enumeration, describing different kinds of error hint that we know how to give.
489#[derive(Clone, Debug)]
490enum ErrorHintInner<'a> {
491    /// There is a misconfigured filesystem permission, reported by `fs-mistrust`.
492    ///
493    /// Tell the user to make their file more private, or to disable `fs-mistrust`.
494    BadPermission {
495        /// The location of the file.
496        filename: &'a std::path::Path,
497        /// The access bits set on the file.
498        bits: u32,
499        /// The access bits that, according to fs-mistrust, should not be set.
500        badbits: u32,
501    },
502}
503
504// TODO: Perhaps we want to lower this logic to fs_mistrust crate, and have a
505// separate `ErrorHint` type for each crate that can originate a hint.  But I'd
506// rather _not_ have that turn into something that forces us to give a Hint for
507// every intermediate crate.
508impl<'a> Display for ErrorHint<'a> {
509    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
510        use fs_mistrust::anon_home::PathExt as _;
511
512        match self.inner {
513            ErrorHintInner::BadPermission {
514                filename,
515                bits,
516                badbits,
517            } => {
518                writeln!(
519                    f,
520                    "Permissions are set too permissively on {}: currently {}",
521                    filename.anonymize_home(),
522                    fs_mistrust::format_access_bits(bits, '=')
523                )?;
524                if 0 != badbits & 0o222 {
525                    writeln!(
526                        f,
527                        "* Untrusted users could modify its contents and override our behavior.",
528                    )?;
529                }
530                if 0 != badbits & 0o444 {
531                    writeln!(f, "* Untrusted users could read its contents.")?;
532                }
533                writeln!(f,
534                    "You can fix this by further restricting the permissions of your filesystem, using:\n\
535                         chmod {} {}",
536                        fs_mistrust::format_access_bits(badbits, '-'),
537                        filename.anonymize_home())?;
538                writeln!(f, "You can suppress this message by setting storage.permissions.dangerously_trust_everyone=true,\n\
539                    or setting ARTI_FS_DISABLE_PERMISSION_CHECKS=yes in your environment.")?;
540            }
541        }
542        Ok(())
543    }
544}
545
546impl Error {
547    /// Return a hint object explaining how to solve this error, if we have one.
548    ///
549    /// Most errors won't have obvious hints, but some do.  For the ones that
550    /// do, we can return an [`ErrorHint`].
551    ///
552    /// Right now, `ErrorHint` is completely opaque: the only supported option
553    /// is to format it for human consumption.
554    pub fn hint(&self) -> Option<ErrorHint> {
555        HintableError::hint(self)
556    }
557}
558
559#[cfg(test)]
560mod test {
561    // @@ begin test lint list maintained by maint/add_warning @@
562    #![allow(clippy::bool_assert_comparison)]
563    #![allow(clippy::clone_on_copy)]
564    #![allow(clippy::dbg_macro)]
565    #![allow(clippy::mixed_attributes_style)]
566    #![allow(clippy::print_stderr)]
567    #![allow(clippy::print_stdout)]
568    #![allow(clippy::single_char_pattern)]
569    #![allow(clippy::unwrap_used)]
570    #![allow(clippy::unchecked_duration_subtraction)]
571    #![allow(clippy::useless_vec)]
572    #![allow(clippy::needless_pass_by_value)]
573    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
574    use super::*;
575
576    /// This code makes sure that our errors implement all the traits we want.
577    #[test]
578    fn traits_ok() {
579        // I had intended to use `assert_impl`, but that crate can't check whether
580        // a type is 'static.
581        fn assert<
582            T: Send + Sync + Clone + std::fmt::Debug + Display + std::error::Error + 'static,
583        >() {
584        }
585        fn check() {
586            assert::<Error>();
587            assert::<ErrorDetail>();
588        }
589        check(); // doesn't do anything, but avoids "unused function" warnings.
590    }
591}