mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
Merge pull request #287 from AikidoSec/bug/win32-command-parsing-beta
Fix double dash argument forwarding on Win32 PowerShell
This commit is contained in:
commit
b6b880d21a
1 changed files with 63 additions and 16 deletions
|
|
@ -6,27 +6,27 @@ $safeChainBin = Join-Path (Join-Path $HOME '.safe-chain') 'bin'
|
||||||
$env:PATH = "$env:PATH$pathSeparator$safeChainBin"
|
$env:PATH = "$env:PATH$pathSeparator$safeChainBin"
|
||||||
|
|
||||||
function npx {
|
function npx {
|
||||||
Invoke-WrappedCommand "npx" $args
|
Invoke-WrappedCommand "npx" $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
function yarn {
|
function yarn {
|
||||||
Invoke-WrappedCommand "yarn" $args
|
Invoke-WrappedCommand "yarn" $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
function pnpm {
|
function pnpm {
|
||||||
Invoke-WrappedCommand "pnpm" $args
|
Invoke-WrappedCommand "pnpm" $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
function pnpx {
|
function pnpx {
|
||||||
Invoke-WrappedCommand "pnpx" $args
|
Invoke-WrappedCommand "pnpx" $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
function bun {
|
function bun {
|
||||||
Invoke-WrappedCommand "bun" $args
|
Invoke-WrappedCommand "bun" $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
function bunx {
|
function bunx {
|
||||||
Invoke-WrappedCommand "bunx" $args
|
Invoke-WrappedCommand "bunx" $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
function npm {
|
function npm {
|
||||||
|
|
@ -37,37 +37,37 @@ function npm {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Invoke-WrappedCommand "npm" $args
|
Invoke-WrappedCommand "npm" $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
function pip {
|
function pip {
|
||||||
Invoke-WrappedCommand "pip" $args
|
Invoke-WrappedCommand "pip" $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
function pip3 {
|
function pip3 {
|
||||||
Invoke-WrappedCommand "pip3" $args
|
Invoke-WrappedCommand "pip3" $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
function uv {
|
function uv {
|
||||||
Invoke-WrappedCommand "uv" $args
|
Invoke-WrappedCommand "uv" $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
function poetry {
|
function poetry {
|
||||||
Invoke-WrappedCommand "poetry" $args
|
Invoke-WrappedCommand "poetry" $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
# `python -m pip`, `python -m pip3`.
|
# `python -m pip`, `python -m pip3`.
|
||||||
function python {
|
function python {
|
||||||
Invoke-WrappedCommand 'python' $args
|
Invoke-WrappedCommand 'python' $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
# `python3 -m pip`, `python3 -m pip3'.
|
# `python3 -m pip`, `python3 -m pip3'.
|
||||||
function python3 {
|
function python3 {
|
||||||
Invoke-WrappedCommand 'python3' $args
|
Invoke-WrappedCommand 'python3' $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
function pipx {
|
function pipx {
|
||||||
Invoke-WrappedCommand "pipx" $args
|
Invoke-WrappedCommand "pipx" $args $MyInvocation.Line $MyInvocation.OffsetInLine
|
||||||
}
|
}
|
||||||
|
|
||||||
function Write-SafeChainWarning {
|
function Write-SafeChainWarning {
|
||||||
|
|
@ -108,13 +108,60 @@ function Invoke-RealCommand {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Get-ReconstructedArguments {
|
||||||
|
param(
|
||||||
|
[string]$RawLine,
|
||||||
|
[int]$RawOffset
|
||||||
|
)
|
||||||
|
|
||||||
|
if (-not $RawLine) { return $null }
|
||||||
|
|
||||||
|
$tokens = [System.Management.Automation.PSParser]::Tokenize($RawLine, [ref]$null)
|
||||||
|
$newArgs = @()
|
||||||
|
$foundCommand = $false
|
||||||
|
|
||||||
|
foreach ($t in $tokens) {
|
||||||
|
if (-not $foundCommand) {
|
||||||
|
if ($t.Start -eq ($RawOffset - 1)) { $foundCommand = $true }
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($t.Type -eq 'Operator' -and $t.Content -match '[|;&]') { break }
|
||||||
|
|
||||||
|
# Stop if complex variable expansion is used
|
||||||
|
if ($t.Type -eq 'Variable' -or $t.Type -eq 'Group' -or $t.Type -eq 'SubExpression') {
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
|
||||||
|
$newArgs += $t.Content
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($foundCommand) {
|
||||||
|
return ,$newArgs
|
||||||
|
}
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
|
||||||
function Invoke-WrappedCommand {
|
function Invoke-WrappedCommand {
|
||||||
param(
|
param(
|
||||||
[string]$OriginalCmd,
|
[string]$OriginalCmd,
|
||||||
[string[]]$Arguments
|
[string[]]$Arguments,
|
||||||
|
[string]$RawLine = $null,
|
||||||
|
[int]$RawOffset = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
if (Test-CommandAvailable "safe-chain") {
|
# Use raw line parsing to recover arguments like '--' that PowerShell consumes
|
||||||
|
if ($RawLine) {
|
||||||
|
$reconstructedArgs = Get-ReconstructedArguments $RawLine $RawOffset
|
||||||
|
if ($null -ne $reconstructedArgs) {
|
||||||
|
$Arguments = $reconstructedArgs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($isWindowsPlatform -and (Test-CommandAvailable "safe-chain.cmd")) {
|
||||||
|
& safe-chain.cmd $OriginalCmd @Arguments
|
||||||
|
}
|
||||||
|
elseif (Test-CommandAvailable "safe-chain") {
|
||||||
& safe-chain $OriginalCmd @Arguments
|
& safe-chain $OriginalCmd @Arguments
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue