Skip to content

Use type conversion in SSHConnection hashtables when value doesn't match expected type - #10720

Merged
Ilya (iSazonov) merged 2 commits into
PowerShell:masterfrom
SeeminglyScience:fix-ssh-connection-hashtable-psobject
Oct 15, 2019
Merged

Ilya (iSazonov) merged 2 commits into
PowerShell:masterfrom
SeeminglyScience:fix-ssh-connection-hashtable-psobject

Conversation

@SeeminglyScience

Copy link
Copy Markdown
Contributor

PR Summary

Use LanguagePrimitives.ConvertTo when reading a hashtable provided to the SSHConnection parameter.

PR Context

Currently if the hashtable value does not match the expected type exactly, an exception is thrown stating that the value was null.

Fixes #10687

PR Checklist

Use LanguagePrimitives.ConvertTo in GetSSHConnectionStringParameter and
GetSSHConnectionIntParameter.

@iSazonov Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add tests.

private static string GetSSHConnectionStringParameter(object param)
{
if (param is string paramValue && !string.IsNullOrEmpty(paramValue))
string paramValue = LanguagePrimitives.ConvertTo<string>(param);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ConvertTo() can throw PSInvalidCastException but we should keep PSArgumentException.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is there a circumstance where converting to string will throw?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, it is not common but possible (if someone creates ToString() method which throw).

}

throw new PSArgumentException(RemotingErrorIdStrings.InvalidSSHConnectionParameter);
return LanguagePrimitives.ConvertTo<int>(param);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The same.

@SeeminglyScience

Copy link
Copy Markdown
Contributor Author

Please add tests.

Any thoughts on that? If I try to test that the conversion works then we'll hit the interactive SSH prompt. I could give it a bad port and/or hostname, but that'll add ~5 seconds per test. If that's acceptable I'll add a few of those.

@iSazonov

Ilya (iSazonov) commented Oct 7, 2019 •

Copy link
Copy Markdown
Collaborator

I'd expect 2 tests for these two code paths modified.
Please use Feature and Slow tags in the tests.

@SeeminglyScience

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) I've added tests and normalized the exception type. In the tests I used port 49151 since it's reserved and shouldn't be in use, but that was the result of a quick search so I'm open to suggestions.


It "<testName>" -TestCases $TestCasesSSHConnection {
param($scriptBlock)
{ & $scriptBlock } | Should -Throw -ErrorId '2100,PSSessionOpenFailed'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It is out of the PR but I'd prefer to see more useful message.

@iSazonov Ilya (iSazonov) self-assigned this Oct 14, 2019
@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Oct 14, 2019
@iSazonov Ilya (iSazonov) added this to the 7.0.0-preview.5 milestone Oct 14, 2019
@iSazonov
Ilya (iSazonov) merged commit beb8b44 into PowerShell:master Oct 15, 2019
@iSazonov

Copy link
Copy Markdown
Collaborator

Patrick Meinecke (@SeeminglyScience) Thanks for your contribution!

@SeeminglyScience
Patrick Meinecke (SeeminglyScience) deleted the fix-ssh-connection-hashtable-psobject branch October 15, 2019 18:29
@ghost

Copy link
Copy Markdown

🎉v7.0.0-preview.5 has been released which incorporates this pull request.:tada:

Handy links:

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
…match expected type (PowerShell#10720)

Before the change if the hashtable value does not match the expected type exactly, an exception is thrown stating that the value was null.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invoke-Command -SSHConnection breaks if the HostName string is [psobject]-wrapped

5 participants