From 7162d4d9aa7b961e5fad5cea6f71f11a6c3fab17 Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee Date: Thu, 19 Oct 2023 02:32:11 +0530 Subject: [PATCH] Escape path that could be a flag (#10721) # Description Files that begin with dashes can be ambiguous when passed to commands like `ls`. For example if there exists a file `--help`, it might be considered a flag if not properly escaped. This PR escapes any file that begins with a dash. # User-Facing Changes Files beginning with dashes will be escaped. # Tests + Formatting Tests are added. --- crates/nu-cli/src/completions/completion_common.rs | 4 +++- crates/nu-cli/tests/completions.rs | 4 ++++ tests/fixtures/quoted_completions/--help | 0 tests/fixtures/quoted_completions/-42 | 0 tests/fixtures/quoted_completions/-inf | 0 tests/fixtures/quoted_completions/4.2 | 0 6 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/quoted_completions/--help create mode 100644 tests/fixtures/quoted_completions/-42 create mode 100644 tests/fixtures/quoted_completions/-inf create mode 100644 tests/fixtures/quoted_completions/4.2 diff --git a/crates/nu-cli/src/completions/completion_common.rs b/crates/nu-cli/src/completions/completion_common.rs index c8ff69246..93629d74b 100644 --- a/crates/nu-cli/src/completions/completion_common.rs +++ b/crates/nu-cli/src/completions/completion_common.rs @@ -152,7 +152,9 @@ pub fn complete_item( pub fn escape_path(path: String, dir: bool) -> String { let filename_contaminated = !dir && path.contains(['\'', '"', ' ', '#', '(', ')']); let dirname_contaminated = dir && path.contains(['\'', '"', ' ', '#']); - if filename_contaminated || dirname_contaminated || path.parse::().is_ok() { + let maybe_flag = path.starts_with('-'); + let maybe_number = path.parse::().is_ok(); + if filename_contaminated || dirname_contaminated || maybe_flag || maybe_number { format!("`{path}`") } else { path diff --git a/crates/nu-cli/tests/completions.rs b/crates/nu-cli/tests/completions.rs index c70c10b16..714709937 100644 --- a/crates/nu-cli/tests/completions.rs +++ b/crates/nu-cli/tests/completions.rs @@ -527,6 +527,10 @@ fn file_completion_quoted() { let suggestions = completer.complete(target_dir, target_dir.len()); let expected_paths: Vec = vec![ + "`--help`".to_string(), + "`-42`".to_string(), + "`-inf`".to_string(), + "`4.2`".to_string(), "`te st.txt`".to_string(), "`te#st.txt`".to_string(), "`te'st.txt`".to_string(), diff --git a/tests/fixtures/quoted_completions/--help b/tests/fixtures/quoted_completions/--help new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/quoted_completions/-42 b/tests/fixtures/quoted_completions/-42 new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/quoted_completions/-inf b/tests/fixtures/quoted_completions/-inf new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/quoted_completions/4.2 b/tests/fixtures/quoted_completions/4.2 new file mode 100644 index 000000000..e69de29bb