-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid using IncrementalHash on .NET Framework (#9430)
On .NET Framework, IncrementalHash uses the OS's implementation of SHA-256, which results in several safe handles being created. These handles are finalizable and can cause lock contention in the CLR if created rapidly, which Razor does. This change adjusts the Checksum.Builder to use the SHA256 class directly on .NET Framework (and netstandard2.0), which causes the managed implementation of SHA256 to be used (if FIPS isn't enabled). That implementation avoids the creation of finalizable safe handles. See dotnet/roslyn#67995 for more detail. Many thanks to @sharwell for pointing out this issue along with the fix.
- Loading branch information
Showing
5 changed files
with
145 additions
and
108 deletions.
There are no files selected for viewing
29 changes: 0 additions & 29 deletions
29
...d/Microsoft.AspNetCore.Razor.Utilities.Shared/PooledObjects/IncrementalHashPool.Policy.cs
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/PooledObjects/IncrementalHashPool.cs
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/Utilities/Checksum.Builder.Policy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Extensions.ObjectPool; | ||
#if NET5_0_OR_GREATER | ||
using System.Diagnostics; | ||
#endif | ||
|
||
#if NET5_0_OR_GREATER | ||
using HashAlgorithmName = System.Security.Cryptography.HashAlgorithmName; | ||
using HashingType = System.Security.Cryptography.IncrementalHash; | ||
#else | ||
using HashingType = System.Security.Cryptography.SHA256; | ||
#endif | ||
|
||
namespace Microsoft.AspNetCore.Razor.Utilities; | ||
|
||
internal sealed partial record Checksum | ||
{ | ||
internal readonly ref partial struct Builder | ||
{ | ||
private sealed class Policy : IPooledObjectPolicy<HashingType> | ||
{ | ||
public static readonly Policy Instance = new(); | ||
|
||
private Policy() | ||
{ | ||
} | ||
|
||
public HashingType Create() | ||
#if NET5_0_OR_GREATER | ||
=> HashingType.CreateHash(HashAlgorithmName.SHA256); | ||
#else | ||
=> HashingType.Create(); | ||
#endif | ||
|
||
public bool Return(HashingType hash) | ||
{ | ||
#if NET5_0_OR_GREATER | ||
Debug.Assert(hash.AlgorithmName == HashAlgorithmName.SHA256); | ||
#endif | ||
|
||
return true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters