You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature Request: Convert SecureString to string #11067
It's becoming more common for scripts to have a secret as a secure string, but needs to use it as a plain text string (like making a REST call). Anyone can search the web to find the APIs needed to convert the SecureString to plain text. It seems we should just make this a part of PowerShell so people aren't blindly copying and pasting script samples from websites.
Ilya (@iSazonov) that's a good point, but I think we should see what comes out of https://github.com/dotnet/corefx/issues/40393. Ideally, I think there should be a SensitiveString type that clears memory when out of scope but is just plain text.
A secure solution would not keep the plain text password in memory for very long. It really should only be exposed for the immediate need and be securely erased from memory after use.
For example, using a password with a 3rd party tool
This doesn't save the password so it shouldn't stay in memory from Powershell but it's too wordy. If we add a method to [SecureString], it can be simplified:
gpg --passphrase ( $securepass.ToPlainText() )`
This is similar to how you get the password with [PSCredential].GetNetworkCredential().Password with the difference it returns only a string and not an object that you have to then select.
Either way, it doesn't prevent someone from saving to a variable
Now, we have saved the password in memory until we replace it, manually delete it, or close powershell. Since authentication usually happens at the beginning, that password will live for the entire duration of the script.
You also tend to set variables in an interactive session and keep them open for long time to incrementally work on a script or problem.
Proposal
Add a new string type, [TemporaryString], that is use-once and/or time-limited. When the condition is met, the contents are erased from memory. Have the new -AsPlainText option return this new type instead of a regular string.
This will allow the last example to work without having the password live for a long time.
If a user tries to reuse the [TemporaryString], throw an error indicating it was securely erased and needs to be retrieved again.
# Convert the [SecureString] to [TemporaryString]$nonsecurePass=$securePass|ConvertFrom-SecureString-AsPlainText
# Pass the [TemporaryString] to tool
gpg --passphrase $nonsecurePass# Throws Error because password was already used.
gpg --passphrase $nonsecurePass# If reuse for several commands are needed
gpg --passphrase ( $securePass.ToPlainText() )
gpg --passphrase ( $securePass.ToPlainText() )
gpg --passphrase ( $securePass|ConvertFrom-SecureString-AsPlainText )
-AsPlainText may be better named more closely to the type if this proposal is accepted. AsTemporaryString or AsTempString
#10847 is related to this. It is another way of getting a secret entered into PowerShell so that it can be used with external commands & REST websites that don't work with SecureString.
That type comes from .NET, so we'd need to do trickery to surface the method. If that trickery is in script, it might have implications for how we handle the value.
One difficulty with strings is that they may be interned. This is unlikely with a value generated by a securestring reversal call, but (1) once the string comes out, the caller can do what they wish with it (including call [string]::Intern($secret)) and (2) .NET could intern a string any time it likes (although I don't think it does for anything other than C# string literals currently). And once a string is interned, it's unlikely to be GC'd. Even without this, trying to make guarantees about the lifetime of string values with the GC is something I'm not sure we can do.
For passing string values into APIs requiring strings, there's not much we can do in terms of protecting the lifetime of the string value; those APIs ultimately get a string and can do whatever they like with them, so you must trust them.
I think the real place to pick up slack is with usability, particularly:
Masking input so it can't be read off the screen or in logs
Preventing formatting output to the screen/logs unless deliberate
A point of feedback is whether -AsPlainText should require -Force like how ConvertTo-SecureString -AsPlainText does require it. My personal opinion is that adding more -Force to cmdlets just forces people to set Force = $true in their $PSDefaultParameterValues and dilutes the meaning of -Force. If such a thing is needed, I would prefer to have it confirm and have users override that. Since the cmdlet already conveys this is working with SecureString, it seems unnecessary to put up an artificial barrier to get it working. I don't believe it needs to have symmetry with ConvertTo-SecureString in this case. Thoughts?
Since in .NET Core I don't think we really have a way to guarantee the input is actually secure in any case... I don't see the need for -Force in either cmdlet at present. 🤷♂
@PowerShell/powershell-committee reviewed this, we agreed that -Force is not to be added to ConvertFrom-SecureString and make -Force on ConvertTo-SecureString a no-op for compatibility
Activity
iSazonov commentedon Nov 14, 2019
In the Engine if we do the convert we clear memory right after using. How do we do this in scripts?
SteveL-MSFT commentedon Nov 15, 2019
Ilya (@iSazonov) that's a good point, but I think we should see what comes out of https://github.com/dotnet/corefx/issues/40393. Ideally, I think there should be a SensitiveString type that clears memory when out of scope but is just plain text.
dragonwolf83 commentedon Nov 15, 2019
A secure solution would not keep the plain text password in memory for very long. It really should only be exposed for the immediate need and be securely erased from memory after use.
For example, using a password with a 3rd party tool
This doesn't save the password so it shouldn't stay in memory from Powershell but it's too wordy. If we add a method to
[SecureString], it can be simplified:This is similar to how you get the password with
[PSCredential].GetNetworkCredential().Passwordwith the difference it returns only a string and not an object that you have to then select.Either way, it doesn't prevent someone from saving to a variable
Now, we have saved the password in memory until we replace it, manually delete it, or close powershell. Since authentication usually happens at the beginning, that password will live for the entire duration of the script.
You also tend to set variables in an interactive session and keep them open for long time to incrementally work on a script or problem.
Proposal
Add a new string type,
[TemporaryString], that is use-once and/or time-limited. When the condition is met, the contents are erased from memory. Have the new-AsPlainTextoption return this new type instead of a regular string.This will allow the last example to work without having the password live for a long time.
If a user tries to reuse the
[TemporaryString], throw an error indicating it was securely erased and needs to be retrieved again.-AsPlainTextmay be better named more closely to the type if this proposal is accepted.AsTemporaryStringorAsTempStringrkeithhill commentedon Nov 15, 2019
#10847 is related to this. It is another way of getting a secret entered into PowerShell so that it can be used with external commands & REST websites that don't work with SecureString.
rjmholt commentedon Nov 19, 2019
That type comes from .NET, so we'd need to do trickery to surface the method. If that trickery is in script, it might have implications for how we handle the value.
One difficulty with strings is that they may be interned. This is unlikely with a value generated by a securestring reversal call, but (1) once the string comes out, the caller can do what they wish with it (including call
[string]::Intern($secret)) and (2) .NET could intern a string any time it likes (although I don't think it does for anything other than C# string literals currently). And once a string is interned, it's unlikely to be GC'd. Even without this, trying to make guarantees about the lifetime of string values with the GC is something I'm not sure we can do.For passing string values into APIs requiring strings, there's not much we can do in terms of protecting the lifetime of the string value; those APIs ultimately get a string and can do whatever they like with them, so you must trust them.
I think the real place to pick up slack is with usability, particularly:
Ideally we have one way to do this and it reuses the
securestringtype, since that's what everyone is familiar with and expects to work.SteveL-MSFT commentedon Nov 20, 2019
@PowerShell/powershell-committee reviewed this and accepts the
-AsPlainTextswitch forConvertFrom-SecureStringSteveL-MSFT commentedon Nov 21, 2019
A point of feedback is whether
-AsPlainTextshould require-Forcelike howConvertTo-SecureString -AsPlainTextdoes require it. My personal opinion is that adding more-Forceto cmdlets just forces people to setForce = $truein their$PSDefaultParameterValuesand dilutes the meaning of-Force. If such a thing is needed, I would prefer to have it confirm and have users override that. Since the cmdlet already conveys this is working with SecureString, it seems unnecessary to put up an artificial barrier to get it working. I don't believe it needs to have symmetry withConvertTo-SecureStringin this case. Thoughts?vexx32 commentedon Nov 21, 2019
Since in .NET Core I don't think we really have a way to guarantee the input is actually secure in any case... I don't see the need for
-Forcein either cmdlet at present. 🤷♂iSazonov commentedon Nov 22, 2019
I'd remove "force" from ConvertTo-SecureString for symmetry.
rjmholt commentedon Nov 22, 2019
ConvertTo-SecureStringprobably needs to keep-Forcefor compatibility (at least in 7), but we should make it a no-opSteveL-MSFT commentedon Dec 4, 2019
@PowerShell/powershell-committee reviewed this, we agreed that
-Forceis not to be added toConvertFrom-SecureStringand make-ForceonConvertTo-SecureStringa no-op for compatibility