You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by er...@apache.org on 2026/06/09 02:53:24 UTC
(opendal) branch fix/confine-compfs-monoiofs-root created (now 7f41d5296)
This is an automated email from the ASF dual-hosted git repository.
erickguan pushed a change to branch fix/confine-compfs-monoiofs-root
in repository https://gitbox.apache.org/repos/asf/opendal.git
at 7f41d5296 Use confined_join and root_join across services
This branch includes the following new commits:
new 7f41d5296 Use confined_join and root_join across services
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(opendal) 01/01: Use confined_join and root_join across services
Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
erickguan pushed a commit to branch fix/confine-compfs-monoiofs-root
in repository https://gitbox.apache.org/repos/asf/opendal.git
commit 7f41d52969e9d7694cda48e9586c9a631a86fb2a
Author: Erick Guan <29...@users.noreply.github.com>
AuthorDate: Tue Jun 9 10:52:58 2026 +0800
Use confined_join and root_join across services
---
core/core/src/raw/path.rs | 28 ++++++++++++++++++++++
core/services/compfs/src/backend.rs | 18 +++++++-------
core/services/compfs/src/core.rs | 43 +++++++++------------------------
core/services/fs/src/core.rs | 29 ++--------------------
core/services/monoiofs/src/backend.rs | 10 ++++----
core/services/monoiofs/src/core.rs | 45 ++++++++++-------------------------
6 files changed, 67 insertions(+), 106 deletions(-)
diff --git a/core/core/src/raw/path.rs b/core/core/src/raw/path.rs
index 55733f47c..4cf467393 100644
--- a/core/core/src/raw/path.rs
+++ b/core/core/src/raw/path.rs
@@ -17,6 +17,7 @@
use crate::*;
use std::hash::{BuildHasher, Hasher};
+use std::path::{Component, Path, PathBuf};
/// build_abs_path will build an absolute path with root.
///
@@ -147,6 +148,33 @@ pub fn normalize_root(v: &str) -> String {
v
}
+/// Join a caller-supplied key onto a base directory while keeping the result
+/// confined to that base.
+///
+/// Services must confine opertaions within configured root. Services could use
+/// this function to confine paths within configured root.
+///
+/// ## Explanation
+///
+/// `normalize_path` strips leading `/` and empty segments but
+/// intentionally does NOT resolve `.`/`..`, and `PathBuf::join` is purely
+/// lexical, so a key such as `../../etc/passwd` would otherwise escape the
+/// configured `root` at syscall time.
+pub fn confined_join(base: &Path, path: &str) -> Result<PathBuf> {
+ let trimmed = path.trim_end_matches('/');
+ if Path::new(trimmed)
+ .components()
+ .any(|c| matches!(c, Component::ParentDir))
+ {
+ return Err(Error::new(
+ ErrorKind::NotFound,
+ "path escapes the configured root via `..`",
+ )
+ .with_context("path", path));
+ }
+ Ok(base.join(trimmed))
+}
+
/// Get basename from path.
pub fn get_basename(path: &str) -> &str {
// Handle root case
diff --git a/core/services/compfs/src/backend.rs b/core/services/compfs/src/backend.rs
index 6b75fb249..6049c8992 100644
--- a/core/services/compfs/src/backend.rs
+++ b/core/services/compfs/src/backend.rs
@@ -176,7 +176,7 @@ impl Access for CompfsBackend {
}
async fn create_dir(&self, path: &str, _: OpCreateDir) -> Result<RpCreateDir> {
- let path = self.core.prepare_path(path)?;
+ let path = self.core.root_join(path)?;
self.core
.exec(move || async move { compio::fs::create_dir_all(path).await })
@@ -186,7 +186,7 @@ impl Access for CompfsBackend {
}
async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
- let path = self.core.prepare_path(path)?;
+ let path = self.core.root_join(path)?;
let meta = self
.core
.exec(move || async move { compio::fs::metadata(path).await })
@@ -220,8 +220,8 @@ impl Access for CompfsBackend {
_: OpCopy,
_opts: OpCopier,
) -> Result<(RpCopy, Self::Copier)> {
- let from = self.core.prepare_path(from)?;
- let to = self.core.prepare_path(to)?;
+ let from = self.core.root_join(from)?;
+ let to = self.core.root_join(to)?;
self.core
.exec(move || async move {
@@ -247,8 +247,8 @@ impl Access for CompfsBackend {
}
async fn rename(&self, from: &str, to: &str, _: OpRename) -> Result<RpRename> {
- let from = self.core.prepare_path(from)?;
- let to = self.core.prepare_path(to)?;
+ let from = self.core.root_join(from)?;
+ let to = self.core.root_join(to)?;
self.core
.exec(move || async move {
@@ -262,7 +262,7 @@ impl Access for CompfsBackend {
Ok(RpRename::default())
}
async fn read(&self, path: &str, _: OpRead) -> Result<(RpRead, Self::Reader)> {
- let path = self.core.prepare_path(path)?;
+ let path = self.core.root_join(path)?;
let file = self
.core
.exec(move || async move {
@@ -281,7 +281,7 @@ impl Access for CompfsBackend {
}
async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
- let path = self.core.prepare_path(path)?;
+ let path = self.core.root_join(path)?;
let append = args.append();
let file = self
.core
@@ -309,7 +309,7 @@ impl Access for CompfsBackend {
}
async fn list(&self, path: &str, _: OpList) -> Result<(RpList, Self::Lister)> {
- let path = self.core.prepare_path(path)?;
+ let path = self.core.root_join(path)?;
let read_dir = match self
.core
diff --git a/core/services/compfs/src/core.rs b/core/services/compfs/src/core.rs
index 05997c574..9606b4b70 100644
--- a/core/services/compfs/src/core.rs
+++ b/core/services/compfs/src/core.rs
@@ -64,31 +64,10 @@ pub(super) struct CompfsCore {
}
impl CompfsCore {
- /// Join a caller-supplied key onto `self.root` while keeping the result
- /// confined to that root.
- ///
- /// `normalize_path` (opendal-core) strips leading `/` and empty segments but
- /// intentionally does NOT resolve `.`/`..`, and `PathBuf::join` is purely
- /// lexical, so a key such as `../../etc/passwd` would otherwise escape the
- /// configured `root` at syscall time. compfs is a local (compio-backed)
- /// filesystem, so the host kernel resolves `..` when the path is used; we
- /// reject any key whose components include a `..` (parent-dir) traversal.
- ///
- /// This mirrors the confinement added to the `fs` backend in #7684.
- pub fn prepare_path(&self, path: &str) -> Result<PathBuf> {
- use std::path::Component;
- let trimmed = path.trim_end_matches('/');
- if Path::new(trimmed)
- .components()
- .any(|c| matches!(c, Component::ParentDir))
- {
- return Err(Error::new(
- ErrorKind::NotFound,
- "path escapes the configured root via `..`",
- )
- .with_context("path", path));
- }
- Ok(self.root.join(trimmed))
+ /// Join a caller-supplied key onto `self.root`, rejecting `..` traversal.
+ #[inline]
+ pub fn root_join(&self, path: &str) -> Result<PathBuf> {
+ confined_join(&self.root, path)
}
pub async fn exec<Fn, Fut, R>(&self, f: Fn) -> opendal_core::Result<R>
@@ -171,10 +150,10 @@ mod tests {
}
#[test]
- fn test_prepare_path_rejects_parent_dir() {
+ fn test_root_join_rejects_parent_dir() {
let core = new_test_core();
for key in ["../etc/passwd", "../../etc/passwd", "a/../../b", "a/.."] {
- let err = core.prepare_path(key).unwrap_err();
+ let err = core.root_join(key).unwrap_err();
assert_eq!(
err.kind(),
ErrorKind::NotFound,
@@ -184,24 +163,24 @@ mod tests {
}
#[test]
- fn test_prepare_path_allows_normal_keys() {
+ fn test_root_join_allows_normal_keys() {
let core = new_test_core();
// Normal keys, `.` (CurDir), and trailing slashes resolve unchanged.
assert_eq!(
- core.prepare_path("a/b.txt").unwrap(),
+ core.root_join("a/b.txt").unwrap(),
PathBuf::from("/data/root/a/b.txt")
);
assert_eq!(
- core.prepare_path("a/b/").unwrap(),
+ core.root_join("a/b/").unwrap(),
PathBuf::from("/data/root/a/b")
);
assert_eq!(
- core.prepare_path("./a/b").unwrap(),
+ core.root_join("./a/b").unwrap(),
PathBuf::from("/data/root/a/b")
);
// A key containing `..` only as a substring of a name is not a traversal.
assert_eq!(
- core.prepare_path("a..b").unwrap(),
+ core.root_join("a..b").unwrap(),
PathBuf::from("/data/root/a..b")
);
}
diff --git a/core/services/fs/src/core.rs b/core/services/fs/src/core.rs
index 388b82def..e447852c4 100644
--- a/core/services/fs/src/core.rs
+++ b/core/services/fs/src/core.rs
@@ -34,40 +34,15 @@ pub struct FsCore {
}
impl FsCore {
- /// Join a caller-supplied key onto a base directory while keeping the result
- /// confined to that base.
- ///
- /// `normalize_path` (opendal-core) strips leading `/` and empty segments but
- /// intentionally does NOT resolve `.`/`..`, and `PathBuf::join` is purely
- /// lexical, so a key such as `../../etc/passwd` would otherwise escape the
- /// configured `root` at syscall time. The fs backend documents that "all
- /// operations will happen under this root", so we reject any key whose
- /// components include a `..` (parent-dir) traversal.
- pub fn confined_join(base: &Path, path: &str) -> Result<PathBuf> {
- use std::path::Component;
- let trimmed = path.trim_end_matches('/');
- if Path::new(trimmed)
- .components()
- .any(|c| matches!(c, Component::ParentDir))
- {
- return Err(Error::new(
- ErrorKind::NotFound,
- "path escapes the configured root via `..`",
- )
- .with_context("path", path));
- }
- Ok(base.join(trimmed))
- }
-
/// Join a caller-supplied key onto `self.root`, rejecting `..` traversal.
#[inline]
pub fn root_join(&self, path: &str) -> Result<PathBuf> {
- Self::confined_join(&self.root, path)
+ confined_join(&self.root, path)
}
// Build write path and ensure the parent dirs created
pub async fn ensure_write_abs_path(&self, parent: &Path, path: &str) -> Result<PathBuf> {
- let p = Self::confined_join(parent, path)?;
+ let p = confined_join(parent, path)?;
// Create dir before write path.
//
diff --git a/core/services/monoiofs/src/backend.rs b/core/services/monoiofs/src/backend.rs
index 5fa16a971..b4eb68391 100644
--- a/core/services/monoiofs/src/backend.rs
+++ b/core/services/monoiofs/src/backend.rs
@@ -104,7 +104,7 @@ impl Access for MonoiofsBackend {
}
async fn stat(&self, path: &str, _args: OpStat) -> Result<RpStat> {
- let path = self.core.prepare_path(path)?;
+ let path = self.core.root_join(path)?;
let meta = self
.core
.dispatch(move || monoio::fs::metadata(path))
@@ -125,7 +125,7 @@ impl Access for MonoiofsBackend {
Ok(RpStat::new(m))
}
async fn read(&self, path: &str, _args: OpRead) -> Result<(RpRead, Self::Reader)> {
- let path = self.core.prepare_path(path)?;
+ let path = self.core.root_join(path)?;
let reader = MonoiofsReader::new(self.core.clone(), path).await?;
Ok((RpRead::default(), oio::PositionReader::new(reader)))
}
@@ -144,7 +144,7 @@ impl Access for MonoiofsBackend {
}
async fn rename(&self, from: &str, to: &str, _args: OpRename) -> Result<RpRename> {
- let from = self.core.prepare_path(from)?;
+ let from = self.core.root_join(from)?;
// ensure file exists
self.core
.dispatch({
@@ -162,7 +162,7 @@ impl Access for MonoiofsBackend {
}
async fn create_dir(&self, path: &str, _args: OpCreateDir) -> Result<RpCreateDir> {
- let path = self.core.prepare_path(path)?;
+ let path = self.core.root_join(path)?;
self.core
.dispatch(move || monoio::fs::create_dir_all(path))
.await
@@ -177,7 +177,7 @@ impl Access for MonoiofsBackend {
_args: OpCopy,
_opts: OpCopier,
) -> Result<(RpCopy, Self::Copier)> {
- let from = self.core.prepare_path(from)?;
+ let from = self.core.root_join(from)?;
// ensure file exists
self.core
.dispatch({
diff --git a/core/services/monoiofs/src/core.rs b/core/services/monoiofs/src/core.rs
index a74cec336..fb7de1dfd 100644
--- a/core/services/monoiofs/src/core.rs
+++ b/core/services/monoiofs/src/core.rs
@@ -96,36 +96,15 @@ impl MonoiofsCore {
}
}
- /// Join a caller-supplied key onto `self.root` while keeping the result
- /// confined to that root.
- ///
- /// `normalize_path` (opendal-core) strips leading `/` and empty segments but
- /// intentionally does NOT resolve `.`/`..`, and `PathBuf::join` is purely
- /// lexical, so a key such as `../../etc/passwd` would otherwise escape the
- /// configured `root` at syscall time. monoiofs is a local (monoio/io_uring)
- /// filesystem, so the host kernel resolves `..` when the path is used; we
- /// reject any key whose components include a `..` (parent-dir) traversal.
- ///
- /// This mirrors the confinement added to the `fs` backend in #7684.
- pub fn prepare_path(&self, path: &str) -> Result<PathBuf> {
- use std::path::Component;
- let trimmed = path.trim_end_matches('/');
- if Path::new(trimmed)
- .components()
- .any(|c| matches!(c, Component::ParentDir))
- {
- return Err(Error::new(
- ErrorKind::NotFound,
- "path escapes the configured root via `..`",
- )
- .with_context("path", path));
- }
- Ok(self.root.join(trimmed))
+ /// Join a caller-supplied key onto `self.root`, rejecting `..` traversal.
+ #[inline]
+ pub fn root_join(&self, path: &str) -> Result<PathBuf> {
+ confined_join(&self.root, path)
}
/// join root and path, create parent dirs
pub async fn prepare_write_path(&self, path: &str) -> Result<PathBuf> {
- let path = self.prepare_path(path)?;
+ let path = self.root_join(path)?;
let parent = path
.parent()
.ok_or_else(|| {
@@ -334,10 +313,10 @@ mod tests {
}
#[tokio::test]
- async fn test_prepare_path_rejects_parent_dir() {
+ async fn test_root_join_rejects_parent_dir() {
let core = Arc::new(MonoiofsCore::new(PathBuf::from("/data/root"), 1, 1024));
for key in ["../etc/passwd", "../../etc/passwd", "a/../../b", "a/.."] {
- let err = core.prepare_path(key).unwrap_err();
+ let err = core.root_join(key).unwrap_err();
assert_eq!(
err.kind(),
ErrorKind::NotFound,
@@ -347,24 +326,24 @@ mod tests {
}
#[tokio::test]
- async fn test_prepare_path_allows_normal_keys() {
+ async fn test_root_join_allows_normal_keys() {
let core = Arc::new(MonoiofsCore::new(PathBuf::from("/data/root"), 1, 1024));
// Normal keys, `.` (CurDir), and trailing slashes resolve unchanged.
assert_eq!(
- core.prepare_path("a/b.txt").unwrap(),
+ core.root_join("a/b.txt").unwrap(),
PathBuf::from("/data/root/a/b.txt")
);
assert_eq!(
- core.prepare_path("a/b/").unwrap(),
+ core.root_join("a/b/").unwrap(),
PathBuf::from("/data/root/a/b")
);
assert_eq!(
- core.prepare_path("./a/b").unwrap(),
+ core.root_join("./a/b").unwrap(),
PathBuf::from("/data/root/a/b")
);
// A key containing `..` only as a substring of a name is not a traversal.
assert_eq!(
- core.prepare_path("a..b").unwrap(),
+ core.root_join("a..b").unwrap(),
PathBuf::from("/data/root/a..b")
);
}