[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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

Fixes bug: unable to spawn .bat with %<environment variable>% in name #51

Closed

Conversation

cspotcode
Copy link

Spawning cmd /D /S /C <command string> is susceptible to environment variable expansion. I've added a failing test that illustrates the problem.

The only solution I've devised is to pass the command to be executed as an environment variable, not after the /c flag. Here's an example:

child_process.spawnSync('cmd', [
  '/d /s /c set secret_env_variable_with_cmd_string=& %secret_env_variable_with_cmd_string%'
], {
  stdio: 'inherit',
  windowsVerbatimArguments: true,
  env: {
    secret_env_variable_with_cmd_string: "%CD%.bat firstArgument secondArgument"
  }
});

cspotcode added a commit to cspotcode/node-cross-spawn that referenced this pull request Dec 11, 2016
…able name>%

Passes command string to cmd.exe as an environment variable
cmd.exe unsets the environment variable and then runs the command
cross-spawn avoids environment variable naming conflicts
cspotcode added a commit to cspotcode/node-cross-spawn that referenced this pull request Dec 11, 2016
…able name>%

Passes command string to cmd.exe as an environment variable
cmd.exe unsets the environment variable and then runs the command
cross-spawn avoids environment variable naming conflicts
@cspotcode cspotcode changed the title Adds failing test: unable to spawn .bat with %<environment variable>% in name Fixes bug: unable to spawn .bat with %<environment variable>% in name Dec 11, 2016
@cspotcode
Copy link
Author

I updated this PR to fix the bug using the technique I described above. All tests are passing, so it should be ready to merge. Let me know if you have any questions.

@satazor
Copy link
Contributor
satazor commented Dec 24, 2016

@cspotcode Thanks for the PR, it's definitively something that I haven't though. I will request a few changes to the code.

@@ -36,13 +40,20 @@ function parseNonShell(parsed) {
if (needsShell) {
// Escape command & arguments
applyQuotes = (parsed.command !== 'echo'); // Do not quote arguments for the special "echo" command
parsed.command = escapeCommand(parsed.command);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we keep everything in the parsed object? Code is concise and no additional variables are necessary.

Copy link
Author

Choose a reason for hiding this comment

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

Keeping everything on the parsed object is more confusing to a new reader. Doing so would mean we are storing a temporary value onto the object with no intention of keeping that value, since it is overwritten on line 57. By using a variable, we're indicating that this is a temporary value used in computation. command would ideally be declared const to make the code's intent even clearer.

lib/parse.js Outdated
@@ -107,4 +118,16 @@ function parse(command, args, options) {
return options.shell ? parseShell(parsed) : parseNonShell(parsed);
}

// Like Object.assign, for older node versions
function assign(target, source) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add lodash.assign as a dependency and use it instead.

Copy link
Author

Choose a reason for hiding this comment

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

Coolcool, it's done.

lib/parse.js Outdated
parsed.args = ['/d', '/s', '/c', '"' + parsed.command + (parsed.args.length ? ' ' + parsed.args.join(' ') : '') + '"'];
// Pass command as environment variable
parsed.options.env = assign({}, parsed.options.env || process.env);
do {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is necessary, just assigning _cross_spawn_command_<Math.round(Math.random() * 10e15).toString(35)> is fine.

Copy link
Author

Choose a reason for hiding this comment

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

My implementation is technically safer, although I agree the possibility of a collision is remote. If you're dedicated to saving 2 lines of code then I'll change it, but at this point the code's already written, so I think it should stay the way it is.

envVariableName = 'cross_spawn_command_' + envVariableSuffix;
envVariableSuffix += 1;
} while (Object.prototype.hasOwnProperty.call(parsed.options.env, envVariableName));
parsed.options.env[envVariableName] = command + (args.length ? ' ' + args.join(' ') : '');
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comment explaining this technique and why it's necessary for future reading.

Copy link
Author

Choose a reason for hiding this comment

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

Ok, I added a description.

@cspotcode
Copy link
Author
cspotcode commented Feb 26, 2017

@satazor Thanks for the comments. I implemented the requested changes or replied with comments explaining why I think my implementation should remain as-is. Although I do have strong opinions, I don't have a problem changing my code if you really want it implemented a certain way. Just let me know.

In the process I also noticed that the passed options object is mutated in certain situations, so I added a failing test and then fixed it.

EDIT: I forgot to mention that I can squash these commits downs, too. I left them separate to make the revisions more obvious.

@cspotcode
Copy link
Author

Any interest in merging this? I published a proof-of-concept at @cspotcode/cross-spawn. Tests pass; everything appears to work correctly.

satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Change escaping on Windows to use `^` instead of quotes:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
- Add a work around for a NodeJS bug when spawning a command with spaces when `options.shell` was enabled
- Remove support for running `echo` on Windows
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Change escaping on Windows to use `^` instead of quotes:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Add a work around for a NodeJS bug when spawning a command with spaces when `options.shell` was enabled, fixes #77
- Fix `options` argument being mutated
- Remove support for running `echo` on Windows
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Change escaping on Windows to use `^` instead of quotes:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Add a work around for a NodeJS bug when spawning a command with spaces when `options.shell` was enabled, fixes #77
- Fix `options` argument being mutated
- Remove support for running `echo` on Windows
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Change escaping on Windows to use `^` instead of quotes:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Change escaping on Windows to use `^` instead of quotes:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
@satazor
Copy link
Contributor
satazor commented Nov 11, 2017

@cspotcode I've been working on #83 that fixes this issue by using a different escaping technique using ^.

I will be closing this in favor of that MR that I will eventually release tomorrow as a major release.
Thanks for reporting the issue and for the time you took to explore solutions to it!

(your failing test was copied as well as the clone of the options arg)

@satazor satazor closed this Nov 11, 2017
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Change escaping on Windows to use `^` instead of quotes:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Change escaping on Windows to use `^` instead of quotes:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Change escaping on Windows to use `^` instead of quotes:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Change escaping on Windows to use `^` instead of quotes:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Change escaping on Windows to use `^` instead of quotes:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Change escaping on Windows to use `^` instead of quotes:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Change escaping on Windows to use `^` instead of quotes:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Add `^` to also escape Windows metachars:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
satazor added a commit that referenced this pull request Nov 11, 2017
- Remove NodeJS v0.10 and v0.12 support
- Add `^` to also escape Windows metachars:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
satazor added a commit that referenced this pull request Nov 12, 2017
- Remove NodeJS v0.10 and v0.12 support
- Add `^` to also escape Windows metachars:
    - Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
    - Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51
- Fix `options` argument being mutated
@cspotcode
Copy link
Author

Awesome, thanks for fixing it.

@satazor
Copy link
Contributor
satazor commented Nov 13, 2017

I'm still waiting some feedback on #83 from some people.
If you could, please give it a test. All the feedback is welcome.

cross-spawn is being used by a lot of people, so I must release stuff with special care.

satazor added a commit that referenced this pull request Jan 22, 2018
…ch syntax error

More specifically:
- Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
- Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51

This was resolved by using `^` to escape all meta chars.
Additionally, double escaping was necessary for cmd-shim files located in `node_modules./bin/`.

Also, this commit was a major overhaul:
- Upgrade tooling
- Upgrate project to es6 (node v4)
- Fix commands as posix unix relatixe paths not working correctly
- Fix `options` argument being mutated
- Improve compliance with node's ENOENT errors
- Improve detection of node's shell option support
- Migrate project to moxystudio

BREAKING CHANGE: remove support for older nodejs versions, only node >= 4 is supported
satazor added a commit that referenced this pull request Jan 22, 2018
…ch syntax error

More specifically:
- Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
- Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51

This was resolved by using `^` to escape all meta chars.
Additionally, double escaping was necessary for cmd-shim files located in `node_modules./bin/`.

Also, this commit was a major overhaul:
- Upgrade tooling
- Upgrate project to es6 (node v4)
- Fix commands as posix unix relatixe paths not working correctly
- Fix `options` argument being mutated
- Improve compliance with node's ENOENT errors
- Improve detection of node's shell option support
- Migrate project to moxystudio

BREAKING CHANGE: remove support for older nodejs versions, only node >= 4 is supported
satazor added a commit that referenced this pull request Jan 22, 2018
…ch syntax error

More specifically:
- Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
- Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51

This was resolved by using `^` to escape all meta chars.
Additionally, double escaping was necessary for cmd-shim files located in `node_modules./bin/`.

Also, this commit was a major overhaul:
- Upgrade tooling
- Upgrate project to es6 (node v4)
- Fix commands as posix unix relatixe paths not working correctly
- Fix `options` argument being mutated
- Improve compliance with node's ENOENT errors
- Improve detection of node's shell option support
- Migrate project to moxystudio

BREAKING CHANGE: remove support for older nodejs versions, only node >= 4 is supported
satazor added a commit that referenced this pull request Jan 22, 2018
…ch syntax error

More specifically:
- Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
- Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51

This was resolved by using `^` to escape all meta chars.
Additionally, double escaping was necessary for cmd-shim files located in `node_modules./bin/`.

Also, this commit was a major overhaul:
- Upgrade tooling
- Upgrate project to es6 (node v4)
- Fix commands as posix unix relatixe paths not working correctly
- Fix `options` argument being mutated
- Improve compliance with node's ENOENT errors
- Improve detection of node's shell option support
- Migrate project to moxystudio

BREAKING CHANGE: remove support for older nodejs versions, only node >= 4 is supported
satazor added a commit that referenced this pull request Jan 22, 2018
…ch syntax error

More specifically:
- Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
- Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51

This was resolved by using `^` to escape all meta chars.
Additionally, double escaping was necessary for cmd-shim files located in `node_modules./bin/`.

Also, this commit was a major overhaul:
- Upgrade tooling
- Upgrate project to es6 (node v4)
- Fix commands as posix unix relatixe paths not working correctly
- Fix `options` argument being mutated
- Improve compliance with node's ENOENT errors
- Improve detection of node's shell option support
- Migrate project to moxystudio

BREAKING CHANGE: remove support for older nodejs versions, only node >= 4 is supported
satazor added a commit that referenced this pull request Jan 22, 2018
…ch syntax error

More specifically:
- Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
- Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51

This was resolved by using `^` to escape all meta chars.
Additionally, double escaping was necessary for cmd-shim files located in `node_modules./bin/`.

Also, this commit was a major overhaul:
- Upgrade tooling
- Upgrate project to es6 (node v4)
- Fix commands as posix unix relatixe paths not working correctly
- Fix `options` argument being mutated
- Improve compliance with node's ENOENT errors
- Improve detection of node's shell option support
- Migrate project to moxystudio

BREAKING CHANGE: remove support for older nodejs versions, only node >= 4 is supported

Fixes #82, #51
satazor added a commit that referenced this pull request Jan 22, 2018
…ch syntax error

More specifically:
- Fix a bug that made it impossible to escape an argument that contained quotes followed by `>` or other special chars, e.g.: `"foo|bar"`, fixes #82
- Fix a bug were a command containing `%x%` would be replaced with the contents of the `x` environment variable, fixes #51

This was resolved by using `^` to escape all meta chars.
Additionally, double escaping was necessary for cmd-shim files located in `node_modules./bin/`.

Also, this commit was a major overhaul:
- Upgrade tooling
- Upgrate project to es6 (node v4)
- Fix commands as posix unix relatixe paths not working correctly
- Fix `options` argument being mutated
- Improve compliance with node's ENOENT errors
- Improve detection of node's shell option support
- Migrate project to moxystudio

BREAKING CHANGE: remove support for older nodejs versions, only node >= 4 is supported

Fixes #82, #51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants