mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
Powershell: only print warning when safe-chain is unavailable
This commit is contained in:
parent
823fa2936e
commit
0e6d96fe1d
1 changed files with 21 additions and 63 deletions
|
|
@ -1,10 +1,24 @@
|
||||||
|
function Write-SafeChainWarning {
|
||||||
|
param([string]$Command)
|
||||||
|
|
||||||
|
# PowerShell equivalent of ANSI color codes: yellow background, black text for "Warning:"
|
||||||
|
Write-Host "Warning:" -BackgroundColor Yellow -ForegroundColor Black -NoNewline
|
||||||
|
Write-Host " safe-chain is not available to protect you from installing malware. $Command will be run directly."
|
||||||
|
|
||||||
|
# Cyan text for the install command
|
||||||
|
Write-Host "Install safe-chain by using " -NoNewline
|
||||||
|
Write-Host "npm install -g @aikidosec/safe-chain" -ForegroundColor Cyan -NoNewline
|
||||||
|
Write-Host "."
|
||||||
|
}
|
||||||
|
|
||||||
function Test-CommandAvailable {
|
function Test-CommandAvailable {
|
||||||
param([string]$Command)
|
param([string]$Command)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Get-Command $Command -ErrorAction Stop | Out-Null
|
Get-Command $Command -ErrorAction Stop | Out-Null
|
||||||
return $true
|
return $true
|
||||||
} catch {
|
}
|
||||||
|
catch {
|
||||||
return $false
|
return $false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -19,62 +33,6 @@ function Invoke-RealCommand {
|
||||||
$realCommand = Get-Command -Name $Command -CommandType Application | Select-Object -First 1
|
$realCommand = Get-Command -Name $Command -CommandType Application | Select-Object -First 1
|
||||||
if ($realCommand) {
|
if ($realCommand) {
|
||||||
& $realCommand.Source @Arguments
|
& $realCommand.Source @Arguments
|
||||||
} else {
|
|
||||||
# Fallback: try to call the .cmd version directly
|
|
||||||
& "$Command.cmd" @Arguments
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function Install-IfCommandNotFound {
|
|
||||||
param([string]$Command)
|
|
||||||
|
|
||||||
# Check if the command already exists
|
|
||||||
if (Test-CommandAvailable $Command) {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if Node.js version is below 18
|
|
||||||
# Safe-chain requires Node.js 18 or higher
|
|
||||||
try {
|
|
||||||
$nodeVersion = (node -v) -replace 'v', '' | ForEach-Object { $_.Split('.')[0] }
|
|
||||||
if ([int]$nodeVersion -lt 18) {
|
|
||||||
return 2
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
return 2
|
|
||||||
}
|
|
||||||
|
|
||||||
# Command not found, ask user if they want to install safe-chain
|
|
||||||
$response = Read-Host "The command '$Command' is not available. Do you want to install safe-chain to provide it? (y/N)"
|
|
||||||
|
|
||||||
if ($response -match '^[Yy]$') {
|
|
||||||
Write-Host "Installing safe-chain..."
|
|
||||||
$installResult = Install-SafeChain
|
|
||||||
|
|
||||||
if ($installResult -ne 0) {
|
|
||||||
Write-Host "`nFailed to install safe-chain. Exiting."
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0
|
|
||||||
} else {
|
|
||||||
Write-Host "Skipping safe-chain installation. Using original command instead."
|
|
||||||
return 2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function Install-SafeChain {
|
|
||||||
try {
|
|
||||||
Invoke-RealCommand "npm" @("install", "-g", "@aikidosec/safe-chain") | Out-Null
|
|
||||||
|
|
||||||
if ($LASTEXITCODE -ne 0) {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
Write-Host "------"
|
|
||||||
return 0
|
|
||||||
} catch {
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -85,13 +43,13 @@ function Invoke-WrappedCommand {
|
||||||
[string[]]$Arguments
|
[string[]]$Arguments
|
||||||
)
|
)
|
||||||
|
|
||||||
$installResult = Install-IfCommandNotFound $AikidoCmd
|
if (Test-CommandAvailable $AikidoCmd) {
|
||||||
|
|
||||||
if ($installResult -eq 2) {
|
|
||||||
Invoke-RealCommand $OriginalCmd $Arguments
|
|
||||||
} else {
|
|
||||||
& $AikidoCmd @Arguments
|
& $AikidoCmd @Arguments
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
Write-SafeChainWarning $OriginalCmd
|
||||||
|
Invoke-RealCommand $OriginalCmd $Arguments
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function npx {
|
function npx {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue