Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

wasi: stat symlinks with trailing slashes correctly in manual resolver (#14387)

Dahale Aditya Dnyaneshwar48fc50b803bf
diff --git a/crates/wasi/src/filesystem/primitives/manually/open.rs b/crates/wasi/src/filesystem/primitives/manually/open.rs
index a145ae19e9..526b9ec010 100644
--- a/crates/wasi/src/filesystem/primitives/manually/open.rs
+++ b/crates/wasi/src/filesystem/primitives/manually/open.rs
@@ -350,6 +350,13 @@ impl<'start> Context<'start> {
Ok(())
}
+ /// Whether a symlink in the last component should be left as-is rather
+ /// than dereferenced. A trailing slash requires a directory, so it
+ /// forces dereferencing even with `FollowSymlinks::No`.
+ fn stops_at_symlink(&self, follow: FollowSymlinks) -> bool {
+ follow == FollowSymlinks::No && !self.trailing_slash
+ }
+
/// Check whether this is the last component and we don't need
/// to dereference; otherwise call `Self::symlink`.
fn maybe_last_component_symlink(
@@ -359,7 +366,7 @@ impl<'start> Context<'start> {
follow: FollowSymlinks,
err: io::Error,
) -> io::Result<()> {
- if follow == FollowSymlinks::No && !self.trailing_slash && self.components.is_empty() {
+ if self.stops_at_symlink(follow) && self.components.is_empty() {
self.canonical_path.push(one);
self.canonical_path.complete();
return Err(err);
@@ -451,9 +458,10 @@ pub(crate) fn stat(start: &fs::File, path: &Path, follow: FollowSymlinks) -> io:
// `stat_unchecked` on it.
let stat = stat_unchecked(&ctx.base, one.as_ref(), FollowSymlinks::No)?;
- // If we weren't asked to follow symlinks, or it wasn't a
+ // If we weren't asked to follow symlinks (and there was no
+ // trailing slash requiring a directory), or it wasn't a
// symlink, we're done.
- if options.follow == FollowSymlinks::No || !stat.file_type().is_symlink() {
+ if ctx.stops_at_symlink(options.follow) || !stat.file_type().is_symlink() {
if stat.is_dir() {
if ctx.dir_precluded {
return Err(errors::is_directory());
@@ -473,7 +481,8 @@ pub(crate) fn stat(start: &fs::File, path: &Path, follow: FollowSymlinks) -> io:
ctx.dir_precluded = true;
}
- // If it was a symlink and we're asked to follow symlinks,
+ // If it was a symlink and we're asked to follow symlinks (or
+ // a trailing slash forces dereferencing to a directory),
// dereference it.
ctx.symlink(&one, &mut symlink_count)?
} else {
diff --git a/crates/wasi/src/filesystem/primitives/tests/fs_additional.rs b/crates/wasi/src/filesystem/primitives/tests/fs_additional.rs
index 85a0e89144..4b3934c1ab 100644
--- a/crates/wasi/src/filesystem/primitives/tests/fs_additional.rs
+++ b/crates/wasi/src/filesystem/primitives/tests/fs_additional.rs
@@ -1380,4 +1380,17 @@ fn trailing_slash_requires_a_directory() {
if !cfg!(windows) {
assert!(p::stat(&start, "file/.".as_ref(), p::FollowSymlinks::No).is_err());
}
+
+ if symlink_supported() {
+ check!(h::symlink_dir(&start, "dir", "sym_dir"));
+ check!(h::symlink_file(&start, "file", "sym_file"));
+
+ let stat_sym = check!(p::stat(&start, "sym_dir".as_ref(), p::FollowSymlinks::No));
+ assert!(stat_sym.file_type().is_symlink());
+
+ let stat_sym_dir = check!(p::stat(&start, "sym_dir/".as_ref(), p::FollowSymlinks::No));
+ assert!(stat_sym_dir.file_type().is_dir());
+
+ assert!(p::stat(&start, "sym_file/".as_ref(), p::FollowSymlinks::No).is_err());
+ }
}