Skip to content

Commit f27d337

Browse files
GrabYourPitchforkswtgodbe
authored andcommitted
Tighten bounds checks around TextEncoder logic
- Replaces unsafe code with safe code where possible - Fixes some surrogate pairs being misinterpreted - Fixes #45994 - Ref: MSRC 62749 (CVE-2021-26701)
1 parent 10fccb8 commit f27d337

24 files changed

+710
-529
lines changed

NuGet.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<add key="dotnet-eng" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json" />
1717
<add key="dotnet5" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" />
1818
<add key="dotnet5-transport" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5-transport/nuget/v3/index.json" />
19+
<!-- Harvesting feed from 2.1 -->
20+
<add key="darc-int-corefx-2.1.26" value="https://pkgs.dev.azure.com/dnceng/internal/_packaging/darc-int-corefx-2.1.26/nuget/v3/index.json" />
1921
</packageSources>
2022
<disabledPackageSources>
2123
<clear />

eng/restore/harvestPackages.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
<!-- Allow to override package download and versions in case there is already a PackageDownload set -->
2525
<ItemGroup>
26+
<PackageDownload Include="System.Text.Encodings.Web" Version="4.5.1" />
2627
<_OverridenPackageDownloads Include="@(_PackageDownload)" Condition="'@(PackageDownload)' == '@(_PackageDownload)' and %(Identity) != ''" />
2728
<_PackageDownload Remove="@(_OverridenPackageDownloads)" />
2829
<_PackageDownload Include="@(PackageDownload)" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<Project>
22
<Import Project="..\Directory.Build.props" />
33
<PropertyGroup>
4+
<AssemblyVersion>5.0.0.1</AssemblyVersion>
5+
<PackageVersion>5.0.1</PackageVersion>
6+
<HarvestVersion>4.5.1</HarvestVersion>
47
<StrongNameKeyId>Open</StrongNameKeyId>
58
</PropertyGroup>
69
</Project>

src/libraries/System.Text.Encodings.Web/ref/System.Text.Encodings.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</ItemGroup>
1818
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or
1919
$(TargetFramework.StartsWith('net4'))">
20+
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
2021
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
2122
</ItemGroup>
2223
</Project>

src/libraries/System.Text.Encodings.Web/src/System.Text.Encodings.Web.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<NoWarn>$(NoWarn);CS3019</NoWarn>
1111
</PropertyGroup>
1212
<ItemGroup>
13+
<Compile Include="System\IO\TextWriterExtensions.cs" />
1314
<Compile Include="System\Text\Encodings\Web\DefaultJavaScriptEncoder.cs" />
1415
<Compile Include="System\Text\Encodings\Web\DefaultJavaScriptEncoderBasicLatin.cs" />
1516
<Compile Include="System\Text\Encodings\Web\HtmlEncoder.cs" />
@@ -40,6 +41,7 @@
4041
<Compile Include="$(CoreLibSharedDir)System\Text\UnicodeDebug.cs" Link="System\Text\UnicodeDebug.cs" />
4142
<Compile Include="$(CoreLibSharedDir)System\Text\UnicodeUtility.cs" Link="System\Text\UnicodeUtility.cs" />
4243
<Compile Include="$(CommonPath)System\HexConverter.cs" Link="Common\System\HexConverter.cs" />
44+
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs" Link="Common\System\Text\ValueStringBuilder.cs" />
4345
</ItemGroup>
4446
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)' or
4547
'$(TargetFramework)' == 'netcoreapp3.0'">
@@ -51,8 +53,12 @@
5153
<Reference Include="System.Runtime.Intrinsics" />
5254
<Reference Include="System.Threading" />
5355
</ItemGroup>
56+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
57+
<Reference Include="System.Buffers" />
58+
</ItemGroup>
5459
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or
5560
$(TargetFramework.StartsWith('net4'))">
61+
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
5662
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
5763
</ItemGroup>
5864
</Project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Diagnostics;
5+
6+
#if !(NETCOREAPP || NETSTANDARD2_1)
7+
using System.Buffers;
8+
#endif
9+
10+
namespace System.IO
11+
{
12+
internal static class TextWriterExtensions
13+
{
14+
/// <summary>
15+
/// Writes a partial string (given offset and count) to the underlying TextWriter.
16+
/// </summary>
17+
public static void WritePartialString(this TextWriter writer, string value, int offset, int count)
18+
{
19+
Debug.Assert(writer != null);
20+
Debug.Assert(value != null);
21+
22+
if (offset == 0 && count == value.Length)
23+
{
24+
// on all platforms, prefer TextWriter.Write(string) if no slicing is required
25+
writer.Write(value);
26+
}
27+
else
28+
{
29+
// if slicing is required, call TextWriter.Write(ROS<char>) if available;
30+
// otherwise rent an array and implement the Write routine ourselves
31+
ReadOnlySpan<char> sliced = value.AsSpan(offset, count);
32+
#if NETCOREAPP || NETSTANDARD2_1
33+
writer.Write(sliced);
34+
#else
35+
char[] rented = ArrayPool<char>.Shared.Rent(sliced.Length);
36+
sliced.CopyTo(rented);
37+
writer.Write(rented, 0, sliced.Length);
38+
ArrayPool<char>.Shared.Return(rented);
39+
#endif
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)