-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathhttp_request_info.rs
More file actions
381 lines (358 loc) · 13.7 KB
/
http_request_info.rs
File metadata and controls
381 lines (358 loc) · 13.7 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
use crate::webserver::content_security_policy::ContentSecurityPolicy;
use crate::AppState;
use actix_multipart::form::bytes::Bytes;
use actix_multipart::form::tempfile::TempFile;
use actix_multipart::form::FieldReader;
use actix_multipart::form::Limits;
use actix_multipart::Multipart;
use actix_web::dev::ServiceRequest;
use actix_web::http::header::Header;
use actix_web::http::header::CONTENT_TYPE;
use actix_web::web;
use actix_web::web::Form;
use actix_web::FromRequest;
use actix_web::HttpMessage as _;
use actix_web::HttpRequest;
use actix_web_httpauth::headers::authorization::Authorization;
use actix_web_httpauth::headers::authorization::Basic;
use anyhow::anyhow;
use anyhow::Context;
use std::collections::HashMap;
use std::net::IpAddr;
use std::rc::Rc;
use std::sync::Arc;
use tokio_stream::StreamExt;
use super::oidc::OidcClaims;
use super::request_variables::param_map;
use super::request_variables::ParamMap;
#[derive(Debug)]
pub struct RequestInfo {
pub method: actix_web::http::Method,
pub path: String,
pub protocol: String,
pub get_variables: ParamMap,
pub post_variables: ParamMap,
pub uploaded_files: Rc<HashMap<String, TempFile>>,
pub headers: ParamMap,
pub client_ip: Option<IpAddr>,
pub cookies: ParamMap,
pub basic_auth: Option<Basic>,
pub app_state: Arc<AppState>,
pub clone_depth: u8,
pub raw_body: Option<Vec<u8>>,
pub oidc_claims: Option<OidcClaims>,
pub content_security_policy: ContentSecurityPolicy,
}
impl RequestInfo {
#[must_use]
pub fn clone_without_variables(&self) -> Self {
Self {
method: self.method.clone(),
path: self.path.clone(),
protocol: self.protocol.clone(),
get_variables: ParamMap::new(),
post_variables: ParamMap::new(),
uploaded_files: self.uploaded_files.clone(),
headers: self.headers.clone(),
client_ip: self.client_ip,
cookies: self.cookies.clone(),
basic_auth: self.basic_auth.clone(),
app_state: self.app_state.clone(),
clone_depth: self.clone_depth + 1,
raw_body: self.raw_body.clone(),
oidc_claims: self.oidc_claims.clone(),
content_security_policy: self.content_security_policy.clone(),
}
}
pub fn is_embedded(&self) -> bool {
self.get_variables.contains_key("_sqlpage_embed")
}
}
impl Clone for RequestInfo {
fn clone(&self) -> Self {
let mut clone = self.clone_without_variables();
clone.get_variables.clone_from(&self.get_variables);
clone.post_variables.clone_from(&self.post_variables);
clone
}
}
pub(crate) async fn extract_request_info(
req: &mut ServiceRequest,
app_state: Arc<AppState>,
) -> anyhow::Result<RequestInfo> {
let (http_req, payload) = req.parts_mut();
let method = http_req.method().clone();
let protocol = http_req.connection_info().scheme().to_string();
let config = &app_state.config;
let (post_variables, uploaded_files, raw_body) =
extract_post_data(http_req, payload, config).await?;
let headers = req.headers().iter().map(|(name, value)| {
(
name.to_string(),
String::from_utf8_lossy(value.as_bytes()).to_string(),
)
});
let get_variables = web::Query::<Vec<(String, String)>>::from_query(req.query_string())
.map(web::Query::into_inner)
.unwrap_or_default();
let client_ip = req.peer_addr().map(|addr| addr.ip());
let raw_cookies = req.cookies();
let cookies = raw_cookies
.iter()
.flat_map(|c| c.iter())
.map(|cookie| (cookie.name().to_string(), cookie.value().to_string()));
let basic_auth = Authorization::<Basic>::parse(req)
.ok()
.map(Authorization::into_scheme);
let oidc_claims: Option<OidcClaims> = req.extensions().get::<OidcClaims>().cloned();
Ok(RequestInfo {
method,
path: req.path().to_string(),
headers: param_map(headers),
get_variables: param_map(get_variables),
post_variables: param_map(post_variables),
uploaded_files: Rc::new(HashMap::from_iter(uploaded_files)),
client_ip,
cookies: param_map(cookies),
basic_auth,
app_state,
protocol,
clone_depth: 0,
raw_body,
oidc_claims,
content_security_policy: ContentSecurityPolicy::with_random_nonce()
})
}
async fn extract_post_data(
http_req: &mut actix_web::HttpRequest,
payload: &mut actix_web::dev::Payload,
config: &crate::app_config::AppConfig,
) -> anyhow::Result<(
Vec<(String, String)>,
Vec<(String, TempFile)>,
Option<Vec<u8>>,
)> {
let content_type = http_req
.headers()
.get(&CONTENT_TYPE)
.map(AsRef::as_ref)
.unwrap_or_default();
if content_type.starts_with(b"application/x-www-form-urlencoded") {
let vars = extract_urlencoded_post_variables(http_req, payload).await?;
Ok((vars, Vec::new(), None))
} else if content_type.starts_with(b"multipart/form-data") {
let (vars, files) = extract_multipart_post_data(http_req, payload, config).await?;
Ok((vars, files, None))
} else {
let body = actix_web::web::Bytes::from_request(http_req, payload)
.await
.map(|bytes| bytes.to_vec())
.unwrap_or_default();
Ok((Vec::new(), Vec::new(), Some(body)))
}
}
async fn extract_urlencoded_post_variables(
http_req: &mut actix_web::HttpRequest,
payload: &mut actix_web::dev::Payload,
) -> anyhow::Result<Vec<(String, String)>> {
Form::<Vec<(String, String)>>::from_request(http_req, payload)
.await
.map(Form::into_inner)
.map_err(|e| {
anyhow!(super::ErrorWithStatus {
status: actix_web::http::StatusCode::BAD_REQUEST,
})
.context(format!(
"could not parse request as urlencoded form data: {e}"
))
})
}
async fn extract_multipart_post_data(
http_req: &mut actix_web::HttpRequest,
payload: &mut actix_web::dev::Payload,
config: &crate::app_config::AppConfig,
) -> anyhow::Result<(Vec<(String, String)>, Vec<(String, TempFile)>)> {
let mut post_variables = Vec::new();
let mut uploaded_files = Vec::new();
let mut multipart = Multipart::from_request(http_req, payload)
.await
.map_err(|e| anyhow!("could not parse request as multipart form data: {e}"))?;
let mut limits = Limits::new(config.max_uploaded_file_size, config.max_uploaded_file_size);
log::trace!(
"Parsing multipart form data with a {:?} KiB limit",
limits.total_limit_remaining / 1024
);
while let Some(part) = multipart.next().await {
let field = part.map_err(|e| anyhow!("unable to read form field: {e}"))?;
let content_disposition = field
.content_disposition()
.ok_or_else(|| anyhow!("missing Content-Disposition in form field"))?;
// test if field is a file
let filename = content_disposition.get_filename();
let field_name = content_disposition
.get_name()
.unwrap_or_default()
.to_string();
log::trace!("Parsing multipart field: {field_name}");
if let Some(filename) = filename {
log::debug!("Extracting file: {field_name} ({filename})");
let extracted = extract_file(http_req, field, &mut limits)
.await
.with_context(|| {
format!(
"Failed to extract file {field_name:?}. Max file size: {} kiB",
config.max_uploaded_file_size / 1_024
)
})?;
log::trace!(
"Extracted file {field_name} to \"{}\"",
extracted.file.path().display()
);
if is_file_field_empty(&extracted).await? {
log::debug!("Ignoring empty file field: {field_name}");
continue;
}
uploaded_files.push((field_name, extracted));
} else {
let text_contents = extract_text(http_req, field, &mut limits).await?;
log::trace!("Extracted field as text: {field_name} = {text_contents:?}");
post_variables.push((field_name, text_contents));
}
}
Ok((post_variables, uploaded_files))
}
async fn extract_text(
req: &HttpRequest,
field: actix_multipart::Field,
limits: &mut Limits,
) -> anyhow::Result<String> {
// field is an async stream of Result<Bytes> objects, we collect them into a Vec<u8>
let data = Bytes::read_field(req, field, limits)
.await
.map(|bytes| bytes.data)
.map_err(|e| anyhow!("failed to read form field data: {e}"))?;
Ok(String::from_utf8(data.to_vec())?)
}
async fn extract_file(
req: &HttpRequest,
field: actix_multipart::Field,
limits: &mut Limits,
) -> anyhow::Result<TempFile> {
// extract a tempfile from the field
let file = TempFile::read_field(req, field, limits)
.await
.map_err(|e| anyhow!("Failed to save uploaded file: {e}"))?;
Ok(file)
}
/// file upload form fields that are left blank result in the browser sending an empty file, with a mime type of application/octet-stream.
/// We don't want to treat this the same as actual empty files, so we check for this case.
async fn is_file_field_empty(
uploaded_file: &actix_multipart::form::tempfile::TempFile,
) -> anyhow::Result<bool> {
Ok(
uploaded_file.content_type == Some(mime_guess::mime::APPLICATION_OCTET_STREAM)
&& uploaded_file
.file_name
.as_ref()
.filter(|x| !x.is_empty())
.is_none()
&& tokio::fs::metadata(&uploaded_file.file.path()).await?.len() == 0,
)
}
#[cfg(test)]
mod test {
use super::super::http::SingleOrVec;
use super::*;
use crate::app_config::AppConfig;
use actix_web::{http::header::ContentType, test::TestRequest};
#[actix_web::test]
async fn test_extract_empty_request() {
let config =
serde_json::from_str::<AppConfig>(r#"{"listen_on": "localhost:1234"}"#).unwrap();
let mut service_request = TestRequest::default().to_srv_request();
let app_data = Arc::new(AppState::init(&config).await.unwrap());
let request_info = extract_request_info(&mut service_request, app_data)
.await
.unwrap();
assert_eq!(request_info.post_variables.len(), 0);
assert_eq!(request_info.uploaded_files.len(), 0);
assert_eq!(request_info.get_variables.len(), 0);
}
#[actix_web::test]
async fn test_extract_urlencoded_request() {
let config =
serde_json::from_str::<AppConfig>(r#"{"listen_on": "localhost:1234"}"#).unwrap();
let mut service_request = TestRequest::get()
.uri("/?my_array[]=5")
.insert_header(ContentType::form_url_encoded())
.set_payload("my_array[]=3&my_array[]=Hello%20World&repeated=1&repeated=2")
.to_srv_request();
let app_data = Arc::new(AppState::init(&config).await.unwrap());
let request_info = extract_request_info(&mut service_request, app_data)
.await
.unwrap();
assert_eq!(
request_info.post_variables,
vec![
(
"my_array".to_string(),
SingleOrVec::Vec(vec!["3".to_string(), "Hello World".to_string()])
),
("repeated".to_string(), SingleOrVec::Single("2".to_string())), // without brackets, only the last value is kept
]
.into_iter()
.collect::<ParamMap>()
);
assert_eq!(request_info.uploaded_files.len(), 0);
assert_eq!(
request_info.get_variables,
vec![(
"my_array".to_string(),
SingleOrVec::Vec(vec!["5".to_string()])
)] // with brackets, even if there is only one value, it is kept as a vector
.into_iter()
.collect::<ParamMap>()
);
}
#[actix_web::test]
async fn test_extract_multipart_form_data() {
let _ = env_logger::try_init();
let config =
serde_json::from_str::<AppConfig>(r#"{"listen_on": "localhost:1234"}"#).unwrap();
let mut service_request = TestRequest::get()
.insert_header(("content-type", "multipart/form-data;boundary=xxx"))
.set_payload(
"--xxx\r\n\
Content-Disposition: form-data; name=\"my_array[]\"\r\n\
Content-Type: text/plain\r\n\
\r\n\
3\r\n\
--xxx\r\n\
Content-Disposition: form-data; name=\"my_uploaded_file\"; filename=\"test.txt\"\r\n\
Content-Type: text/plain\r\n\
\r\n\
Hello World\r\n\
--xxx--\r\n"
)
.to_srv_request();
let app_data = Arc::new(AppState::init(&config).await.unwrap());
let request_info = extract_request_info(&mut service_request, app_data)
.await
.unwrap();
assert_eq!(
request_info.post_variables,
vec![(
"my_array".to_string(),
SingleOrVec::Vec(vec!["3".to_string()])
),]
.into_iter()
.collect::<ParamMap>()
);
assert_eq!(request_info.uploaded_files.len(), 1);
let my_upload = &request_info.uploaded_files["my_uploaded_file"];
assert_eq!(my_upload.file_name.as_ref().unwrap(), "test.txt");
assert_eq!(request_info.get_variables.len(), 0);
assert_eq!(std::fs::read(&my_upload.file).unwrap(), b"Hello World");
assert_eq!(request_info.get_variables.len(), 0);
}
}