Skip to main content

tor_dirclient/
err.rs

1//! Declare dirclient-specific errors.
2
3use std::sync::Arc;
4
5use thiserror::Error;
6use tor_error::{Bug, ErrorKind, HasKind};
7use tor_linkspec::OwnedChanTarget;
8use tor_rtcompat::TimeoutError;
9
10use crate::SourceInfo;
11
12/// An error originating from the tor-dirclient crate.
13#[derive(Error, Debug, Clone)]
14#[non_exhaustive]
15#[allow(clippy::large_enum_variant)] // TODO(nickm) worth fixing as we do #587
16pub enum Error {
17    /// Error while getting a circuit
18    #[error("Error while getting a circuit")]
19    CircMgr(#[from] tor_circmgr::Error),
20
21    /// An error that has occurred after we have contacted a directory cache and made a circuit to it.
22    #[error("Error fetching directory information")]
23    RequestFailed(#[from] RequestFailedError),
24
25    /// We ran into a problem that is probably due to a programming issue.
26    #[error("Internal error")]
27    Bug(#[from] Bug),
28}
29
30/// An error that has occurred after we have contacted a directory cache and made a circuit to it.
31///
32/// Generally, all errors of these kinds imply that the interfacing application
33/// may retry the request, either at a different time or at a different endpoint
34/// and potentially even both.
35#[derive(Error, Debug, Clone)]
36#[allow(clippy::exhaustive_structs)] // TODO should not be exhaustive
37#[error("Request failed from {}: {error}", FromSource(.source))]
38pub struct RequestFailedError {
39    /// The source that gave us this error.
40    pub source: Option<SourceInfo>,
41
42    /// The underlying error that occurred.
43    #[source]
44    pub error: RequestError,
45}
46
47/// Helper type to display an optional source of directory information.
48struct FromSource<'a>(&'a Option<SourceInfo>);
49
50impl std::fmt::Display for FromSource<'_> {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        if let Some(si) = self.0 {
53            write!(f, "{}", si)
54        } else {
55            write!(f, "N/A")
56        }
57    }
58}
59
60/// An error originating from the tor-dirclient crate.
61///
62/// See [`RequestFailedError`] for notes on retrying failed requests.
63#[derive(Error, Debug, Clone)]
64#[non_exhaustive]
65pub enum RequestError {
66    /// The directory cache took too long to reply to us.
67    #[error("directory timed out")]
68    DirTimeout,
69
70    /// We got an EOF before we were done with the headers.
71    #[error("truncated HTTP headers")]
72    TruncatedHeaders,
73
74    /// Received a response that was longer than we expected.
75    #[error("response too long; gave up after {0} bytes")]
76    ResponseTooLong(usize),
77
78    /// Received too many bytes in our headers.
79    #[error("headers too long; gave up after {0} bytes")]
80    HeadersTooLong(usize),
81
82    /// Data received was not UTF-8 encoded.
83    #[error("Couldn't decode data as UTF-8.")]
84    Utf8Encoding(#[from] std::string::FromUtf8Error),
85
86    /// Io error while reading on connection
87    #[error("IO error")]
88    IoError(#[source] Arc<std::io::Error>),
89
90    /// A protocol error while launching a stream
91    #[error("Protocol error while launching a stream")]
92    Proto(#[from] tor_proto::Error),
93
94    /// A protocol error while launching a stream
95    #[error("Tunnel error")]
96    Tunnel(#[from] tor_circmgr::Error),
97
98    /// Error when parsing http
99    #[error("Couldn't parse HTTP headers")]
100    HttparseError(#[from] httparse::Error),
101
102    /// Error while creating http request
103    //
104    // TODO this should be abolished, in favour of a `Bug` variant,
105    // so that we get a stack trace, as per the notes for EK::Internal.
106    // We could convert via into_internal!, or a custom `From` impl.
107    #[error("Couldn't create HTTP request")]
108    HttpError(#[source] Arc<http::Error>),
109
110    /// Unrecognized content-encoding
111    #[error("Unrecognized content encoding: {0:?}")]
112    ContentEncoding(String),
113
114    /// Too much clock skew between us and the directory.
115    ///
116    /// (We've giving up on this request early, since any directory that it
117    /// believes in, we would reject as untimely.)
118    #[error("Too much clock skew with directory cache")]
119    TooMuchClockSkew,
120
121    /// We tried to launch a request without any requested objects.
122    ///
123    /// This can happen if (for example) we request an empty list of
124    /// microdescriptors or certificates.
125    #[error("We didn't have any objects to request")]
126    EmptyRequest,
127
128    /// Successful GET response (i.e. status code 200) with an empty body.
129    ///
130    /// This should not be returned as the only semantically valid purpose of
131    /// an empty body would be a 404 response, which is treated differently.
132    #[error("Empty successful response")]
133    EmptyResponse,
134
135    /// HTTP status code indicates a not completely successful request
136    #[error("HTTP status code {0}: {1:?}")]
137    HttpStatus(u16, String),
138}
139
140impl From<TimeoutError> for RequestError {
141    fn from(_: TimeoutError) -> Self {
142        RequestError::DirTimeout
143    }
144}
145
146impl From<std::io::Error> for RequestError {
147    fn from(err: std::io::Error) -> Self {
148        Self::IoError(Arc::new(err))
149    }
150}
151
152impl From<http::Error> for RequestError {
153    fn from(err: http::Error) -> Self {
154        Self::HttpError(Arc::new(err))
155    }
156}
157
158impl Error {
159    /// Return true if this error means that the circuit shouldn't be used
160    /// for any more directory requests.
161    pub fn should_retire_circ(&self) -> bool {
162        // TODO: probably this is too aggressive, and we should
163        // actually _not_ dump the circuit under all circumstances.
164        match self {
165            Error::CircMgr(_) => true, // should be unreachable.
166            Error::RequestFailed(RequestFailedError { error, .. }) => error.should_retire_circ(),
167            Error::Bug(_) => true,
168        }
169    }
170
171    /// Return the peer or peers that are to be blamed for the error.
172    ///
173    /// (This can return multiple peers if the request failed because multiple
174    /// circuit attempts all failed.)
175    pub fn cache_ids(&self) -> Vec<&OwnedChanTarget> {
176        match &self {
177            Error::CircMgr(e) => e.peers(),
178            Error::RequestFailed(RequestFailedError {
179                source: Some(source),
180                ..
181            }) => vec![source.cache_id()],
182            _ => Vec::new(),
183        }
184    }
185}
186
187impl RequestError {
188    /// Return true if this error means that the circuit shouldn't be used
189    /// for any more directory requests.
190    pub fn should_retire_circ(&self) -> bool {
191        // TODO: probably this is too aggressive, and we should
192        // actually _not_ dump the circuit under all circumstances.
193        true
194    }
195}
196
197impl HasKind for RequestError {
198    fn kind(&self) -> ErrorKind {
199        use ErrorKind as EK;
200        use RequestError as E;
201        match self {
202            E::DirTimeout => EK::TorNetworkTimeout,
203            E::TruncatedHeaders => EK::TorProtocolViolation,
204            E::ResponseTooLong(_) => EK::TorProtocolViolation,
205            E::HeadersTooLong(_) => EK::TorProtocolViolation,
206            E::Utf8Encoding(_) => EK::TorProtocolViolation,
207            // TODO: it would be good to get more information out of the IoError
208            // in this case, but that would require a bunch of gnarly
209            // downcasting.
210            E::IoError(_) => EK::TorDirectoryError,
211            E::Proto(e) => e.kind(),
212            E::HttparseError(_) => EK::TorProtocolViolation,
213            E::HttpError(_) => EK::Internal,
214            E::ContentEncoding(_) => EK::TorProtocolViolation,
215            E::TooMuchClockSkew => EK::TorDirectoryError,
216            E::EmptyRequest => EK::Internal,
217            E::EmptyResponse => EK::TorProtocolViolation,
218            E::HttpStatus(_, _) => EK::TorDirectoryError,
219            E::Tunnel(e) => e.kind(),
220        }
221    }
222}
223
224impl HasKind for RequestFailedError {
225    fn kind(&self) -> ErrorKind {
226        self.error.kind()
227    }
228}
229
230impl HasKind for Error {
231    fn kind(&self) -> ErrorKind {
232        use Error as E;
233        match self {
234            E::CircMgr(e) => e.kind(),
235            E::RequestFailed(e) => e.kind(),
236            E::Bug(e) => e.kind(),
237        }
238    }
239}
240
241#[cfg(any(feature = "hs-client", feature = "hs-service"))]
242impl Error {
243    /// Return true if this error is one that we should report as a suspicious event,
244    /// along with the dirserver and description of the relevant document,
245    /// if the request was made anonymously.
246    pub fn should_report_as_suspicious_if_anon(&self) -> bool {
247        use Error as E;
248        match self {
249            E::CircMgr(_) => false,
250            E::RequestFailed(e) => e.error.should_report_as_suspicious_if_anon(),
251            E::Bug(_) => false,
252        }
253    }
254}
255#[cfg(any(feature = "hs-client", feature = "hs-service"))]
256impl RequestError {
257    /// Return true if this error is one that we should report as a suspicious event,
258    /// along with the dirserver and description of the relevant document,
259    /// if the request was made anonymously.
260    pub fn should_report_as_suspicious_if_anon(&self) -> bool {
261        use tor_proto::Error as PE;
262        match self {
263            RequestError::ResponseTooLong(_) => true,
264            RequestError::HeadersTooLong(_) => true,
265            RequestError::Proto(PE::ExcessInboundCells) => true,
266            RequestError::Proto(_) => false,
267            RequestError::DirTimeout => false,
268            RequestError::TruncatedHeaders => false,
269            RequestError::Utf8Encoding(_) => false,
270            RequestError::IoError(_) => false,
271            RequestError::HttparseError(_) => false,
272            RequestError::HttpError(_) => false,
273            RequestError::ContentEncoding(_) => false,
274            RequestError::TooMuchClockSkew => false,
275            RequestError::EmptyRequest => false,
276            RequestError::EmptyResponse => false,
277            RequestError::HttpStatus(_, _) => false,
278            RequestError::Tunnel(_) => false,
279        }
280    }
281}