Repository navigation
Closed
Labels
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues
Description
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 $TestCredsExpected 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 $TestCredsThere 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
Metadata
Metadata
Assignees
Labels
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues
Activity
markekraus commentedon Mar 1, 2019
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
PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Line 805 in ff3edaa
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).
burkasaurusrex commentedon Mar 13, 2019
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
PSCredentialwith a$nullusername? If you're not supposed to be able to create aPSCredentialwith 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
PSCredentialwith a$nullusername, but that sounds more like a design choice than a bug. I could see something likeGet-Credential -AllowNullUsernameor something, as well as aPSCredentialconstructor that just takes aSecureString.The counter argument would be that a
PSCredentialwith a$nullusername is really just aSecureString. It seems more secure to keep the password in aPSCredentialand 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.
vexx32 commentedon Mar 13, 2019
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.
burkasaurusrex commentedon Mar 13, 2019
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
$nullusername.I don't believe there's currently a way to feed a
SecureStringtoInvoke-WebRequest -Credentialwithout a) decrypting theSecureStringor b) passing a non-null username?markekraus commentedon May 5, 2019
burkasaurusrex PR #9536