forked from dotnet/roslyn-analyzers
/
HardcodedBytesSources.cs
103 lines (98 loc) · 4.53 KB
/
HardcodedBytesSources.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using System.Linq;
using Analyzer.Utilities.PooledObjects;
using Microsoft.CodeAnalysis;
namespace Analyzer.Utilities.FlowAnalysis.Analysis.TaintedDataAnalysis
{
internal static class HardcodedBytesSources
{
/// <summary>
/// <see cref="SourceInfo"/>s for hardcoded bytes tainted data sources.
/// </summary>
public static ImmutableHashSet<SourceInfo> SourceInfos { get; }
/// <summary>
/// Statically constructs.
/// </summary>
static HardcodedBytesSources()
{
var builder = PooledHashSet<SourceInfo>.GetInstance();
builder.AddSourceInfo(
WellKnownTypeNames.SystemConvert,
isInterface: false,
taintedProperties: null,
taintedMethodsNeedsPointsToAnalysis: null,
taintedMethodsNeedsValueContentAnalysis: new (MethodMatcher, ValueContentCheck[])[]{
(
(methodName, arguments) =>
methodName == "FromBase64String",
new ValueContentCheck[]{
(argumentPointsTos, argumentValueContents) => argumentValueContents.All(o => o.IsLiteralState),
}
),
});
builder.AddSourceInfoSpecifyingTaintedTargets(
WellKnownTypeNames.SystemTextEncoding,
isInterface: false,
taintedProperties: null,
taintedMethodsNeedsPointsToAnalysis: null,
taintedMethodsNeedsValueContentAnalysis: new (MethodMatcher, (ValueContentCheck, string)[])[]{
(
(methodName, arguments) =>
methodName == "GetBytes" &&
arguments.Count() == 5 &&
arguments[0].Parameter.Type.SpecialType == SpecialType.System_String,
new (ValueContentCheck, string)[]{
(
(argumentPointsTos, argumentValueContents) =>
argumentValueContents[0].IsLiteralState,
"chars"
),
}
),
(
(methodName, arguments) =>
methodName == "GetBytes" &&
arguments.Count() == 1 &&
arguments[0].Parameter.Type.SpecialType == SpecialType.System_String,
new (ValueContentCheck, string)[]{
(
(argumentPointsTos, argumentValueContents) =>
argumentValueContents[0].IsLiteralState,
TaintedTargetValue.Return
),
}
),
},
transferMethods: new (MethodMatcher, (string, string)[])[]{
(
(methodName, arguments) =>
methodName == "GetBytes" &&
arguments.Count() == 5 &&
arguments[0].Parameter.Type is IArrayTypeSymbol arrayTypeSymbol &&
arrayTypeSymbol.ElementType.SpecialType == SpecialType.System_Char,
new (string, string)[]{
("chars", "bytes"),
}
),
(
(methodName, arguments) =>
methodName == "GetBytes" &&
arguments.Count() == 5 &&
arguments[0].Parameter.Type.SpecialType == SpecialType.System_String,
new (string, string)[]{
("chars", "bytes"),
}
),
});
builder.AddSourceInfo(
WellKnownTypeNames.SystemByte,
isInterface: false,
taintedProperties: null,
taintedMethodsNeedsPointsToAnalysis: null,
taintedMethodsNeedsValueContentAnalysis: null,
taintConstantArray: true);
SourceInfos = builder.ToImmutableAndFree();
}
}
}