Skip to content
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

Removed value from SecureString/PSCredential string conversion exception message #19977

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/System.Management.Automation/engine/LanguagePrimitives.cs
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 IConversionData FigureConversion(object valueToConvert, Type res

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
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
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 test/powershell/engine/Basic/Credential.Tests.ps1
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"
}
}