diff --git a/cmd/mo/main.go b/cmd/mo/main.go index 2cfc3a2e5..46aa48ea9 100644 --- a/cmd/mo/main.go +++ b/cmd/mo/main.go @@ -2,8 +2,7 @@ // // Package managers that can only expose an executable (winget portable // installs) put this on PATH as `mo`. It passes every argument through as a -// literal PowerShell string, the same call shape as the mo.cmd launcher that -// install.ps1 writes, and returns the script's exit code. +// literal PowerShell string and returns the script's exit code. package main import ( @@ -110,6 +109,16 @@ func commandLine(script string, args []string) string { return strings.Join(parts, " ") + "; exit $LASTEXITCODE" } +// PowerShell also treats these Unicode characters as single-quote delimiters. +// Double each delimiter to preserve it inside a single-quoted literal. +var singleQuoteEscaper = strings.NewReplacer( + "'", "''", + "\u2018", "\u2018\u2018", + "\u2019", "\u2019\u2019", + "\u201a", "\u201a\u201a", + "\u201b", "\u201b\u201b", +) + func quote(value string) string { - return "'" + strings.ReplaceAll(value, "'", "''") + "'" + return "'" + singleQuoteEscaper.Replace(value) + "'" } diff --git a/cmd/mo/main_test.go b/cmd/mo/main_test.go index 6afff97bd..f61f42bbf 100644 --- a/cmd/mo/main_test.go +++ b/cmd/mo/main_test.go @@ -25,6 +25,16 @@ func TestCommandLineWithoutArgumentsOpensTheMenu(t *testing.T) { } } +func TestCommandLineQuotesSmartSingleQuotes(t *testing.T) { + got := commandLine("C:\\Users\\O\u2019Brien\\mole.ps1", []string{ + "\u2018left", "right\u2019", "low\u201a", "reversed\u201b", "\u201cquoted\u201d\u201e", + }) + want := "& 'C:\\Users\\O\u2019\u2019Brien\\mole.ps1' '\u2018\u2018left' 'right\u2019\u2019' 'low\u201a\u201a' 'reversed\u201b\u201b' '\u201cquoted\u201d\u201e'; exit $LASTEXITCODE" + if got != want { + t.Fatalf("commandLine:\n got %s\nwant %s", got, want) + } +} + func TestPowerShellCandidatesTryWindowsPowerShellFirst(t *testing.T) { t.Setenv("SystemRoot", `C:\TestWindows`) got := powerShellCandidates() diff --git a/docs/incidents.md b/docs/incidents.md index d31e86ebe..f5270dab5 100644 --- a/docs/incidents.md +++ b/docs/incidents.md @@ -56,3 +56,4 @@ record which rows it finds. | I-25 | 2026-11-11 | Updating an existing source install through `quick-install.ps1` in Windows PowerShell 5.1 stopped with "Installation failed: From https://github.com/tw93/Mole", and reinstalling hit the same path because the source folder stays | 92b19729 | 8b43e8d8 | write-time | `Defines Invoke-GitCommand the same way in both scripts`, `Returns git's exit code instead of throwing on its stderr progress in <_>` | | I-26 | 2026-11-12 | After `cmd/status` and `cmd/analyze` changed past V1.44.0-windows, a fresh `quick-install.ps1` on a PC without Go installed neither `status.exe` nor `analyze.exe`, and `mo status` said to check the network | 10334ee2 | ead9c74d | write-time | `Uses the verified release build for missing <_> when only upstream commits changed it`, `Offers the tags in HEAD history newest first, and none for local edits, a HEAD beside the tags or a Git override` | | I-27 | 2026-11-12 | Once `cmd/` changed after a release, `mo update` on a source install without Go stopped with "Could not refresh ... Install Go and rerun this installer", and a fresh install after a version bump whose release was not out yet got no `status.exe` or `analyze.exe` | 59b3bcc4 | f9603ca4 | write-time | `Refreshes both tools from the release build without Go when the source is ahead of it`, `Tries the previous release when the newest one is not published yet` | +| I-28 | 2026-11-17 | `mo.exe` failed to start from an install path containing a typographic single quote, and the same quotes in arguments could change the values passed to the script (#2021) | unknown | 816fbeea | unit | `TestCommandLineQuotesSmartSingleQuotes`, `Preserves quote U+ in the native script path and arguments` | diff --git a/tests/InstallLifecycle.Tests.ps1 b/tests/InstallLifecycle.Tests.ps1 index 3011c5988..46e6b3fc2 100644 --- a/tests/InstallLifecycle.Tests.ps1 +++ b/tests/InstallLifecycle.Tests.ps1 @@ -10,6 +10,44 @@ BeforeAll { $script:Colors = @{ Red=''; Green=''; Yellow=''; Blue=''; Cyan=''; Gray=''; NC='' } } +Describe 'Native launcher literal arguments' { + BeforeAll { + $script:NativeLauncher = Join-Path $TestDrive 'native-mo.exe' + Push-Location $script:RepoRoot + try { + & go build -o $script:NativeLauncher ./cmd/mo + if ($LASTEXITCODE -ne 0) { throw 'Could not build the native launcher' } + } + finally { Pop-Location } + } + + It 'Preserves quote U+ in the native script path and arguments' -ForEach @( + @{ Code = '0027'; Value = 0x0027 } + @{ Code = '2018'; Value = 0x2018 } + @{ Code = '2019'; Value = 0x2019 } + @{ Code = '201A'; Value = 0x201A } + @{ Code = '201B'; Value = 0x201B } + ) { + $root = Join-Path $TestDrive ('O' + [char]$Value + 'Brien-' + $Code) + $null = New-Item -ItemType Directory -Path $root + $launcher = Join-Path $root 'mo.exe' + Copy-Item -LiteralPath $script:NativeLauncher -Destination $launcher + @' +[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject @($args) -Compress))) +exit 7 +'@ | Set-Content -LiteralPath (Join-Path $root 'mole.ps1') -Encoding ASCII + $argument = 'literal ' + [char]$Value + ' $env:USERPROFILE ' + [char]0x201C + 'double' + [char]0x201D + [char]0x201E + $output = & $launcher '--version' $argument + $LASTEXITCODE | Should -Be 7 + @($output).Count | Should -Be 1 + $json = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String([string]$output)) + $actual = ConvertFrom-Json $json + @($actual).Count | Should -Be 2 + $actual[0] | Should -BeExactly '--version' + $actual[1] | Should -BeExactly $argument + } +} + Describe 'Source native tool refresh' { BeforeEach { $script:Root = Join-Path $TestDrive ([guid]::NewGuid().ToString('N'))