The command-safety hook only checked the first word of each segment, so destructive commands inside if/for/while blocks, subshells, braces, PowerShell script blocks, or behind wrapper options (xargs -0, sudo -E, timeout 30) ran unchecked. A comment with an apostrophe, an escaped quote, or a line continuation also hid the following command. Skip shell keywords, split on braces, drop comments, honor backslash escapes, join continued lines, and check every suffix after a wrapper. CLI mode also strips the byte order mark that Windows PowerShell 5.1 adds to piped stdin, which let every command pass there. with-credential.ps1 returned the child's exit code from a function, so the child's stdout was captured and the exit code became 0. Run the child as a statement and exit with its code. add-credentials now refreshes the installed helper on every run so fixes reach existing machines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
40 lines
1.5 KiB
PowerShell
40 lines
1.5 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[ValidateSet('Codex', 'Claude', 'Both')][string]$Client,
|
|
[string]$Key,
|
|
[switch]$ValueFromStdin,
|
|
[string]$HomePath = [Environment]::GetFolderPath('UserProfile')
|
|
)
|
|
$ErrorActionPreference = 'Stop'
|
|
$root = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
|
|
. (Join-Path $root 'scripts/lib/install-targets.ps1')
|
|
$selectedClient = $Client
|
|
$selectedKey = $Key
|
|
. (Join-Path $root 'scripts/lib/with-credential.ps1')
|
|
$Client = $selectedClient
|
|
$Key = $selectedKey
|
|
|
|
if (-not $Client) { $Client = Read-InstallClient -Client $Client }
|
|
if (-not $Key) { $Key = Read-Host 'Credential key' }
|
|
Assert-CredentialKey $Key
|
|
if (-not [System.IO.Path]::IsPathRooted($HomePath)) { throw 'HomePath must be absolute.' }
|
|
if ($ValueFromStdin) {
|
|
$text = [Console]::In.ReadToEnd().TrimEnd("`r", "`n")
|
|
$Value = ConvertTo-SecureString $text -AsPlainText -Force
|
|
$text = $null
|
|
} else {
|
|
$Value = Read-Host 'Credential value' -AsSecureString
|
|
}
|
|
|
|
$bin = Join-Path $HomePath '.my-ai-configuration/bin'
|
|
New-Item -ItemType Directory -Path $bin -Force | Out-Null
|
|
foreach ($source in @('with-credential.ps1', 'with-credential.sh')) {
|
|
# Always refresh so fixes to the helper reach machines that already have it.
|
|
Copy-Item -LiteralPath (Join-Path $root "scripts/lib/$source") -Destination (Join-Path $bin $source) -Force
|
|
}
|
|
foreach ($target in $(if ($Client -eq 'Both') { @('Codex', 'Claude') } else { @($Client) })) {
|
|
Set-ManagedCredential $target $Key $Value
|
|
Write-Output "Credential stored for $($target.ToLowerInvariant())."
|
|
}
|
|
$Value = $null
|