-
Notifications
You must be signed in to change notification settings - Fork 771
/
Copy pathFindPossibleSecretsVisitor.cs
201 lines (173 loc) · 9.01 KB
/
FindPossibleSecretsVisitor.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Bicep.Core.Extensions;
using Bicep.Core.Semantics;
using Bicep.Core.Semantics.Namespaces;
using Bicep.Core.Syntax;
using Bicep.Core.TypeSystem;
using Bicep.Core.TypeSystem.Types;
using Microsoft.WindowsAzure.ResourceStack.Common.Extensions;
namespace Bicep.Core.Analyzers.Linter.Common
{
public sealed class FindPossibleSecretsVisitor : AstVisitor
{
public record PossibleSecret(SyntaxBase Syntax, string FoundMessage) { }
private readonly SemanticModel semanticModel;
private readonly List<PossibleSecret> possibleSecrets = new();
private uint trailingAccessExpressions = 0;
/// <summary>
/// Searches in an expression for possible references to sensitive data, such as secure parameters or list* functions (many but
/// not all of which return sensitive information)
/// </summary>
public static IEnumerable<PossibleSecret> FindPossibleSecretsInExpression(SemanticModel semanticModel, SyntaxBase syntax)
{
FindPossibleSecretsVisitor visitor = new(semanticModel);
visitor.Visit(syntax);
return visitor.possibleSecrets;
}
private FindPossibleSecretsVisitor(SemanticModel model)
{
this.semanticModel = model;
}
public override void VisitVariableAccessSyntax(VariableAccessSyntax syntax)
{
// Look for references of secure values, e.g.:
//
// @secure()
// param secureParam string
// output badResult string = 'this is the value ${secureParam}'
possibleSecrets.AddRange(FindPathsToSecureTypeComponents(semanticModel.GetTypeInfo(syntax))
.Select(pathToSecureComponent => new PossibleSecret(syntax.Name, PossibleSecretMessage(syntax.Name.IdentifierName + pathToSecureComponent))));
base.VisitVariableAccessSyntax(syntax);
}
public override void VisitPropertyAccessSyntax(PropertyAccessSyntax syntax)
{
possibleSecrets.AddRange(FindPathsToSecureTypeComponents(semanticModel.GetTypeInfo(syntax))
.Select(pathToSecureComponent => new PossibleSecret(syntax.PropertyName, PossibleSecretMessage(syntax.ToString() + pathToSecureComponent))));
trailingAccessExpressions++;
base.VisitPropertyAccessSyntax(syntax);
trailingAccessExpressions--;
}
public override void VisitArrayAccessSyntax(ArrayAccessSyntax syntax)
{
possibleSecrets.AddRange(FindPathsToSecureTypeComponents(semanticModel.GetTypeInfo(syntax))
.Select(pathToSecureComponent => new PossibleSecret(syntax.IndexExpression, PossibleSecretMessage(syntax.ToString() + pathToSecureComponent))));
trailingAccessExpressions++;
base.VisitArrayAccessSyntax(syntax);
trailingAccessExpressions--;
}
private static string PossibleSecretMessage(string possibleSecretName) => string.Format(CoreResources.PossibleSecretMessageSecureParam, possibleSecretName);
private IEnumerable<string> FindPathsToSecureTypeComponents(TypeSymbol type) => FindPathsToSecureTypeComponents(type, "", new());
private IEnumerable<string> FindPathsToSecureTypeComponents(TypeSymbol type, string path, HashSet<TypeSymbol> visited)
{
// types can be recursive. cut out early if we've already seen this type
if (visited.Contains(type))
{
yield break;
}
visited.Add(type);
if (type.ValidationFlags.HasFlag(TypeSymbolValidationFlags.IsSecure))
{
yield return path;
}
if (type is UnionType union)
{
foreach (var variantPath in union.Members.SelectMany(m => FindPathsToSecureTypeComponents(m.Type, path, visited)))
{
yield return variantPath;
}
}
// if the expression being visited is dereferencing a specific property or index of this type, we shouldn't warn if the type under inspection
// *contains* properties or indices that are flagged as secure. We will have already warned if those have been accessed in the expression, and
// if they haven't, then the value dereferenced isn't sensitive
//
// param p {
// prop: {
// @secure()
// nestedSecret: string
// nestedInnocuousProperty: string
// }
// }
//
// output objectContainingSecrets object = p // <-- should be flagged
// output propertyContainingSecrets object = p.prop // <-- should be flagged
// output nestedSecret string = p.prop.nestedSecret // <-- should be flagged
// output siblingOfSecret string = p.prop.nestedInnocuousData // <-- should NOT be flagged
if (trailingAccessExpressions == 0)
{
switch (type)
{
case ObjectType obj:
if (obj.AdditionalProperties?.TypeReference.Type is TypeSymbol addlPropsType)
{
foreach (var dictMemberPath in FindPathsToSecureTypeComponents(addlPropsType, $"{path}.*", visited))
{
yield return dictMemberPath;
}
}
foreach (var propertyPath in obj.Properties.SelectMany(p => FindPathsToSecureTypeComponents(p.Value.TypeReference.Type, $"{path}.{p.Key}", visited)))
{
yield return propertyPath;
}
break;
case TupleType tuple:
foreach (var pathFromIndex in tuple.Items.SelectMany((ITypeReference typeAtIndex, int index) => FindPathsToSecureTypeComponents(typeAtIndex.Type, $"{path}[{index}]", visited)))
{
yield return pathFromIndex;
}
break;
case ArrayType array:
foreach (var pathFromElement in FindPathsToSecureTypeComponents(array.Item.Type, $"{path}[*]", visited))
{
yield return pathFromElement;
}
break;
}
}
visited.Remove(type);
}
public override void VisitFunctionCallSyntax(FunctionCallSyntax syntax)
{
// Look for usage of list*() stand-alone function, e.g.:
//
// output badResult object = listKeys(resourceId('Microsoft.Storage/storageAccounts', 'storageName'), '2021-02-01')
//
if (SemanticModelHelper.TryGetFunctionInNamespace(semanticModel, AzNamespaceType.BuiltInName, syntax) is FunctionCallSyntaxBase listFunction
&& listFunction.Name.IdentifierName.StartsWithOrdinalInsensitively(LanguageConstants.ListFunctionPrefix))
{
string foundMessage = string.Format(CoreResources.PossibleSecretMessageFunction, syntax.Name.IdentifierName);
this.possibleSecrets.Add(new PossibleSecret(syntax, foundMessage));
}
base.VisitFunctionCallSyntax(syntax);
}
public override void VisitInstanceFunctionCallSyntax(InstanceFunctionCallSyntax syntax)
{
if (syntax.Name.IdentifierName.StartsWithOrdinalInsensitively(LanguageConstants.ListFunctionPrefix))
{
bool found = false;
if (semanticModel.ResourceMetadata.TryLookup(syntax.BaseExpression) is not null)
{
// It's a usage of a list*() member function for a resource value, e.g.:
//
// output badResult object = stg.listKeys().keys[0].value
//
found = true;
}
else if (SemanticModelHelper.TryGetFunctionInNamespace(semanticModel, AzNamespaceType.BuiltInName, syntax) is not null)
{
// It's a usage of a built-in list*() function as a member of the built-in "az" module, e.g.:
//
// output badResult object = az.listKeys(resourceId('Microsoft.Storage/storageAccounts', 'storageName'), '2021-02-01')
//
found = true;
}
if (found)
{
string foundMessage = string.Format(CoreResources.PossibleSecretMessageFunction, syntax.Name.IdentifierName);
this.possibleSecrets.Add(new PossibleSecret(syntax, foundMessage));
}
}
base.VisitInstanceFunctionCallSyntax(syntax);
}
}
}