diff options
author | Sven-Hendrik Haase <svenstaro@gmail.com> | 2022-09-20 00:31:13 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-20 00:31:13 +0000 |
commit | 5a68df14385c730d6087a845250d28adab3c3751 (patch) | |
tree | 62789ab21c8c6a41b2865ae4bf69aed3f87ac644 /src/file_upload.rs | |
parent | Add CHANGELOG for plain text READMEs (diff) | |
parent | Merge branch 'svenstaro:master' into restrict-upload-dir (diff) | |
download | miniserve-5a68df14385c730d6087a845250d28adab3c3751.tar.gz miniserve-5a68df14385c730d6087a845250d28adab3c3751.zip |
Merge pull request #858 from jonasdiemer/restrict-upload-dir
Added option restrict-upload-dir
Diffstat (limited to 'src/file_upload.rs')
-rw-r--r-- | src/file_upload.rs | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/file_upload.rs b/src/file_upload.rs index 6643c68..cf214b8 100644 --- a/src/file_upload.rs +++ b/src/file_upload.rs @@ -171,6 +171,17 @@ pub async fn upload_file( ContextualError::IoError("Failed to resolve path served by miniserve".to_string(), e) })?; + // Disallow paths outside of allowed directories + let upload_allowed = conf.allowed_upload_dir.is_empty() + || conf + .allowed_upload_dir + .iter() + .any(|s| upload_path.starts_with(s)); + + if !upload_allowed { + return Err(ContextualError::UploadForbiddenError); + } + // Disallow the target path to go outside of the served directory // The target directory shouldn't be canonicalized when it gets passed to // handle_multipart so that it can check for symlinks if needed @@ -207,7 +218,7 @@ pub async fn upload_file( /// and optionally prevent traversing hidden directories. /// /// See the unit tests tests::test_sanitize_path* for examples -fn sanitize_path(path: &Path, traverse_hidden: bool) -> Option<PathBuf> { +pub fn sanitize_path(path: &Path, traverse_hidden: bool) -> Option<PathBuf> { let mut buf = PathBuf::new(); for comp in path.components() { |