Skip to content

Commit 3a1d5a0

Browse files
committed
Remove constant char array out of SourceInfos.
1 parent 4b720f2 commit 3a1d5a0

3 files changed

Lines changed: 39 additions & 48 deletions

File tree

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -116,27 +116,6 @@ public void TestMethod(byte[] bytes, string path)
116116
GetCSharpResultAt(12, 9, 10, 38, "X509Certificate.X509Certificate(string fileName)", "void TestClass.TestMethod(byte[] bytes, string path)", "string chars", "int ASCIIEncoding.GetBytes(string chars, int charIndex, int charCount, byte[] bytes, int byteIndex)"));
117117
}
118118

119-
[Fact]
120-
public void Test_Source_ASCIIEncodingGetBytes_WithCharArrayAndInt32AndInt32AndByteArrayAndInt32Parameters_WithConstantCharArray_Diagnostic()
121-
{
122-
VerifyCSharp(@"
123-
using System.IO;
124-
using System.Text;
125-
using System.Security.Cryptography.X509Certificates;
126-
127-
class TestClass
128-
{
129-
public void TestMethod(byte[] bytes, string path)
130-
{
131-
char[] chars = new char[] {'1', '2', '3'};
132-
new ASCIIEncoding().GetBytes(chars, 0, 3, bytes, 0);
133-
File.WriteAllBytes(path, bytes);
134-
new X509Certificate(path);
135-
}
136-
}",
137-
GetCSharpResultAt(13, 9, 10, 24, "X509Certificate.X509Certificate(string fileName)", "void TestClass.TestMethod(byte[] bytes, string path)", "char[]", "void TestClass.TestMethod(byte[] bytes, string path)"));
138-
}
139-
140119
[Fact]
141120
public void Test_Sink_X509Certificate_WithStringAndSecureStringAndX509KeyStorageFlagsParameters_Diagnostic()
142121
{
@@ -347,6 +326,26 @@ public void TestMethod(string s, string path)
347326
}");
348327
}
349328

329+
[Fact]
330+
public void Test_Source_ASCIIEncodingGetBytes_WithCharArrayAndInt32AndInt32AndByteArrayAndInt32Parameters_WithConstantCharArray_NoDiagnostic()
331+
{
332+
VerifyCSharp(@"
333+
using System.IO;
334+
using System.Text;
335+
using System.Security.Cryptography.X509Certificates;
336+
337+
class TestClass
338+
{
339+
public void TestMethod(byte[] bytes, string path)
340+
{
341+
char[] chars = new char[] {'1', '2', '3'};
342+
new ASCIIEncoding().GetBytes(chars, 0, 3, bytes, 0);
343+
File.WriteAllBytes(path, bytes);
344+
new X509Certificate(path);
345+
}
346+
}");
347+
}
348+
350349
protected override DiagnosticAnalyzer GetBasicDiagnosticAnalyzer()
351350
{
352351
return new DoNotHardCodeCertificate();

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,26 +155,6 @@ public void TestMethod(byte[] key, byte[] someOtherBytesForIV)
155155
GetCSharpResultAt(11, 9, 9, 38, "ICryptoTransform SymmetricAlgorithm.CreateEncryptor(byte[] rgbKey, byte[] rgbIV)", "void TestClass.TestMethod(byte[] key, byte[] someOtherBytesForIV)", "string chars", "int ASCIIEncoding.GetBytes(string chars, int charIndex, int charCount, byte[] bytes, int byteIndex)"));
156156
}
157157

158-
[Fact]
159-
public void Test_ASCIIEncodingGetBytesWithCharArrayAndInt32AndInt32AndByteArrayAndInt32Parameters_CreateEncryptor_Diagnostic()
160-
{
161-
VerifyCSharp(@"
162-
using System.Text;
163-
using System.Security.Cryptography;
164-
165-
class TestClass
166-
{
167-
public void TestMethod(byte[] key, byte[] someOtherBytesForIV)
168-
{
169-
char[] chars = new char[] {'1', '2', '3'};
170-
new ASCIIEncoding().GetBytes(chars, 0, 3, key, 0);
171-
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
172-
rijn.CreateEncryptor(key, someOtherBytesForIV);
173-
}
174-
}",
175-
GetCSharpResultAt(12, 9, 9, 24, "ICryptoTransform SymmetricAlgorithm.CreateEncryptor(byte[] rgbKey, byte[] rgbIV)", "void TestClass.TestMethod(byte[] key, byte[] someOtherBytesForIV)", "char[]", "void TestClass.TestMethod(byte[] key, byte[] someOtherBytesForIV)"));
176-
}
177-
178158
[Fact]
179159
public void Test_HardcodedInStringWithVariable_CreateEncryptor_Diagnostic()
180160
{
@@ -668,6 +648,25 @@ public void TestMethod(char[] chars, byte[] key, byte[] someOtherBytesForIV)
668648
}");
669649
}
670650

651+
[Fact]
652+
public void Test_ASCIIEncodingGetBytesWithConstantCharArrayAndInt32AndInt32AndByteArrayAndInt32Parameters_CreateEncryptor_NoDiagnostic()
653+
{
654+
VerifyCSharp(@"
655+
using System.Text;
656+
using System.Security.Cryptography;
657+
658+
class TestClass
659+
{
660+
public void TestMethod(byte[] key, byte[] someOtherBytesForIV)
661+
{
662+
char[] chars = new char[] {'1', '2', '3'};
663+
new ASCIIEncoding().GetBytes(chars, 0, 3, key, 0);
664+
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
665+
rijn.CreateEncryptor(key, someOtherBytesForIV);
666+
}
667+
}");
668+
}
669+
671670
[Fact]
672671
public void Test_ElementTypeIsTypeParameter_NoDiagnostic()
673672
{

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@ arguments[0].Parameter.Type is IArrayTypeSymbol arrayTypeSymbol &&
9696
taintedMethodsNeedsPointsToAnalysis: null,
9797
taintedMethodsNeedsValueContentAnalysis: null,
9898
taintConstantArray: true);
99-
builder.AddSourceInfo(
100-
WellKnownTypeNames.SystemChar,
101-
isInterface: false,
102-
taintedProperties: null,
103-
taintedMethodsNeedsPointsToAnalysis: null,
104-
taintedMethodsNeedsValueContentAnalysis: null,
105-
taintConstantArray: true);
10699

107100
SourceInfos = builder.ToImmutableAndFree();
108101
}

0 commit comments

Comments
 (0)