Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2017

Conversation

iSazonov
Copy link
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

@vors vors removed their request for review August 29, 2017 16:19
@lzybkr lzybkr requested a review from PaulHigin August 29, 2017 22:48
@lzybkr lzybkr assigned TravisEz13 and unassigned lzybkr Aug 29, 2017
Copy link
Contributor

@PaulHigin PaulHigin left a comment

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
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
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
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
Member

@Indhukrishna FYI

Copy link
Member

@TravisEz13 TravisEz13 left a comment

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
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
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
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
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
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
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
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
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
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
Contributor

@PaulHigin PaulHigin left a comment

Choose a reason for hiding this comment

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

LGTM

@TravisEz13 TravisEz13 dismissed their stale review September 1, 2017 18:48

Agree with the consensus

@TravisEz13 TravisEz13 merged commit 58a296e into PowerShell:master Sep 1, 2017
@iSazonov iSazonov deleted the credential-empty branch September 4, 2017 04:25
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