-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(nsis): transition UAC elevation to pure powershell + Start-Process + RunAs
#9765
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
Draft
mmaietta
wants to merge
6
commits into
master
Choose a base branch
from
feat/uac-powershell
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+158
−7
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e876cbc
feat(nsis): transition UAC elevation to pure `powershell + Start-Proc…
mmaietta 1560a68
check for elevate before attempting to use it
mmaietta 0eb3bcb
address PR comments
mmaietta 8dcc69a
address PR comments
mmaietta 764d869
Merge branch 'master' into feat/uac-powershell
mmaietta 4d01916
Merge branch 'master' into feat/uac-powershell
mmaietta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "electron-updater": minor | ||
| "app-builder-lib": minor | ||
| --- | ||
|
|
||
| feat(nsis): transition UAC elevation to pure `powershell + Start-Process + RunAs` with legacy `elevate.exe` as fallback |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`NsisUpdater.doInstall elevation > uses PowerShell with -NoProfile and -EncodedCommand when isAdminRightsRequired 1`] = ` | ||
| [ | ||
| "-NonInteractive", | ||
| "-NoProfile", | ||
| "-EncodedCommand", | ||
| ] | ||
| `; | ||
|
|
||
| exports[`NsisUpdater.doInstall elevation > uses PowerShell with -NoProfile and -EncodedCommand when isAdminRightsRequired 2`] = `"Start-Process -FilePath '/fake/installer.exe' -ArgumentList @('--updated','/S') -Verb RunAs"`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import * as path from "path" | ||
| import { vi, beforeEach, afterEach } from "vitest" | ||
| import { createNsisUpdater } from "../helpers/updaterTestUtil" | ||
|
|
||
| describe("NsisUpdater.doInstall elevation", () => { | ||
| beforeEach(() => { | ||
| // process.resourcesPath is Electron-specific; stub it for the Node test environment | ||
| Object.defineProperty(process, "resourcesPath", { value: "/fake/resources", writable: true, configurable: true }) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| Object.defineProperty(process, "resourcesPath", { value: undefined, writable: true, configurable: true }) | ||
| }) | ||
|
|
||
| test("uses PowerShell with -NoProfile and -EncodedCommand when isAdminRightsRequired", async ({ expect }) => { | ||
| const updater = await createNsisUpdater() | ||
| const spawnLogMock = vi.spyOn(updater as any, "spawnLog").mockResolvedValue(true) | ||
| vi.spyOn(updater as any, "installerPath", "get").mockReturnValue("/fake/installer.exe") | ||
|
|
||
| ;(updater as any).doInstall({ isSilent: true, isForceRunAfter: false, isAdminRightsRequired: true }) | ||
|
|
||
| expect(spawnLogMock).toHaveBeenCalledOnce() | ||
| const [cmd, args] = spawnLogMock.mock.calls[0] as [string, string[]] | ||
| expect(cmd).toBe("powershell.exe") | ||
| const encodedIdx = args.indexOf("-EncodedCommand") | ||
| const script = Buffer.from(args[encodedIdx + 1], "base64").toString("utf16le") | ||
| expect(args.slice(0, encodedIdx + 1)).toMatchSnapshot() | ||
| expect(script).toMatchSnapshot() | ||
| }) | ||
|
|
||
| test("wraps installer args containing spaces in Win32 double-quotes", async ({ expect }) => { | ||
| const updater = await createNsisUpdater() | ||
| updater.installDirectory = "C:\\Program Files\\My App" | ||
| const spawnLogMock = vi.spyOn(updater as any, "spawnLog").mockResolvedValue(true) | ||
| vi.spyOn(updater as any, "installerPath", "get").mockReturnValue("/fake/installer.exe") | ||
|
|
||
| ;(updater as any).doInstall({ isSilent: false, isForceRunAfter: false, isAdminRightsRequired: true }) | ||
|
|
||
| const [, args] = spawnLogMock.mock.calls[0] as [string, string[]] | ||
| const encodedIdx = args.indexOf("-EncodedCommand") | ||
| const script = Buffer.from(args[encodedIdx + 1], "base64").toString("utf16le") | ||
| expect(script).toContain('"/D=C:\\Program Files\\My App"') | ||
| }) | ||
|
|
||
| test("dispatches error when powershell.exe fails with non-ENOENT error", async ({ expect }) => { | ||
| const updater = await createNsisUpdater() | ||
| const error = Object.assign(new Error("spawn UNKNOWN"), { code: "UNKNOWN" }) | ||
| vi.spyOn(updater as any, "spawnLog").mockRejectedValue(error) | ||
| vi.spyOn(updater as any, "installerPath", "get").mockReturnValue("/fake/installer.exe") | ||
| const dispatchErrorMock = vi.spyOn(updater as any, "dispatchError").mockImplementation(() => {}) | ||
|
|
||
| ;(updater as any).doInstall({ isSilent: false, isForceRunAfter: false, isAdminRightsRequired: true }) | ||
| await new Promise(resolve => setTimeout(resolve, 10)) | ||
|
|
||
| expect(dispatchErrorMock).toHaveBeenCalledWith(error) | ||
| }) | ||
|
|
||
| test("falls back to elevate.exe spawn when powershell.exe not found", async ({ expect }) => { | ||
| const updater = await createNsisUpdater() | ||
| const psError = Object.assign(new Error("spawn ENOENT"), { code: "ENOENT" }) | ||
| const spawnLogMock = vi.spyOn(updater as any, "spawnLog").mockRejectedValueOnce(psError).mockResolvedValueOnce(true) | ||
| vi.spyOn(updater as any, "installerPath", "get").mockReturnValue("/fake/installer.exe") | ||
|
|
||
| ;(updater as any).doInstall({ isSilent: false, isForceRunAfter: false, isAdminRightsRequired: true }) | ||
| await new Promise(resolve => setTimeout(resolve, 10)) | ||
|
|
||
| expect(spawnLogMock).toHaveBeenCalledTimes(2) | ||
| expect(spawnLogMock.mock.calls[0][0]).toBe("powershell.exe") | ||
| expect(spawnLogMock.mock.calls[1][0]).toBe(path.join("/fake/resources", "elevate.exe")) | ||
| }) | ||
| }) | ||
|
|
||
| describe.ifWindows("NsisUpdater.doInstall elevation — Windows integration", () => { | ||
| test("powershell.exe accepts -EncodedCommand on this system", async ({ expect }) => { | ||
| const { execFile } = await import("child_process") | ||
| const { promisify } = await import("util") | ||
| const execFileAsync = promisify(execFile) | ||
| const script = "Write-Output 'elevation-test-ok'" | ||
| const encoded = Buffer.from(script, "utf16le").toString("base64") | ||
| const { stdout } = await execFileAsync("powershell.exe", ["-NoProfile", "-NonInteractive", "-EncodedCommand", encoded], { encoding: "utf8" }) | ||
| expect(stdout.trim()).toBe("elevation-test-ok") | ||
| }) | ||
|
|
||
| test("generated Start-Process script is syntactically valid for plain args", async ({ expect }) => { | ||
| const installerPath = "C:\\fake\\installer.exe" | ||
| const args = ["--updated", "/S"] | ||
| const psInstallArgs = args.map(a => `'${a.replace(/'/g, "''")}'`).join(",") | ||
| const psScript = `Start-Process -FilePath '${installerPath.replace(/'/g, "''")}' -ArgumentList @(${psInstallArgs}) -Verb RunAs` | ||
| await assertPsScriptParses(psScript) | ||
| expect(true).toBe(true) | ||
| }) | ||
|
|
||
| test("generated Start-Process script is syntactically valid for args containing spaces", async ({ expect }) => { | ||
| const installerPath = "C:\\fake\\installer.exe" | ||
| const args = ["--updated", "/D=C:\\Program Files\\My App"] | ||
| const psInstallArgs = args.map(a => (a.includes(" ") ? `'"${a.replace(/"/g, '""')}"'` : `'${a.replace(/'/g, "''")}'`)).join(",") | ||
| const psScript = `Start-Process -FilePath '${installerPath.replace(/'/g, "''")}' -ArgumentList @(${psInstallArgs}) -Verb RunAs` | ||
| await assertPsScriptParses(psScript) | ||
| expect(true).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| async function assertPsScriptParses(script: string): Promise<void> { | ||
| const { execFile } = await import("child_process") | ||
| const { promisify } = await import("util") | ||
| const execFileAsync = promisify(execFile) | ||
| // Count parse errors without executing the script | ||
| const parseCmd = `$errors = $null; $null = [System.Management.Automation.Language.Parser]::ParseInput(${JSON.stringify(script)}, [ref]$null, [ref]$errors); exit $errors.Count` | ||
| const encoded = Buffer.from(parseCmd, "utf16le").toString("base64") | ||
| // Throws on non-zero exit code (= parse errors found) | ||
| await execFileAsync("powershell.exe", ["-NoProfile", "-NonInteractive", "-EncodedCommand", encoded]) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.