mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
fix(deployment): don't download through symbolic links out of the chosen folder (#2895)
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.
This commit is contained in:
@@ -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/<name of selected>/<rest>`. `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<PathBuf> {
|
||||
let selected = selected.trim_end_matches('/');
|
||||
let name = selected.rsplit('/').next()?;
|
||||
|
||||
@@ -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 |_, _, _| {})
|
||||
|
||||
Reference in New Issue
Block a user