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

rm: fix issue #6620 (refuse to remove '.' and '..') #6623

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,24 @@ pub fn remove(files: &[&OsStr], options: &Options) -> bool {
fn handle_dir(path: &Path, options: &Options) -> bool {
let mut had_err = false;

let del = std::path::MAIN_SEPARATOR;

if path
.to_str()
.unwrap()
.ends_with(format!("{}.", del).as_str())
|| path
.to_str()
.unwrap()
Comment on lines +331 to +337
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The conversion is done twice. That's not a big deal, but maybe just saving the result in a variable is easy enough to avoid doing the same work twice?
  2. .to_str().unwrap() will cause problems with non-UTF-8 filenames. Please consider using os_str_as_bytes: If it works great (and add a test), if it doesn't (or is too much work or feels out of scope) then disregard this suggestion.
  3. I'm not sure whether this is the right condition. Maybe a trailing path separator ("del") should count, too? Here's a small example that demonstrates the difference:
    $ rm -rf foo && mkdir -p foo/bar/baz && rm -rf foo/bar/baz/../
    rm: refusing to remove '.' or '..' directory: skipping 'foo/bar/baz/../'
    [$? = 1]
    $ find foo/
    foo/
    foo/bar
    foo/bar/baz
    $ rm -rf foo && mkdir -p foo/bar/baz && cargo run -q --features rm rm -rf foo/bar/baz/../
    rm: cannot remove 'foo/bar/baz/../': No such file or directory (os error 2)
    [$? = 1]
    $ find foo
    foo
    foo/bar
    
    As you can see, we still delete a directory, causing a confusing error message. I'm not sure whether it suffices to add the two conditions ends_with("/./") and ".ends_with("/../")". Can you find any other edge cases?

.ends_with(format!("{}..", del).as_str())
{
show_error!(
"refusing to remove '.' or '..' directory: skipping {}",
path.quote()
);
return true;
}

let is_root = path.has_root() && path.parent().is_none();
if options.recursive && (!is_root || !options.preserve_root) {
if options.interactive != InteractiveMode::Always && !options.verbose {
Expand Down
28 changes: 28 additions & 0 deletions tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,3 +713,31 @@ fn test_non_utf8() {
ucmd.arg(file).succeeds();
assert!(!at.file_exists(file));
}

#[test]
fn test_rm_no_del_parent() {
let (at, mut ucmd) = at_and_ucmd!();

let del = std::path::MAIN_SEPARATOR;

at.mkdir("test");
at.mkdir(&format!("test{del}dir"));
at.touch(&format!("test{del}dir{del}file"));
ucmd.arg("-rf").arg(format!("test{del}dir{del}..")).fails();
assert!(at.dir_exists("test"));
assert!(at.dir_exists(&format!("test{del}dir")));
assert!(at.file_exists(&format!("test{del}dir{del}file")));
}

#[test]
fn test_rm_no_del_cur() {
let (at, mut ucmd) = at_and_ucmd!();

let del = std::path::MAIN_SEPARATOR;

at.mkdir("test");
at.touch(&format!("test{del}file"));
ucmd.arg("-rf").arg(format!("test{del}.")).fails();
assert!(at.dir_exists("test"));
assert!(at.file_exists(&format!("test{del}file")));
}
Loading