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

Quoted command-line argument with space treats trailing backslash as escaping quote #21854

Closed
TrueWill opened this issue Jul 17, 2018 · 16 comments
Labels
cli Issues and PRs related to the Node.js command line interface. windows Issues and PRs related to the Windows platform. wrong repo Issues that should be opened in another repository.

Comments

@TrueWill
Copy link

  • Version: v8.11.3
  • Platform: Windows 10 Pro 64-bit
  • Subsystem:

console.log(process.argv);

The above will show the issue in a console application run from PowerShell.

If:

  • a command-line argument is passed
  • the argument is quoted
  • the argument is a path
  • the path contains a space
  • the path ends in a backslash

then the backslash will escape the final quote, which will be included in the argument value.

Example:
node foo "C:\Program Files\"

[ 'C:\\Program Files\\nodejs\\node.exe',
  'C:\\SourceCode\\foo\\foo.js',
  'C:\\Program Files"' ]

Note that the final argument ends with a double-quote instead of a backslash.

@vsemozhetbyt
Copy link
Contributor

FWIW, shell commands and batch files process such arguments properly:

> echo "C:\Program Files\"
"C:\Program Files\"

test.bat:

echo %1
> test.bat "C:\Program Files\"

> echo "C:\Program Files\"
"C:\Program Files\"

@vsemozhetbyt vsemozhetbyt added the cli Issues and PRs related to the Node.js command line interface. label Jul 17, 2018
@ryzokuken
Copy link
Contributor

/cc @nodejs/platform-windows

@vsemozhetbyt can you confirm that this happens on master?

@ryzokuken
Copy link
Contributor

ryzokuken commented Jul 17, 2018

Cannot reproduce on MacOS.

out/Release/node ~/Code/temp/node/cli.js "/dev/null"
[ '/Users/ryzokuken/Code/nodejs/node/out/Release/node',
  '/Users/ryzokuken/Code/temp/node/cli.js',
  '/dev/null' ]

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Jul 17, 2018

Yes, this happens with 6.14.3, 8.11.3, 10.6.0 and 11.0.0-nightly20180717ab10bfe376.
Tested in Windows 7 x64 on cmd.exe.

@seishun
Copy link
Contributor

seishun commented Jul 17, 2018

Not a Node.js issue. This is how PowerShell and cmd parse arguments. Same behavior in Python.

@seishun seishun closed this as completed Jul 17, 2018
@ryzokuken
Copy link
Contributor

The logic of actually setting the value is fairly trivial and fool-proof:

  // process.argv
  Local<Array> arguments = Array::New(env->isolate(), argc);
  for (int i = 0; i < argc; ++i) {
    arguments->Set(i, String::NewFromUtf8(env->isolate(), argv[i]));
  }
  process->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "argv"), arguments);

Perhaps there's something wrong with the argv value supplied itself?

@ryzokuken
Copy link
Contributor

@seishun thanks for the clarification.

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Jul 17, 2018

Refs: https://ss64.com/nt/syntax-esc.html

Some commands (e.g. REG and FINDSTR) use the standard escape character of \ (as used by C, Python, SQL, bash and many other languages.)
The \ escape can cause problems with quoted directory paths that contain a trailing backslash because the closing quote " at the end of the line will be escaped \".

To save a directory path with a trailing backslash (\) requires adding a second backslash to 'escape the escape'
so for example instead of "C:\My Docs" use "C:\My Docs\\"

@TrueWill
Copy link
Author

@seishun If this is a PowerShell bug, why does this work correctly?

Get-ChildItem "C:\Program Files\"

    Directory: C:\Program Files


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         5/4/2018  11:12 PM                7-Zip
...

@seishun
Copy link
Contributor

seishun commented Jul 17, 2018

Get-ChildItem is a PowerShell intrinsic. Try it with any executable.

@vsemozhetbyt
Copy link
Contributor

The similar issue with FINDSTR:

> cat test.txt

"C:\Program Files\"

> FINDSTR "C:\Program Files\\" test.txt
"C:\Program Files\"

> FINDSTR "C:\Program Files\" test.txt

The last commannd just waits for inputs (processing all the parameters as a one string?)

@TrueWill
Copy link
Author

I have confirmed that @seishun is correct - this is a bug in PowerShell.

foo.c:

#include<stdio.h>
 
int main(int argc, char *argv[])
{
    if (argc >= 2)
        printf("%s", argv[1]);

    return 0;
}

Compiled with Microsoft C/C++ compiler v19.14.26433 for x86 to foo.exe

./foo "C:\Program Files\"

C:\Program Files"

I apologize for the erroneous bug report.

@TrueWill
Copy link
Author

Opened issue with PowerShell: PowerShell/PowerShell#7400

@speller
Copy link

speller commented Jul 11, 2021

I'm having this issue without PowerShell. I'm running the node process by WinAPI CreateProcess function with a command line like node.exe script.js "\some path\" and I'm getting the argv[2] value with the trailing quote. Moreover, if I call node.exe script.js "\some path\" another_arg the argument interpretation becomes broken and the argv[2] value becomes \some path" another_arg. If I run node with a command like node.exe script.js "\some path\\" doubling the training backslash, I get the result I need. But this command becomes nonsense, because either all backslashes should be doubled or none. Doubling the leading backslash brings it as a double slash in node. As far as I remember, the OS doesn't have any involvement in the command line parsing an app. So I assume this is a nodejs bug.

Node v14.17.3

@speller
Copy link

speller commented Jul 11, 2021

As a kind of proof that this is not a Windows bug: running this command in the cmd console works as expected: find "\root\" log.txt (looking for the \root\ string in the log.txt file).

@speller
Copy link

speller commented Jul 12, 2021

Some commands (e.g. REG and FINDSTR) use the standard escape character of \ (as used by C, Python, SQL, bash and many other languages.)

This also clearly states that the issue is with some programs that interpret the command line wrongly, not with a shell or the command line itself. This means, that nodejs just adopted the common bug that existed in some other programs that use the same RTL. Is the RTL bug a good excuse to not fix it in node?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Issues and PRs related to the Node.js command line interface. windows Issues and PRs related to the Windows platform. wrong repo Issues that should be opened in another repository.
Projects
None yet
Development

No branches or pull requests

6 participants