From 646fc394f664b8b1abfd4735968511548336377f Mon Sep 17 00:00:00 2001 From: "jovie :)" <44442031+jovielarue@users.noreply.github.com> Date: Tue, 31 Dec 2024 01:16:14 -0700 Subject: [PATCH] env: don't allow '=' with -u (#7008) * Added error handling to ensure '=' after -u returns 125 as '=' is not allowed * Added tests for disallow = on short options * Added error handling to ensure '=' after -u returns 125 as '=' is not allowed * Added tests for disallow = on short options * Disallow only short unset option from having '=' as its starting character * Remove needless tests and update function name for clarity --- src/uu/env/src/env.rs | 13 +++++++++++++ tests/by-util/test_env.rs | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/uu/env/src/env.rs b/src/uu/env/src/env.rs index 9e2e56d06..ea31c0107 100644 --- a/src/uu/env/src/env.rs +++ b/src/uu/env/src/env.rs @@ -370,6 +370,19 @@ impl EnvAppData { self.had_string_argument = true; } _ => { + let arg_str = arg.to_string_lossy(); + + // Short unset option (-u) is not allowed to contain '=' + if arg_str.contains('=') + && arg_str.starts_with("-u") + && !arg_str.starts_with("--") + { + return Err(USimpleError::new( + 125, + format!("cannot unset '{}': Invalid argument", &arg_str[2..]), + )); + } + all_args.push(arg.clone()); } } diff --git a/tests/by-util/test_env.rs b/tests/by-util/test_env.rs index a1b13e020..2b33f725d 100644 --- a/tests/by-util/test_env.rs +++ b/tests/by-util/test_env.rs @@ -882,6 +882,29 @@ fn test_env_arg_ignore_signal_empty() { .no_stderr() .stdout_contains("hello"); } + +#[test] +fn disallow_equals_sign_on_short_unset_option() { + let ts = TestScenario::new(util_name!()); + + ts.ucmd() + .arg("-u=") + .fails() + .code_is(125) + .stderr_contains("env: cannot unset '=': Invalid argument"); + ts.ucmd() + .arg("-u=A1B2C3") + .fails() + .code_is(125) + .stderr_contains("env: cannot unset '=A1B2C3': Invalid argument"); + ts.ucmd().arg("--split-string=A1B=2C3=").succeeds(); + ts.ucmd() + .arg("--unset=") + .fails() + .code_is(125) + .stderr_contains("env: cannot unset '': Invalid argument"); +} + #[cfg(test)] mod tests_split_iterator {