diff --git a/actix-files/src/files.rs b/actix-files/src/files.rs index afc852a18..a5e22a6d0 100644 --- a/actix-files/src/files.rs +++ b/actix-files/src/files.rs @@ -96,6 +96,9 @@ impl Files { /// If the mount path is set as the root path `/`, services registered after this one will /// be inaccessible. Register more specific handlers and services first. /// + /// If `serve_from` cannot be canonicalized at startup, an error is logged and the original + /// path is preserved. Requests will return `404 Not Found` until the path exists. + /// /// `Files` utilizes the existing Tokio thread-pool for blocking filesystem operations. /// The number of running threads is adjusted over time as needed, up to a maximum of 512 times /// the number of server [workers](actix_web::HttpServer::workers), by default. @@ -105,7 +108,8 @@ impl Files { Ok(canon_dir) => canon_dir, Err(_) => { log::error!("Specified path is not a directory: {:?}", orig_dir); - PathBuf::new() + // Preserve original path so requests don't fall back to CWD. + orig_dir } }; diff --git a/actix-files/src/lib.rs b/actix-files/src/lib.rs index b0bb76ace..43bda9585 100644 --- a/actix-files/src/lib.rs +++ b/actix-files/src/lib.rs @@ -780,6 +780,16 @@ mod tests { assert_eq!(resp.status(), StatusCode::NOT_FOUND); } + #[actix_rt::test] + async fn test_static_files_bad_directory_does_not_serve_cwd_files() { + let service = Files::new("/", "./missing").new_service(()).await.unwrap(); + + let req = TestRequest::with_uri("/Cargo.toml").to_srv_request(); + let resp = test::call_service(&service, req).await; + + assert_eq!(resp.status(), StatusCode::NOT_FOUND); + } + #[actix_rt::test] async fn test_default_handler_file_missing() { let st = Files::new("/", ".")