Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/System.Management.Automation/engine/Credential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ private static bool IsValidUserName(string input,
out string user,
out string domain)
{
if (String.IsNullOrEmpty(input))
{
user = domain = null;
return false;
}

SplitUserDomain(input, out user, out domain);

if ((user == null) ||
Expand Down
6 changes: 6 additions & 0 deletions test/powershell/engine/Basic/Credential.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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.

# We should explicitly check that the expression returns $null
[PSCredential]::Empty.GetNetworkCredential() | Should Be $null
}
}