forked from FIRSTdotorg/cvss-v4-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
189 lines (180 loc) · 6.26 KB
/
app.js
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
// Copyright FIRST, Red Hat, and contributors
// SPDX-License-Identifier: BSD-2-Clause
const app = Vue.createApp({
data() {
return {
cvssConfigData: cvssConfig,
maxComposedData: maxComposed,
maxSeverityData: maxSeverity,
expectedMetricOrder: expectedMetricOrder,
cvssMacroVectorDetailsData: cvssMacroVectorDetails,
cvssMacroVectorValuesData: cvssMacroVectorValues,
showDetails: false,
cvssSelected: null,
header_height: 0,
lookup: cvssLookup_global,
macroVector: null
}
},
methods: {
buttonClass(isPrimary, big = false) {
result = "btn btn-m"
if (isPrimary) {
result += " btn-primary"
}
if (!big) {
result += " btn-sm"
}
return result
},
scoreClass(qualScore) {
if (qualScore == "Low") {
return "c-hand text-success"
}
else if (qualScore == "Medium") {
return "c-hand text-warning"
}
else if (qualScore == "High") {
return "c-hand text-error"
}
else if (qualScore == "Critical") {
return "c-hand text-error text-bold"
}
else {
return "c-hand text-gray"
}
},
copyVector() {
navigator.clipboard.writeText(this.vector)
window.location.hash = this.vector
},
onButton(metric, value) {
this.cvssSelected[metric] = value
window.location.hash = this.vector
},
setButtonsToVector(vector) {
this.resetSelected()
metrics = vector.split("/")
// Remove hash + CVSS v4.0 prefix
prefix = metrics[0].slice(1);
if (prefix != "CVSS:4.0") {
console.log("Error invalid vector, missing CVSS v4.0 prefix")
return
}
metrics.shift()
// Ensure compliance first
toSelect = {}
oi = 0
for (index in metrics) {
[key, value] = metrics[index].split(":")
expected = Object.entries(this.expectedMetricOrder)[oi++]
while (true) {
// If out of possible metrics ordering, it not a valid value thus
// the vector is invalid
if (expected == undefined) {
console.log("Error invalid vector, too many metric values")
return
}
if (key != expected[0]) {
// If not this metric but is mandatory, the vector is invalid
// As the only mandatory ones are from the Base group, 11 is the
// number of metrics part of it.
if (oi <= 11) {
console.log("Error invalid vector, missing mandatory metrics")
return
}
// If a non-mandatory, retry
expected = Object.entries(this.expectedMetricOrder)[oi++]
continue
}
break
}
// The value MUST be part of the metric's values, case insensitive
if (!expected[1].includes(value)) {
console.log("Error invalid vector, for key " + key + ", value " + value + " is not in " + expected[1])
return
}
if (key in this.cvssSelected) {
toSelect[key] = value
}
}
// Apply iff is compliant
for (key in toSelect) {
this.cvssSelected[key] = toSelect[key]
}
this.macroVector = macroVector(this.cvssSelected)
},
onReset() {
window.location.hash = ""
},
resetSelected() {
this.cvssSelected = {}
for ([metricType, metricTypeData] of Object.entries(this.cvssConfigData)) {
for ([metricGroup, metricGroupData] of Object.entries(metricTypeData.metric_groups)) {
for ([metric, metricData] of Object.entries(metricGroupData)) {
this.cvssSelected[metricData.short] = metricData.selected
}
}
}
},
splitObjectEntries(object, chunkSize) {
arr = Object.entries(object)
res = [];
for (let i = 0; i < arr.length; i += chunkSize) {
chunk = arr.slice(i, i + chunkSize)
res.push(chunk)
}
return res
}
},
computed: {
vector() {
value = "CVSS:4.0"
for (metric in this.expectedMetricOrder) {
selected = this.cvssSelected[metric]
if (selected != "X") {
value = value.concat("/" + metric + ":" + selected)
}
}
return value
},
score() {
return cvss_score(
this.cvssSelected,
this.lookup,
this.maxSeverityData,
this.macroVector)
},
qualScore() {
if (this.score == 0) {
return "None"
}
else if (this.score < 4.0) {
return "Low"
}
else if (this.score < 7.0) {
return "Medium"
}
else if (this.score < 9.0) {
return "High"
}
else {
return "Critical"
}
},
},
beforeMount() {
this.resetSelected()
},
mounted() {
this.setButtonsToVector(window.location.hash)
window.addEventListener("hashchange", () => {
this.setButtonsToVector(window.location.hash)
})
const resizeObserver = new ResizeObserver(() => {
this.header_height = document.getElementById('header').clientHeight
})
resizeObserver.observe(document.getElementById('header'))
}
})
app.mount("#app")