Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
23 changes: 21 additions & 2 deletions 23 src/System.Management.Automation/engine/LanguagePrimitives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Security;

using Dbg = System.Management.Automation.Diagnostics;
using MethodCacheEntry = System.Management.Automation.DotNetAdapter.MethodCacheEntry;
Expand Down Expand Up @@ -4918,8 +4919,26 @@ internal static Tuple<string, string> GetInvalidCastMessages(object valueToConve

typeConversion.WriteLine("Type Conversion failed.");
errorId = "ConvertToFinalInvalidCastException";
errorMsg = StringUtil.Format(ExtendedTypeSystem.InvalidCastException, valueToConvert.ToString(),
ObjectToTypeNameString(valueToConvert), resultType.ToString());

string valueToConvertTypeName = ObjectToTypeNameString(valueToConvert);
string resultTypeName = resultType.ToString();

if (resultType == typeof(SecureString) || resultType == typeof(PSCredential))
{
errorMsg = StringUtil.Format(
ExtendedTypeSystem.InvalidCastExceptionWithoutValue,
valueToConvertTypeName,
resultTypeName);
}
else
{
errorMsg = StringUtil.Format(
ExtendedTypeSystem.InvalidCastException,
valueToConvert.ToString(),
valueToConvertTypeName,
resultTypeName);
}

return Tuple.Create(errorId, errorMsg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@
<data name="InvalidCastException" xml:space="preserve">
<value>Cannot convert the "{0}" value of type "{1}" to type "{2}".</value>
</data>
<data name="InvalidCastExceptionWithoutValue" xml:space="preserve">
<value>Cannot convert the value of type "{0}" to type "{1}".</value>
</data>
<data name="InvalidCastExceptionWithInnerException" xml:space="preserve">
<value>Cannot convert value "{0}" to type "{1}". Error: "{2}"</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ Describe "SecureString conversion tests" -Tags "CI" {
$ss2 = $encodedStr | ConvertTo-SecureString -Key $key
$ss2 | ConvertFrom-SecureString -AsPlainText | Should -BeExactly $testString
}

It "Using invalid secure string with ConvertFrom-SecureString produces an exception message without value" {
$ex = { ConvertFrom-SecureString "1234" } | Should -Throw -ErrorId "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand" -PassThru
$ex.Exception.Message | Should -Not -Match "1234"
}

It "Using invalid securestring with cast produces an exception message without value" {
$ex = { [securestring]"1234" } | Should -Throw -ErrorId "ConvertToFinalInvalidCastException" -PassThru
$ex.Exception.Message | Should -Not -Match "1234"
}
}
5 changes: 5 additions & 0 deletions 5 test/powershell/engine/Basic/Credential.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ Describe "Credential tests" -Tags "CI" {
# We should explicitly check that the expression returns $null
[PSCredential]::Empty.GetNetworkCredential() | Should -BeNullOrEmpty
}

It "Explicit credential cast with string produces an exception message without value" {
$ex = { [pscredential]"1234" } | Should -Throw -ErrorId "ConvertToFinalInvalidCastException" -PassThru
$ex.Exception.Message | Should -Not -Match "1234"
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.