Skip to content

Commit 74cfc0f

Browse files
Obsolete Encoding.UTF7 property and UTF7Encoding ctors (#37535)
- Disallow Encoding.GetEncoding("utf-7", ...) from returning an Encoding instance - Minor code cleanup by removing unused ifdefs - A compat switch is available to re-enable Encoding.GetEncoding("utf-7", ...)
1 parent 7007cc2 commit 74cfc0f

File tree

27 files changed

+329
-56
lines changed

27 files changed

+329
-56
lines changed

docs/project/list-of-obsoletions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ Currently the identifiers `MSLIB0001` through `MSLIB0999` are carved out for obs
1313

1414
| Diagnostic ID | Description |
1515
| :--------------- | :---------- |
16-
| __`MSLIB0001`__ | (Reserved for `Encoding.UTF7`.) |
16+
| __`MSLIB0001`__ | The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead. |
1717
| __`MSLIB0002`__ | `PrincipalPermissionAttribute` is not honored by the runtime and must not be used. |

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchive.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,7 @@ private set
369369

370370
if (value != null &&
371371
(value.Equals(Encoding.BigEndianUnicode)
372-
|| value.Equals(Encoding.Unicode)
373-
#if FEATURE_UTF32
374-
|| value.Equals(Encoding.UTF32)
375-
#endif // FEATURE_UTF32
376-
#if FEATURE_UTF7
377-
|| value.Equals(Encoding.UTF7)
378-
#endif // FEATURE_UTF7
379-
))
372+
|| value.Equals(Encoding.Unicode)))
380373
{
381374
throw new ArgumentException(SR.EntryNameEncodingNotSupported, nameof(EntryNameEncoding));
382375
}

src/libraries/System.IO.Ports/tests/SerialPort/Encoding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void Encoding_ISCIIAssemese()
111111
public void Encoding_UTF7()
112112
{
113113
Debug.WriteLine("Verifying UTF7Encoding Encoding");
114-
VerifyException(Encoding.UTF7, ThrowAt.Set, typeof(ArgumentException));
114+
VerifyException(LegacyUTF7Encoding, ThrowAt.Set, typeof(ArgumentException));
115115
}
116116

117117
[ConditionalFact(nameof(HasOneSerialPort))]

src/libraries/System.IO.Ports/tests/SerialPort/ReadTo.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ private void PerformReadOnCom1FromCom2(SerialPort com1, SerialPort com2, string
691691
int totalCharsRead;
692692
int lastIndexOfNewLine = -newLineStringLength;
693693
string expectedString;
694-
bool isUTF7Encoding = com1.Encoding.EncodingName == Encoding.UTF7.EncodingName;
694+
bool isUTF7Encoding = IsUTF7Encoding(com1.Encoding);
695695

696696
char[] charsToWrite = strToWrite.ToCharArray();
697697
byte[] bytesToWrite = com1.Encoding.GetBytes(charsToWrite);
@@ -805,7 +805,7 @@ private void VerifyReadToWithWriteLine(Encoding encoding, string newLine)
805805
Random rndGen = new Random(-55);
806806
StringBuilder strBldrToWrite;
807807
string strExpected;
808-
bool isUTF7Encoding = encoding.EncodingName == Encoding.UTF7.EncodingName;
808+
bool isUTF7Encoding = IsUTF7Encoding(encoding);
809809

810810
Debug.WriteLine("Verifying ReadTo with WriteLine encoding={0}, newLine={1}", encoding, newLine);
811811

@@ -861,10 +861,10 @@ private string GenRandomNewLine(bool validAscii)
861861

862862
private int GetUTF7EncodingBytes(char[] chars, int index, int count)
863863
{
864-
byte[] bytes = Encoding.UTF7.GetBytes(chars, index, count);
864+
byte[] bytes = LegacyUTF7Encoding.GetBytes(chars, index, count);
865865
int byteCount = bytes.Length;
866866

867-
while (Encoding.UTF7.GetCharCount(bytes, 0, byteCount) == count)
867+
while (LegacyUTF7Encoding.GetCharCount(bytes, 0, byteCount) == count)
868868
{
869869
--byteCount;
870870
}

src/libraries/System.IO.Ports/tests/SerialPort/Read_char_int_int.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -950,18 +950,9 @@ private void VerifyBytesFollowedByChars(Encoding encoding)
950950
Fail("ERROR!!!: Expected to read {0} chars actually read {1}", xmitCharBuffer.Length, numRead);
951951
}
952952

953-
if (encoding.EncodingName == Encoding.UTF7.EncodingName)
953+
if (IsUTF7Encoding(encoding))
954954
{
955-
//If UTF7Encoding is being used we might leave a - in the stream
956-
if (com1.BytesToRead == xmitByteBuffer.Length + 1)
957-
{
958-
int byteRead;
959-
960-
if ('-' != (char)(byteRead = com1.ReadByte()))
961-
{
962-
Fail("Err_29282naie Expected '-' to be left in the stream with UTF7Encoding and read {0}", byteRead);
963-
}
964-
}
955+
Fail("UTF-7 encoding not expected to be passed to this test.");
965956
}
966957

967958
if (xmitByteBuffer.Length != (numRead = com1.Read(rcvByteBuffer, 0, rcvByteBuffer.Length)))

src/libraries/System.IO.Ports/tests/Support/PortsTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Text;
56
using System.IO;
67
using Legacy.Support;
78
using Xunit;
@@ -36,5 +37,20 @@ public static void Fail(string format, params object[] args)
3637
{
3738
Assert.True(false, string.Format(format, args));
3839
}
40+
41+
#pragma warning disable MSLIB0001 // Encoding.UTF7 property is obsolete
42+
protected static Encoding LegacyUTF7Encoding => Encoding.UTF7;
43+
#pragma warning restore MSLIB0001
44+
45+
/// <summary>
46+
/// Returns a value stating whether <paramref name="encoding"/> is UTF-7.
47+
/// </summary>
48+
/// <remarks>
49+
/// This method checks only for the code page 65000.
50+
/// </remarks>
51+
internal static bool IsUTF7Encoding(Encoding encoding)
52+
{
53+
return (encoding.CodePage == LegacyUTF7Encoding.CodePage);
54+
}
3955
}
4056
}

src/libraries/System.Net.Http.Json/tests/UnitTests/TranscodingReadStreamTests.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,6 @@ public Task ReadAsync_Works_WhenInputIs_Unicode(string message)
232232
return ReadAsyncTest(sourceEncoding, message);
233233
}
234234

235-
[Theory]
236-
[MemberData(nameof(ReadAsyncInputLatin), "utf-7")]
237-
[MemberData(nameof(ReadAsyncInputUnicode), "utf-7")]
238-
public Task ReadAsync_Works_WhenInputIs_UTF7(string message)
239-
{
240-
Encoding sourceEncoding = Encoding.UTF7;
241-
return ReadAsyncTest(sourceEncoding, message);
242-
}
243-
244235
[Theory]
245236
[MemberData(nameof(ReadAsyncInputLatin), "iso-8859-1")]
246237
public Task ReadAsync_Works_WhenInputIs_WesternEuropeanEncoding(string message)

src/libraries/System.Net.Http.Json/tests/UnitTests/TranscodingWriteStreamTests.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ public Task WriteAsync_Works_WhenOutputIs_Unicode(string message)
4242
return WriteAsyncTest(targetEncoding, message);
4343
}
4444

45-
[Theory]
46-
[MemberData(nameof(WriteAsyncInputLatin))]
47-
public Task WriteAsync_Works_WhenOutputIs_UTF7(string message)
48-
{
49-
Encoding targetEncoding = Encoding.UTF7;
50-
return WriteAsyncTest(targetEncoding, message);
51-
}
52-
5345
[Theory]
5446
[MemberData(nameof(WriteAsyncInputLatin))]
5547
public Task WriteAsync_Works_WhenOutputIs_WesternEuropeanEncoding(string message)

src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
<type fullname="System.Globalization.GlobalizationMode">
1010
<method signature="System.Boolean get_Invariant()" body="stub" value="true" feature="System.Globalization.Invariant" featurevalue="true" />
1111
</type>
12+
<type fullname="System.LocalAppContextSwitches">
13+
<method signature="System.Boolean get_EnableUnsafeUTF7Encoding()" body="stub" value="false" feature="System.Text.Encoding.EnableUnsafeUTF7Encoding" featurevalue="false" />
14+
</type>
1215
</assembly>
1316
</linker>

src/libraries/System.Private.CoreLib/src/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3742,6 +3742,9 @@
37423742
<data name="SwitchExpressionException_UnmatchedValue" xml:space="preserve">
37433743
<value>Unmatched value was {0}.</value>
37443744
</data>
3745+
<data name="Encoding_UTF7_Disabled" xml:space="preserve">
3746+
<value>Support for UTF-7 is disabled. See {0} for more information.</value>
3747+
</data>
37453748
<data name="IDynamicInterfaceCastable_DoesNotImplementRequested" xml:space="preserve">
37463749
<value>Type '{0}' returned by IDynamicInterfaceCastable does not implement the requested interface '{1}'.</value>
37473750
</data>

0 commit comments

Comments
 (0)