Skip to content

Commit 7432a11

Browse files
committed
util typescript conversion
1 parent 39d3c0c commit 7432a11

File tree

5 files changed

+594
-277
lines changed

5 files changed

+594
-277
lines changed

lib/cvss.js renamed to lib/cvss.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const util = require("./util");
2-
const score = require("./score");
1+
import { util } from "./util";
2+
import { score } from "./score";
33

44
/**
55
* Creates a new CVSS object
@@ -192,7 +192,7 @@ function CVSS(vector) {
192192
updateVectorValue,
193193
isValid,
194194
getImpactSubScore,
195-
getExploitabilitySubScore,
195+
getExploitabilitySubScore
196196
};
197197
}
198198

lib/score.js renamed to lib/score.ts

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const util = require("./util");
1+
import { util } from "./util";
22

33
/**
44
* Parses the vector to a number score
@@ -40,11 +40,7 @@ function getTemporalScore(vector) {
4040
const rcMetric = util.findMetricValue("RC", vectorObject);
4141
const reportConfidence = rcMetric ? rcMetric.numerical : 1;
4242

43-
return roundUp(
44-
baseScore * exploitCodeMaturity * remediationLevel * reportConfidence,
45-
1,
46-
vector
47-
);
43+
return roundUp(baseScore * exploitCodeMaturity * remediationLevel * reportConfidence, 1, vector);
4844
}
4945

5046
const calculateISCBase = function (vectorObject) {
@@ -62,18 +58,10 @@ const calculateISCBase = function (vectorObject) {
6258
*/
6359
function getEnvironmentalScore(vector) {
6460
const vectorObject = util.getVectorObject(vector);
65-
const scopeChanged =
66-
vectorObject.MS === "X" ? vectorObject.S === "C" : vectorObject.MS === "C";
61+
const scopeChanged = vectorObject.MS === "X" ? vectorObject.S === "C" : vectorObject.MS === "C";
6762
const modifiedISCBase = calculateISCModifiedBase(vectorObject);
68-
const modifiedExploitability = calculateModifiedExploitability(
69-
vectorObject,
70-
scopeChanged
71-
);
72-
const modifiedISC = calculateModifiedISC(
73-
modifiedISCBase,
74-
scopeChanged,
75-
vector
76-
);
63+
const modifiedExploitability = calculateModifiedExploitability(vectorObject, scopeChanged);
64+
const modifiedISC = calculateModifiedISC(modifiedISCBase, scopeChanged, vector);
7765

7866
if (modifiedISC <= 0) return 0;
7967

@@ -95,11 +83,7 @@ function getEnvironmentalScore(vector) {
9583
);
9684
}
9785
return roundUp(
98-
roundUp(
99-
Math.min(1.08 * (modifiedISC + modifiedExploitability), 10),
100-
1,
101-
vector
102-
) *
86+
roundUp(Math.min(1.08 * (modifiedISC + modifiedExploitability), 10), 1, vector) *
10387
eValue *
10488
rlValue *
10589
rcValue,
@@ -122,9 +106,7 @@ const calculateModifiedISC = function (iscBase, scopeChanged, vector) {
122106
if (util.getVersion(vector) === "3.0") {
123107
return 7.52 * (iscBase - 0.029) - 3.25 * Math.pow(iscBase - 0.02, 15);
124108
} else if (util.getVersion(vector) === "3.1") {
125-
return (
126-
7.52 * (iscBase - 0.029) - 3.25 * Math.pow(iscBase * 0.9731 - 0.02, 13)
127-
);
109+
return 7.52 * (iscBase - 0.029) - 3.25 * Math.pow(iscBase * 0.9731 - 0.02, 13);
128110
}
129111
};
130112

@@ -147,12 +129,9 @@ function calculateISCModifiedBase(vectorObject) {
147129
const irValue = util.findMetricValue("IR", vectorObject).numerical;
148130
const arValue = util.findMetricValue("AR", vectorObject).numerical;
149131

150-
if (!mcValue || mcValue.abbr === "X")
151-
mcValue = util.findMetricValue("C", vectorObject);
152-
if (!miValue || miValue.abbr === "X")
153-
miValue = util.findMetricValue("I", vectorObject);
154-
if (!maValue || maValue.abbr === "X")
155-
maValue = util.findMetricValue("A", vectorObject);
132+
if (!mcValue || mcValue.abbr === "X") mcValue = util.findMetricValue("C", vectorObject);
133+
if (!miValue || miValue.abbr === "X") miValue = util.findMetricValue("I", vectorObject);
134+
if (!maValue || maValue.abbr === "X") maValue = util.findMetricValue("A", vectorObject);
156135

157136
return Math.min(
158137
1 -
@@ -169,26 +148,14 @@ const calculateModifiedExploitability = function (vectorObject, scopeChanged) {
169148
let mprMetrics = util.findMetricValue("MPR", vectorObject);
170149
let muiValue = util.findMetricValue("MUI", vectorObject);
171150

172-
if (!mavValue || mavValue.abbr === "X")
173-
mavValue = util.findMetricValue("AV", vectorObject);
174-
if (!macValue || macValue.abbr === "X")
175-
macValue = util.findMetricValue("AC", vectorObject);
176-
if (!mprMetrics || mprMetrics.abbr === "X")
177-
mprMetrics = util.findMetricValue("PR", vectorObject);
178-
if (!muiValue || muiValue.abbr === "X")
179-
muiValue = util.findMetricValue("UI", vectorObject);
180-
181-
const mprValue = scopeChanged
182-
? mprMetrics.numerical.changed
183-
: mprMetrics.numerical.unchanged;
184-
185-
return (
186-
8.22 *
187-
mavValue.numerical *
188-
macValue.numerical *
189-
mprValue *
190-
muiValue.numerical
191-
);
151+
if (!mavValue || mavValue.abbr === "X") mavValue = util.findMetricValue("AV", vectorObject);
152+
if (!macValue || macValue.abbr === "X") macValue = util.findMetricValue("AC", vectorObject);
153+
if (!mprMetrics || mprMetrics.abbr === "X") mprMetrics = util.findMetricValue("PR", vectorObject);
154+
if (!muiValue || muiValue.abbr === "X") muiValue = util.findMetricValue("UI", vectorObject);
155+
156+
const mprValue = scopeChanged ? mprMetrics.numerical.changed : mprMetrics.numerical.unchanged;
157+
158+
return 8.22 * mavValue.numerical * macValue.numerical * mprValue * muiValue.numerical;
192159
};
193160

194161
/**
@@ -243,10 +210,10 @@ function getExploitabilitySubScore(vector) {
243210
return Number(calculateExploitability(vectorObject, S === "C").toFixed(1));
244211
}
245212

246-
module.exports = {
213+
export const score = {
247214
getScore,
248215
getTemporalScore,
249216
getEnvironmentalScore,
250217
getImpactSubScore,
251-
getExploitabilitySubScore,
218+
getExploitabilitySubScore
252219
};

lib/testFile.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { util } from "./util";
2+
3+
console.log("Test File delete before merge");
4+
5+
const vectorObject = util.getDetailedVectorObject(
6+
"CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:H/A:N/E:X/RL:X/RC:X/CR:X/IR:X/AR:X/MAV:X/MAC:X/MPR:X/MUI:X/MS:X/MC:X/MI:X/MA:X"
7+
);
8+
9+
console.log(vectorObject);

0 commit comments

Comments
 (0)