From 0e0d5bc7d741772b3b3bb01ac425618b69fbd414 Mon Sep 17 00:00:00 2001 From: jpol34 Date: Wed, 16 Sep 2026 03:14:43 -0500 Subject: [PATCH] Scope stdout-safe warning suppression to non-interactive sessions only Follow-up to #20: that fix set \$WarningPreference = 'SilentlyContinue' for every invocation, which also silences legitimate warnings for a human typing at a real terminal - the contamination bug it fixes only manifests when stdout is redirected (SSH, a shell script), not interactively. Moved Test-InteractiveTerminal earlier in the file (was only defined later, for Assert-RevealAllowed) and gated the suppression on it, matching the same interactive/non-interactive distinction --real already draws. Also reworded the comment to state the current invariant instead of narrating the bug's history, per doc-comment discipline. Verified: 43/43 Pester tests pass; confirmed on the Pi that `strongbox get POSTGRES_USER --real` still returns clean bytes non-interactively. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01WGra6GqN8qAxEDHbhmJwP2 --- bin/strongbox.ps1 | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/bin/strongbox.ps1 b/bin/strongbox.ps1 index 6579282..13f794f 100644 --- a/bin/strongbox.ps1 +++ b/bin/strongbox.ps1 @@ -11,14 +11,20 @@ param( [Parameter(Position = 1, ValueFromRemainingArguments)] [string[]] $Rest = @() ) $ErrorActionPreference = 'Stop' + +function Test-InteractiveTerminal { + -not ([Console]::IsInputRedirected -or [Console]::IsOutputRedirected) +} + # get/reveal --stdout are documented as piping-safe ("Print a secret's value to stdout, for -# scripting/piping"). PowerShell's warning stream writes ANSI-colored text directly onto stdout -# in a non-interactive session (no real console attached, e.g. invoked over SSH or from a shell -# script) rather than staying separate from it - the module's own unapproved-verb warning on -# import was landing ahead of the actual secret value in every `strongbox get` capture. Suppress -# warnings for the whole CLI, not just at import, since any future warning anywhere in this -# script's execution would hit the same contamination. -$WarningPreference = 'SilentlyContinue' +# scripting/piping"), which only holds if stdout carries nothing but the secret. In a +# non-interactive session (no real console - SSH, a shell script), PowerShell's warning stream +# writes ANSI-colored text directly onto stdout instead of staying separate from it, so any +# warning anywhere in this script's execution would corrupt a piped capture. A human at a real +# interactive prompt still sees warnings normally. +if (-not (Test-InteractiveTerminal)) { + $WarningPreference = 'SilentlyContinue' +} try { Import-Module Strongbox -ErrorAction Stop @@ -78,10 +84,6 @@ strongbox [args] $script:SandboxSecretName = 'tools.StrongboxSelfTest' -function Test-InteractiveTerminal { - -not ([Console]::IsInputRedirected -or [Console]::IsOutputRedirected) -} - function Assert-RevealAllowed { <# Prevents a script or automated agent from grabbing a real secret when it's only trying to