Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix infinite symlink detection
  • Loading branch information
arendjr committed Jul 28, 2023
commit c6c37ba85f85a175f31566f15782d6497d72261c
71 changes: 56 additions & 15 deletions crates/rome_cli/tests/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,34 +836,75 @@ fn fs_error_dereferenced_symlink() {
}

#[test]
fn fs_error_infinite_symlink_expansion() {
fn fs_error_infinite_symlink_expansion_to_dirs() {
let fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let root_path = temp_dir().join("check_rome_test_infinite_symlink_expansion");
let root_path = temp_dir().join("check_rome_test_infinite_symlink_expansion_to_dirs");
let subdir1_path = root_path.join("prefix");
let subdir2_path = root_path.join("foo").join("bar");

let _ = remove_dir_all(&root_path);
create_dir_all(subdir1_path.clone()).unwrap();
create_dir_all(subdir2_path.clone()).unwrap();
create_dir_all(&subdir1_path).unwrap();
create_dir_all(&subdir2_path).unwrap();

#[cfg(target_family = "unix")]
{
symlink(subdir1_path.clone(), root_path.join("self_symlink1")).unwrap();
symlink(subdir1_path, subdir2_path.join("self_symlink2")).unwrap();
symlink(&subdir2_path, subdir1_path.join("symlink1")).unwrap();
symlink(subdir1_path, subdir2_path.join("symlink2")).unwrap();
}

#[cfg(target_os = "windows")]
{
check_windows_symlink!(symlink_dir(
subdir1_path.clone(),
root_path.join("self_symlink1")
));
check_windows_symlink!(symlink_dir(
subdir1_path,
subdir2_path.join("self_symlink2")
));
check_windows_symlink!(symlink_dir(&subdir2_path, subdir1_path.join("symlink1")));
check_windows_symlink!(symlink_dir(subdir1_path, subdir2_path.join("symlink2")));
}

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem)),
&mut console,
Args::from([("check"), (root_path.display().to_string().as_str())].as_slice()),
);

remove_dir_all(root_path).unwrap();

assert!(result.is_err(), "run_cli returned {result:?}");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"fs_error_infinite_symlink_expansion_to_dirs",
fs,
console,
result,
));
}

#[test]
fn fs_error_infinite_symlink_expansion_to_files() {
let fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let root_path = temp_dir().join("check_rome_test_infinite_symlink_expansion_to_files");
let subdir1_path = root_path.join("prefix");
let subdir2_path = root_path.join("foo").join("bar");

let _ = remove_dir_all(&root_path);
create_dir_all(&subdir1_path).unwrap();
create_dir_all(&subdir2_path).unwrap();

let symlink1_path = subdir1_path.join("symlink1");
let symlink2_path = subdir2_path.join("symlink2");

#[cfg(target_family = "unix")]
{
symlink(&symlink2_path, &symlink1_path).unwrap();
symlink(symlink1_path, symlink2_path).unwrap();
}

#[cfg(target_os = "windows")]
{
check_windows_symlink!(symlink_dir(&symlink2_path, symlink1_path));
check_windows_symlink!(symlink_dir(symlink1_path, symlink2_path));
}

let result = run_cli(
Expand All @@ -878,7 +919,7 @@ fn fs_error_infinite_symlink_expansion() {

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"fs_error_infinite_symlink_expansion",
"fs_error_infinite_symlink_expansion_to_files",
fs,
console,
result,
Expand Down
71 changes: 55 additions & 16 deletions crates/rome_cli/tests/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,36 +834,75 @@ fn fs_error_dereferenced_symlink() {
}

#[test]
fn fs_error_infinite_symlink_expansion() {
fn fs_error_infinite_symlink_expansion_to_dirs() {
let fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let root_path = temp_dir().join("lint_rome_test_infinite_symlink_expansion");
let root_path = temp_dir().join("lint_rome_test_infinite_symlink_expansion_to_dirs");
let subdir1_path = root_path.join("prefix");
let subdir2_path = root_path.join("foo").join("bar");

let _ = remove_dir_all(&root_path);
create_dir(&root_path).unwrap();
create_dir(subdir1_path.clone()).unwrap();
create_dir_all(&subdir1_path).unwrap();
create_dir_all(&subdir2_path).unwrap();

#[cfg(target_family = "unix")]
{
symlink(&subdir2_path, subdir1_path.join("symlink1")).unwrap();
symlink(subdir1_path, subdir2_path.join("symlink2")).unwrap();
}

#[cfg(target_os = "windows")]
{
check_windows_symlink!(symlink_dir(&subdir2_path, subdir1_path.join("symlink1")));
check_windows_symlink!(symlink_dir(subdir1_path, subdir2_path.join("symlink2")));
}

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem)),
&mut console,
Args::from([("lint"), (root_path.display().to_string().as_str())].as_slice()),
);

remove_dir_all(root_path).unwrap();

assert!(result.is_err(), "run_cli returned {result:?}");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"fs_error_infinite_symlink_expansion_to_dirs",
fs,
console,
result,
));
}

#[test]
fn fs_error_infinite_symlink_expansion_to_files() {
let fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let root_path = temp_dir().join("lint_rome_test_infinite_symlink_expansion_to_files");
let subdir1_path = root_path.join("prefix");
let subdir2_path = root_path.join("foo").join("bar");

let _ = remove_dir_all(&root_path);
create_dir_all(&subdir1_path).unwrap();
create_dir_all(&subdir2_path).unwrap();

create_dir_all(subdir2_path.clone()).unwrap();
let symlink1_path = subdir1_path.join("symlink1");
let symlink2_path = subdir2_path.join("symlink2");

#[cfg(target_family = "unix")]
{
symlink(subdir1_path.clone(), root_path.join("self_symlink1")).unwrap();
symlink(subdir1_path, subdir2_path.join("self_symlink2")).unwrap();
symlink(&symlink2_path, &symlink1_path).unwrap();
symlink(symlink1_path, symlink2_path).unwrap();
}

#[cfg(target_os = "windows")]
{
check_windows_symlink!(symlink_dir(
subdir1_path.clone(),
root_path.join("self_symlink1")
));
check_windows_symlink!(symlink_dir(
subdir1_path,
subdir2_path.join("self_symlink2")
));
check_windows_symlink!(symlink_dir(&symlink2_path, symlink1_path));
check_windows_symlink!(symlink_dir(symlink1_path, symlink2_path));
}

let result = run_cli(
Expand All @@ -878,7 +917,7 @@ fn fs_error_infinite_symlink_expansion() {

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"fs_error_infinite_symlink_expansion",
"fs_error_infinite_symlink_expansion_to_files",
fs,
console,
result,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
# Termination Message

```block
internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× No files were processed in the specified paths.



```

# Emitted Messages

```block
Checked 0 file(s) in <TIME>
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
# Termination Message

```block
internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× No files were processed in the specified paths.



```

# Emitted Messages

```block
<TEMP_DIR>/check_rome_test_infinite_symlink_expansion_to_files/foo/bar/symlink2 internalError/fs ━━━━━━━━━━━━━━━━━━━━

! Deeply nested symlink expansion

× Rome encountered a file system entry with too many nested symbolic links, possibly forming an infinite cycle: <TEMP_DIR>/check_rome_test_infinite_symlink_expansion_to_files/foo/bar/symlink2


```

```block
<TEMP_DIR>/check_rome_test_infinite_symlink_expansion_to_files/prefix/symlink1 internalError/fs ━━━━━━━━━━━━━━━━━━━━

! Deeply nested symlink expansion

× Rome encountered a file system entry with too many nested symbolic links, possibly forming an infinite cycle: <TEMP_DIR>/check_rome_test_infinite_symlink_expansion_to_files/prefix/symlink1


```

```block
Checked 0 file(s) in <TIME>
```


This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
# Termination Message

```block
internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× No files were processed in the specified paths.



```

# Emitted Messages

```block
Checked 0 file(s) in <TIME>
```


Loading