aboutsummaryrefslogtreecommitdiff
path: root/src/request.rs
blob: e64f9ddd6dfe0ed091348bb2a764bd84b067ad5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! Provides the [`SputnikParts`] and [`SputnikBody`] traits.

use cookie::Cookie;
use mime::Mime;
use serde::{Deserialize, de::DeserializeOwned};
use hyper::{HeaderMap, body::Bytes, header, http::request::Parts};
use time::Duration;
use std::{collections::HashMap, sync::Arc};
use rand::{Rng, distributions::Alphanumeric};
use async_trait::async_trait;

use crate::response::SputnikHeaders;

const CSRF_COOKIE_NAME : &str = "csrf";

pub trait SputnikParts {
    /// Parses the query string of the request into a given struct.
    fn query<X: DeserializeOwned>(&self) -> Result<X,QueryError>;

    /// Parses the cookies of the request.
    fn cookies(&mut self) -> Arc<HashMap<String, Cookie<'static>>>;

    /// Enforces a specific Content-Type.
    fn enforce_content_type(&self, mime: Mime) -> Result<(), WrongContentTypeError>;

    /// A map of response headers to allow methods of this trait to set response
    /// headers without needing to take a [`Response`](hyper::http::response::Response) as an argument.
    ///
    /// You need to take care to append these headers to the response yourself.
    /// This is intended to be done after your routing logic so that your
    /// individual request handlers don't have to worry about it.
    fn response_headers(&mut self) -> &mut HeaderMap;

    /// Returns a CSRF token, either extracted from the `csrf` cookie or newly
    /// generated if the cookie wasn't sent (in which case a set-cookie header is
    /// appended to [`Self::response_headers`]).
    ///
    /// If there is no cookie, calling this method multiple times only generates
    /// a new token on the first call, further calls return the previously
    /// generated token.
    fn csrf_token(&mut self) -> CsrfToken;
}

impl SputnikParts for hyper::http::request::Parts {
    fn query<T: DeserializeOwned>(&self) -> Result<T,QueryError> {
        serde_urlencoded::from_str::<T>(self.uri.query().unwrap_or("")).map_err(QueryError)
    }

    fn response_headers(&mut self) -> &mut HeaderMap {
        if self.extensions.get::<HeaderMap>().is_none() {
            self.extensions.insert(HeaderMap::new());
        }
        self.extensions.get_mut::<HeaderMap>().unwrap()
    }

    fn cookies(&mut self) -> Arc<HashMap<String, Cookie<'static>>> {
        let cookies: Option<&Arc<HashMap<String, Cookie>>> = self.extensions.get();
        if let Some(cookies) = cookies {
            return cookies.clone();
        }

        let mut cookies = HashMap::new();
        for header in self.headers.get_all(header::COOKIE) {
            let raw_str = match std::str::from_utf8(header.as_bytes()) {
                Ok(string) => string,
                Err(_) => continue
            };

            for cookie_str in raw_str.split(';').map(|s| s.trim()) {
                if let Ok(cookie) = Cookie::parse_encoded(cookie_str) {
                    cookies.insert(cookie.name().to_string(), cookie.into_owned());
                }
            }
        }
        let cookies = Arc::new(cookies);
        self.extensions.insert(cookies.clone());
        cookies
    }

    fn enforce_content_type(&self, mime: Mime) -> Result<(), WrongContentTypeError> {
        if let Some(content_type) = self.headers.get(header::CONTENT_TYPE) {
            if *content_type == mime.to_string() {
                return Ok(())
            }
        }
        Err(WrongContentTypeError{expected: mime, received: self.headers.get(header::CONTENT_TYPE).as_ref().and_then(|h| h.to_str().ok().map(|s| s.to_owned()))})
    }

    fn csrf_token(&mut self) -> CsrfToken {
        if let Some(token) = self.extensions.get::<CsrfToken>() {
            return token.clone()
        }
        csrf_token_from_cookies(self)
        .unwrap_or_else(|| {
            let token: String = rand::thread_rng().sample_iter(
                Alphanumeric
                // must be HTML-safe because we embed it in CsrfToken::html_input
            ).take(16).collect();
            let mut c = Cookie::new(CSRF_COOKIE_NAME, token.clone());
            c.set_secure(Some(true));
            c.set_max_age(Some(Duration::hours(1)));

            self.response_headers().set_cookie(c);
            let token = CsrfToken(token);
            self.extensions.insert(token.clone());
            token
        })
    }
}

/// A CSRF token retrievable with [`SputnikParts::csrf_token`].
#[derive(Clone)]
pub struct CsrfToken(String);

impl std::fmt::Display for CsrfToken {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl CsrfToken {
    /// Returns a hidden HTML input to be embedded in forms that are received
    /// with [`crate::request::SputnikBody::into_form_csrf`].
    pub fn html_input(&self) -> String {
        format!("<input name=csrf type=hidden value=\"{}\">", self)
    }
}

#[async_trait]
pub trait SputnikBody {
    async fn into_bytes(self) -> Result<Bytes, BodyError>;

    /// Parses a `application/x-www-form-urlencoded` request body into a given struct.
    ///
    /// This does make you vulnerable to CSRF, so you normally want to use
    /// [`SputnikBody::into_form_csrf()`] instead.
    async fn into_form<T: DeserializeOwned>(self) -> Result<T, FormError>;

    /// Parses a `application/x-www-form-urlencoded` request body into a given struct.
    /// Protects from CSRF by checking that the request body contains the same token retrieved from the cookies.
    ///
    /// The HTML form must embed a hidden input generated with [`CsrfToken::html_input`].
    async fn into_form_csrf<T: DeserializeOwned>(self, req: &mut Parts) -> Result<T, CsrfProtectedFormError>;

    /// Attempts to deserialize the request body as JSON.
    #[cfg(feature = "json")]
    #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
    async fn into_json<T: DeserializeOwned>(self) -> Result<T, JsonError>;
}

fn csrf_token_from_cookies(req: &mut Parts) -> Option<CsrfToken> {
    req.cookies()
        .get(CSRF_COOKIE_NAME)
        .map(|cookie| {
            let token = CsrfToken(cookie.value().to_string());
            req.extensions.insert(token.clone());
            token
        })
}

#[async_trait]
impl SputnikBody for hyper::Body {
    async fn into_bytes(self) -> Result<Bytes, BodyError> {
        hyper::body::to_bytes(self).await.map_err(BodyError)
    }

    async fn into_form<T: DeserializeOwned>(self) -> Result<T, FormError> {
        let full_body = self.into_bytes().await?;
        Ok(serde_urlencoded::from_bytes::<T>(&full_body)?)
    }

    async fn into_form_csrf<T: DeserializeOwned>(self, req: &mut Parts) -> Result<T, CsrfProtectedFormError> {
        let full_body = self.into_bytes().await?;
        let csrf_data = serde_urlencoded::from_bytes::<CsrfData>(&full_body).map_err(|_| CsrfProtectedFormError::NoCsrf)?;
        match csrf_token_from_cookies(req) {
            Some(token) => if token.to_string() == csrf_data.csrf {
                Ok(serde_urlencoded::from_bytes::<T>(&full_body)?)
            } else {
                Err(CsrfProtectedFormError::Mismatch)
            }
            None => Err(CsrfProtectedFormError::NoCookie)
        }
    }

    #[cfg(feature = "json")]
    #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
    async fn into_json<T: DeserializeOwned>(self) -> Result<T, JsonError> {
        let full_body = self.into_bytes().await?;
        Ok(serde_json::from_slice::<T>(&full_body)?)
    }
}

#[derive(Deserialize)]
struct CsrfData {
    csrf: String,
}

#[derive(thiserror::Error, Debug)]
#[error("query deserialize error: {0}")]
pub struct QueryError(pub serde_urlencoded::de::Error);

#[derive(thiserror::Error, Debug)]
#[error("failed to read body")]
pub struct BodyError(pub hyper::Error);

#[derive(thiserror::Error, Debug)]
#[error("expected Content-Type {expected} but received {}", received.as_ref().unwrap_or(&"nothing".to_owned()))]
pub struct WrongContentTypeError {
    pub expected: Mime,
    pub received: Option<String>,
}

#[derive(thiserror::Error, Debug)]
pub enum FormError {
    #[error("{0}")]
    Body(#[from] BodyError),

    #[error("form deserialize error: {0}")]
    Deserialize(#[from] serde_urlencoded::de::Error),
}

#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
#[derive(thiserror::Error, Debug)]
pub enum JsonError {
    #[error("{0}")]
    Body(#[from] BodyError),

    #[error("json deserialize error: {0}")]
    Deserialize(#[from] serde_json::Error),
}

#[derive(thiserror::Error, Debug)]
pub enum CsrfProtectedFormError {
    #[error("{0}")]
    Body(#[from] BodyError),

    #[error("form deserialize error: {0}")]
    Deserialize(#[from] serde_urlencoded::de::Error),

    #[error("no csrf token in form data")]
    NoCsrf,

    #[error("no csrf cookie")]
    NoCookie,

    #[error("csrf parameter doesn't match csrf cookie")]
    Mismatch,
}

#[cfg(test)]
mod tests {
    use hyper::Request;

    use super::*;

    #[test]
    fn test_csrf_token() {
        let mut parts = Request::new(hyper::Body::empty()).into_parts().0;
        let tok1 = parts.csrf_token();
        let tok2 = parts.csrf_token();
        assert_eq!(tok1.to_string(), tok2.to_string());
        assert_eq!(parts.response_headers().len(), 1);
    }
}