Skip to content

Feature Request: Convert SecureString to string #11067

Closed

Description

Summary of the new feature/enhancement

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.

Proposed technical implementation details (optional)

Proposal is to add -AsPlainText to ConvertFrom-SecureString cmdlet.

Activity

  1. added
    Issue-Enhancement
    Review - Committee
    on Nov 14, 2019
  2. iSazonov commented on Nov 14, 2019

    @iSazonov
    Collaborator

    In the Engine if we do the convert we clear memory right after using. How do we do this in scripts?

  3. added
    WG-Security
    on Nov 14, 2019
  4. SteveL-MSFT commented on Nov 15, 2019

    @SteveL-MSFT
    MemberAuthor

    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.

  5. dragonwolf83 commented on Nov 15, 2019

    @dragonwolf83

    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

    gpg --passphrase ( $securePass | ConvertFrom-SecureString -AsPlainText )

    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

    $nonsecurePass = $securePass | ConvertFrom-SecureString -AsPlainText
    gpg --passphrase $nonsecurePass
    
    Start-Sleep -Seconds 300
    $nonsecurePass

    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

  6. rkeithhill commented on Nov 15, 2019

    @rkeithhill
    Collaborator

    #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.

  7. rjmholt commented on Nov 19, 2019

    @rjmholt
    Collaborator

    If we add a method to [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
    • Troy Campbell (@dragonwolf83)'s affine-typed string is a nice idea too

    Ideally we have one way to do this and it reuses the securestring type, since that's what everyone is familiar with and expects to work.

  8. added
    Committee-Reviewed
    and removed
    Review - Committee
    on Nov 20, 2019
  9. SteveL-MSFT commented on Nov 20, 2019

    @SteveL-MSFT
    MemberAuthor

    @PowerShell/powershell-committee reviewed this and accepts the -AsPlainText switch for ConvertFrom-SecureString

  10. SteveL-MSFT commented on Nov 21, 2019

    @SteveL-MSFT
    MemberAuthor

    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?

  11. vexx32 commented on Nov 21, 2019

    @vexx32
    Collaborator

    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. 🤷‍♂

  12. iSazonov commented on Nov 22, 2019

    @iSazonov
    Collaborator

    I'd remove "force" from ConvertTo-SecureString for symmetry.

  13. rjmholt commented on Nov 22, 2019

    @rjmholt
    Collaborator

    ConvertTo-SecureString probably needs to keep -Force for compatibility (at least in 7), but we should make it a no-op

  14. added
    Review - Committee
    and removed
    Committee-Reviewed
    on Nov 22, 2019
  15. SteveL-MSFT commented on Dec 4, 2019

    @SteveL-MSFT
    MemberAuthor

    @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

  16. added
    Committee-Reviewed
    and removed
    Review - Committee
    on Dec 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Committee-ReviewedIssue-EnhancementResolution-FixedWG-Security

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions