rust-shell-script / rust_cmd_lib

Common rust command-line macros and utilities, to write shell-script like tasks in a clean, natural and rusty way
https://docs.rs/cmd_lib/
Apache License 2.0
1.05k stars 37 forks source link

Unnecessary character escape occurs on dynamic input #44

Closed TheOnlyArtz closed 2 years ago

TheOnlyArtz commented 2 years ago

OS: Windows (don't know if it's an issue on Linux)

Let's say my application accepts arbitrary commands dynamically via user input I have the next:

        #[cfg(target_os = "windows")]
        let output = run_fun! (
            cmd /c $user_input
        );

The issue lies whenever that user input includes any thing escapable such as double quotes. Let's say a user tries to run the next:

echo "Hello World"

After looking through proc explorer and exploring the source I've noticed that what's being passed to the command line looks as follow:

echo \"Hello World\"

As you can see there is the unnecessary escape of the double quotes, what's more interesting that through debugging the library's code I've found out that it escapes more than that, through this exact process:

Now ... for this specific command it ain't that bad of an issue but where it comes to more sophisticated commands such as:

SCHTASKS /Create /TN "New Task" /SC HOURLY /TR xxx.exe /RL xxx

the issue arises in the parameter of the /TN flag, trying to run this specific command through:

        let input = r#"SCHTASKS /Create /TN "New Task" /SC HOURLY /TR blah.exe /RL HIGHEST"#

        #[cfg(target_os = "windows")]
        let output = run_fun! (
            cmd /c $input
        );

We get an error back because the quotes were escaped, instead of successfully creating the task/running the command at all, what we're getting back is an error:

ERROR: Invalid argument/option - 'Task"'.
Type "SCHTASKS /CREATE /?" for usage.

this happens because the command cmd_lib actually runs looks like: SCHTASKS /Create /TN \"New Task\" /SC HOURLY /TR blah.exe /RL HIGHEST <- Notice the escaped double quotes

Is there anything we can do about it? I've tried to hack into the internals of the library with little to no success in that matter. any help will be appreciated.

EDIT: https://internals.rust-lang.org/t/std-process-on-windows-is-escaping-raw-literals-which-causes-problems-with-chaining-commands/8163/16?u=xpyder

https://users.rust-lang.org/t/std-process-is-escaping-a-raw-string-literal-when-i-dont-want-it-to/19441

Found the above threads, actually an issue which goes back to MS-DOS on windows, nothing you can really do about it :(

rust-shell-script commented 2 years ago

$input will always be treated as one single argument, you need to use vector $[input] if you want to have a list of arguments. See readme and related bug #42.