mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Remove input text from the error message resulted by SecureString and PSCredential conversion failure (#19977)
This commit is contained in:
@@ -21,6 +21,7 @@ using System.Runtime.CompilerServices;
|
||||
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;
|
||||
@@ -4918,8 +4919,26 @@ namespace System.Management.Automation
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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,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"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user