Skip to content

Web Cmdlets don't allow $null username in a PSCredential for basic auth #9017

Closed

Description

@burkasaurusrex

It would be great if there was a way to pass a PSCredential with a $null username to the web cmdlets. I ran into this with an API that has no username, just a API key that is used for basic auth.

Steps to reproduce

$TestCreds = Get-Credential

if ($TestCreds.UserName) {
    $TestCreds = New-Object System.Management.Automation.PSCredential -ArgumentList (
        [pscustomobject] @{ 
            UserName = $null
            Password = $TestCreds.Password
        }
    )
}

Invoke-RestMethod -Uri 'https://httpbin.org/headers' `
                  -Authentication Basic `
                  -Credential $TestCreds

Expected behavior

$TestCreds = Get-Credential

if ($TestCreds.UserName) {
    $TestCreds = New-Object System.Management.Automation.PSCredential -ArgumentList (
        [pscustomobject] @{ 
            UserName = ' '
            Password = $TestCreds.Password
        }
    )
}

Invoke-RestMethod -Uri 'https://httpbin.org/headers' `
                  -Authentication Basic `
                  -Credential $TestCreds

There is a workaround where you pass a username with a single space. The space is base64 encoded however. So, in the example below, I passed 'username' and 'password' as the creds. You'll notice that 'IDpwYXNzd29yZA==' was passed in the basic auth header, which decodes to ' :password' (note the leading space).

PowerShell credential request
Enter your credentials.
User: username
Password for user username: ********

headers
-------
@{Authorization=Basic IDpwYXNzd29yZA==; Host=httpbin.org; User-Agent=Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.17134; en-US) PowerShell/6.1.2}

Actual behavior

Invoke-RestMethod : Object reference not set to an instance of an object.
At C:\Users\bdavis\Documents\Scripts\temp_01.ps1:12 char:1
+ Invoke-RestMethod -Uri 'https://httpbin.org/headers' `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Environment data

Name                           Value
----                           -----
PSVersion                      6.1.2
PSEdition                      Core
GitCommitId                    6.1.2
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    Issue-Question
    on Mar 1, 2019
  2. markekraus commented on Mar 1, 2019

    @markekraus
    Contributor

    This seems more like an abuse of the PSCrednetial API which is not supposed to work with null usernames... but.. we could maybe address this edge case here

    really though, I think even needing an empty username with basic auth is an edge case... in most APIs where a null/empty basic auth user is acceptable, so to us any string in the username (e.g. Azure DevOps REST API).

  3. burkasaurusrex commented on Mar 13, 2019

    @burkasaurusrex
    Author

    Yea, no argument that needing an empty username with basic auth is an edge case (ha, not the only peculiarity I'm dealing with for this API...).

    I didn't realize that you shouldn't be able to make a PSCredential with a $null username? If you're not supposed to be able to create a PSCredential with a $null username, then I don't see any reason to handle the above scenario in the web cmdlets.

    It seems like you should be able to create a PSCredential with a $null username, but that sounds more like a design choice than a bug. I could see something like Get-Credential -AllowNullUsername or something, as well as a PSCredential constructor that just takes a SecureString.

    The counter argument would be that a PSCredential with a $null username is really just a SecureString. It seems more secure to keep the password in a PSCredential and pass that variable to various cmdlets instead of decrypting the password, storing it in an intermediary variable unencrypted (and consequently memory), and then passing it as a parameter.

    Hopefully that makes sense.

  4. vexx32 commented on Mar 13, 2019

    @vexx32
    Collaborator

    There's no need to decrypt the securestring, since those can be passed around to cmdlets if you choose. The barrier there is mainly just that most cmdlets accept the PSCredential, but not securestring directly.

    If the username isn't going to be used, you can generally just use a random username, even if it's literally just a single space.

  5. burkasaurusrex commented on Mar 13, 2019

    @burkasaurusrex
    Author

    Thanks for the comment. Yea, you'll notice I called out that workaround in the original issue. The point of my last comment is that a random username or a ' ' username is not the same as a $null username.

    I don't believe there's currently a way to feed a SecureString to Invoke-WebRequest -Credential without a) decrypting the SecureString or b) passing a non-null username?

  6. markekraus commented on May 5, 2019

    @markekraus
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-QuestionResolution-FixedWG-Cmdlets

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions