Skip to content

Commit

Permalink
fix(core): Fix arguments parsing method of Invoke-ExternalCommand() (
Browse files Browse the repository at this point in the history
  • Loading branch information
niheaven committed Mar 25, 2024
1 parent 9770c86 commit 77b66cc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -45,6 +45,7 @@
- **config:** Warn users about misconfigured GitHub token ([#5777](https://github.com/ScoopInstaller/Scoop/issues/5777))
- **update/uninstall:** Remove items from PATH correctly ([#5833](https://github.com/ScoopInstaller/Scoop/issues/5833))
- **shim:** Allow GUI applications to attach to the shell's console when launched using the GUI shim ([#5721](https://github.com/ScoopInstaller/Scoop/issues/5721))
- **core:** Fix arguments parsing method of `Invoke-ExternalCommand()` ([#5839](https://github.com/ScoopInstaller/Scoop/issues/5839))

### Performance Improvements

Expand Down
21 changes: 11 additions & 10 deletions lib/core.ps1
Expand Up @@ -600,8 +600,7 @@ function Invoke-ExternalCommand {
[CmdletBinding(DefaultParameterSetName = "Default")]
[OutputType([Boolean])]
param (
[Parameter(Mandatory = $true,
Position = 0)]
[Parameter(Mandatory = $true, Position = 0)]
[Alias("Path")]
[ValidateNotNullOrEmpty()]
[String]
Expand Down Expand Up @@ -651,6 +650,7 @@ function Invoke-ExternalCommand {
$Process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
}
if ($ArgumentList.Length -gt 0) {
$ArgumentList = $ArgumentList | ForEach-Object { [regex]::Split($_.Replace('"', ''), '(?<=(?<![:\w])[/-]\w+) | (?=[/-])') }
if ($FilePath -match '^((cmd|cscript|wscript|msiexec)(\.exe)?|.*\.(bat|cmd|js|vbs|wsf))$') {
$Process.StartInfo.Arguments = $ArgumentList -join ' '
} elseif ($Process.StartInfo.ArgumentList.Add) {
Expand All @@ -661,14 +661,15 @@ function Invoke-ExternalCommand {
} else {
# escape arguments manually in lower versions, refer to https://docs.microsoft.com/en-us/previous-versions/17w5ykft(v=vs.85)
$escapedArgs = $ArgumentList | ForEach-Object {
# escape N consecutive backslash(es), which are followed by a double quote, to 2N consecutive ones
$s = $_ -replace '(\\+)"', '$1$1"'
# escape N consecutive backslash(es), which are at the end of the string, to 2N consecutive ones
$s = $s -replace '(\\+)$', '$1$1'
# escape double quotes
$s = $s -replace '"', '\"'
# quote the argument
"`"$s`""
# escape N consecutive backslash(es), which are followed by a double quote or at the end of the string, to 2N consecutive ones
$s = $_ -replace '(\\+)(""|$)', '$1$1$2'
# quote the path if it contains spaces and is not NSIS's '/D' argument
# ref: https://nsis.sourceforge.io/Docs/Chapter3.html
if ($s -match ' ' -and $s -notmatch '/D=[A-Z]:[\\/].*') {
$s -replace '([A-Z]:[\\/].*)', '"$1"'
} else {
$s
}
}
$Process.StartInfo.Arguments = $escapedArgs -join ' '
}
Expand Down

0 comments on commit 77b66cc

Please sign in to comment.