Skip to content

Commit

Permalink
♻️ Enforce specified ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
superbuggy committed Sep 10, 2024
1 parent f7c34e7 commit 19fdcf8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions cvss40.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,15 @@ class Vector {
return lookup;
}, {});

const metricsEntries = Object.entries(metricsLookup);

const requiredMetrics = Object.keys(Vector.METRICS.BASE);

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


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

for (let [metricType, metricValue] of Object.entries(metricsLookup)) {
for (let [metricType, metricValue] of metricsEntries) {

if (!metricType in Vector.ALL_METRICS) {
if ( !(metricType in Vector.ALL_METRICS) ) {
throw new Error(`Invalid CVSS v4.0 vector: Unknown metric \`${metricType}\` in \`${vector}\``);
}

Expand All @@ -425,6 +428,11 @@ class Vector {
}
}

const utilizedMetricTypes = Object.keys(Vector.ALL_METRICS).filter(metricType => metricType in metricsLookup);
if (metricsEntries.some(([metricType], index) => utilizedMetricTypes[index] !== metricType)) {
throw new Error(`Invalid CVSS v4.0 vector: Metrics are in wrong order \`${vector}\``);
}

return true;
}

Expand Down

0 comments on commit 19fdcf8

Please sign in to comment.