Skip to content

fix(util): escape Unicode single quotes in quotePowerShell - #1527

Open
gargarnav wants to merge 1 commit into
google:mainfrom
gargarnav:fix/quote-powershell-unicode-quotes
Open

gargarnav wants to merge 1 commit into
google:mainfrom
gargarnav:fix/quote-powershell-unicode-quotes

Conversation

@gargarnav

@gargarnav gargarnav commented Oct 15, 2026 •

Copy link
Copy Markdown

Fixes: suggests an improvement (hardening of quotePowerShell)

Why

quotePowerShell() wraps a value in ASCII single quotes and doubles only the ASCII apostrophe. PowerShell also treats U+2018, U+2019, U+201A and U+201B as single-quote characters, so a value containing one of them closes the string early and the remainder is parsed as PowerShell code.

With usePwsh() (or $.quote = quotePowerShell) and untrusted input:

import {$, usePwsh} from 'zx'
usePwsh()
const v = 'x’; Write-Output INJECTED; ‘y'
await $`Write-Output ${v}`
// before: prints x / INJECTED / y (the second statement ran)
// after:  prints the value literally

I confirmed the before behavior on PowerShell 7.6.7 (macOS) for U+2018 and U+2019 by running the emitted command shape (<cmd>; exit $LastExitCode) directly. U+201A and U+201B are listed as single quotes in PowerShell's quoting rules and are covered by the fix.

Change

src/util.ts: arg.replace(/['‘’‚‛]/g, (c) => c + c), plus a test for the four characters and the regenerated build/util.cjs.

Checks

  • Setup Node.js current (v25.6.1 locally)
  • Build: ran npm run build; only build/util.cjs changed meaningfully (other generated noise reverted)
  • Tests: test/util.test.js passes; the full unit run has the same pre-existing failures with and without this change in my environment (thenable args, exports chalk level, log/fetch tests)
  • Docs: no docs change needed
  • Sign
  • CoC
  • Review: I reviewed the change and tested it myself, it is not solely generated by AI tools

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates PowerShell argument quoting in build/util.cjs and src/util.ts to handle special single quote characters (U+2018, U+2019, U+201A, and U+201B) by doubling them, and adds corresponding unit tests. The review feedback suggests optimizing the replacement logic in src/util.ts by using a regex capture group and a replacement string instead of a callback function to improve performance.

Comment thread src/util.ts Outdated
@gargarnav
gargarnav force-pushed the fix/quote-powershell-unicode-quotes branch 2 times, most recently from b92f0d5 to 9f2994a Compare October 17, 2026 10:20
PowerShell treats U+2018, U+2019, U+201A and U+201B as single-quote
characters, so a value containing one of them ended the quoted string
early and the rest of the value was parsed as PowerShell code. Double
all of them, as is done for the ASCII apostrophe, and add tests.
@gargarnav
gargarnav force-pushed the fix/quote-powershell-unicode-quotes branch from 9f2994a to 02c638d Compare October 17, 2026 10:21
@gargarnav

Copy link
Copy Markdown
Author

Addressed the review feedback and re-signed the commit. Could a maintainer approve the pending workflows when you get a chance? Thanks!

This branch has not been deployed

No deployments
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.

1 participant