Skip to content

Use -EncodedCommand for the elevated Developer Mode script - #727

Merged
Nikola Metulev (nmetulev) merged 3 commits into
mainfrom
azchohfi-devmode-encoded-command
Aug 11, 2026
Merged

Nikola Metulev (nmetulev) merged 3 commits into
mainfrom
azchohfi-devmode-encoded-command

Conversation

@azchohfi

Copy link
Copy Markdown
Collaborator

What

DevModeService.EnsureWin11DevModeAsync embedded its registry script inside a -Command "& { ... }" argument:

var exit = RunElevated(ps,
    $"-NoProfile -ExecutionPolicy Bypass -Command \"& {{ {EscapeForPSArg(psScript)} }}\"");

EscapeForPSArg only escaped double quotes, which is the fragile half of a quoting problem rather than a fix for it. The script itself is hardcoded, so there is no injection path today — but the shape invites one the moment anything dynamic is added, and the escaping helper looks more complete than it is.

How

Switch to -EncodedCommand, which takes base64 UTF-16LE:

string encoded = Convert.ToBase64String(Encoding.Unicode.GetBytes(psScript));
var exit = RunElevated(ps, $"-NoProfile -ExecutionPolicy Bypass -EncodedCommand {encoded}");

Base64 output contains no characters with meaning to the shell, so the quoting problem disappears rather than being escaped around, and EscapeForPSArg is deleted.

This is already the established pattern elsewhere in the repo — scripts/winapp-pr.ps1 and the debug-output tests both use -EncodedCommand for the same reason.

The cmd.exe + reg.exe fallback is untouched.

Validation

DevModeServiceTests passes. One test asserted on the raw argument string containing AppModelUnlock, which base64 naturally breaks; it now decodes the payload and asserts on the script text:

var match = Regex.Match(psi.Arguments, @"-EncodedCommand\s+(?<b64>[A-Za-z0-9+/=]+)");
var script = Encoding.Unicode.GetString(Convert.FromBase64String(match.Groups["b64"].Value));
StringAssert.Contains(script, "AppModelUnlock");

That is a stronger assertion than the substring match it replaces — it verifies the script that will actually execute, not just the command line.

EnsureWin11DevModeAsync embedded its registry script inside a
-Command "& { ... }" argument and relied on EscapeForPSArg, which only escaped
double quotes. That is the fragile half of a quoting problem rather than a fix
for it.

-EncodedCommand takes base64 UTF-16LE, so the script never passes through
command-line quoting at all and the escaping helper is no longer needed. This
matches the pattern already used in scripts/winapp-pr.ps1 and the debug-output
tests.

The test that asserted on the raw argument string now decodes the payload and
asserts on the script text, which is a stronger check than the substring match
it replaces.
Copilot AI balanced review requested due to automatic review settings August 11, 2026 18:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Replaces fragile PowerShell command quoting with UTF-16LE -EncodedCommand.

Changes:

  • Base64-encodes the Developer Mode script.
  • Updates tests to decode and validate the payload.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
DevModeService.cs Uses encoded PowerShell commands.
DevModeServiceTests.cs Validates the decoded script.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

github-actions Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Build Metrics Report

Binary Sizes

Artifact Baseline Current Delta
CLI (ARM64) 38.62 MB 38.62 MB ✅ 0.0 KB (0.00%)
CLI (x64) 38.73 MB 38.73 MB 📉 -0.5 KB (-0.00%)
MSIX (ARM64) 16.02 MB 16.02 MB 📈 +0.6 KB (+0.00%)
MSIX (x64) 17.01 MB 17.01 MB 📉 -0.8 KB (-0.00%)
NPM Package 33.42 MB 33.42 MB 📈 +0.6 KB (+0.00%)
NuGet Package 33.45 MB 33.45 MB 📉 -0.1 KB (-0.00%)

Test Results

✅ 4549 passed, 5 skipped out of 4554 tests in 607.7s (-37.5s vs. baseline)

Test Coverage

✅ 89.1% line coverage, 82.4% branch coverage · ✅ no change vs. baseline

CLI Startup Time

47ms median (x64, winapp --version) · ✅ no change vs. baseline

Try This Build

Installs the MSIX for your architecture, replacing any previously installed build. Needs the GitHub CLI — the command offers to install it and sign you in if it is missing.

& ([scriptblock]::Create((irm https://raw.githubusercontent.com/microsoft/winappCli/main/scripts/winapp-pr.ps1))) 727
Switching between builds often?

Put the tool on your PATH once:

& ([scriptblock]::Create((irm https://raw.githubusercontent.com/microsoft/winappCli/main/scripts/winapp-pr.ps1))) -AddToPath

Then this build is just:

winapp-pr 727

Run winapp-pr with no arguments to pick from a list of open PRs.


Updated 2026-08-11 22:33:30 UTC · commit b547117 · workflow run

@nmetulev
Nikola Metulev (nmetulev) merged commit b381943 into main Aug 11, 2026
30 checks passed
@nmetulev
Nikola Metulev (nmetulev) deleted the azchohfi-devmode-encoded-command branch August 11, 2026 22:47
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.

3 participants