-
Notifications
You must be signed in to change notification settings - Fork 5
/
FindingsList.tsx
273 lines (246 loc) · 7.1 KB
/
FindingsList.tsx
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
269
270
271
272
273
import React = require("react");
import { IGrypeFinding } from "../../IGrypeFinding";
import { Table } from "semantic-ui-react";
import _ = require("lodash");
import { Description } from "./Description";
export function FindingsList(props: IProps): JSX.Element {
const [state, dispatch] = React.useReducer(reducer, initialState);
const { column, direction } = state;
const currentRows = state.sort(rows(props.findings));
return (
<Table sortable>
<Table.Header>
<Table.Row>
<Table.HeaderCell
sorted={column === "packageName" ? direction : undefined}
onClick={() =>
dispatch({ type: "CHANGE_SORT", column: "packageName" })
}
>
Package
</Table.HeaderCell>
<Table.HeaderCell
sorted={column === "vulnerability" ? direction : undefined}
onClick={() =>
dispatch({ type: "CHANGE_SORT", column: "vulnerability" })
}
>
Vulnerability
</Table.HeaderCell>
<Table.HeaderCell
sorted={column === "severity" ? direction : undefined}
onClick={() =>
dispatch({ type: "CHANGE_SORT", column: "severity" })
}
>
Severity
</Table.HeaderCell>
<Table.HeaderCell
sorted={column === "fixedInVersion" ? direction : undefined}
onClick={() =>
dispatch({ type: "CHANGE_SORT", column: "fixedInVersion" })
}
>
Fixed In Version
</Table.HeaderCell>
<Table.HeaderCell
sorted={column === "description" ? direction : undefined}
onClick={() =>
dispatch({ type: "CHANGE_SORT", column: "description" })
}
>
Description
</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
{currentRows.map(
(
{
packageName,
vulnerability,
severity,
fixedInVersion,
description,
},
index
) => (
<Table.Row key={index}>
<Table.Cell>{packageName}</Table.Cell>
<Table.Cell>{tryHyperlinking(vulnerability)}</Table.Cell>
<Table.Cell>{tryColoring(severity)}</Table.Cell>
<Table.Cell>{fixedInVersion}</Table.Cell>
<Table.Cell>
<Description
text={description.text}
location={description.location}
openFile={props.openFile}
/>
</Table.Cell>
</Table.Row>
)
)}
</Table.Body>
</Table>
);
}
interface IProps {
findings: IGrypeFinding[];
openFile(uri: string): void;
}
interface IState {
column: string | undefined;
direction: Direction | undefined;
sort(rows: IRow[]): IRow[];
}
interface IRow {
packageName: string; // note: "package" is a reserved word in strict mode, hence "packageName"
vulnerability: string;
severity: string;
fixedInVersion: string;
description: IDescription;
}
interface IDescription {
text: string;
location: string;
}
interface IAction {
type: "CHANGE_SORT";
column: Column;
}
type Direction = "ascending" | "descending";
type Column =
| "packageName"
| "vulnerability"
| "severity"
| "fixedInVersion"
| "description";
function rows(findings: IGrypeFinding[]): IRow[] {
return findings.map((f) => {
const row: IRow = {
packageName: `${f.artifact.name} (${f.artifact.version})`,
vulnerability: f.vulnerability.id,
severity: f.vulnerability.severity,
fixedInVersion: f.vulnerability.fixedInVersion || "",
description: {
text: f.vulnerability.description,
location: f.artifact.locations[0],
},
};
return row;
});
}
function tryColoring(severity: string): JSX.Element {
let color: string | null = null;
switch (severity.toLocaleLowerCase()) {
case "critical":
color = "#f72e13";
break;
case "high":
color = "#f75813";
break;
case "medium":
color = "#f78913";
break;
case "low":
color = "#e8b73c";
break;
case "negligible":
color = "#95bf48";
break;
}
if (color) {
return <span style={{ color }}>{severity}</span>;
}
return <span>{severity}</span>;
}
function tryHyperlinking(vulnerabilityID: string): JSX.Element {
function hyperlink(url: string, vulnerabilityID: string): JSX.Element {
return (
<span className="vulnerabilityID">
<a href={url}>{vulnerabilityID}</a>
</span>
);
}
if (vulnerabilityID.startsWith("CVE-")) {
const url = `https://cve.mitre.org/cgi-bin/cvename.cgi?name=${vulnerabilityID}`;
return hyperlink(url, vulnerabilityID);
} else if (vulnerabilityID.startsWith("GHSA-")) {
const url = `https://github.com/advisories/${vulnerabilityID}`;
return hyperlink(url, vulnerabilityID);
}
return <span className="vulnerabilityID">{vulnerabilityID}</span>;
}
function severityNumber(severity: string): number {
switch (severity.toLowerCase()) {
case "critical":
return 100;
case "high":
return 80;
case "medium":
return 60;
case "low":
return 40;
case "negligible":
return 20;
}
return 1000; // Surface unrecognized input values lest they get hidden
}
function sort(rows: IRow[], column: Column, direction: Direction): IRow[] {
const sorted = _.orderBy(
rows,
[
(row) => {
switch (column) {
case "packageName":
return row.packageName.toLowerCase();
case "vulnerability":
return row.vulnerability.toLowerCase();
case "severity":
return severityNumber(row.severity);
case "fixedInVersion":
return row.fixedInVersion?.toLowerCase();
case "description":
return row.description.text.toLowerCase();
}
},
],
direction === "ascending" ? "asc" : "desc"
);
return sorted;
}
function initialSort(rows: IRow[]): IRow[] {
return sort(rows, "severity", "descending");
}
function initialDirection(column: Column): Direction {
return column === "severity" ? "descending" : "ascending";
}
function updatedDirection(prior: Direction | undefined): Direction {
return prior === "ascending" ? "descending" : "ascending";
}
function reducer(state: IState, action: IAction): IState {
switch (action.type) {
case "CHANGE_SORT":
if (state.column === action.column) {
return {
...state,
direction: updatedDirection(state.direction),
sort: (rows) =>
sort(rows, action.column, updatedDirection(state.direction)),
};
}
return {
column: action.column,
direction: initialDirection(action.column),
sort: (rows) =>
sort(rows, action.column, initialDirection(action.column)),
};
default:
throw new Error();
}
}
const initialState: IState = {
column: "severity",
direction: "descending",
sort: (rows) => initialSort(rows),
};