Skip to content

Commit 4844f62

Browse files
authored
Merge pull request #89 from turingsecure/update-vector-function
add: update vector function
2 parents e0bdfec + 40e6dd7 commit 4844f62

File tree

8 files changed

+166
-90
lines changed

8 files changed

+166
-90
lines changed

README.md

+29-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1 align="center">cvss.js by <a href="https://turingpoint.eu" target="_blank">turingpoint.</a></h1>
22
<p>
3-
<img alt="Version" src="https://img.shields.io/badge/version-1.4.6-blue.svg?cacheSeconds=2592000" />
3+
<img alt="Version" src="https://img.shields.io/badge/version-1.4.7-blue.svg?cacheSeconds=2592000" />
44
<a href="#" target="_blank">
55
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" />
66
</a>
@@ -20,9 +20,9 @@ yarn add @turingpointde/cvss.js
2020
Import the library to use it in your code:
2121

2222
```js
23-
const CVSS = require('@turingpointde/cvss.js');
23+
const CVSS = require("@turingpointde/cvss.js");
2424
// or
25-
import CVSS from '@turingpointde/cvss.js';
25+
import CVSS from "@turingpointde/cvss.js";
2626
```
2727

2828
You can also use the library directly from the **CDN** (instead of yarn or npm):
@@ -37,30 +37,30 @@ After importing the library, the CVSS function must first be called with the vec
3737

3838
```js
3939
// Vector only with base score
40-
const vector1 = CVSS('CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L');
40+
const vector1 = CVSS("CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L");
4141
// Vector with temporal score
4242
const vector2 = CVSS(
43-
'CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:L/I:L/A:L/E:U/RL:T/RC:R',
43+
"CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:L/I:L/A:L/E:U/RL:T/RC:R"
4444
);
4545
// Vector with environmental score
4646
const vector3 = CVSS(
47-
'CVSS:3.0/AV:L/AC:H/PR:N/UI:R/S:U/C:L/I:L/A:N/CR:M/IR:H/AR:M/MAV:N/MAC:H/MPR:L/MUI:N/MS:C/MC:N/MI:L/MA:L',
47+
"CVSS:3.0/AV:L/AC:H/PR:N/UI:R/S:U/C:L/I:L/A:N/CR:M/IR:H/AR:M/MAV:N/MAC:H/MPR:L/MUI:N/MS:C/MC:N/MI:L/MA:L"
4848
);
4949
```
5050

5151
It is possible to pass in an object as well
5252

5353
```js
5454
const vectorObject = {
55-
CVSS: '3.0',
56-
AV: 'N',
57-
AC: 'H',
58-
PR: 'H',
59-
UI: 'R',
60-
S: 'U',
61-
C: 'H',
62-
I: 'N',
63-
A: 'N',
55+
CVSS: "3.0",
56+
AV: "N",
57+
AC: "H",
58+
PR: "H",
59+
UI: "R",
60+
S: "U",
61+
C: "H",
62+
I: "N",
63+
A: "N",
6464
};
6565

6666
console.log(CVSS(vectorObject).vector); // "CVSS:3.0/AV:N/AC:H/PR:H/UI:R/S:U/C:H/I:N/A:N"
@@ -71,7 +71,7 @@ To get the scores, simply call the respective function.
7171
```js
7272
// Create a vector
7373
const vector = CVSS(
74-
'CVSS:3.0/AV:L/AC:H/PR:N/UI:R/S:U/C:L/I:L/A:N/E:P/RL:O/CR:M/IR:H/AR:M/MAV:N/MAC:H/MPR:L/MUI:N/MS:C/MC:N/MI:L/MA:L',
74+
"CVSS:3.0/AV:L/AC:H/PR:N/UI:R/S:U/C:L/I:L/A:N/E:P/RL:O/CR:M/IR:H/AR:M/MAV:N/MAC:H/MPR:L/MUI:N/MS:C/MC:N/MI:L/MA:L"
7575
);
7676

7777
console.log(vector.getScore()); // 3.6
@@ -82,7 +82,7 @@ console.log(vector.getEnvironmentalScore()); // 5.1
8282
Sometimes it is useful to get a qualitative rating of a score
8383

8484
```js
85-
const vector = CVSS('CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L');
85+
const vector = CVSS("CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L");
8686

8787
console.log(vector.getRating()); // Medium
8888
console.log(vector.getTemporalRating()); // Medium
@@ -92,7 +92,7 @@ console.log(vector.getEnvironmentalRating()); // Low
9292
A few useful variables/functions to work with the vectors:
9393

9494
```js
95-
const vector = CVSS('CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L');
95+
const vector = CVSS("CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L");
9696

9797
console.log(vector.isValid); // true
9898
console.log(vector.vector); // CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:N/I:L/A:L
@@ -103,7 +103,7 @@ The following functions are suitable for displaying the vector in a human-readab
103103

104104
```js
105105
const vector = CVSS(
106-
'CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:L/I:L/A:L/E:U/RL:T/RC:R/MAC:X/MUI:X/MA:X/MI:X',
106+
"CVSS:3.0/AV:N/AC:H/PR:L/UI:R/S:C/C:L/I:L/A:L/E:U/RL:T/RC:R/MAC:X/MUI:X/MA:X/MI:X"
107107
);
108108

109109
console.log(vector.getVectorObject()); // { CVSS: "3.0", AV: "N", AC: "H", PR: "L", UI: "R", S: "C", C: "L", I: "L", A: "L", E: "U", RL: "T", RC: "R", CR: "X", IR: "X", AR: "X", MAV: "X", MAC: "X", MPR: "X", MUI: "X", MS: "X" , MC: "X", MI: "X", MA: "X" }
@@ -229,12 +229,21 @@ console.log(vector.getDetailedVectorObject()); // see spoiler below
229229

230230
</details>
231231

232+
To update a vector's metric:
233+
234+
```js
235+
const vector = CVSS(
236+
"CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:H/A:N/RL:X/RC:X"
237+
).updateVectorValue("AV", "L");
238+
console.log(vector); // "CVSS:3.0/AV:L/AC:L/PR:N/UI:N/S:U/C:L/I:H/A:N"
239+
```
240+
232241
## Contributing
233242

234243
Contributions, issues and feature requests are welcome.
235244
Feel free to check out the [issues page](https://github.com/turingpointde/cvss.js/issues) if you want to contribute.
236245

237246
## License
238247

239-
Copyright © 2021 [turingpoint GmbH](https://turingpoint.eu).
248+
Copyright © 2022 [turingpoint GmbH](https://turingpoint.eu).
240249
This project is [MIT](LICENSE) licensed.

dist/cvss.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ declare function CVSS(vector: string): {
1616
getDetailedVectorObject: () => any;
1717
getVersion: () => string;
1818
getCleanVectorString: () => string;
19+
updateVectorValue: (metric: string, value: string) => string;
1920
isValid: true;
2021
};

dist/production.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/util.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export function findMetricValue(abbr: string, vectorObject: any): any;
4545
* @returns {String} returns one of the five possible ratings
4646
*/
4747
export function getRating(score: any): string;
48+
export function updateVectorValue(vector: any, metric: any, value: any): string;
4849
/**
4950
* Checks whether the vector passed is valid
5051
*

lib/cvss.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,13 @@ function CVSS(vector) {
8080

8181
/**
8282
* Retrives the version from the vector string
83-
*
84-
* @return {String} returns the version number
83+
*
84+
* @return {String} returns the version number
8585
*/
86-
function getVersion(){
86+
function getVersion() {
8787
return util.getVersion(vector);
8888
}
8989

90-
9190
/**
9291
* Parses the vector to a number score
9392
*
@@ -125,16 +124,26 @@ function CVSS(vector) {
125124
return util.getCleanVectorString(vector);
126125
}
127126

128-
vector = parseVectorObjectToString(vector);
127+
/**
128+
* Updates a vector's metric by a specific value
129+
*
130+
* @param {String} vector
131+
* @param {String} metric
132+
* @param {String} value
133+
* @returns {String} Vector with updated value
134+
*/
135+
function updateVectorValue(metric, value) {
136+
return util.updateVectorValue(vector, metric, value);
137+
}
129138

139+
vector = parseVectorObjectToString(vector);
130140

131141
//Check if vector version is valid
132142
const isVersionValid = getVersion();
133143
if (isVersionValid === "Error") {
134144
throw new Error("The vector version is not valid");
135145
}
136146

137-
138147
//Check if vector format is valid
139148
const isValid = isVectorValid();
140149
if (!isValid) {
@@ -153,8 +162,9 @@ function CVSS(vector) {
153162
getDetailedVectorObject,
154163
getVersion,
155164
getCleanVectorString,
156-
isValid
165+
updateVectorValue,
166+
isValid,
157167
};
158168
}
159169

160-
module.exports = CVSS;
170+
module.exports = CVSS;

lib/util.js

+48-23
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ const findMetric = function (abbr) {
1717
*/
1818
const findMetricValue = function (abbr, vectorObject) {
1919
const definition = findMetric(abbr);
20-
const value = definition.metrics.find((metric) => metric.abbr === vectorObject[definition.abbr]);
20+
const value = definition.metrics.find(
21+
(metric) => metric.abbr === vectorObject[definition.abbr]
22+
);
2123

2224
return value;
2325
};
@@ -57,7 +59,9 @@ function roundUpExact(num) {
5759
function getVectorObject(vector) {
5860
const vectorArray = vector.split("/");
5961
const vectorObject = {};
60-
definitions.definitions.forEach((definition) => (vectorObject[definition["abbr"]] = "X"));
62+
definitions.definitions.forEach(
63+
(definition) => (vectorObject[definition["abbr"]] = "X")
64+
);
6165

6266
for (const entry of vectorArray) {
6367
const values = entry.split(":");
@@ -102,13 +106,17 @@ function getDetailedVectorObject(vector) {
102106
abbr: vectorDef.abbr,
103107
fullName: `${vectorDef.name} (${vectorDef.abbr})`,
104108
value: vectorDef.metrics.find((def) => def.abbr === values[1]).name,
105-
valueAbbr: values[1]
109+
valueAbbr: values[1],
106110
};
107111
return Object.assign(vectorObjectAccumulator, {
108-
metrics: Object.assign(metrics, { [values[0].trim()]: detailedVectorObject })
112+
metrics: Object.assign(metrics, {
113+
[values[0].trim()]: detailedVectorObject,
114+
}),
109115
});
110116
} else {
111-
return Object.assign(vectorObjectAccumulator, { [values[0].trim()]: values[1] });
117+
return Object.assign(vectorObjectAccumulator, {
118+
[values[0].trim()]: values[1],
119+
});
112120
}
113121
},
114122
{ metrics: {} }
@@ -153,21 +161,25 @@ const isVectorValid = function (vector) {
153161
* Exit example:
154162
* ((((((((((AV:[NALP]|AC:[LH])|PR:[NLH])|UI:[NR])|S:[UC])|C:[NLW])|I:[NLW])|A:[NLW])|E:[XUPFH])|RL:[XOTWU])|RC:[XURC])
155163
*/
156-
const expression = definitions.definitions.reduce((accumulator, currentValue, index) => {
157-
const serializedAbbr = `${currentValue.abbr}:[${currentValue.metrics.reduce(
158-
(accumulator2, currentValue2) => {
164+
const expression = definitions.definitions.reduce(
165+
(accumulator, currentValue, index) => {
166+
const serializedAbbr = `${
167+
currentValue.abbr
168+
}:[${currentValue.metrics.reduce((accumulator2, currentValue2) => {
159169
return accumulator2 + currentValue2.abbr;
160-
},
161-
""
162-
)}]`;
163-
if (index !== 0) {
164-
return `(${accumulator}|${serializedAbbr})`;
165-
} else {
166-
return serializedAbbr;
167-
}
168-
}, "");
170+
}, "")}]`;
171+
if (index !== 0) {
172+
return `(${accumulator}|${serializedAbbr})`;
173+
} else {
174+
return serializedAbbr;
175+
}
176+
},
177+
""
178+
);
169179

170-
const totalExpressionVector = new RegExp("^CVSS:3.(0|1)(/" + expression + ")+$");
180+
const totalExpressionVector = new RegExp(
181+
"^CVSS:3.(0|1)(/" + expression + ")+$"
182+
);
171183

172184
//Checks if the vector is in valid format
173185
if (!totalExpressionVector.test(vector)) {
@@ -185,9 +197,12 @@ const isVectorValid = function (vector) {
185197
*/
186198
const allExpressions = definitions.definitions.map((currentValue) => {
187199
return new RegExp(
188-
`/${currentValue.abbr}:[${currentValue.metrics.reduce((accumulator2, currentValue2) => {
189-
return accumulator2 + currentValue2.abbr;
190-
}, "")}]`,
200+
`/${currentValue.abbr}:[${currentValue.metrics.reduce(
201+
(accumulator2, currentValue2) => {
202+
return accumulator2 + currentValue2.abbr;
203+
},
204+
""
205+
)}]`,
191206
"g"
192207
);
193208
});
@@ -206,7 +221,7 @@ const isVectorValid = function (vector) {
206221
/\/S:[UC]/g,
207222
/\/C:[NLH]/g,
208223
/\/I:[NLH]/g,
209-
/\/A:[NLH]/g
224+
/\/A:[NLH]/g,
210225
];
211226

212227
//Checks whether all mandatory parameters are present in the vector
@@ -244,6 +259,15 @@ function parseVectorObjectToString(obj) {
244259
return vectorString;
245260
}
246261

262+
function updateVectorValue(vector, metric, value) {
263+
const vectorObject = getVectorObject(vector);
264+
vectorObject[metric] = value;
265+
266+
const vectorString = parseVectorObjectToString(vectorObject);
267+
268+
return getCleanVectorString(vectorString);
269+
}
270+
247271
/**
248272
* Retrives the version from the vector string
249273
*
@@ -268,8 +292,9 @@ module.exports = {
268292
findMetric,
269293
findMetricValue,
270294
getRating,
295+
updateVectorValue,
271296
isVectorValid,
272297
parseVectorObjectToString,
273298
getVersion,
274-
getCleanVectorString
299+
getCleanVectorString,
275300
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@turingpointde/cvss.js",
3-
"version": "1.4.6",
3+
"version": "1.4.7",
44
"description": "A tiny library to work with cvss vectors",
55
"scripts": {
66
"build": "webpack && tsc",

0 commit comments

Comments
 (0)