Skip to content

Commit 19fdcf8

Browse files
committed
♻️ Enforce specified ordering
1 parent f7c34e7 commit 19fdcf8

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

cvss40.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,15 @@ class Vector {
396396
return lookup;
397397
}, {});
398398

399+
const metricsEntries = Object.entries(metricsLookup);
400+
399401
const requiredMetrics = Object.keys(Vector.METRICS.BASE);
400402

401403
if (!requiredMetrics.every(metricType => metricType in metricsLookup)) {
402404
throw new Error(`Invalid CVSS v4.0 vector: Missing required metrics in \`${vector}\``);
403405
}
404406

407+
405408
if (metrics.length > Object.keys(metricsLookup).length) {
406409
throw new Error(`Invalid CVSS v4.0 vector: Duplicated metric types in \`${vector}\``);
407410
}
@@ -413,9 +416,9 @@ class Vector {
413416
throw new Error(`Invalid CVSS v4.0 vector: Unknown/excessive metric types in \`${vector}\``);
414417
}
415418

416-
for (let [metricType, metricValue] of Object.entries(metricsLookup)) {
419+
for (let [metricType, metricValue] of metricsEntries) {
417420

418-
if (!metricType in Vector.ALL_METRICS) {
421+
if ( !(metricType in Vector.ALL_METRICS) ) {
419422
throw new Error(`Invalid CVSS v4.0 vector: Unknown metric \`${metricType}\` in \`${vector}\``);
420423
}
421424

@@ -425,6 +428,11 @@ class Vector {
425428
}
426429
}
427430

431+
const utilizedMetricTypes = Object.keys(Vector.ALL_METRICS).filter(metricType => metricType in metricsLookup);
432+
if (metricsEntries.some(([metricType], index) => utilizedMetricTypes[index] !== metricType)) {
433+
throw new Error(`Invalid CVSS v4.0 vector: Metrics are in wrong order \`${vector}\``);
434+
}
435+
428436
return true;
429437
}
430438

0 commit comments

Comments
 (0)