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
|
/*
* SonarQube
* Copyright (C) 2009-2022 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { flatten, groupBy, sortBy } from 'lodash';
import {
renderCWECategory,
renderOwaspTop102021Category,
renderOwaspTop10Category,
renderSansTop25Category,
renderSonarSourceSecurityCategory
} from '../../helpers/security-standard';
import { SecurityStandard } from '../../types/security';
import {
Hotspot,
HotspotResolution,
HotspotStatus,
HotspotStatusFilter,
HotspotStatusOption,
RawHotspot,
ReviewHistoryElement,
ReviewHistoryType,
RiskExposure
} from '../../types/security-hotspots';
import {
Dict,
FlowLocation,
SourceViewerFile,
StandardSecurityCategories
} from '../../types/types';
const OTHERS_SECURITY_CATEGORY = 'others';
export const RISK_EXPOSURE_LEVELS = [RiskExposure.HIGH, RiskExposure.MEDIUM, RiskExposure.LOW];
export const SECURITY_STANDARDS = [
SecurityStandard.SONARSOURCE,
SecurityStandard.OWASP_TOP10,
SecurityStandard.OWASP_TOP10_2021,
SecurityStandard.SANS_TOP25,
SecurityStandard.CWE
];
export const SECURITY_STANDARD_RENDERER = {
[SecurityStandard.OWASP_TOP10]: renderOwaspTop10Category,
[SecurityStandard.OWASP_TOP10_2021]: renderOwaspTop102021Category,
[SecurityStandard.SANS_TOP25]: renderSansTop25Category,
[SecurityStandard.SONARSOURCE]: renderSonarSourceSecurityCategory,
[SecurityStandard.CWE]: renderCWECategory
};
export function mapRules(rules: Array<{ key: string; name: string }>): Dict<string> {
return rules.reduce((ruleMap: Dict<string>, r) => {
ruleMap[r.key] = r.name;
return ruleMap;
}, {});
}
export function groupByCategory(
hotspots: RawHotspot[] = [],
securityCategories: StandardSecurityCategories
) {
const groups = groupBy(hotspots, h => h.securityCategory);
const groupList = Object.keys(groups).map(key => ({
key,
title: getCategoryTitle(key, securityCategories),
hotspots: groups[key]
}));
return [
...sortBy(
groupList.filter(group => group.key !== OTHERS_SECURITY_CATEGORY),
group => group.title
),
...groupList.filter(({ key }) => key === OTHERS_SECURITY_CATEGORY)
];
}
export function sortHotspots(hotspots: RawHotspot[], securityCategories: Dict<{ title: string }>) {
return sortBy(hotspots, [
h => RISK_EXPOSURE_LEVELS.indexOf(h.vulnerabilityProbability),
h => getCategoryTitle(h.securityCategory, securityCategories),
h => h.message
]);
}
function getCategoryTitle(key: string, securityCategories: StandardSecurityCategories) {
return securityCategories[key] ? securityCategories[key].title : key;
}
export function constructSourceViewerFile(
{ component, project }: Hotspot,
lines?: number
): SourceViewerFile {
return {
key: component.key,
measures: { lines: lines ? lines.toString() : undefined },
path: component.path || '',
project: project.key,
projectName: project.name,
q: component.qualifier,
uuid: ''
};
}
export function getHotspotReviewHistory(hotspot: Hotspot): ReviewHistoryElement[] {
const history: ReviewHistoryElement[] = [];
if (hotspot.creationDate) {
history.push({
type: ReviewHistoryType.Creation,
date: hotspot.creationDate,
user: {
...hotspot.authorUser,
name: hotspot.authorUser.name || hotspot.authorUser.login
}
});
}
if (hotspot.changelog && hotspot.changelog.length > 0) {
history.push(
...hotspot.changelog.map(log => ({
type: ReviewHistoryType.Diff,
date: log.creationDate,
user: {
active: log.isUserActive,
avatar: log.avatar,
name: log.userName || log.user
},
diffs: log.diffs
}))
);
}
if (hotspot.comment && hotspot.comment.length > 0) {
history.push(
...hotspot.comment.map(comment => ({
type: ReviewHistoryType.Comment,
date: comment.createdAt,
updatable: comment.updatable,
user: {
...comment.user,
name: comment.user.name || comment.user.login
},
html: comment.htmlText,
key: comment.key,
markdown: comment.markdown
}))
);
}
return sortBy(history, elt => elt.date).reverse();
}
const STATUS_AND_RESOLUTION_TO_STATUS_OPTION = {
[HotspotStatus.TO_REVIEW]: HotspotStatusOption.TO_REVIEW,
[HotspotStatus.REVIEWED]: HotspotStatusOption.FIXED,
[HotspotResolution.ACKNOWLEDGED]: HotspotStatusOption.ACKNOWLEDGED,
[HotspotResolution.FIXED]: HotspotStatusOption.FIXED,
[HotspotResolution.SAFE]: HotspotStatusOption.SAFE
};
export function getStatusOptionFromStatusAndResolution(
status: HotspotStatus,
resolution?: HotspotResolution
) {
// Resolution is the most determinist info here, so we use it first to get the matching status option
// If not provided, we use the status (which will be TO_REVIEW)
return STATUS_AND_RESOLUTION_TO_STATUS_OPTION[resolution ?? status];
}
const STATUS_OPTION_TO_STATUS_AND_RESOLUTION_MAP = {
[HotspotStatusOption.TO_REVIEW]: { status: HotspotStatus.TO_REVIEW, resolution: undefined },
[HotspotStatusOption.ACKNOWLEDGED]: {
status: HotspotStatus.REVIEWED,
resolution: HotspotResolution.ACKNOWLEDGED
},
[HotspotStatusOption.FIXED]: {
status: HotspotStatus.REVIEWED,
resolution: HotspotResolution.FIXED
},
[HotspotStatusOption.SAFE]: {
status: HotspotStatus.REVIEWED,
resolution: HotspotResolution.SAFE
}
};
export function getStatusAndResolutionFromStatusOption(statusOption: HotspotStatusOption) {
return STATUS_OPTION_TO_STATUS_AND_RESOLUTION_MAP[statusOption];
}
const STATUS_OPTION_TO_STATUS_FILTER = {
[HotspotStatusOption.TO_REVIEW]: HotspotStatusFilter.TO_REVIEW,
[HotspotStatusOption.ACKNOWLEDGED]: HotspotStatusFilter.ACKNOWLEDGED,
[HotspotStatusOption.FIXED]: HotspotStatusFilter.FIXED,
[HotspotStatusOption.SAFE]: HotspotStatusFilter.SAFE
};
export function getStatusFilterFromStatusOption(statusOption: HotspotStatusOption) {
return STATUS_OPTION_TO_STATUS_FILTER[statusOption];
}
function getSecondaryLocations(flows: RawHotspot['flows']) {
const parsedFlows: FlowLocation[][] = (flows || [])
.filter(flow => flow.locations !== undefined)
.map(flow => flow.locations!.filter(location => location.textRange != null))
.map(flow =>
flow.map(location => {
return { ...location };
})
);
const onlySecondaryLocations = parsedFlows.every(flow => flow.length === 1);
return onlySecondaryLocations
? { secondaryLocations: orderLocations(flatten(parsedFlows)), flows: [] }
: { secondaryLocations: [], flows: parsedFlows.map(reverseLocations) };
}
export function getLocations(rawFlows: RawHotspot['flows'], selectedFlowIndex: number | undefined) {
const { flows, secondaryLocations } = getSecondaryLocations(rawFlows);
if (selectedFlowIndex !== undefined) {
return flows[selectedFlowIndex] || [];
}
return flows.length > 0 ? flows[0] : secondaryLocations;
}
function orderLocations(locations: FlowLocation[]) {
return sortBy(
locations,
location => location.textRange && location.textRange.startLine,
location => location.textRange && location.textRange.startOffset
);
}
function reverseLocations(locations: FlowLocation[]): FlowLocation[] {
const x = [...locations];
x.reverse();
return x;
}
export function getFilePath(component: string, project: string) {
return component.replace(project, '').replace(':', '');
}
|