-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCycodeTreeViewControl.xaml.cs
More file actions
268 lines (223 loc) · 10.5 KB
/
CycodeTreeViewControl.xaml.cs
File metadata and controls
268 lines (223 loc) · 10.5 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using Cycode.VisualStudio.Extension.Shared.Cli.DTO;
using Cycode.VisualStudio.Extension.Shared.Cli.DTO.ScanResult;
using Cycode.VisualStudio.Extension.Shared.Cli.DTO.ScanResult.Iac;
using Cycode.VisualStudio.Extension.Shared.Cli.DTO.ScanResult.Sast;
using Cycode.VisualStudio.Extension.Shared.Cli.DTO.ScanResult.Sca;
using Cycode.VisualStudio.Extension.Shared.Cli.DTO.ScanResult.Secret;
using Cycode.VisualStudio.Extension.Shared.Components.TreeView.Nodes;
using Cycode.VisualStudio.Extension.Shared.Components.TreeView.Nodes.DetectionNodes;
using Cycode.VisualStudio.Extension.Shared.DTO;
using Cycode.VisualStudio.Extension.Shared.Helpers;
using Cycode.VisualStudio.Extension.Shared.Icons;
using Cycode.VisualStudio.Extension.Shared.Services;
namespace Cycode.VisualStudio.Extension.Shared.Components.TreeView;
public partial class CycodeTreeViewControl {
private static readonly IScanResultsService _scanResultsService = ServiceLocator.GetService<IScanResultsService>();
private static readonly IToolWindowMessengerService _toolWindowMessengerService =
ServiceLocator.GetService<IToolWindowMessengerService>();
private static readonly ITemporaryStateService _tempState =
ServiceLocator.GetService<ITemporaryStateService>();
private static readonly ILoggerService _logger = ServiceLocator.GetService<ILoggerService>();
public CycodeTreeViewControl() {
InitializeComponent();
RootNodesManager.AddRootNodes(TreeView.Items);
}
private RootNodesManager RootNodesManager { get; } = new();
private void OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) {
string filePath = string.Empty;
int line = 0;
switch (e.NewValue) {
case SecretDetectionNode secretDetectionNode: {
SecretDetection detection = secretDetectionNode.Detection;
filePath = detection.DetectionDetails.GetFilePath();
line = detection.DetectionDetails.GetLineNumber();
_toolWindowMessengerService.Send(
new MessageEventArgs(MessengerCommand.LoadSecretViolationCardControl, detection)
);
break;
}
case ScaDetectionNode scaDetectionNode: {
ScaDetection detection = scaDetectionNode.Detection;
filePath = detection.DetectionDetails.GetFilePath();
line = detection.DetectionDetails.GetLineNumber();
_toolWindowMessengerService.Send(
new MessageEventArgs(MessengerCommand.LoadScaViolationCardControl, detection)
);
break;
}
case IacDetectionNode iacDetectionNode: {
IacDetection detection = iacDetectionNode.Detection;
filePath = detection.DetectionDetails.GetFilePath();
line = detection.DetectionDetails.GetLineNumber();
_toolWindowMessengerService.Send(
new MessageEventArgs(MessengerCommand.LoadIacViolationCardControl, detection)
);
break;
}
case SastDetectionNode sastDetectionNode: {
SastDetection detection = sastDetectionNode.Detection;
filePath = detection.DetectionDetails.GetFilePath();
line = detection.DetectionDetails.GetLineNumber();
_toolWindowMessengerService.Send(
new MessageEventArgs(MessengerCommand.LoadSastViolationCardControl, detection)
);
break;
}
}
if (string.IsNullOrEmpty(filePath)) return;
FileNavigator.NavigateToFileAndLine(filePath, line);
}
private static int GetSeverityWeight(string severity) {
return severity.ToLower() switch {
"critical" => 5,
"high" => 4,
"medium" => 3,
"low" => 2,
"info" => 1,
_ => 0
};
}
private static HashSet<string> GetEnabledSeverityFilters() {
HashSet<string> enabledSeverityFilters = [];
if (_tempState.IsTreeViewFilterByCriticalSeverityEnabled) {
enabledSeverityFilters.Add("critical");
}
if (_tempState.IsTreeViewFilterByHighSeverityEnabled) {
enabledSeverityFilters.Add("high");
}
if (_tempState.IsTreeViewFilterByMediumSeverityEnabled) {
enabledSeverityFilters.Add("medium");
}
if (_tempState.IsTreeViewFilterByLowSeverityEnabled) {
enabledSeverityFilters.Add("low");
}
if (_tempState.IsTreeViewFilterByInfoSeverityEnabled) {
enabledSeverityFilters.Add("info");
}
return enabledSeverityFilters;
}
private static string GetRootNodeSummary(IEnumerable<DetectionBase> sortedDetections) {
// detections must be sorted by severity already
IEnumerable<IGrouping<string, DetectionBase>> groupedBySeverity = sortedDetections
.GroupBy(detection => detection.Severity);
return string.Join(" | ", groupedBySeverity
.Select(group => $"{group.Key} - {group.Count()}"));
}
private static string GetRelativeToProjectPath(string filePath) {
string projectRootPath = Path.GetFullPath(SolutionHelper.GetSolutionRootDirectory());
string normalizedFilePath = Path.GetFullPath(filePath);
string relativePath = normalizedFilePath.Replace(projectRootPath, string.Empty);
return relativePath.StartsWith(Path.DirectorySeparatorChar.ToString())
? relativePath.Substring(1)
: relativePath;
}
private void CreateDetectionNodes(
CliScanType scanType,
IEnumerable<DetectionBase> detections,
Func<DetectionBase, BaseNode> createNodeCallback
) {
HashSet<string> enabledSeverityFilters = GetEnabledSeverityFilters();
List<DetectionBase> severityFilteredDetections = detections
.Where(detection => !enabledSeverityFilters.Contains(detection.Severity.ToLower()))
.ToList();
IEnumerable<IGrouping<string, DetectionBase>> detectionsByFile =
severityFilteredDetections.GroupBy(detection => detection.GetDetectionDetails().GetFilePath());
ScanTypeNode scanTypeNode = RootNodesManager.GetScanTypeNode(scanType);
scanTypeNode.Summary = GetRootNodeSummary(severityFilteredDetections);
foreach (IGrouping<string, DetectionBase> detectionsInFile in detectionsByFile) {
string filePath = detectionsInFile.Key;
FileNode fileNode = new() {
Title = GetRelativeToProjectPath(filePath),
Summary = $"{detectionsInFile.Count()} vulnerabilities",
Icon = ExtensionIcons.GetFileIconPath(filePath)
};
List<DetectionBase> sortedDetectionsInFile = detectionsInFile
.OrderByDescending(detection => GetSeverityWeight(detection.Severity))
.ThenBy(detection => detection.GetDetectionDetails().GetLineNumber())
.ToList();
foreach (DetectionBase detection in sortedDetectionsInFile) fileNode.Items.Add(createNodeCallback(detection));
scanTypeNode.Items.Add(fileNode);
}
}
private void CreateSecretDetectionNodes() {
IEnumerable<DetectionBase> detections = _scanResultsService.GetDetections(CliScanType.Secret);
CreateDetectionNodes(CliScanType.Secret, detections, detectionBase => {
SecretDetection detection = (SecretDetection)detectionBase;
SecretDetectionNode node = new() {
Title = detection.GetFormattedNodeTitle(),
Icon = ExtensionIcons.GetSeverityIconPath(detection.Severity),
Detection = detection
};
return node;
});
}
private void CreateScaDetectionNodes() {
IEnumerable<DetectionBase> detections = _scanResultsService.GetDetections(CliScanType.Sca);
CreateDetectionNodes(CliScanType.Sca, detections, detectionBase => {
ScaDetection detection = (ScaDetection)detectionBase;
ScaDetectionNode node = new() {
Title = detection.GetFormattedNodeTitle(),
Icon = ExtensionIcons.GetSeverityIconPath(detection.Severity),
Detection = detection
};
return node;
});
}
private void CreateIacDetectionNodes() {
IEnumerable<DetectionBase> detections = _scanResultsService.GetDetections(CliScanType.Iac);
CreateDetectionNodes(CliScanType.Iac, detections, detectionBase => {
IacDetection detection = (IacDetection)detectionBase;
IacDetectionNode node = new() {
Title = detection.GetFormattedNodeTitle(),
Icon = ExtensionIcons.GetSeverityIconPath(detection.Severity),
Detection = detection
};
return node;
});
}
private void CreateSastDetectionNodes() {
IEnumerable<DetectionBase> detections = _scanResultsService.GetDetections(CliScanType.Sast);
CreateDetectionNodes(CliScanType.Sast, detections, detectionBase => {
SastDetection detection = (SastDetection)detectionBase;
SastDetectionNode node = new() {
Title = detection.GetFormattedNodeTitle(),
Icon = ExtensionIcons.GetSeverityIconPath(detection.Severity),
Detection = detection
};
return node;
});
}
private void CreateNodes() {
RootNodesManager.AddRootNodes(TreeView.Items);
CreateSecretDetectionNodes();
CreateScaDetectionNodes();
CreateIacDetectionNodes();
CreateSastDetectionNodes();
}
public void RefreshTree() {
_logger.Debug("Refresh tree view");
TreeView.Items.Clear();
CreateNodes();
}
private void SetIsExpandedToAllNodes(bool isExpanded) {
Stack<BaseNode> stack = new(TreeView.Items.Cast<BaseNode>());
while (stack.Any()) {
BaseNode node = stack.Pop();
foreach (BaseNode child in node.Items) stack.Push(child);
node.IsExpanded = isExpanded;
}
TreeView.UpdateLayout();
}
public void ExpandAllNodes() {
_logger.Debug("Expand all nodes");
SetIsExpandedToAllNodes(true);
}
public void CollapseAllNodes() {
_logger.Debug("Collapse all nodes");
SetIsExpandedToAllNodes(false);
}
}