From d09156ca8a266957bb1b7835393959d2a192c4e5 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:23:52 +0800 Subject: [PATCH] fix(deployment): don't download through symbolic links out of the chosen folder (#2895) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Download to… checked local paths lexically, so a link already in the chosen folder sent files elsewhere. Folders and files are now written only when their closest existing ancestor resolves inside the folder and the path itself is not a link. --- crates/ide-remote/src/service.rs | 32 +++++++++++++++++++++++++++++++- crates/ide-remote/tests/local.rs | 18 ++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/crates/ide-remote/src/service.rs b/crates/ide-remote/src/service.rs index 87d55e6c8..5234a1a52 100644 --- a/crates/ide-remote/src/service.rs +++ b/crates/ide-remote/src/service.rs @@ -329,8 +329,11 @@ impl RemoteService { Err(e) => report.fail(remote.clone(), e), } } + let root = tokio::fs::canonicalize(dir).await?; for folder in &folders { - if let Err(e) = tokio::fs::create_dir_all(folder).await { + if !stays_inside(&root, folder).await { + report.fail(folder.display().to_string(), outside(folder)); + } else if let Err(e) = tokio::fs::create_dir_all(folder).await { report.fail(folder.display().to_string(), e.into()); } } @@ -339,6 +342,10 @@ impl RemoteService { break; } progress(remote, i, jobs.len()); + if !stays_inside(&root, local).await { + report.fail(remote.clone(), outside(local)); + continue; + } if let Some(parent) = local.parent() && let Err(e) = tokio::fs::create_dir_all(parent).await { @@ -595,6 +602,29 @@ async fn remote_tree( /// Where remote `path`, inside the downloaded `selected` file or folder, lands under local `dir`: /// `dir//`. `None` for a name that would leave `dir`. +/// Whether writing `path` stays in the download folder `root` (canonical): its closest +/// existing ancestor resolves inside it, and `path` itself is not a symbolic link, so a link +/// already in the folder can't send a download elsewhere. +async fn stays_inside(root: &Path, path: &Path) -> bool { + if tokio::fs::symlink_metadata(path).await.is_ok_and(|m| m.file_type().is_symlink()) { + return false; + } + let mut existing = path; + loop { + match tokio::fs::canonicalize(existing).await { + Ok(real) => return real.starts_with(root), + Err(_) => match existing.parent() { + Some(parent) => existing = parent, + None => return false, + }, + } + } +} + +fn outside(path: &Path) -> IdeError { + IdeError::invalid(format!("{} leads outside the download folder through a symbolic link", path.display())) +} + fn download_target(dir: &Path, selected: &str, path: &str) -> Option { let selected = selected.trim_end_matches('/'); let name = selected.rsplit('/').next()?; diff --git a/crates/ide-remote/tests/local.rs b/crates/ide-remote/tests/local.rs index 57b6eac51..d96a486f7 100644 --- a/crates/ide-remote/tests/local.rs +++ b/crates/ide-remote/tests/local.rs @@ -347,6 +347,24 @@ async fn download_into_copies_only_the_selected_items() { assert_eq!(std::fs::read_to_string(project.join("index.php")).unwrap(), "php"); assert!(!project.join("srv").exists() && !project.join("www").exists()); + // A link already in the folder doesn't take the download elsewhere. + #[cfg(unix)] + { + let elsewhere = tmp.path().join("elsewhere"); + std::fs::create_dir_all(&elsewhere).unwrap(); + std::fs::remove_dir_all(project.join("drush/sub")).unwrap(); + std::os::unix::fs::symlink(&elsewhere, project.join("drush/sub")).unwrap(); + std::fs::remove_file(project.join("drush/a.txt")).unwrap(); + std::os::unix::fs::symlink(elsewhere.join("a.txt"), project.join("drush/a.txt")).unwrap(); + let report = service + .download_into(&FsConnection::Local, &remotes[..1], &project, &CancellationToken::new(), &mut |_, _, _| {}) + .await + .unwrap(); + // The linked folder, the file below it, and the linked file. + assert_eq!((report.files, report.errors.len()), (0, 3), "{report:?}"); + assert!(std::fs::read_dir(&elsewhere).unwrap().next().is_none(), "nothing written through the links"); + } + let missing = [server.join("gone").to_str().unwrap().to_owned()]; let report = service .download_into(&FsConnection::Local, &missing, &project, &CancellationToken::new(), &mut |_, _, _| {})