mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Create warnings when UTF7 is specified as an encoding (#13430)
This commit is contained in:
@@ -217,7 +217,21 @@ namespace Microsoft.PowerShell.Commands
|
||||
[ArgumentToEncodingTransformationAttribute]
|
||||
[ArgumentEncodingCompletionsAttribute]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding();
|
||||
public Encoding Encoding
|
||||
{
|
||||
get
|
||||
{
|
||||
return _encoding;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
EncodingConversion.WarnIfObsolete(this, value);
|
||||
_encoding = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Encoding _encoding = ClrFacade.GetDefaultEncoding();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets property that sets append parameter.
|
||||
@@ -592,7 +606,21 @@ namespace Microsoft.PowerShell.Commands
|
||||
[ArgumentToEncodingTransformationAttribute]
|
||||
[ArgumentEncodingCompletionsAttribute]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding();
|
||||
public Encoding Encoding
|
||||
{
|
||||
get
|
||||
{
|
||||
return _encoding;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
EncodingConversion.WarnIfObsolete(this, value);
|
||||
_encoding = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Encoding _encoding = ClrFacade.GetDefaultEncoding();
|
||||
|
||||
/// <summary>
|
||||
/// Avoid writing out duplicate warning messages when there are one or more unspecified names.
|
||||
|
||||
+14
-1
@@ -71,7 +71,20 @@ namespace Microsoft.PowerShell.Commands
|
||||
[ArgumentToEncodingTransformationAttribute()]
|
||||
[ArgumentEncodingCompletionsAttribute]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding();
|
||||
public Encoding Encoding
|
||||
{
|
||||
get
|
||||
{
|
||||
return _encoding;
|
||||
}
|
||||
set
|
||||
{
|
||||
EncodingConversion.WarnIfObsolete(this, value);
|
||||
_encoding = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Encoding _encoding = ClrFacade.GetDefaultEncoding();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets count of bytes to read from the input stream.
|
||||
|
||||
+15
-1
@@ -78,7 +78,21 @@ namespace Microsoft.PowerShell.Commands
|
||||
[ArgumentToEncodingTransformationAttribute()]
|
||||
[ArgumentEncodingCompletionsAttribute]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding();
|
||||
public Encoding Encoding
|
||||
{
|
||||
get
|
||||
{
|
||||
return _encoding;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
EncodingConversion.WarnIfObsolete(this, value);
|
||||
_encoding = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Encoding _encoding = ClrFacade.GetDefaultEncoding();
|
||||
|
||||
/// <summary>
|
||||
/// Property that sets append parameter.
|
||||
|
||||
+15
-1
@@ -76,7 +76,21 @@ namespace Microsoft.PowerShell.Commands
|
||||
[ArgumentToEncodingTransformationAttribute()]
|
||||
[ArgumentEncodingCompletionsAttribute]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding();
|
||||
public Encoding Encoding
|
||||
{
|
||||
get
|
||||
{
|
||||
return _encoding;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
EncodingConversion.WarnIfObsolete(this, value);
|
||||
_encoding = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Encoding _encoding = ClrFacade.GetDefaultEncoding();
|
||||
|
||||
#endregion Parameters
|
||||
|
||||
|
||||
@@ -1349,7 +1349,21 @@ namespace Microsoft.PowerShell.Commands
|
||||
[ArgumentToEncodingTransformationAttribute()]
|
||||
[ArgumentEncodingCompletionsAttribute]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding();
|
||||
public Encoding Encoding
|
||||
{
|
||||
get
|
||||
{
|
||||
return _encoding;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
EncodingConversion.WarnIfObsolete(this, value);
|
||||
_encoding = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Encoding _encoding = ClrFacade.GetDefaultEncoding();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the number of context lines to collect. If set to a
|
||||
|
||||
@@ -63,7 +63,21 @@ namespace Microsoft.PowerShell.Commands
|
||||
[ValidateNotNullOrEmpty]
|
||||
[ArgumentEncodingCompletionsAttribute]
|
||||
[ArgumentToEncodingTransformationAttribute]
|
||||
public Encoding Encoding { get; set; } = Encoding.ASCII;
|
||||
public Encoding Encoding
|
||||
{
|
||||
get
|
||||
{
|
||||
return _encoding;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
EncodingConversion.WarnIfObsolete(this, value);
|
||||
_encoding = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Encoding _encoding = Encoding.ASCII;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the address collection that contains the
|
||||
|
||||
@@ -114,7 +114,21 @@ namespace Microsoft.PowerShell.Commands
|
||||
[ArgumentToEncodingTransformationAttribute()]
|
||||
[ArgumentEncodingCompletionsAttribute]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding();
|
||||
public Encoding Encoding
|
||||
{
|
||||
get
|
||||
{
|
||||
return _encoding;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
EncodingConversion.WarnIfObsolete(this, value);
|
||||
_encoding = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Encoding _encoding = ClrFacade.GetDefaultEncoding();
|
||||
|
||||
#endregion Command Line Parameters
|
||||
|
||||
|
||||
@@ -6744,7 +6744,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </returns>
|
||||
public object GetContentReaderDynamicParameters(string path)
|
||||
{
|
||||
return new FileSystemContentReaderDynamicParameters();
|
||||
return new FileSystemContentReaderDynamicParameters(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -6878,7 +6878,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </returns>
|
||||
public object GetContentWriterDynamicParameters(string path)
|
||||
{
|
||||
return new FileSystemContentWriterDynamicParameters();
|
||||
return new FileSystemContentWriterDynamicParameters(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -7593,6 +7593,13 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </summary>
|
||||
public class FileSystemContentDynamicParametersBase
|
||||
{
|
||||
internal FileSystemContentDynamicParametersBase(FileSystemProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
private FileSystemProvider _provider;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the encoding method used when
|
||||
/// reading data from the file.
|
||||
@@ -7610,6 +7617,10 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
set
|
||||
{
|
||||
if (value == System.Text.Encoding.UTF7)
|
||||
{
|
||||
_provider.WriteWarning(PathUtilsStrings.Utf7EncodingObsolete);
|
||||
}
|
||||
_encoding = value;
|
||||
// If an encoding was explicitly set, be sure to capture that.
|
||||
WasStreamTypeSpecified = true;
|
||||
@@ -7659,6 +7670,8 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </summary>
|
||||
public class FileSystemContentWriterDynamicParameters : FileSystemContentDynamicParametersBase
|
||||
{
|
||||
internal FileSystemContentWriterDynamicParameters(FileSystemProvider provider) : base(provider) { }
|
||||
|
||||
/// <summary>
|
||||
/// False to add a newline to the end of the output string, true if not.
|
||||
/// </summary>
|
||||
@@ -7684,6 +7697,8 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </summary>
|
||||
public class FileSystemContentReaderDynamicParameters : FileSystemContentDynamicParametersBase
|
||||
{
|
||||
internal FileSystemContentReaderDynamicParameters(FileSystemProvider provider) : base (provider) { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the delimiter to use when reading the file. Custom delimiters
|
||||
/// may not be used when the file is opened with a "Byte" encoding.
|
||||
|
||||
@@ -117,6 +117,9 @@
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<data name="Utf7EncodingObsolete" xml:space="preserve">
|
||||
<value>Encoding 'UTF-7' is obsolete, please use UTF-8.</value>
|
||||
</data>
|
||||
<data name="UtilityFileExistsNoClobber" xml:space="preserve">
|
||||
<value>File {0} already exists and {1} was specified.</value>
|
||||
</data>
|
||||
|
||||
@@ -63,6 +63,12 @@ namespace System.Management.Automation
|
||||
Encoding foundEncoding;
|
||||
if (encodingMap.TryGetValue(encoding, out foundEncoding))
|
||||
{
|
||||
// Write a warning if using utf7 as it is obsolete in .NET5
|
||||
if (string.Compare(encoding, Utf7, StringComparison.OrdinalIgnoreCase) == 0)
|
||||
{
|
||||
cmdlet.WriteWarning(PathUtilsStrings.Utf7EncodingObsolete);
|
||||
}
|
||||
|
||||
return foundEncoding;
|
||||
}
|
||||
|
||||
@@ -81,6 +87,19 @@ namespace System.Management.Automation
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Warn if the encoding has been designated as obsolete.
|
||||
/// </summary>
|
||||
/// <param name="cmdlet">A cmdlet instance which is used to emit the warning.</param>
|
||||
/// <param name="encoding">The encoding to check for obsolescence.</param>
|
||||
internal static void WarnIfObsolete(Cmdlet cmdlet, Encoding encoding)
|
||||
{
|
||||
if (encoding == System.Text.Encoding.UTF7)
|
||||
{
|
||||
cmdlet.WriteWarning(PathUtilsStrings.Utf7EncodingObsolete);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -99,4 +99,30 @@ Describe "File encoding tests" -Tag CI {
|
||||
(Get-Content $testFile -AsByteStream) -join "-" | Should -BeExactly $expectedBytes
|
||||
}
|
||||
}
|
||||
|
||||
Context "Using encoding utf7 results in a warning" {
|
||||
BeforeAll {
|
||||
$expectedString = "Encoding 'UTF-7' is obsolete, please use UTF-8."
|
||||
$testCases = @(
|
||||
@{ Command = 'Add-Content'; Script = { "test" | Add-Content -Encoding utf7 -Path TESTDRIVE:/file 3>TESTDRIVE:/warning } }
|
||||
@{ Command = 'Export-CliXml'; Script = { "test" | Export-Clixml -Path TESTDRIVE:/file.ps1xml -Encoding utf7 3>TESTDRIVE:/warning } }
|
||||
@{ Command = 'Export-Csv'; Script = { "test" | Export-Csv -Path TESTDRIVE:/export.csv -Encoding utf7 3>TESTDRIVE:/warning } }
|
||||
@{ Command = 'Format-Hex'; Script = { "test" | Format-Hex -Encoding utf7 > TESTDRIVE:/output 3>TESTDRIVE:/warning } }
|
||||
@{ Command = 'Get-Content'; Script = { "output" > TESTDRIVE:/input; $null = Get-Content -Path TESTDRIVE:/input -Encoding utf7 3>TESTDRIVE:/warning } }
|
||||
@{ Command = 'Import-Csv'; Script = { "test" | Export-Csv -Path TESTDRIVE:/output.csv; $null = Import-Csv -Path TESTDRIVE:/output.csv -Encoding utf7 3>TESTDRIVE:/warning } }
|
||||
@{ Command = 'Out-File'; Script = { "test" | Out-File -Path TESTDRIVE:/output.txt -Encoding utf7 3>TESTDRIVE:/warning } }
|
||||
@{ Command = 'Select-String'; Script = { "aa" | Select-String -pattern bb -Encoding utf7 3>TESTDRIVE:/warning } }
|
||||
@{ Command = 'Set-Content'; Script = { "aa" | Set-Content -Path TESTDRIVE:/output.txt -Encoding utf7 3>TESTDRIVE:/warning } }
|
||||
)
|
||||
}
|
||||
BeforeEach {
|
||||
Remove-Item TESTDRIVE:/* -force -ErrorAction Ignore
|
||||
}
|
||||
It "'<command> has a warning when '-Encoding utf7' is used" -TestCases $testCases {
|
||||
param ($command, $script)
|
||||
& $script
|
||||
$observed = Get-Content TESTDRIVE:/warning
|
||||
$observed | Should -BeExactly $expectedString
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user