Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,11 @@ jobs:
- name: Run host mount mapping tests
run: cargo test -p bashkit --test integration --features realfs -- host_mounts_tests

# The VFS is `/`-separated on every host; host-separator joins only show
# up here (#2425, #2933). Reuses the realfs build from the steps above.
- name: Run VFS separator and device path tests
run: cargo test -p bashkit --test integration --features realfs -- windows_containment dev_fd_path_tests dev_null_tests

- name: Run command resolver tests
run: cargo test -p bashkit --test integration -- command_resolver_tests

Expand Down
3 changes: 3 additions & 0 deletions crates/bashkit/benches/remote_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
//! Run with: `cargo bench --bench remote_fs`
//! Counts only: `cargo bench --bench remote_fs -- --counts`

// Reads the host checkout; the VFS separator ban targets library code.
#![allow(clippy::disallowed_methods)]

use bashkit::testing::{CallCounts, CountingFs};
use bashkit::{Bash, FileSystem, FsLimits};
use criterion::{BenchmarkId, Criterion, criterion_group};
Expand Down
14 changes: 14 additions & 0 deletions crates/bashkit/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Decision: the VFS is POSIX-rooted on every host (TM-ESC-033). `Path::join`,
# `PathBuf::push` and friends join with the host separator, so on Windows they
# turn `/a` + `b` into `/a\b`. That broke `/dev/stderr` redirects (#2933) and
# printed mixed separators (#2425); Linux CI and fuzzing cannot see it because
# the host separator there is already `/`. Build VFS paths with
# `crate::fs::vfs_join` instead. Code that really handles host paths (RealFs,
# host mounts, test temp dirs) opts out with a scoped
# `#[allow(clippy::disallowed_methods)]` and a reason.
disallowed-methods = [
{ path = "std::path::Path::join", reason = "joins with the host separator (`\\` on Windows); use crate::fs::vfs_join for VFS paths" },
{ path = "std::path::PathBuf::push", reason = "joins with the host separator (`\\` on Windows); use crate::fs::vfs_join for VFS paths" },
{ path = "std::path::PathBuf::set_file_name", reason = "joins with the host separator (`\\` on Windows); build VFS paths with crate::fs::vfs_join" },
{ path = "std::path::Path::with_file_name", reason = "joins with the host separator (`\\` on Windows); build VFS paths with crate::fs::vfs_join" },
]
19 changes: 4 additions & 15 deletions crates/bashkit/examples/custom_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,9 @@ impl SimpleStorage {
}

fn normalize(path: &Path) -> PathBuf {
let mut result = PathBuf::from("/");
for component in path.components() {
match component {
std::path::Component::Normal(name) => result.push(name),
std::path::Component::ParentDir => {
result.pop();
}
_ => {}
}
}
if result.as_os_str().is_empty() {
result.push("/");
}
result
// The VFS is `/`-separated on every host. Use bashkit's normalizer, not
// `PathBuf::push`, which joins with `\` on Windows.
bashkit::normalize_path(path)
}
}

Expand Down Expand Up @@ -114,7 +103,7 @@ impl FsBackend for SimpleStorage {
let mut current = PathBuf::from("/");
let mut entries = self.entries.write().unwrap();
for component in path.components().skip(1) {
current.push(component);
current = bashkit::vfs_join(&current, component);
entries
.entry(current.clone())
.or_insert(StorageEntry::Directory);
Expand Down
25 changes: 4 additions & 21 deletions crates/bashkit/examples/custom_filesystem_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,9 @@ impl SessionFileSystemAdapter {
}

fn normalize_path(&self, path: &Path) -> PathBuf {
let mut result = PathBuf::new();
for component in path.components() {
match component {
std::path::Component::RootDir => {
result.push("/");
}
std::path::Component::Normal(name) => {
result.push(name);
}
std::path::Component::ParentDir => {
result.pop();
}
std::path::Component::CurDir => {}
std::path::Component::Prefix(_) => {}
}
}
if result.as_os_str().is_empty() {
result.push("/");
}
result
// The VFS is `/`-separated on every host. Use bashkit's normalizer, not
// `PathBuf::push`, which joins with `\` on Windows.
bashkit::normalize_path(path)
}
}

Expand Down Expand Up @@ -163,7 +146,7 @@ impl FileSystem for SessionFileSystemAdapter {
if recursive {
let mut current = PathBuf::new();
for component in path.components() {
current.push(component);
current = bashkit::vfs_join(&current, component);
let mut files = self.store.files.write().unwrap();
if !files.contains_key(&current) {
files.insert(current.clone(), Vec::new());
Expand Down
3 changes: 3 additions & 0 deletions crates/bashkit/examples/generate_snapshot_fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
//! reads bytes written by an older one. See
//! `knowledge/foundations/snapshot-history.md`.

// Writes host fixture files; the VFS separator ban targets VFS paths.
#![allow(clippy::disallowed_methods)]

use bashkit::{Bash, SnapshotOptions};
use std::path::Path;

Expand Down
3 changes: 3 additions & 0 deletions crates/bashkit/examples/realfs_readonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
//!
//! Run with: cargo run --example realfs_readonly --features realfs

// Host paths for the RealFs mount; the VFS separator ban targets VFS paths.
#![allow(clippy::disallowed_methods)]

use bashkit::Bash;

#[tokio::main]
Expand Down
3 changes: 3 additions & 0 deletions crates/bashkit/examples/realfs_readwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
//!
//! Run with: cargo run --example realfs_readwrite --features realfs

// Host paths for the RealFs mount; the VFS separator ban targets VFS paths.
#![allow(clippy::disallowed_methods)]

use bashkit::Bash;

#[tokio::main]
Expand Down
2 changes: 2 additions & 0 deletions crates/bashkit/examples/terminal_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ impl Recorder {
#[tokio::main]
async fn main() {
let out_path = std::env::args().nth(1).unwrap_or_else(|| {
// Host temp dir, not a VFS path.
#[allow(clippy::disallowed_methods)]
std::env::temp_dir()
.join("terminal-demo.cast")
.display()
Expand Down
12 changes: 6 additions & 6 deletions crates/bashkit/src/builtins/git/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ impl GitClient {
) -> Result<()> {
Self::validate_ref_name(name)?;
let git_dir = vfs_join(repo_path, ".git");
let branch_path = vfs_join(&git_dir, "refs/heads").join(name);
let branch_path = vfs_join(&vfs_join(&git_dir, "refs/heads"), name);

if !fs.exists(&git_dir).await? {
return Err(Error::Internal(format!(
Expand Down Expand Up @@ -1238,7 +1238,7 @@ impl GitClient {
Self::validate_ref_name(target)?;
let git_dir = vfs_join(repo_path, ".git");
let head_path = vfs_join(&git_dir, "HEAD");
let branch_path = vfs_join(&git_dir, "refs/heads").join(target);
let branch_path = vfs_join(&vfs_join(&git_dir, "refs/heads"), target);

if !fs.exists(&git_dir).await? {
return Err(Error::Internal(format!(
Expand Down Expand Up @@ -1511,7 +1511,7 @@ impl GitClient {
let head = String::from_utf8_lossy(&head_content);
if let Some(branch) = head.trim().strip_prefix("ref: refs/heads/") {
Self::validate_ref_name(branch)?;
let ref_path = vfs_join(&git_dir, "refs/heads").join(branch);
let ref_path = vfs_join(&vfs_join(&git_dir, "refs/heads"), branch);
if fs.exists(&ref_path).await? {
let hash = fs.read_file(&ref_path).await?;
// Sanitize hash read from ref file (TM-GIT-015)
Expand All @@ -1536,7 +1536,7 @@ impl GitClient {
arg
))
})?;
let ref_path = vfs_join(&git_dir, "refs/heads").join(arg);
let ref_path = vfs_join(&vfs_join(&git_dir, "refs/heads"), arg);
if fs.exists(&ref_path).await? {
let hash = fs.read_file(&ref_path).await?;
// Sanitize hash read from ref file (TM-GIT-015)
Expand Down Expand Up @@ -1735,7 +1735,7 @@ impl GitClient {
let head = String::from_utf8_lossy(&head_content);
if let Some(branch) = head.trim().strip_prefix("ref: refs/heads/") {
Self::validate_ref_name(branch)?;
let ref_path = vfs_join(git_dir, "refs/heads").join(branch);
let ref_path = vfs_join(&vfs_join(git_dir, "refs/heads"), branch);
if fs.exists(&ref_path).await? {
let hash = fs.read_file(&ref_path).await?;
return Ok(String::from_utf8_lossy(&hash).trim().to_string());
Expand All @@ -1746,7 +1746,7 @@ impl GitClient {

// Try as branch name. Invalid ref names cannot be resolved via the VFS.
if Self::validate_ref_name(refspec).is_ok() {
let ref_path = vfs_join(git_dir, "refs/heads").join(refspec);
let ref_path = vfs_join(&vfs_join(git_dir, "refs/heads"), refspec);
if fs.exists(&ref_path).await? {
let hash = fs.read_file(&ref_path).await?;
return Ok(String::from_utf8_lossy(&hash).trim().to_string());
Expand Down
3 changes: 2 additions & 1 deletion crates/bashkit/src/builtins/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ async fn logical_dir(fs: &dyn crate::fs::FileSystem, path: &std::path::Path) ->
let mut out = PathBuf::from("/");
for comp in path.components() {
match comp {
Component::Normal(name) => out.push(name),
// vfs_join, not `push`: keep `/` separators on Windows (#2933).
Component::Normal(name) => out = crate::fs::vfs_join(&out, name),
Component::ParentDir => {
if !fs.stat(&out).await.is_ok_and(|m| m.file_type.is_dir()) {
return None;
Expand Down
3 changes: 2 additions & 1 deletion crates/bashkit/src/builtins/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ async fn follow_readlink_symlinks(

match component {
Component::RootDir => continue,
Component::Normal(part) => current.push(part),
// vfs_join, not `push`: keep `/` separators on Windows (#2933).
Component::Normal(part) => current = vfs_join(&current, part),
_ => continue,
}

Expand Down
1 change: 1 addition & 0 deletions crates/bashkit/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ pub fn vfs_join(base: &Path, child: impl AsRef<Path>) -> PathBuf {
// the VFS (paths come from shell words and `DirEntry::name`, both `String`),
// so fall back rather than mangle them through a lossy conversion.
let (Some(base), Some(child)) = (base.to_str(), child.to_str()) else {
#[allow(clippy::disallowed_methods)] // non-UTF-8 fallback, see above
return base.join(child);
};
match (base, child) {
Expand Down
3 changes: 2 additions & 1 deletion crates/bashkit/src/fs/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ impl OverlayFs {
let mut current = PathBuf::from("/");
let mut missing = 0u64;
for component in path.components().skip(1) {
current.push(component);
// vfs_join, not `push`: keep `/` separators on Windows (#2933).
current = vfs_join(&current, component);
if !self.upper.exists(&current).await.unwrap_or(false) {
missing = missing.saturating_add(1);
}
Expand Down
25 changes: 23 additions & 2 deletions crates/bashkit/src/fs/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ use std::sync::Arc;

use super::backend::FsBackend;
use super::limits::{FsLimits, FsUsage};
use super::normalize_path;
use super::traits::{DirEntry, FileSystem, FileSystemExt, Metadata, fs_errors};
use super::{normalize_path, vfs_join};
use crate::error::Result;

/// POSIX-compatible filesystem wrapper.
Expand Down Expand Up @@ -191,7 +191,9 @@ impl<B: FsBackend + 'static> FileSystem for PosixFs<B> {
if let Some(parent) = path.parent() {
let mut current = PathBuf::from("/");
for component in parent.components().skip(1) {
current.push(component);
// vfs_join, not `push`: `push` joins with `\` on Windows
// and the backend would see `/a\b` (#2933).
current = vfs_join(&current, component);
if let Ok(meta) = self.backend.stat(&current).await
&& !meta.file_type.is_dir()
{
Expand Down Expand Up @@ -307,6 +309,8 @@ mod tests {
files: Mutex<HashSet<PathBuf>>,
symlinks: Mutex<HashSet<PathBuf>>,
reads: AtomicUsize,
/// Every path `stat` received, as text (`PathBuf` equality would hide `\`).
stats: Mutex<Vec<String>>,
}

impl AppendCreatesFileBackend {
Expand All @@ -318,6 +322,7 @@ mod tests {
files: Mutex::new(files),
symlinks: Mutex::new(HashSet::new()),
reads: AtomicUsize::new(0),
stats: Mutex::new(Vec::new()),
}
}
}
Expand Down Expand Up @@ -354,6 +359,10 @@ mod tests {
}

async fn stat(&self, path: &Path) -> Result<Metadata> {
self.stats
.lock()
.expect("backend lock poisoned")
.push(path.to_string_lossy().into_owned());
if self
.symlinks
.lock()
Expand Down Expand Up @@ -526,4 +535,16 @@ mod tests {
"raw backends may follow symlinks, so PosixFs must not delegate the read"
);
}

/// #2933 family: `mkdir -p` checks each parent on the backend. Those
/// paths must be `/`-joined on every host, or a string-keyed backend on
/// Windows sees `/a\b`. Named for the Windows CI `windows_containment` filter.
#[tokio::test]
async fn windows_containment_mkdir_p_hands_backend_slash_paths() {
let fs = PosixFs::new(AppendCreatesFileBackend::new());
fs.mkdir(Path::new("/a/b/c/d"), true).await.unwrap();
let stats = fs.backend.stats.lock().unwrap().clone();
assert!(stats.iter().any(|p| p == "/a/b/c"), "{stats:?}");
assert!(stats.iter().all(|p| !p.contains('\\')), "{stats:?}");
}
}
3 changes: 3 additions & 0 deletions crates/bashkit/src/fs/realfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// Security: path traversal is prevented by canonicalizing the resolved path or
// the nearest existing ancestor, then checking the root prefix before I/O.
// This module is only available with the `realfs` feature flag.
// Host paths are built with `Path::join` on purpose: below `resolve` these are
// host paths, where the host separator is correct (see crates/bashkit/clippy.toml).
#![allow(clippy::disallowed_methods)]

//! Real filesystem backend.
//!
Expand Down
Loading
Loading