]> git.proxmox.com Git - proxmox-backup.git/blob - src/server/rest.rs
server: rest: implement max URI path and query length request limits
[proxmox-backup.git] / src / server / rest.rs
1 use std::collections::HashMap;
2 use std::future::Future;
3 use std::hash::BuildHasher;
4 use std::path::{Path, PathBuf};
5 use std::pin::Pin;
6 use std::sync::Arc;
7 use std::task::{Context, Poll};
8
9 use anyhow::{bail, format_err, Error};
10 use futures::future::{self, FutureExt, TryFutureExt};
11 use futures::stream::TryStreamExt;
12 use hyper::header::{self, HeaderMap};
13 use hyper::http::request::Parts;
14 use hyper::{Body, Request, Response, StatusCode};
15 use lazy_static::lazy_static;
16 use serde_json::{json, Value};
17 use tokio::fs::File;
18 use tokio::time::Instant;
19 use url::form_urlencoded;
20 use regex::Regex;
21
22 use proxmox::http_err;
23 use proxmox::api::{
24 ApiHandler,
25 ApiMethod,
26 HttpError,
27 Permission,
28 RpcEnvironment,
29 RpcEnvironmentType,
30 check_api_permission,
31 };
32 use proxmox::api::schema::{
33 ObjectSchema,
34 parse_parameter_strings,
35 parse_simple_value,
36 verify_json_object,
37 };
38
39 use super::environment::RestEnvironment;
40 use super::formatter::*;
41 use super::ApiConfig;
42
43 use crate::auth_helpers::*;
44 use crate::api2::types::Userid;
45 use crate::tools;
46 use crate::tools::ticket::Ticket;
47 use crate::config::cached_user_info::CachedUserInfo;
48
49 extern "C" { fn tzset(); }
50
51 pub struct RestServer {
52 pub api_config: Arc<ApiConfig>,
53 }
54
55 const MAX_URI_QUERY_LENGTH: usize = 3072;
56
57 impl RestServer {
58
59 pub fn new(api_config: ApiConfig) -> Self {
60 Self { api_config: Arc::new(api_config) }
61 }
62 }
63
64 impl tower_service::Service<&tokio_openssl::SslStream<tokio::net::TcpStream>> for RestServer {
65 type Response = ApiService;
66 type Error = Error;
67 type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
68
69 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
70 Poll::Ready(Ok(()))
71 }
72
73 fn call(&mut self, ctx: &tokio_openssl::SslStream<tokio::net::TcpStream>) -> Self::Future {
74 match ctx.get_ref().peer_addr() {
75 Err(err) => {
76 future::err(format_err!("unable to get peer address - {}", err)).boxed()
77 }
78 Ok(peer) => {
79 future::ok(ApiService { peer, api_config: self.api_config.clone() }).boxed()
80 }
81 }
82 }
83 }
84
85 impl tower_service::Service<&tokio::net::TcpStream> for RestServer {
86 type Response = ApiService;
87 type Error = Error;
88 type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
89
90 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
91 Poll::Ready(Ok(()))
92 }
93
94 fn call(&mut self, ctx: &tokio::net::TcpStream) -> Self::Future {
95 match ctx.peer_addr() {
96 Err(err) => {
97 future::err(format_err!("unable to get peer address - {}", err)).boxed()
98 }
99 Ok(peer) => {
100 future::ok(ApiService { peer, api_config: self.api_config.clone() }).boxed()
101 }
102 }
103 }
104 }
105
106 pub struct ApiService {
107 pub peer: std::net::SocketAddr,
108 pub api_config: Arc<ApiConfig>,
109 }
110
111 fn log_response(
112 peer: &std::net::SocketAddr,
113 method: hyper::Method,
114 path: &str,
115 resp: &Response<Body>,
116 ) {
117
118 if resp.extensions().get::<NoLogExtension>().is_some() { return; };
119
120 // we also log URL-to-long requests, so avoid message bigger than PIPE_BUF (4k on Linux)
121 // to profit from atomicty guarantees for O_APPEND opened logfiles
122 let path = &path[..MAX_URI_QUERY_LENGTH.min(path.len())];
123
124 let status = resp.status();
125
126 if !(status.is_success() || status.is_informational()) {
127 let reason = status.canonical_reason().unwrap_or("unknown reason");
128
129 let mut message = "request failed";
130 if let Some(data) = resp.extensions().get::<ErrorMessageExtension>() {
131 message = &data.0;
132 }
133
134 log::error!("{} {}: {} {}: [client {}] {}", method.as_str(), path, status.as_str(), reason, peer, message);
135 }
136 }
137
138 fn get_proxied_peer(headers: &HeaderMap) -> Option<std::net::SocketAddr> {
139 lazy_static! {
140 static ref RE: Regex = Regex::new(r#"for="([^"]+)""#).unwrap();
141 }
142 let forwarded = headers.get(header::FORWARDED)?.to_str().ok()?;
143 let capture = RE.captures(&forwarded)?;
144 let rhost = capture.get(1)?.as_str();
145
146 rhost.parse().ok()
147 }
148
149 impl tower_service::Service<Request<Body>> for ApiService {
150 type Response = Response<Body>;
151 type Error = Error;
152 type Future = Pin<Box<dyn Future<Output = Result<Response<Body>, Self::Error>> + Send>>;
153
154 fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
155 Poll::Ready(Ok(()))
156 }
157
158 fn call(&mut self, req: Request<Body>) -> Self::Future {
159 let path = req.uri().path().to_owned();
160 let method = req.method().clone();
161
162 let config = Arc::clone(&self.api_config);
163 let peer = match get_proxied_peer(req.headers()) {
164 Some(proxied_peer) => proxied_peer,
165 None => self.peer,
166 };
167 async move {
168 let response = match handle_request(config, req, &peer).await {
169 Ok(response) => response,
170 Err(err) => {
171 let (err, code) = match err.downcast_ref::<HttpError>() {
172 Some(apierr) => (apierr.message.clone(), apierr.code),
173 _ => (err.to_string(), StatusCode::BAD_REQUEST),
174 };
175 Response::builder().status(code).body(err.into())?
176 }
177 };
178 log_response(&peer, method, &path, &response);
179 Ok(response)
180 }
181 .boxed()
182 }
183 }
184
185 fn parse_query_parameters<S: 'static + BuildHasher + Send>(
186 param_schema: &ObjectSchema,
187 form: &str, // x-www-form-urlencoded body data
188 parts: &Parts,
189 uri_param: &HashMap<String, String, S>,
190 ) -> Result<Value, Error> {
191
192 let mut param_list: Vec<(String, String)> = vec![];
193
194 if !form.is_empty() {
195 for (k, v) in form_urlencoded::parse(form.as_bytes()).into_owned() {
196 param_list.push((k, v));
197 }
198 }
199
200 if let Some(query_str) = parts.uri.query() {
201 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
202 if k == "_dc" { continue; } // skip extjs "disable cache" parameter
203 param_list.push((k, v));
204 }
205 }
206
207 for (k, v) in uri_param {
208 param_list.push((k.clone(), v.clone()));
209 }
210
211 let params = parse_parameter_strings(&param_list, param_schema, true)?;
212
213 Ok(params)
214 }
215
216 async fn get_request_parameters<S: 'static + BuildHasher + Send>(
217 param_schema: &ObjectSchema,
218 parts: Parts,
219 req_body: Body,
220 uri_param: HashMap<String, String, S>,
221 ) -> Result<Value, Error> {
222
223 let mut is_json = false;
224
225 if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
226 match value.to_str().map(|v| v.split(';').next()) {
227 Ok(Some("application/x-www-form-urlencoded")) => {
228 is_json = false;
229 }
230 Ok(Some("application/json")) => {
231 is_json = true;
232 }
233 _ => bail!("unsupported content type {:?}", value.to_str()),
234 }
235 }
236
237 let body = req_body
238 .map_err(|err| http_err!(BAD_REQUEST, "Promlems reading request body: {}", err))
239 .try_fold(Vec::new(), |mut acc, chunk| async move {
240 if acc.len() + chunk.len() < 64*1024 { //fimxe: max request body size?
241 acc.extend_from_slice(&*chunk);
242 Ok(acc)
243 } else {
244 Err(http_err!(BAD_REQUEST, "Request body too large"))
245 }
246 }).await?;
247
248 let utf8_data = std::str::from_utf8(&body)
249 .map_err(|err| format_err!("Request body not uft8: {}", err))?;
250
251 if is_json {
252 let mut params: Value = serde_json::from_str(utf8_data)?;
253 for (k, v) in uri_param {
254 if let Some((_optional, prop_schema)) = param_schema.lookup(&k) {
255 params[&k] = parse_simple_value(&v, prop_schema)?;
256 }
257 }
258 verify_json_object(&params, param_schema)?;
259 return Ok(params);
260 } else {
261 parse_query_parameters(param_schema, utf8_data, &parts, &uri_param)
262 }
263 }
264
265 struct NoLogExtension();
266
267 async fn proxy_protected_request(
268 info: &'static ApiMethod,
269 mut parts: Parts,
270 req_body: Body,
271 peer: &std::net::SocketAddr,
272 ) -> Result<Response<Body>, Error> {
273
274 let mut uri_parts = parts.uri.clone().into_parts();
275
276 uri_parts.scheme = Some(http::uri::Scheme::HTTP);
277 uri_parts.authority = Some(http::uri::Authority::from_static("127.0.0.1:82"));
278 let new_uri = http::Uri::from_parts(uri_parts).unwrap();
279
280 parts.uri = new_uri;
281
282 let mut request = Request::from_parts(parts, req_body);
283 request
284 .headers_mut()
285 .insert(header::FORWARDED, format!("for=\"{}\";", peer).parse().unwrap());
286
287 let reload_timezone = info.reload_timezone;
288
289 let resp = hyper::client::Client::new()
290 .request(request)
291 .map_err(Error::from)
292 .map_ok(|mut resp| {
293 resp.extensions_mut().insert(NoLogExtension());
294 resp
295 })
296 .await?;
297
298 if reload_timezone { unsafe { tzset(); } }
299
300 Ok(resp)
301 }
302
303 pub async fn handle_api_request<Env: RpcEnvironment, S: 'static + BuildHasher + Send>(
304 mut rpcenv: Env,
305 info: &'static ApiMethod,
306 formatter: &'static OutputFormatter,
307 parts: Parts,
308 req_body: Body,
309 uri_param: HashMap<String, String, S>,
310 ) -> Result<Response<Body>, Error> {
311
312 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
313
314 let result = match info.handler {
315 ApiHandler::AsyncHttp(handler) => {
316 let params = parse_query_parameters(info.parameters, "", &parts, &uri_param)?;
317 (handler)(parts, req_body, params, info, Box::new(rpcenv)).await
318 }
319 ApiHandler::Sync(handler) => {
320 let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
321 (handler)(params, info, &mut rpcenv)
322 .map(|data| (formatter.format_data)(data, &rpcenv))
323 }
324 ApiHandler::Async(handler) => {
325 let params = get_request_parameters(info.parameters, parts, req_body, uri_param).await?;
326 (handler)(params, info, &mut rpcenv)
327 .await
328 .map(|data| (formatter.format_data)(data, &rpcenv))
329 }
330 };
331
332 let resp = match result {
333 Ok(resp) => resp,
334 Err(err) => {
335 if let Some(httperr) = err.downcast_ref::<HttpError>() {
336 if httperr.code == StatusCode::UNAUTHORIZED {
337 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
338 }
339 }
340 (formatter.format_error)(err)
341 }
342 };
343
344 if info.reload_timezone { unsafe { tzset(); } }
345
346 Ok(resp)
347 }
348
349 fn get_index(
350 userid: Option<Userid>,
351 csrf_token: Option<String>,
352 language: Option<String>,
353 api: &Arc<ApiConfig>,
354 parts: Parts,
355 ) -> Response<Body> {
356
357 let nodename = proxmox::tools::nodename();
358 let user = userid.as_ref().map(|u| u.as_str()).unwrap_or("");
359
360 let csrf_token = csrf_token.unwrap_or_else(|| String::from(""));
361
362 let mut debug = false;
363 let mut template_file = "index";
364
365 if let Some(query_str) = parts.uri.query() {
366 for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
367 if k == "debug" && v != "0" && v != "false" {
368 debug = true;
369 } else if k == "console" {
370 template_file = "console";
371 }
372 }
373 }
374
375 let mut lang = String::from("");
376 if let Some(language) = language {
377 if Path::new(&format!("/usr/share/pbs-i18n/pbs-lang-{}.js", language)).exists() {
378 lang = language;
379 }
380 }
381
382 let data = json!({
383 "NodeName": nodename,
384 "UserName": user,
385 "CSRFPreventionToken": csrf_token,
386 "language": lang,
387 "debug": debug,
388 });
389
390 let (ct, index) = match api.render_template(template_file, &data) {
391 Ok(index) => ("text/html", index),
392 Err(err) => {
393 ("text/plain", format!("Error rendering template: {}", err))
394 }
395 };
396
397 Response::builder()
398 .status(StatusCode::OK)
399 .header(header::CONTENT_TYPE, ct)
400 .body(index.into())
401 .unwrap()
402 }
403
404 fn extension_to_content_type(filename: &Path) -> (&'static str, bool) {
405
406 if let Some(ext) = filename.extension().and_then(|osstr| osstr.to_str()) {
407 return match ext {
408 "css" => ("text/css", false),
409 "html" => ("text/html", false),
410 "js" => ("application/javascript", false),
411 "json" => ("application/json", false),
412 "map" => ("application/json", false),
413 "png" => ("image/png", true),
414 "ico" => ("image/x-icon", true),
415 "gif" => ("image/gif", true),
416 "svg" => ("image/svg+xml", false),
417 "jar" => ("application/java-archive", true),
418 "woff" => ("application/font-woff", true),
419 "woff2" => ("application/font-woff2", true),
420 "ttf" => ("application/font-snft", true),
421 "pdf" => ("application/pdf", true),
422 "epub" => ("application/epub+zip", true),
423 "mp3" => ("audio/mpeg", true),
424 "oga" => ("audio/ogg", true),
425 "tgz" => ("application/x-compressed-tar", true),
426 _ => ("application/octet-stream", false),
427 };
428 }
429
430 ("application/octet-stream", false)
431 }
432
433 async fn simple_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
434
435 let (content_type, _nocomp) = extension_to_content_type(&filename);
436
437 use tokio::io::AsyncReadExt;
438
439 let mut file = File::open(filename)
440 .await
441 .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
442
443 let mut data: Vec<u8> = Vec::new();
444 file.read_to_end(&mut data)
445 .await
446 .map_err(|err| http_err!(BAD_REQUEST, "File read failed: {}", err))?;
447
448 let mut response = Response::new(data.into());
449 response.headers_mut().insert(
450 header::CONTENT_TYPE,
451 header::HeaderValue::from_static(content_type));
452 Ok(response)
453 }
454
455 async fn chuncked_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
456 let (content_type, _nocomp) = extension_to_content_type(&filename);
457
458 let file = File::open(filename)
459 .await
460 .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?;
461
462 let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
463 .map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze()));
464 let body = Body::wrap_stream(payload);
465
466 // fixme: set other headers ?
467 Ok(Response::builder()
468 .status(StatusCode::OK)
469 .header(header::CONTENT_TYPE, content_type)
470 .body(body)
471 .unwrap()
472 )
473 }
474
475 async fn handle_static_file_download(filename: PathBuf) -> Result<Response<Body>, Error> {
476
477 let metadata = tokio::fs::metadata(filename.clone())
478 .map_err(|err| http_err!(BAD_REQUEST, "File access problems: {}", err))
479 .await?;
480
481 if metadata.len() < 1024*32 {
482 simple_static_file_download(filename).await
483 } else {
484 chuncked_static_file_download(filename).await
485 }
486 }
487
488 fn extract_auth_data(headers: &http::HeaderMap) -> (Option<String>, Option<String>, Option<String>) {
489
490 let mut ticket = None;
491 let mut language = None;
492 if let Some(raw_cookie) = headers.get("COOKIE") {
493 if let Ok(cookie) = raw_cookie.to_str() {
494 ticket = tools::extract_cookie(cookie, "PBSAuthCookie");
495 language = tools::extract_cookie(cookie, "PBSLangCookie");
496 }
497 }
498
499 let token = match headers.get("CSRFPreventionToken").map(|v| v.to_str()) {
500 Some(Ok(v)) => Some(v.to_owned()),
501 _ => None,
502 };
503
504 (ticket, token, language)
505 }
506
507 fn check_auth(
508 method: &hyper::Method,
509 ticket: &Option<String>,
510 token: &Option<String>,
511 user_info: &CachedUserInfo,
512 ) -> Result<Userid, Error> {
513 let ticket_lifetime = tools::ticket::TICKET_LIFETIME;
514
515 let ticket = ticket.as_ref().map(String::as_str);
516 let userid: Userid = Ticket::parse(&ticket.ok_or_else(|| format_err!("missing ticket"))?)?
517 .verify_with_time_frame(public_auth_key(), "PBS", None, -300..ticket_lifetime)?;
518
519 if !user_info.is_active_user(&userid) {
520 bail!("user account disabled or expired.");
521 }
522
523 if method != hyper::Method::GET {
524 if let Some(token) = token {
525 verify_csrf_prevention_token(csrf_secret(), &userid, &token, -300, ticket_lifetime)?;
526 } else {
527 bail!("missing CSRF prevention token");
528 }
529 }
530
531 Ok(userid)
532 }
533
534 async fn handle_request(
535 api: Arc<ApiConfig>,
536 req: Request<Body>,
537 peer: &std::net::SocketAddr,
538 ) -> Result<Response<Body>, Error> {
539
540 let (parts, body) = req.into_parts();
541 let method = parts.method.clone();
542 let (path, components) = tools::normalize_uri_path(parts.uri.path())?;
543
544 let comp_len = components.len();
545
546 let query = parts.uri.query().unwrap_or_default();
547 if path.len() + query.len() > MAX_URI_QUERY_LENGTH {
548 return Ok(Response::builder()
549 .status(StatusCode::URI_TOO_LONG)
550 .body("".into())
551 .unwrap());
552 }
553
554 let env_type = api.env_type();
555 let mut rpcenv = RestEnvironment::new(env_type);
556
557 rpcenv.set_client_ip(Some(*peer));
558
559 let user_info = CachedUserInfo::new()?;
560
561 let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
562 let access_forbidden_time = std::time::Instant::now() + std::time::Duration::from_millis(500);
563
564 if comp_len >= 1 && components[0] == "api2" {
565
566 if comp_len >= 2 {
567
568 let format = components[1];
569
570 let formatter = match format {
571 "json" => &JSON_FORMATTER,
572 "extjs" => &EXTJS_FORMATTER,
573 _ => bail!("Unsupported output format '{}'.", format),
574 };
575
576 let mut uri_param = HashMap::new();
577 let api_method = api.find_method(&components[2..], method.clone(), &mut uri_param);
578
579 let mut auth_required = true;
580 if let Some(api_method) = api_method {
581 if let Permission::World = *api_method.access.permission {
582 auth_required = false; // no auth for endpoints with World permission
583 }
584 }
585
586 if auth_required {
587 let (ticket, token, _) = extract_auth_data(&parts.headers);
588 match check_auth(&method, &ticket, &token, &user_info) {
589 Ok(userid) => rpcenv.set_user(Some(userid.to_string())),
590 Err(err) => {
591 // always delay unauthorized calls by 3 seconds (from start of request)
592 let err = http_err!(UNAUTHORIZED, "authentication failed - {}", err);
593 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
594 return Ok((formatter.format_error)(err));
595 }
596 }
597 }
598
599 match api_method {
600 None => {
601 let err = http_err!(NOT_FOUND, "Path '{}' not found.", path);
602 return Ok((formatter.format_error)(err));
603 }
604 Some(api_method) => {
605 let user = rpcenv.get_user();
606 if !check_api_permission(api_method.access.permission, user.as_deref(), &uri_param, user_info.as_ref()) {
607 let err = http_err!(FORBIDDEN, "permission check failed");
608 tokio::time::delay_until(Instant::from_std(access_forbidden_time)).await;
609 return Ok((formatter.format_error)(err));
610 }
611
612 let result = if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
613 proxy_protected_request(api_method, parts, body, peer).await
614 } else {
615 handle_api_request(rpcenv, api_method, formatter, parts, body, uri_param).await
616 };
617
618 if let Err(err) = result {
619 return Ok((formatter.format_error)(err));
620 }
621 return result;
622 }
623 }
624
625 }
626 } else {
627 // not Auth required for accessing files!
628
629 if method != hyper::Method::GET {
630 bail!("Unsupported HTTP method {}", method);
631 }
632
633 if comp_len == 0 {
634 let (ticket, token, language) = extract_auth_data(&parts.headers);
635 if ticket != None {
636 match check_auth(&method, &ticket, &token, &user_info) {
637 Ok(userid) => {
638 let new_token = assemble_csrf_prevention_token(csrf_secret(), &userid);
639 return Ok(get_index(Some(userid), Some(new_token), language, &api, parts));
640 }
641 _ => {
642 tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
643 return Ok(get_index(None, None, language, &api, parts));
644 }
645 }
646 } else {
647 return Ok(get_index(None, None, language, &api, parts));
648 }
649 } else {
650 let filename = api.find_alias(&components);
651 return handle_static_file_download(filename).await;
652 }
653 }
654
655 Err(http_err!(NOT_FOUND, "Path '{}' not found.", path))
656 }