Skip to content

GetNetworkCredential() returns null if PSCredential has null or empty user name - #4697

Merged
Travis Plunk (TravisEz13) merged 2 commits into
PowerShell:masterfrom
iSazonov:credential-empty
Sep 1, 2017
Merged

GetNetworkCredential() returns null if PSCredential has null or empty user name#4697
Travis Plunk (TravisEz13) merged 2 commits into
PowerShell:masterfrom
iSazonov:credential-empty

Conversation

@iSazonov

Copy link
Copy Markdown
Collaborator

Fix #4696

Problem descriptions

System.Management.Automation.PSCredential allow user name being empty but explicit cast [pscredential]::Empty.GetNetworkCredential() throws.

Fix

GetNetworkCredential() returns null if PSCredential has null or empty user name

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this should return $null rather than throw a NR exception. I have just one minor test comment.

Describe "Credential tests" -Tags "CI" {
It "Explicit cast for an empty credential returns null" {
# We should explicitly check that the expression returns $null
[PSCredential]::Empty.GetNetworkCredential() -eq $null | Should Be $true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the test be: "[PSCredential]::Empty.GetNetworkCredential() | Should Be $Null"?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A agree this would make any error, have more information to debug the issue. Therefore making a rerun less likely to be needed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@TravisEz13

Copy link
Copy Markdown
Member

@TravisEz13 Travis Plunk (TravisEz13) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a comment. I think at least one other test case needs to be added.

@@ -0,0 +1,6 @@
Describe "Credential tests" -Tags "CI" {
It "Explicit cast for an empty credential returns null" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a case for an empty username? How do you get the password from the object if GetNetworkCredential returns $null

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PSCredential constructor fails if username is empty. The only way to get an empty credentials is [PSCredential]::Empty. And if we can [PSCredential]::Empty any methods shouldn't throw for the empty credential - we're just fix the issue.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, you are able to create empty PSCredential instances with a public constructor PSCredential(psobject pso)

$psobj = [pscustomobject] @{}
$cred = [pscredential]::new($psobj)

The current implementation of GetNetworkCredential() doesn't allow returning a null value -- it throws if the user name is invalid. I think we should keep it that way and throw exception when user name is null or empty.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we allow the sample? It seems constructor should throw in the case.
On the other hand, [PSCredential]::Empty is a valid object - why any its method should throw?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we allow the sample?

Do you mean the XML comment? I missed the comment, and it looks totally fine to return null then :)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant your sample:

$psobj = [pscustomobject] @{}
$cred = [pscredential]::new($psobj)

Why the constructor don't throw? It seems the constructor should throw.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the original intention, but changing this behavior would be a breaking change for sure -- it makes something that works stop working. It feels insignificant to me, so I don't think it's worth to be fixed. But you can go both ways -- it's insignificant so a breaking change might not matter.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the PR I think we can leave the constructor as is.

Please clarify what is your conclusion about the PR fix?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the existing XML comments for GetNetworkCredential(), I withdraw my comment about making GetNetworkCredential throw. Returning null is totally fine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TravisEz13
Travis Plunk (TravisEz13) dismissed their stale review September 1, 2017 18:48

Agree with the consensus

@TravisEz13
Travis Plunk (TravisEz13) merged commit 58a296e into PowerShell:master Sep 1, 2017
@iSazonov
Ilya (iSazonov) deleted the credential-empty branch September 4, 2017 04:25
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj/PowerShell that referenced this pull request Aug 6, 2026
…r empty user name (PowerShell#4697)

* GetNetworkCredential() returns null if PSCredential has empty user name

* Fix test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants