Skip to content

Commit 03f10f0

Browse files
SteveL-MSFTadityapatwardhan
authored andcommitted
Add -AsPlainText to ConvertFrom-SecureString (PowerShell#11142)
1 parent 5605bf4 commit 03f10f0

2 files changed

Lines changed: 24 additions & 18 deletions

File tree

src/Microsoft.PowerShell.Security/security/SecureStringCommands.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ public SecureString SecureString
139139
}
140140
}
141141

142+
/// <summary>
143+
/// Gets or sets a switch to get the secure string as plain text.
144+
/// </summary>
145+
[Parameter(ParameterSetName = "AsPlainText")]
146+
public SwitchParameter AsPlainText { get; set; }
147+
142148
/// <summary>
143149
/// Processes records from the input pipeline.
144150
/// For each input object, the command encrypts
@@ -165,6 +171,19 @@ protected override void ProcessRecord()
165171
{
166172
encryptionResult = SecureStringHelper.Encrypt(SecureString, Key);
167173
}
174+
else if (AsPlainText)
175+
{
176+
IntPtr valuePtr = IntPtr.Zero;
177+
try
178+
{
179+
valuePtr = Marshal.SecureStringToGlobalAllocUnicode(SecureString);
180+
exportedString = Marshal.PtrToStringUni(valuePtr);
181+
}
182+
finally
183+
{
184+
Marshal.ZeroFreeGlobalAllocUnicode(valuePtr);
185+
}
186+
}
168187
else
169188
{
170189
exportedString = SecureStringHelper.Protect(SecureString);
@@ -342,22 +361,8 @@ protected override void ProcessRecord()
342361
}
343362
else
344363
{
345-
if (!Force)
346-
{
347-
string error =
348-
SecureStringCommands.ForceRequired;
349-
Exception e = new ArgumentException(error);
350-
WriteError(new ErrorRecord(e, "ImportSecureString_ForceRequired", ErrorCategory.InvalidArgument, null));
351-
}
352-
else
353-
{
354-
// The entire purpose of the SecureString is to prevent a secret from being
355-
// permanently stored in memory as a .Net string. If they use the
356-
// -AsPlainText and -Force flags, they consciously have made the decision to be OK
357-
// with that.
358-
importedString = new SecureString();
359-
foreach (char currentChar in String) { importedString.AppendChar(currentChar); }
360-
}
364+
importedString = new SecureString();
365+
foreach (char currentChar in String) { importedString.AppendChar(currentChar); }
361366
}
362367
}
363368
catch (ArgumentException e)

test/powershell/Modules/Microsoft.PowerShell.Security/SecureString.Tests.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ Describe "SecureString conversion tests" -Tags "CI" {
2222
$ss = ConvertTo-SecureString -AsPlainText -Force abcd
2323
$ss | Should -BeOfType SecureString
2424
}
25+
2526
It "can convert back from a secure string" {
2627
$secret = "abcd"
2728
$ss1 = ConvertTo-SecureString -AsPlainText -Force $secret
28-
$ss2 = convertfrom-securestring $ss1 | convertto-securestring
29-
[pscredential]::New("user",$ss2).GetNetworkCredential().Password | Should -Be $secret
29+
$ss2 = ConvertFrom-SecureString $ss1 | ConvertTo-SecureString
30+
$ss2 | ConvertFrom-SecureString -AsPlainText | Should -Be $secret
3031
}
3132
}

0 commit comments

Comments
 (0)