mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Attempted fix for powershell swallowing '--'
This commit is contained in:
parent
3210b68b43
commit
b7f793f1f9
1 changed files with 50 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 {
|
||||||
|
|
@ -111,10 +111,44 @@ function Invoke-RealCommand {
|
||||||
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) {
|
||||||
|
$tokens = [System.Management.Automation.PSParser]::Tokenize($RawLine, [ref]$null)
|
||||||
|
$newArgs = @()
|
||||||
|
$foundCommand = $false
|
||||||
|
$canUseRaw = $true
|
||||||
|
|
||||||
|
foreach ($t in $tokens) {
|
||||||
|
# Find the command token based on offset
|
||||||
|
if (-not $foundCommand) {
|
||||||
|
if ($t.Start -eq ($RawOffset - 1)) { $foundCommand = $true }
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
# Stop at command separators
|
||||||
|
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') {
|
||||||
|
$canUseRaw = $false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
$newArgs += $t.Content
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($foundCommand -and $canUseRaw) {
|
||||||
|
$Arguments = $newArgs
|
||||||
|
Write-Host "Safe-chain Powershell Wrapper: Reconstructed args: $($Arguments -join ' ')"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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