Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: VFS should not walk circular symlinks #17093

Merged
merged 1 commit into from
Apr 18, 2024
Merged
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
28 changes: 27 additions & 1 deletion crates/vfs-notify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

#![warn(rust_2018_idioms, unused_lifetimes)]

use std::fs;
use std::{
fs,
path::{Component, Path},
};

use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
Expand Down Expand Up @@ -206,6 +209,11 @@ impl NotifyActor {
return true;
}
let path = entry.path();

if path_is_parent_symlink(path) {
return false;
}

root == path
|| dirs.exclude.iter().chain(&dirs.include).all(|it| it != path)
});
Expand Down Expand Up @@ -258,3 +266,21 @@ fn read(path: &AbsPath) -> Option<Vec<u8>> {
fn log_notify_error<T>(res: notify::Result<T>) -> Option<T> {
res.map_err(|err| tracing::warn!("notify error: {}", err)).ok()
}

/// Is `path` a symlink to a parent directory?
///
/// Including this path is guaranteed to cause an infinite loop. This
/// heuristic is not sufficient to catch all symlink cycles (it's
/// possible to construct cycle using two or more symlinks), but it
/// catches common cases.
fn path_is_parent_symlink(path: &Path) -> bool {
let Ok(destination) = std::fs::read_link(path) else {
return false;
};

// If the symlink is of the form "../..", it's a parent symlink.
let is_relative_parent =
destination.components().all(|c| matches!(c, Component::CurDir | Component::ParentDir));

is_relative_parent || path.starts_with(destination)
}