Skip to content

Commit 5fcefd2

Browse files
fix: mitigate command injection risk in runPowerShell by using -EncodedCommand
1 parent 0fd170e commit 5fcefd2

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/powershell.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ export function runPowerShell(
4444
config: ServerConfig
4545
): Promise<PowerShellResult> {
4646
return new Promise((resolve, reject) => {
47+
// Encode the script as Base64 UTF-16LE and use -EncodedCommand so that
48+
// no part of the script (including any user-supplied parameter values
49+
// embedded in it) is ever parsed as a command-line argument by PowerShell.
50+
// This eliminates the command-injection risk present with -Command.
51+
const encodedCommand = Buffer.from(script, "utf16le").toString("base64");
4752
const pwsh = spawn(
4853
config.powershellExe,
4954
[
@@ -52,8 +57,8 @@ export function runPowerShell(
5257
"-NoLogo",
5358
"-ExecutionPolicy",
5459
"Bypass",
55-
"-Command",
56-
script,
60+
"-EncodedCommand",
61+
encodedCommand,
5762
],
5863
{ shell: false }
5964
);

0 commit comments

Comments
 (0)