Skip to content

Commit 82894b3

Browse files
authored
Merge pull request #2945 from LLLXXXCCC/DoNotHardCodeCertificate
Do not hard code certificate
2 parents d9c044e + 9f3c4af commit 82894b3

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/Microsoft.NetCore.Analyzers/UnitTests/Security/DoNotHardCodeCertificateTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,25 @@ public void TestMethod(string path, string password)
292292
GetCSharpResultAt(11, 9, 9, 24, "X509Certificate.X509Certificate(byte[] rawData, string password)", "void TestClass.TestMethod(string path, string password)", "byte[]", "void TestClass.TestMethod(string path, string password)"));
293293
}
294294

295+
[Fact]
296+
public void Test_X509Certificates2_Diagnostic()
297+
{
298+
VerifyCSharp(@"
299+
using System.IO;
300+
using System.Security.Cryptography.X509Certificates;
301+
302+
class TestClass
303+
{
304+
public void TestMethod(string path)
305+
{
306+
byte[] bytes = new byte[] {1, 2, 3};
307+
File.WriteAllBytes(path, bytes);
308+
new X509Certificate2(path);
309+
}
310+
}",
311+
GetCSharpResultAt(11, 9, 9, 24, "X509Certificate2.X509Certificate2(string fileName)", "void TestClass.TestMethod(string path)", "byte[]", "void TestClass.TestMethod(string path)"));
312+
}
313+
295314
// For now, we didn't take serialization into consideration.
296315
[Fact]
297316
public void Test_Sink_X509Certificate_WithSerializationInfoAndStreamingContextParameters_NoDiagnostic()
@@ -347,6 +366,23 @@ public void TestMethod(string s, string path)
347366
}");
348367
}
349368

369+
[Fact]
370+
public void Test_X509Certificate2_NoDiagnostic()
371+
{
372+
VerifyCSharp(@"
373+
using System.IO;
374+
using System.Security.Cryptography.X509Certificates;
375+
376+
class TestClass
377+
{
378+
public void TestMethod(byte[] bytes, string path)
379+
{
380+
File.WriteAllBytes(path, bytes);
381+
new X509Certificate2(path);
382+
}
383+
}");
384+
}
385+
350386
protected override DiagnosticAnalyzer GetBasicDiagnosticAnalyzer()
351387
{
352388
return new DoNotHardCodeCertificate();

src/Utilities/Compiler/WellKnownTypeNames.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ internal static class WellKnownTypeNames
326326
public const string SystemSecurityCryptographyX509CertificatesX509Store = "System.Security.Cryptography.X509Certificates.X509Store";
327327
public const string SystemSecurityCryptographyX509CertificatesStoreName = "System.Security.Cryptography.X509Certificates.StoreName";
328328
public const string SystemSecurityCryptographyX509CertificatesX509Certificate = "System.Security.Cryptography.X509Certificates.X509Certificate";
329+
public const string SystemSecurityCryptographyX509CertificatesX509Certificate2 = "System.Security.Cryptography.X509Certificates.X509Certificate2";
329330
public const string SystemSecurityCryptographyRSA = "System.Security.Cryptography.RSA";
330331
public const string SystemSecurityCryptographyDSA = "System.Security.Cryptography.DSA";
331332
public const string SystemSecurityCryptographyAsymmetricAlgorithm = "System.Security.Cryptography.AsymmetricAlgorithm";

src/Utilities/FlowAnalysis/FlowAnalysis/Analysis/TaintedDataAnalysis/HardcodedCertificateSinks.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ static HardcodedCertificateSinks()
2525
sinkMethodParameters: new[] {
2626
( ".ctor", new[] { "rawData", "data" }),
2727
});
28+
builder.AddSinkInfo(
29+
WellKnownTypeNames.SystemSecurityCryptographyX509CertificatesX509Certificate2,
30+
SinkKind.HardcodedCertificate,
31+
isInterface: false,
32+
isAnyStringParameterInConstructorASink: true,
33+
sinkProperties: null,
34+
sinkMethodParameters: new[] {
35+
( ".ctor", new[] { "rawData", "data" }),
36+
});
2837

2938
SinkInfos = builder.ToImmutableAndFree();
3039
}

0 commit comments

Comments
 (0)