SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
798 stars 229 forks source link

Fix S4790 FN: New HashData overloads not recognized #8758

Open jilles-sg opened 9 months ago

jilles-sg commented 9 months ago

Description

Rule S4790 reports the use of obsolete cryptographic hash algorithms such as SHA1. It catches things like HashAlgorithm.Create("SHA1") and SHA1.Create(). However, at least some of the static HashData methods added by .NET 5 are not caught.

Repro steps

using System.Security.Cryptography;

byte[] data = { 1, 2, 3, 4 };
Span<byte> hash = stackalloc byte[20];
SHA1.HashData(data, destination: hash);
Console.WriteLine($"hash[0] = {hash[0]}");

This example doesn't really motivate the use of the span-based overload, but suppose the hash needs to be combined with other data into a larger message.

Expected behavior

Rule S4790 warns about an obsolete hash algorithm.

Actual behavior

No warning.

Known workarounds

Related information

gregory-paidis-sonarsource commented 9 months ago

Hey there! Thanks for reporting this, I confirm it is indeed an FN. I added a reproducer to keep track of it.

pierre-loup-tristant-sonarsource commented 2 months ago

This FN was also reported by a prospect https://discuss.sonarsource.com/t/rspec-2077-rspec-4790-false-negatives/20022