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

Escape quotes in arguments for PowerShell #319

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ChristianLW
Copy link

A fix for #318. This is just a simple patch and can of course be changed if necessary.

The String.prototype.replaceAll.call(arg, ...) instead of just arg.replaceAll(...) is because appArguments might contain things other than strings.

This doesn't add escaping to the app name itself.

I'm not sure if you want to set up a unit test for this scenario, and I'm not sure if this change marks a breaking API change (if some people already put ` in their strings even though that wouldn't work on other platforms).

index.js Outdated
@@ -202,7 +202,7 @@ const baseOpen = async options => {
}

if (appArguments.length > 0) {
appArguments = appArguments.map(arg => `"\`"${arg}\`""`);
appArguments = appArguments.map(arg => `"\`"${String.prototype.replaceAll.call(arg, '"', '`"')}\`""`);
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
appArguments = appArguments.map(arg => `"\`"${String.prototype.replaceAll.call(arg, '"', '`"')}\`""`);
appArguments = appArguments.map(arg => `"\`"${String(arg).replaceAll('"', '`"')}\`""`);

Copy link
Owner

Choose a reason for hiding this comment

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

This does not handle the case where the quote is already escaped: \"

Copy link
Author

Choose a reason for hiding this comment

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

Sorry for the late response. Could you please elaborate on the \" escaping? I can't find anything on using \ as an escape character in PowerShell.

Alternatively, would it make sense to use single quote strings, as they don't interpret special characters? You can include single quotes in single quote strings by doubling them. This would almost certainly eliminate any edge cases.

First convert to string and then call replaceAll, instead of calling replaceAll from the string prototype

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
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.

None yet

2 participants