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

Windows: fix escaping of external commands #3214

Merged

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Feb 13, 2021

The escapeshellcmd() function apparently does not escape spaces within a path on Windows which can result in broken functionality.

While the sniffs and report affected by this are apparently not used that much based on the lack of bug reports, fixing it still seemed like the right thing to do.

Noticed while running the unit tests on a fresh install on Windows 10.

At some point over the past years, Node has apparently changed their default install directory on Windows and the order in which they register their paths to the Windows system PATH.
This means that where csslint may result in a $cmd path like C:\Program Files\nodejs\csslint.cmd, which would be escaped to C:^\Program Files^\nodejs^\csslint.cmd on Windows, which in turn results in a 'C:\Program' is not recognized as an internal or external command, operable program or batch file. error.

I could have changed the install path for NVM on my machine, but that would just have hidden the underlying issue.

It does appear to be a known issue with the function based on the last two comments in this upstream bug report: https://bugs.php.net/bug.php?id=43261, however as that issue is closed, I don't expect this to be fixed in PHP itself, though it might be worth it to open a new issue upstream about it (as those two comments were left on a closed issue years after the close).

Fixed now by checking an escaped path for unescaped spaces when on Windows and if necessary, escaping them.
The escaping is done in such a way that, even if PHP itself would start escaping these spaces, the Common::escapeshellcmd() function will still handle this correctly.

The `escapeshellcmd()` function apparently does not escape spaces within a path on Windows which can result in broken functionality.

While the sniffs and report affected by this are apparently not used that much based on the lack of bug reports, fixing it still seemed like the _right thing to do_.

Noticed while running the unit tests on a fresh install on Windows 10.

At some point over the past years, Node has apparently changed their default install directory on Windows and the order in which they register their paths to the Windows system `PATH`.
This means that `where csslint` may result in a `$cmd` path like `C:\Program Files\nodejs\csslint.cmd`, which would be escaped to `C:^\Program Files^\nodejs^\csslint.cmd` on Windows, which in turn results in a `'C:\Program' is not recognized as an internal or external command, operable program or batch file.` error.

I could have changed the install path for NVM on my machine, but that would just have hidden the underlying issue.

It does appear to be a known issue with the function based on the last two comments in this upstream bug report: https://bugs.php.net/bug.php?id=43261, however as that issue is closed, I don't expect this to be fixed in PHP itself, though it might be worth it to open a new issue upstream about it (as those two comments were left on a closed issue years after the close).

Fixed now by checking an escaped path for unescaped spaces when on Windows and if necessary, escaping them.
The escaping is done in such a way that, even if PHP itself would start escaping these spaces, the `Common::escapeshellcmd()` function will still handle this correctly.
@gsherwood gsherwood added this to the 3.6.0 milestone Feb 14, 2021
gsherwood added a commit that referenced this pull request Feb 14, 2021
@gsherwood gsherwood merged commit df3ccb5 into squizlabs:master Feb 14, 2021
@gsherwood
Copy link
Member

There are still 3 of these paths in version 4, so very much worth doing. Thanks.

@jrfnl jrfnl deleted the feature/windows-fix-running-external-progs branch February 14, 2021 22:41
@jrfnl
Copy link
Contributor Author

jrfnl commented Feb 14, 2021

Yvw

jrfnl added a commit to jrfnl/doc-en that referenced this pull request Apr 2, 2021
It is a known issue that spaces are not escaped in shell commands, which can be especially problematic on Windows.
This adds a warning about this behaviour to the function, including a way to solve this in userland code.

Ref: https://bugs.php.net/bug.php?id=43261 (last two comments)

Also see: squizlabs/PHP_CodeSniffer#3214
jrfnl added a commit to jrfnl/doc-en that referenced this pull request Apr 2, 2021
It is a known issue that spaces are not escaped in shell commands, which can be especially problematic on Windows.
This adds a warning about this behaviour to the function, including a way to solve this in userland code.

Ref: https://bugs.php.net/bug.php?id=43261 (last two comments)

Also see: squizlabs/PHP_CodeSniffer#3214
cmb69 pushed a commit to php/doc-en that referenced this pull request Apr 5, 2021
It is a known issue that spaces are not escaped in shell commands, which can be especially problematic on Windows.
This adds a warning about this behaviour to the function, including a way to solve this in userland code.

Ref: https://bugs.php.net/bug.php?id=43261 (last two comments)

Also see: squizlabs/PHP_CodeSniffer#3214

Co-authored-by: jrfnl <[email protected]>
mumumu added a commit to php/doc-ja that referenced this pull request Apr 8, 2021
It is a known issue that spaces are not escaped in shell commands, which can be especially problematic on Windows.
This adds a warning about this behaviour to the function, including a way to solve this in userland code.

Ref: https://bugs.php.net/bug.php?id=43261 (last two comments)

Also see: squizlabs/PHP_CodeSniffer#3214

Co-authored-by: jrfnl <[email protected]>
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