This is an automated email from the ASF dual-hosted git repository. Xuanwo pushed a commit to branch xuanwo/fix-dav-server-collection-shadowing-and-nonempty-rmdir in repository https://gitbox.apache.org/repos/asf/opendal.git
commit 7ee18723341ddfa7832a6543fc007a744945469d Author: Xuanwo <[email protected]> AuthorDate: Mon Nov 23 15:13:36 2026 +0100 fix(integrations/dav-server): prevent collection shadowing on open and reject non-empty remove_dir OpendalFile::open did not check whether a collection already existed at {path}/ when opening a path without a trailing slash. On memory and object stores that treat file and directory marker keys separately, PUT or PATCH /dir wrote a file key alongside dir/ and shadowed the collection in metadata and PROPFIND, while read opens lazily succeeded. OpendalFs::remove_dir delegated directly to Operator::delete without checking for children. For Depth: 0 DELETE against non-empty collections, object stores could delete only the directory marker while leaving children intact, and memory/fs surfaced an Unexpected error as HTTP 500 instead of FsError::Exists (HTTP 405). Check for explicit and implicit collections before opening files in OpendalFile::open, and check for child entries, file targets, and root removal in OpendalFs::remove_dir while preserving idempotent removal for implicit object-store directories emptied during recursive DELETE. Add regression tests over both memory and fs backends. --- integrations/dav-server/src/file.rs | 84 +++++- integrations/dav-server/src/fs.rs | 44 ++- integrations/dav-server/tests/test.rs | 520 +++++++++++++++++++++++++++++++++- 3 files changed, 644 insertions(+), 4 deletions(-) diff --git a/integrations/dav-server/src/file.rs b/integrations/dav-server/src/file.rs index ef1877a4c0..81a0e38f40 100644 --- a/integrations/dav-server/src/file.rs +++ b/integrations/dav-server/src/file.rs @@ -23,7 +23,7 @@ use dav_server::fs::{DavFile, OpenOptions}; use dav_server::fs::{DavMetaData, FsResult}; use dav_server::fs::{FsError, FsFuture}; use futures::FutureExt; -use futures::{AsyncReadExt, AsyncSeekExt}; +use futures::{AsyncReadExt, AsyncSeekExt, StreamExt}; use opendal_core::{ErrorKind, FuturesAsyncReader, Operator, Writer}; use super::metadata::OpendalMetaData; @@ -68,9 +68,78 @@ enum State { } impl OpendalFile { + /// Return whether `dir_path` (ending with `/`) exists as an explicit + /// directory marker or an implicit prefix with children. + async fn dir_exists(op: &Operator, dir_path: &str) -> FsResult<bool> { + let capability = op.info().capability(); + if capability.stat { + match op.stat(dir_path).await { + Ok(meta) => { + if meta.is_dir() { + return Ok(true); + } + } + Err(err) if err.kind() == ErrorKind::NotADirectory => return Ok(false), + Err(err) if err.kind() == ErrorKind::NotFound => {} + Err(err) => return Err(convert_error(err)), + } + } + + if capability.list { + let mut lister = match op.lister_with(dir_path).limit(1).await { + Ok(lister) => lister, + Err(err) + if matches!(err.kind(), ErrorKind::NotFound | ErrorKind::NotADirectory) => + { + return Ok(false); + } + Err(err) => return Err(convert_error(err)), + }; + match lister.next().await { + Some(Ok(_)) => return Ok(true), + Some(Err(err)) + if matches!(err.kind(), ErrorKind::NotFound | ErrorKind::NotADirectory) => + { + return Ok(false); + } + Some(Err(err)) => return Err(convert_error(err)), + None => return Ok(false), + } + } + + Ok(false) + } + /// Create a new opendal file. pub async fn open(op: Operator, path: String, options: OpenOptions) -> FsResult<Self> { + if path.is_empty() || path.ends_with('/') { + return Err(FsError::Forbidden); + } + let state = if options.read { + if op.info().capability().stat { + match op.stat(&path).await { + Ok(meta) if meta.is_dir() => return Err(FsError::Forbidden), + Ok(_) => {} + Err(err) if err.kind() == ErrorKind::IsADirectory => { + return Err(FsError::Forbidden); + } + Err(err) if err.kind() == ErrorKind::NotFound => { + let dir_path = format!("{path}/"); + if Self::dir_exists(&op, &dir_path).await? { + return Err(FsError::Forbidden); + } + return Err(FsError::NotFound); + } + Err(err) => return Err(convert_error(err)), + } + } else { + let dir_path = format!("{path}/"); + if Self::dir_exists(&op, &dir_path).await? { + return Err(FsError::Forbidden); + } + } + let r = op .reader(&path) .await @@ -80,6 +149,19 @@ impl OpendalFile { .map_err(convert_error)?; State::Read(Box::new(r)) } else if options.write { + // Reject opening a path as a file when a collection already exists + // at `{path}/`. Object stores and memory treat `dir` and `dir/` as + // separate keys, so writing `dir` would otherwise shadow the + // collection in `metadata` and `PROPFIND`. + let dir_path = format!("{path}/"); + if Self::dir_exists(&op, &dir_path).await? { + return Err(if options.create_new { + FsError::Exists + } else { + FsError::Forbidden + }); + } + let mut w = op .writer_with(&path) .append(options.append) diff --git a/integrations/dav-server/src/fs.rs b/integrations/dav-server/src/fs.rs index 551ce23507..b27017c64d 100644 --- a/integrations/dav-server/src/fs.rs +++ b/integrations/dav-server/src/fs.rs @@ -222,8 +222,48 @@ impl DavFileSystem for OpendalFs { fn remove_dir<'a>(&'a self, path: &'a DavPath) -> FsFuture<'a, ()> { async move { - let path = self.fs_dir_path(path)?; - self.op.delete(&path).await.map_err(convert_error) + let dir_path = self.fs_dir_path(path)?; + + // Removing the root collection is never allowed. + if dir_path == "/" { + return Err(FsError::Forbidden); + } + + let file_path = dir_path.trim_end_matches('/'); + + if self.op.info().capability().list { + let mut lister = self + .op + .lister_with(dir_path.as_str()) + .limit(2) + .await + .map_err(convert_error)?; + let mut found = false; + while let Some(entry) = lister.next().await { + let entry = entry.map_err(convert_error)?; + if entry.path() == dir_path { + found = true; + } else { + return Err(FsError::Exists); + } + } + if !found { + // Some services do not yield the directory itself when + // listing an empty directory, while an implicit + // object-store directory disappears as soon as its last + // child is removed during recursive DELETE. Refuse only + // when the path actually names a file, then fall through + // to the idempotent delete of `dir_path`. + if self.op.info().capability().stat + && !self.path_exists(&dir_path).await? + && self.path_exists(file_path).await? + { + return Err(FsError::Forbidden); + } + } + } + + self.op.delete(&dir_path).await.map_err(convert_error) } .boxed() } diff --git a/integrations/dav-server/tests/test.rs b/integrations/dav-server/tests/test.rs index 1781dea90e..2393b66124 100644 --- a/integrations/dav-server/tests/test.rs +++ b/integrations/dav-server/tests/test.rs @@ -24,7 +24,8 @@ use dav_server_opendalfs::dav_server::davpath::DavPath; use dav_server_opendalfs::dav_server::fs::{DavFile, FsError, OpenOptions}; use dav_server_opendalfs::dav_server::fs::{DavFileSystem, ReadDirMeta}; use futures::{StreamExt, TryStreamExt}; -use http::{HeaderValue, Method, Request, Response, StatusCode, header::ETAG}; +use http::{HeaderValue, Method, Request, Response, StatusCode, header, header::ETAG}; +use http_body_util::BodyExt; use opendal::services::{Fs, Http}; use opendal::{HttpBody, HttpTransport, HttpTransporter, Operator}; use opendal_core::raw::{ @@ -1025,3 +1026,520 @@ async fn test_append_seek_and_cursor_accounting() -> Result<()> { assert_eq!(op.read("append.txt").await?.to_bytes().as_ref(), b"abcdef"); Ok(()) } + +async fn dav_request(dav: &DavHandler, req: Request<Body>) -> Result<(StatusCode, Bytes)> { + let resp = dav.handle(req).await; + let status = resp.status(); + let body = BodyExt::collect(resp.into_body()).await?.to_bytes(); + Ok((status, body)) +} + +fn propfind_is_collection(xml: &[u8]) -> Result<bool> { + let text = std::str::from_utf8(xml)?; + let doc = roxmltree::Document::parse(text)?; + Ok(doc + .descendants() + .any(|node| node.tag_name().name() == "collection")) +} + +async fn assert_put_and_patch_on_collection_rejected(op: Operator, label: &str) -> Result<()> { + const SABREDAV_PARTIAL_UPDATE: &str = "application/x-sabredav-partialupdate"; + + let dav = DavHandler::builder() + .filesystem(OpendalFs::new(op.clone())) + .build_handler(); + + let (status, _) = dav_request( + &dav, + Request::builder() + .method("MKCOL") + .uri("/dir/") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::CREATED, "{label}: MKCOL /dir/"); + + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::PUT) + .uri("/dir/child.txt") + .body(Body::from(Bytes::from_static(b"keep")))?, + ) + .await?; + assert_eq!(status, StatusCode::CREATED, "{label}: PUT /dir/child.txt"); + + // PUT on the collection URI (without or with trailing slash) must be rejected + // rather than creating a shadowing file key. + for uri in ["/dir", "/dir/"] { + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::PUT) + .uri(uri) + .body(Body::from(Bytes::from_static(b"shadow")))?, + ) + .await?; + assert_eq!(status, StatusCode::FORBIDDEN, "{label}: PUT {uri}"); + } + + // SabreDAV append PATCH targeting the collection must also be rejected. + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::PATCH) + .uri("/dir") + .header(header::CONTENT_TYPE, SABREDAV_PARTIAL_UPDATE) + .header("X-Update-Range", "append") + .header(header::CONTENT_LENGTH, "6") + .body(Body::from(Bytes::from_static(b"shadow")))?, + ) + .await?; + assert_eq!(status, StatusCode::FORBIDDEN, "{label}: PATCH /dir"); + + // Direct DavFileSystem::open must reject read, write, append, and create_new + // against an existing collection. + let webdavfs = OpendalFs::new(op.clone()); + let dir_path = DavPath::new("/dir")?; + assert!( + matches!( + webdavfs + .open( + &dir_path, + OpenOptions { + read: true, + ..OpenOptions::default() + }, + ) + .await, + Err(FsError::Forbidden) + ), + "{label}: open(read) /dir" + ); + assert!( + matches!( + webdavfs + .open( + &dir_path, + OpenOptions { + write: true, + ..OpenOptions::default() + }, + ) + .await, + Err(FsError::Forbidden) + ), + "{label}: open(write) /dir" + ); + assert!( + matches!( + webdavfs + .open( + &dir_path, + OpenOptions { + write: true, + append: true, + ..OpenOptions::default() + }, + ) + .await, + Err(FsError::Forbidden) + ), + "{label}: open(append) /dir" + ); + assert!( + matches!( + webdavfs + .open( + &dir_path, + OpenOptions { + write: true, + create_new: true, + ..OpenOptions::default() + }, + ) + .await, + Err(FsError::Exists) + ), + "{label}: open(create_new) /dir" + ); + + // No shadowing file key was created, PROPFIND still reports a collection, + // and the existing child content is intact. + assert!( + !matches!(op.stat("dir").await, Ok(meta) if meta.is_file()), + "{label}: dir must not exist as a file" + ); + let (status, body) = dav_request( + &dav, + Request::builder() + .method("PROPFIND") + .uri("/dir") + .header("Depth", "0") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::MULTI_STATUS, "{label}: PROPFIND /dir"); + assert!( + propfind_is_collection(&body)?, + "{label}: PROPFIND /dir must still report a collection" + ); + + let (status, body) = dav_request( + &dav, + Request::builder() + .method(Method::GET) + .uri("/dir/child.txt") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::OK, "{label}: GET /dir/child.txt"); + assert_eq!(body.as_ref(), b"keep", "{label}: child content"); + Ok(()) +} + +#[tokio::test] +async fn test_put_and_patch_on_existing_collection_rejected() -> Result<()> { + let memory = Operator::new(opendal::services::Memory::default())?; + assert_put_and_patch_on_collection_rejected(memory, "memory").await?; + + let root = tempfile::tempdir()?; + let fs_op = Operator::new(Fs::default().root(root.path().to_str().unwrap()))?; + assert_put_and_patch_on_collection_rejected(fs_op, "fs").await?; + Ok(()) +} + +#[tokio::test] +async fn test_put_and_open_on_implicit_collection_rejected() -> Result<()> { + const SABREDAV_PARTIAL_UPDATE: &str = "application/x-sabredav-partialupdate"; + + let op = Operator::new(opendal::services::Memory::default())?; + let dav = DavHandler::builder() + .filesystem(OpendalFs::new(op.clone())) + .build_handler(); + + // Create only a nested file without MKCOL /implicit/ to form an implicit prefix. + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::PUT) + .uri("/implicit/child.txt") + .body(Body::from(Bytes::from_static(b"keep")))?, + ) + .await?; + assert_eq!(status, StatusCode::CREATED); + + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::PUT) + .uri("/implicit") + .body(Body::from(Bytes::from_static(b"shadow")))?, + ) + .await?; + assert_eq!(status, StatusCode::FORBIDDEN); + + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::PATCH) + .uri("/implicit") + .header(header::CONTENT_TYPE, SABREDAV_PARTIAL_UPDATE) + .header("X-Update-Range", "append") + .header(header::CONTENT_LENGTH, "6") + .body(Body::from(Bytes::from_static(b"shadow")))?, + ) + .await?; + assert_eq!(status, StatusCode::FORBIDDEN); + + let webdavfs = OpendalFs::new(op.clone()); + assert!(matches!( + webdavfs + .open( + &DavPath::new("/implicit")?, + OpenOptions { + read: true, + ..OpenOptions::default() + }, + ) + .await, + Err(FsError::Forbidden) + )); + + assert!(matches!( + op.stat("implicit").await, + Err(err) if err.kind() == ErrorKind::NotFound + )); + let (status, body) = dav_request( + &dav, + Request::builder() + .method("PROPFIND") + .uri("/implicit") + .header("Depth", "0") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::MULTI_STATUS); + assert!(propfind_is_collection(&body)?); + + let (status, body) = dav_request( + &dav, + Request::builder() + .method(Method::GET) + .uri("/implicit/child.txt") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::OK); + assert_eq!(body.as_ref(), b"keep"); + Ok(()) +} + +async fn assert_delete_depth_zero_and_missing(op: Operator, label: &str) -> Result<()> { + let dav = DavHandler::builder() + .filesystem(OpendalFs::new(op.clone())) + .build_handler(); + + let (status, _) = dav_request( + &dav, + Request::builder() + .method("MKCOL") + .uri("/dir/") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::CREATED, "{label}: MKCOL /dir/"); + + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::PUT) + .uri("/dir/child.txt") + .body(Body::from(Bytes::from_static(b"keep")))?, + ) + .await?; + assert_eq!(status, StatusCode::CREATED, "{label}: PUT /dir/child.txt"); + + // Depth: 0 DELETE on a non-empty collection must fail and keep children intact. + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::DELETE) + .uri("/dir/") + .header("Depth", "0") + .body(Body::empty())?, + ) + .await?; + assert_eq!( + status, + StatusCode::METHOD_NOT_ALLOWED, + "{label}: DELETE /dir/ Depth: 0 on non-empty collection" + ); + + let (status, body) = dav_request( + &dav, + Request::builder() + .method(Method::GET) + .uri("/dir/child.txt") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::OK, "{label}: child survives"); + assert_eq!(body.as_ref(), b"keep"); + + // Direct remove_dir must refuse non-empty collections, files, and the root. + let webdavfs = OpendalFs::new(op.clone()); + assert!( + matches!( + webdavfs.remove_dir(&DavPath::new("/dir/")?).await, + Err(FsError::Exists) + ), + "{label}: remove_dir non-empty" + ); + assert!( + matches!( + webdavfs.remove_dir(&DavPath::new("/dir/child.txt")?).await, + Err(FsError::Forbidden) + ), + "{label}: remove_dir on file" + ); + assert!( + matches!( + webdavfs.remove_dir(&DavPath::new("/")?).await, + Err(FsError::Forbidden) + ), + "{label}: remove_dir on root" + ); + + // After removing the child, Depth: 0 DELETE removes the now-empty collection. + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::DELETE) + .uri("/dir/child.txt") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::NO_CONTENT, "{label}: DELETE child"); + + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::DELETE) + .uri("/dir/") + .header("Depth", "0") + .body(Body::empty())?, + ) + .await?; + assert_eq!( + status, + StatusCode::NO_CONTENT, + "{label}: DELETE empty /dir/ Depth: 0" + ); + + // Deleting a missing collection returns 404 for both Depth: 0 and default Depth. + for depth in [Some("0"), None] { + let mut req = Request::builder().method(Method::DELETE).uri("/missing/"); + if let Some(depth) = depth { + req = req.header("Depth", depth); + } + let (status, _) = dav_request(&dav, req.body(Body::empty())?).await?; + assert_eq!( + status, + StatusCode::NOT_FOUND, + "{label}: DELETE /missing/ depth={depth:?}" + ); + } + + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::DELETE) + .uri("/dir/") + .header("Depth", "0") + .body(Body::empty())?, + ) + .await?; + assert_eq!( + status, + StatusCode::NOT_FOUND, + "{label}: DELETE already-removed /dir/" + ); + Ok(()) +} + +#[tokio::test] +async fn test_delete_collection_depth_zero_and_missing() -> Result<()> { + let memory = Operator::new(opendal::services::Memory::default())?; + assert_delete_depth_zero_and_missing(memory, "memory").await?; + + let root = tempfile::tempdir()?; + let fs_op = Operator::new(Fs::default().root(root.path().to_str().unwrap()))?; + assert_delete_depth_zero_and_missing(fs_op, "fs").await?; + Ok(()) +} + +#[tokio::test] +async fn test_delete_implicit_collection_depth_zero_and_infinity() -> Result<()> { + let op = Operator::new(opendal::services::Memory::default())?; + let dav = DavHandler::builder() + .filesystem(OpendalFs::new(op)) + .build_handler(); + + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::PUT) + .uri("/implicit/child.txt") + .body(Body::from(Bytes::from_static(b"keep")))?, + ) + .await?; + assert_eq!(status, StatusCode::CREATED); + + // Depth: 0 must refuse an implicit non-empty collection. + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::DELETE) + .uri("/implicit/") + .header("Depth", "0") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::METHOD_NOT_ALLOWED); + + let (status, body) = dav_request( + &dav, + Request::builder() + .method(Method::GET) + .uri("/implicit/child.txt") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::OK); + assert_eq!(body.as_ref(), b"keep"); + + // Default Depth: infinity still deletes an implicit collection cleanly. + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::DELETE) + .uri("/implicit/") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::NO_CONTENT); + + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::GET) + .uri("/implicit/child.txt") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::NOT_FOUND); + + // Recursive DELETE of an explicit collection containing an implicit subdir + // must also succeed cleanly. + let (status, _) = dav_request( + &dav, + Request::builder() + .method("MKCOL") + .uri("/outer/") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::CREATED); + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::PUT) + .uri("/outer/sub/child.txt") + .body(Body::from(Bytes::from_static(b"deep")))?, + ) + .await?; + assert_eq!(status, StatusCode::CREATED); + + let (status, _) = dav_request( + &dav, + Request::builder() + .method(Method::DELETE) + .uri("/outer/") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::NO_CONTENT); + + let (status, _) = dav_request( + &dav, + Request::builder() + .method("PROPFIND") + .uri("/outer/") + .header("Depth", "0") + .body(Body::empty())?, + ) + .await?; + assert_eq!(status, StatusCode::NOT_FOUND); + Ok(()) +}
