Harden password prompt against non-secure values

This commit is contained in:
2026-02-02 16:57:36 +01:00
parent eff1df44dc
commit e365002373

View File

@@ -51,6 +51,9 @@ function Read-PlainOrPrompt([string]$Value, [string]$Prompt, [bool]$Secure = $fa
if ($Value) { return $Value } if ($Value) { return $Value }
if ($Secure) { if ($Secure) {
$secure = Read-Host -Prompt $Prompt -AsSecureString $secure = Read-Host -Prompt $Prompt -AsSecureString
if (-not ($secure -is [System.Security.SecureString])) {
$secure = ConvertTo-SecureString -String ([string]$secure) -AsPlainText -Force
}
$ptr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure) $ptr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
try { return [Runtime.InteropServices.Marshal]::PtrToStringUni($ptr) } try { return [Runtime.InteropServices.Marshal]::PtrToStringUni($ptr) }
finally { finally {