diff --git a/src/shell-integration/startup-scripts/init-pwsh.ps1 b/src/shell-integration/startup-scripts/init-pwsh.ps1 index 58a19c9..7d85ee1 100644 --- a/src/shell-integration/startup-scripts/init-pwsh.ps1 +++ b/src/shell-integration/startup-scripts/init-pwsh.ps1 @@ -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 { param([string]$Command) try { Get-Command $Command -ErrorAction Stop | Out-Null return $true - } catch { + } + catch { return $false } } @@ -19,62 +33,6 @@ function Invoke-RealCommand { $realCommand = Get-Command -Name $Command -CommandType Application | Select-Object -First 1 if ($realCommand) { & $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 } } @@ -84,14 +42,14 @@ function Invoke-WrappedCommand { [string]$AikidoCmd, [string[]]$Arguments ) - - $installResult = Install-IfCommandNotFound $AikidoCmd - - if ($installResult -eq 2) { - Invoke-RealCommand $OriginalCmd $Arguments - } else { + + if (Test-CommandAvailable $AikidoCmd) { & $AikidoCmd @Arguments } + else { + Write-SafeChainWarning $OriginalCmd + Invoke-RealCommand $OriginalCmd $Arguments + } } function npx {