Skip to content

Commit 1217179

Browse files
committed
CodeQL fixes
This takes care of a number of CodeQL violations in our code base. Virtually all of these were about uses of MD5 or SHA-1 that we have to support due to the file formats we produce and consume. As such I added suppressions for those cases. There was one real case that could be migrated that I took care of.
1 parent af73fba commit 1217179

File tree

5 files changed

+19
-10
lines changed

5 files changed

+19
-10
lines changed

src/Compilers/Core/CodeAnalysisTest/Emit/EmitOptionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void TestCtors()
138138
tolerateErrors: true,
139139
includePrivateMembers: false,
140140
instrumentationKinds: ImmutableArray.Create(InstrumentationKind.TestCoverage),
141-
pdbChecksumAlgorithm: HashAlgorithmName.MD5);
141+
pdbChecksumAlgorithm: HashAlgorithmName.MD5); // CodeQL [SM02196] This is testing an algorithm that our codebase must support for PDBs
142142

143143
Assert.Equal(options1, options2.WithInstrumentationKinds(default));
144144
Assert.Equal(options2, options3.WithPdbChecksumAlgorithm(HashAlgorithmName.SHA256));

src/Compilers/Core/Portable/CryptographicHashProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ internal static int GetHashSize(SourceHashAlgorithm algorithmId)
8282
switch (algorithmId)
8383
{
8484
case SourceHashAlgorithm.Sha1:
85+
// CodeQL [SM02196] This is not enabled by default but exists as a compat option for existing builds.
8586
return SHA1.Create();
8687

8788
case SourceHashAlgorithm.Sha256:
@@ -97,6 +98,7 @@ internal static HashAlgorithmName GetAlgorithmName(SourceHashAlgorithm algorithm
9798
switch (algorithmId)
9899
{
99100
case SourceHashAlgorithm.Sha1:
101+
// CodeQL [SM02196] This is not enabled by default but exists as a compat option for existing builds.
100102
return HashAlgorithmName.SHA1;
101103

102104
case SourceHashAlgorithm.Sha256:
@@ -113,6 +115,7 @@ internal static HashAlgorithmName GetAlgorithmName(SourceHashAlgorithm algorithm
113115
{
114116
case AssemblyHashAlgorithm.None:
115117
case AssemblyHashAlgorithm.Sha1:
118+
// CodeQL [SM02196] ECMA-335 requires us to support SHA-1
116119
return SHA1.Create();
117120

118121
case AssemblyHashAlgorithm.Sha256:
@@ -166,6 +169,8 @@ internal static ImmutableArray<byte> ComputeSha1(Stream stream)
166169
if (stream != null)
167170
{
168171
stream.Seek(0, SeekOrigin.Begin);
172+
173+
// CodeQL [SM02196] ECMA-335 requires us to use SHA-1 and there is no alternative.
169174
using (var hashProvider = SHA1.Create())
170175
{
171176
return ImmutableArray.Create(hashProvider.ComputeHash(stream));
@@ -182,6 +187,7 @@ internal static ImmutableArray<byte> ComputeSha1(ImmutableArray<byte> bytes)
182187

183188
internal static ImmutableArray<byte> ComputeSha1(byte[] bytes)
184189
{
190+
// CodeQL [SM02196] ECMA-335 requires us to use SHA-1 and there is no alternative.
185191
using (var hashProvider = SHA1.Create())
186192
{
187193
return ImmutableArray.Create(hashProvider.ComputeHash(bytes));

src/Compilers/Core/Portable/PEWriter/SigningUtilities.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,25 @@ internal static class SigningUtilities
1919
{
2020
internal static byte[] CalculateRsaSignature(IEnumerable<Blob> content, RSAParameters privateKey)
2121
{
22-
var hash = CalculateSha1(content);
22+
var hash = calculateSha1(content);
2323

2424
using (var rsa = RSA.Create())
2525
{
2626
rsa.ImportParameters(privateKey);
27+
// CodeQL [SM02196] ECMA-335 requires us to use SHA-1 and there is no alternative.
2728
var signature = rsa.SignHash(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
2829
Array.Reverse(signature);
2930
return signature;
3031
}
31-
}
3232

33-
internal static byte[] CalculateSha1(IEnumerable<Blob> content)
34-
{
35-
using (var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA1))
33+
static byte[] calculateSha1(IEnumerable<Blob> content)
3634
{
37-
hash.AppendData(content);
38-
return hash.GetHashAndReset();
35+
// CodeQL [SM02196] ECMA-335 requires us to use SHA-1 and there is no alternative.
36+
using (var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA1))
37+
{
38+
hash.AppendData(content);
39+
return hash.GetHashAndReset();
40+
}
3941
}
4042
}
4143

src/Compilers/Core/Portable/Text/SourceHashAlgorithms.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ private static HashAlgorithm CreateInstance(SourceHashAlgorithm algorithm)
5050
{
5151
return algorithm switch
5252
{
53+
// CodeQL [SM02196] This is not enabled by default but exists as a compat option for existing builds.
5354
SourceHashAlgorithm.Sha1 => SHA1.Create(),
5455
SourceHashAlgorithm.Sha256 => SHA256.Create(),
5556
_ => throw ExceptionUtilities.UnexpectedValue(algorithm)

src/Tools/BuildBoss/CompilerNuGetCheckerUtil.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ private static string GetChecksum(string filePath)
330330

331331
private static string GetChecksum(Stream stream)
332332
{
333-
using var md5 = MD5.Create();
334-
return BitConverter.ToString(md5.ComputeHash(stream));
333+
using var hash = SHA256.Create();
334+
return BitConverter.ToString(hash.ComputeHash(stream));
335335
}
336336

337337
/// <summary>

0 commit comments

Comments
 (0)