Skip to content

Commit 5be6be3

Browse files
Potential fix for code scanning alert no. 3: Uncontrolled command line (#8)
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 59240d1 commit 5be6be3

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/core/selfUpdate.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -170,30 +170,42 @@ export function pickAsset(release: ReleaseInfo, mode: 'auto' | 'installer' | 'ex
170170
export async function applyUpdateInstaller(installerPath: string, silent: boolean, elevate: boolean) {
171171
logger.info('Launching installer...');
172172

173-
const args = [];
173+
const args: string[] = [];
174174
if (silent) {
175175
args.push('/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART');
176176
}
177177

178-
if (elevate) {
179-
// Use PowerShell Start-Process -Verb RunAs
180-
// To prevent command injection, we pass arguments via environment variables.
181-
const envVars: Record<string, string> = {
182-
'PS_INSTALLER_PATH': installerPath,
183-
'PS_INSTALLER_ARGS': args.join(' ')
184-
};
178+
// Use PowerShell Start-Process with environment variables for both elevated and non-elevated runs
179+
const envVars: Record<string, string> = {
180+
'PS_INSTALLER_PATH': installerPath,
181+
'PS_INSTALLER_ARGS': args.join(' ')
182+
};
183+
184+
const basePsCommand = `
185+
$p = [System.Environment]::GetEnvironmentVariable('PS_INSTALLER_PATH')
186+
$a = [System.Environment]::GetEnvironmentVariable('PS_INSTALLER_ARGS')
187+
`.trim();
185188

189+
if (elevate) {
190+
// Elevated: use -Verb RunAs
186191
const psCommand = `
187-
$p = [System.Environment]::GetEnvironmentVariable('PS_INSTALLER_PATH')
188-
$a = [System.Environment]::GetEnvironmentVariable('PS_INSTALLER_ARGS')
192+
${basePsCommand}
189193
Start-Process -FilePath $p -ArgumentList $a -Verb RunAs -Wait
190194
`.trim();
191195

192196
await execa('powershell', ['-NoProfile', '-NonInteractive', '-Command', psCommand], {
193197
env: { ...process.env, ...envVars }
194198
});
195199
} else {
196-
await execa(installerPath, args);
200+
// Non-elevated: run without -Verb RunAs
201+
const psCommand = `
202+
${basePsCommand}
203+
Start-Process -FilePath $p -ArgumentList $a -Wait
204+
`.trim();
205+
206+
await execa('powershell', ['-NoProfile', '-NonInteractive', '-Command', psCommand], {
207+
env: { ...process.env, ...envVars }
208+
});
197209
}
198210
}
199211

0 commit comments

Comments
 (0)