Skip to content

Commit cfc4f36

Browse files
authored
fix: auto-update powershell script for powershell version test with ConvertTo-Json test (#9232)
1 parent 44b2899 commit cfc4f36

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

.changeset/hot-cars-attack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"electron-updater": patch
3+
---
4+
5+
fix: auto-update powershell script

packages/electron-updater/src/windowsExecutableCodeSignatureVerifier.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import { parseDn } from "builder-util-runtime"
2-
import { execFile, execFileSync } from "child_process"
2+
import { execFile, execFileSync, ExecFileOptions } from "child_process"
33
import * as os from "os"
44
import { Logger } from "./types"
55
import * as path from "path"
66

7+
function preparePowerShellExec(command: string, timeout?: number) {
8+
// https://github.com/electron-userland/electron-builder/issues/2421
9+
// https://github.com/electron-userland/electron-builder/issues/2535
10+
// Resetting PSModulePath is necessary https://github.com/electron-userland/electron-builder/issues/7127
11+
// semicolon wont terminate the set command and run chcp thus leading to verification errors on certificats with special chars like german umlauts, so rather
12+
// join commands using & https://github.com/electron-userland/electron-builder/issues/8162
13+
const executable = `set "PSModulePath=" & chcp 65001 >NUL & powershell.exe`
14+
const args = ["-NoProfile", "-NonInteractive", "-InputFormat", "None", "-Command", command]
15+
const options: ExecFileOptions = {
16+
shell: true,
17+
timeout,
18+
}
19+
return [executable, args, options] as const
20+
}
21+
722
// $certificateInfo = (Get-AuthenticodeSignature 'xxx\yyy.exe'
823
// | where {$_.Status.Equals([System.Management.Automation.SignatureStatus]::Valid) -and $_.SignerCertificate.Subject.Contains("CN=siemens.com")})
924
// | Out-String ; if ($certificateInfo) { exit 0 } else { exit 1 }
@@ -30,18 +45,7 @@ export function verifySignature(publisherNames: Array<string>, unescapedTempUpda
3045
const tempUpdateFile = unescapedTempUpdateFile.replace(/'/g, "''")
3146
logger.info(`Verifying signature ${tempUpdateFile}`)
3247

33-
// https://github.com/electron-userland/electron-builder/issues/2421
34-
// https://github.com/electron-userland/electron-builder/issues/2535
35-
// Resetting PSModulePath is necessary https://github.com/electron-userland/electron-builder/issues/7127
36-
// semicolon wont terminate the set command and run chcp thus leading to verification errors on certificats with special chars like german umlauts, so rather
37-
// join commands using & https://github.com/electron-userland/electron-builder/issues/8162
38-
execFile(
39-
`set "PSModulePath=" & chcp 65001 >NUL & powershell.exe`,
40-
["-NoProfile", "-NonInteractive", "-InputFormat", "None", "-Command", `"Get-AuthenticodeSignature -LiteralPath '${tempUpdateFile}' | ConvertTo-Json -Compress"`],
41-
{
42-
shell: true,
43-
timeout: 20 * 1000,
44-
},
48+
execFile(...preparePowerShellExec(`"Get-AuthenticodeSignature -LiteralPath '${tempUpdateFile}' | ConvertTo-Json -Compress"`, 20 * 1000),
4549
(error, stdout, stderr) => {
4650
try {
4751
if (error != null || stderr) {
@@ -123,7 +127,7 @@ function handleError(logger: Logger, error: Error | null, stderr: string | null,
123127
}
124128

125129
try {
126-
execFileSync("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", "ConvertTo-Json test"], { timeout: 10 * 1000 } as any)
130+
execFileSync(...preparePowerShellExec("ConvertTo-Json test", 10 * 1000))
127131
} catch (testError: any) {
128132
logger.warn(
129133
`Cannot execute ConvertTo-Json: ${testError.message}. Ignoring signature validation due to unsupported powershell version. Please upgrade to powershell 3 or higher.`

0 commit comments

Comments
 (0)