From 436adb98efeacc5a7b1a4f1a313d891af8d31730 Mon Sep 17 00:00:00 2001 From: Trisha Date: Fri, 25 Mar 2022 16:04:51 -0500 Subject: [PATCH] :construction: swich to the new routing func --- .../ptth_server/src/file_server/internal.rs | 20 +++++++++---------- crates/ptth_server_gui/src/main.rs | 14 +++++++++---- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/crates/ptth_server/src/file_server/internal.rs b/crates/ptth_server/src/file_server/internal.rs index 7a442b7..bf99d11 100644 --- a/crates/ptth_server/src/file_server/internal.rs +++ b/crates/ptth_server/src/file_server/internal.rs @@ -252,7 +252,7 @@ pub struct FileRoots <'a> { struct RoutedPath <'a> { root: &'a Path, - path: std::borrow::Cow <'a, str>, + path_s: std::borrow::Cow <'a, str>, } impl <'a> FileRoots <'a> { @@ -266,11 +266,11 @@ impl <'a> FileRoots <'a> { let encoded_path = &path [0..]; - let path = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?; + let path_s = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?; Ok (Some (RoutedPath { root: self.files, - path, + path_s, })) } } @@ -317,19 +317,17 @@ pub async fn serve_all ( return serve_api (roots.files, &uri, hidden_path, path).await; } - let path = match path.strip_prefix ("/files/") { - Some (x) => x, + let RoutedPath { + root, + path_s, + } = match roots.route (path)? { None => return Ok (NotFound), + Some (x) => x, }; - // TODO: There is totally a dir traversal attack in here somewhere - - let encoded_path = &path [0..]; - - let path_s = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?; let path = Path::new (&*path_s); - let full_path = roots.files.join (path); + let full_path = root.join (path); trace! ("full_path = {:?}", full_path); diff --git a/crates/ptth_server_gui/src/main.rs b/crates/ptth_server_gui/src/main.rs index fe6dba8..4e02078 100644 --- a/crates/ptth_server_gui/src/main.rs +++ b/crates/ptth_server_gui/src/main.rs @@ -56,14 +56,20 @@ fn main () Some (Message::RunServer) => { let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel (); + let roots = match &config_file_opt { + None => Default::default (), + Some (cf) => match &cf.file_server_roots { + None => Default::default (), + Some (x) => x.clone (), + }, + }; + let config_file = ptth_server::ConfigFile { name: gui.input_name.value ().to_string (), api_key: gui.input_api_key.value ().to_string (), relay_url: gui.input_relay_url.value ().to_string (), file_server_root: PathBuf::from (gui.input_file_server_root.value ()), - file_server_roots: config_file_opt.as_ref () - .map (|cf| cf.file_server_roots.clone ()) - .unwrap_or_else (|| Default::default ()), + file_server_roots: roots, throttle_upload: false, client_keys: Default::default (), allow_any_client: true, @@ -114,7 +120,7 @@ pub struct ConfigFile { pub api_key: String, pub relay_url: Option , pub file_server_root: Option , - pub file_server_roots: BTreeMap , + pub file_server_roots: Option >, } impl Gui {