@abdulr7man/pensieve 0.0.0-stage → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. package/LICENSE +684 -0
  2. package/README.md +180 -2
  3. package/VERSION +1 -0
  4. package/agents/README.md +16 -0
  5. package/agents/pensieve-code-auditor.md +58 -0
  6. package/agents/pensieve-mapper.md +46 -0
  7. package/agents/pensieve-planner.md +67 -0
  8. package/agents/pensieve-reconner.md +76 -0
  9. package/agents/pensieve-reporter.md +61 -0
  10. package/agents/pensieve-roe-checker.md +46 -0
  11. package/agents/pensieve-scoper.md +77 -0
  12. package/agents/pensieve-tester.md +83 -0
  13. package/agents/pensieve-validator.md +62 -0
  14. package/bin/pensieve +452 -0
  15. package/bin/pensieve-installer.cjs +5 -0
  16. package/commands/pensieve-close.md +6 -0
  17. package/commands/pensieve-map.md +6 -0
  18. package/commands/pensieve-new-engagement.md +6 -0
  19. package/commands/pensieve-pause.md +15 -0
  20. package/commands/pensieve-plan.md +6 -0
  21. package/commands/pensieve-progress.md +34 -0
  22. package/commands/pensieve-quick.md +8 -0
  23. package/commands/pensieve-recon.md +6 -0
  24. package/commands/pensieve-report.md +6 -0
  25. package/commands/pensieve-resume.md +6 -0
  26. package/commands/pensieve-retest.md +10 -0
  27. package/commands/pensieve-scope.md +49 -0
  28. package/commands/pensieve-surface.md +8 -0
  29. package/commands/pensieve-test.md +6 -0
  30. package/commands/pensieve-validate.md +6 -0
  31. package/docker/Dockerfile.pensieve-toolbox +75 -0
  32. package/docker/compose.pensieve-net.yml +37 -0
  33. package/docker/squid.conf.template +21 -0
  34. package/package.json +39 -4
  35. package/packs/ad/ad-killchain/checklist.md +64 -0
  36. package/packs/ad/ad-killchain/pack.yaml +19 -0
  37. package/packs/cloud/cloud-cis/checklist.md +66 -0
  38. package/packs/cloud/cloud-cis/pack.yaml +20 -0
  39. package/packs/fintech/practice-core/checklist.md +33 -0
  40. package/packs/fintech/practice-core/learnings.md +5 -0
  41. package/packs/fintech/practice-core/pack.yaml +18 -0
  42. package/packs/infra/ptes-infra/checklist.md +22 -0
  43. package/packs/infra/ptes-infra/pack.yaml +19 -0
  44. package/packs/mobile/owasp-masvs/checklist.md +80 -0
  45. package/packs/mobile/owasp-masvs/pack.yaml +20 -0
  46. package/packs/owasp-api-v4/checklist.md +85 -0
  47. package/packs/owasp-api-v4/learnings.md +6 -0
  48. package/packs/owasp-api-v4/pack.yaml +23 -0
  49. package/packs/owasp-api-v4/runbooks/API1/SKILL.md +66 -0
  50. package/packs/owasp-api-v4/runbooks/API10/SKILL.md +67 -0
  51. package/packs/owasp-api-v4/runbooks/API2/SKILL.md +129 -0
  52. package/packs/owasp-api-v4/runbooks/API3/SKILL.md +55 -0
  53. package/packs/owasp-api-v4/runbooks/API4/SKILL.md +59 -0
  54. package/packs/owasp-api-v4/runbooks/API5/SKILL.md +60 -0
  55. package/packs/owasp-api-v4/runbooks/API6/SKILL.md +63 -0
  56. package/packs/owasp-api-v4/runbooks/API7/SKILL.md +70 -0
  57. package/packs/owasp-api-v4/runbooks/API8/SKILL.md +72 -0
  58. package/packs/owasp-api-v4/runbooks/API9/SKILL.md +66 -0
  59. package/packs/thickclient/thickclient-core/checklist.md +60 -0
  60. package/packs/thickclient/thickclient-core/pack.yaml +24 -0
  61. package/packs/web/asvs-l2/checklist.md +50 -0
  62. package/packs/web/asvs-l2/learnings.md +6 -0
  63. package/packs/web/asvs-l2/pack.yaml +19 -0
  64. package/packs/web/asvs-l2/runbooks/V2-AUTH-ENUM/SKILL.md +75 -0
  65. package/packs/web/asvs-l2/runbooks/V2-AUTH-LIVE/SKILL.md +78 -0
  66. package/packs/web/asvs-l2/runbooks/V2-AUTH-MFA/SKILL.md +76 -0
  67. package/packs/web/asvs-l2/runbooks/V2-AUTH-RATE/SKILL.md +72 -0
  68. package/packs/web/owasp-testing-checklist/checklist.md +52 -0
  69. package/packs/web/owasp-testing-checklist/learnings.md +47 -0
  70. package/packs/web/owasp-testing-checklist/pack.yaml +21 -0
  71. package/packs/web/unauth-perimeter/checklist.md +72 -0
  72. package/packs/web/unauth-perimeter/pack.yaml +18 -0
  73. package/packs/web/unauth-perimeter/runbooks/PERIM-01/SKILL.md +60 -0
  74. package/packs/web/unauth-perimeter/runbooks/PERIM-02/SKILL.md +63 -0
  75. package/packs/web/unauth-perimeter/runbooks/PERIM-03/SKILL.md +62 -0
  76. package/packs/web/unauth-perimeter/runbooks/PERIM-04/SKILL.md +75 -0
  77. package/packs/web/unauth-perimeter/runbooks/PERIM-05/SKILL.md +66 -0
  78. package/packs/web/unauth-perimeter/runbooks/PERIM-06/SKILL.md +59 -0
  79. package/packs/web/unauth-perimeter/runbooks/PERIM-07/SKILL.md +56 -0
  80. package/packs/web/unauth-perimeter/runbooks/PERIM-08/SKILL.md +57 -0
  81. package/packs/web/unauth-perimeter/runbooks/PERIM-09/SKILL.md +62 -0
  82. package/packs/web/unauth-perimeter/runbooks/PERIM-10/SKILL.md +60 -0
  83. package/packs/whitebox/code-audit-core/checklist.md +36 -0
  84. package/packs/whitebox/code-audit-core/learnings.md +6 -0
  85. package/packs/whitebox/code-audit-core/pack.yaml +14 -0
  86. package/prompts/orchestrator-core.md +52 -0
  87. package/reference/attack-playbooks.md +57 -0
  88. package/reference/otp-relay.md +98 -0
  89. package/reference/param-profiles.md +48 -0
  90. package/reference/templates/FINDING.md +97 -0
  91. package/reference/tool-selection.md +64 -0
  92. package/reference/vulnerability-finding-standard.md +80 -0
  93. package/runtimes/hermes/hooks/injection-scan.py +290 -0
  94. package/runtimes/hermes/hooks/scope-guard.py +136 -0
  95. package/skills/pensieve/SKILL.md +134 -0
  96. package/skills/pensieve-close/SKILL.md +31 -0
  97. package/skills/pensieve-map/SKILL.md +25 -0
  98. package/skills/pensieve-new-engagement/SKILL.md +28 -0
  99. package/skills/pensieve-pause/SKILL.md +22 -0
  100. package/skills/pensieve-plan/SKILL.md +39 -0
  101. package/skills/pensieve-progress/SKILL.md +35 -0
  102. package/skills/pensieve-quick/SKILL.md +47 -0
  103. package/skills/pensieve-recon/SKILL.md +30 -0
  104. package/skills/pensieve-report/SKILL.md +46 -0
  105. package/skills/pensieve-resume/SKILL.md +33 -0
  106. package/skills/pensieve-retest/SKILL.md +35 -0
  107. package/skills/pensieve-scope/SKILL.md +35 -0
  108. package/skills/pensieve-surface/SKILL.md +36 -0
  109. package/skills/pensieve-test/SKILL.md +35 -0
  110. package/skills/pensieve-validate/SKILL.md +28 -0
  111. package/skills/pensieve-wiki/SKILL.md +39 -0
  112. package/src/installer/cli.cjs +209 -0
  113. package/src/installer/home.cjs +155 -0
  114. package/src/installer/runtimes.cjs +276 -0
  115. package/src/installer/uninstall.cjs +342 -0
  116. package/src/pensieve-sdk/bin/pensieve-sdk.cjs +1045 -0
  117. package/src/pensieve-sdk/lib/agent-translate.cjs +50 -0
  118. package/src/pensieve-sdk/lib/approvals.cjs +408 -0
  119. package/src/pensieve-sdk/lib/audit.cjs +290 -0
  120. package/src/pensieve-sdk/lib/checklist.cjs +337 -0
  121. package/src/pensieve-sdk/lib/clientlist.cjs +814 -0
  122. package/src/pensieve-sdk/lib/close.cjs +133 -0
  123. package/src/pensieve-sdk/lib/config.cjs +156 -0
  124. package/src/pensieve-sdk/lib/cvss.cjs +91 -0
  125. package/src/pensieve-sdk/lib/env.cjs +272 -0
  126. package/src/pensieve-sdk/lib/evidence.cjs +217 -0
  127. package/src/pensieve-sdk/lib/findings.cjs +1096 -0
  128. package/src/pensieve-sdk/lib/frontmatter.cjs +178 -0
  129. package/src/pensieve-sdk/lib/graph.cjs +607 -0
  130. package/src/pensieve-sdk/lib/ids.cjs +63 -0
  131. package/src/pensieve-sdk/lib/init.cjs +79 -0
  132. package/src/pensieve-sdk/lib/liveness.cjs +176 -0
  133. package/src/pensieve-sdk/lib/loop.cjs +619 -0
  134. package/src/pensieve-sdk/lib/migrate.cjs +57 -0
  135. package/src/pensieve-sdk/lib/monitor.cjs +69 -0
  136. package/src/pensieve-sdk/lib/pack.cjs +607 -0
  137. package/src/pensieve-sdk/lib/paths.cjs +73 -0
  138. package/src/pensieve-sdk/lib/pdf.cjs +291 -0
  139. package/src/pensieve-sdk/lib/recon.cjs +296 -0
  140. package/src/pensieve-sdk/lib/renderers/__pycache__/formal_report.cpython-314.pyc +0 -0
  141. package/src/pensieve-sdk/lib/renderers/formal_report.py +608 -0
  142. package/src/pensieve-sdk/lib/report.cjs +1135 -0
  143. package/src/pensieve-sdk/lib/runbook.cjs +124 -0
  144. package/src/pensieve-sdk/lib/sarif.cjs +154 -0
  145. package/src/pensieve-sdk/lib/scope.cjs +621 -0
  146. package/src/pensieve-sdk/lib/security.cjs +61 -0
  147. package/src/pensieve-sdk/lib/skills.cjs +193 -0
  148. package/src/pensieve-sdk/lib/state.cjs +610 -0
  149. package/src/pensieve-sdk/lib/subagent.cjs +658 -0
  150. package/src/pensieve-sdk/lib/target.cjs +149 -0
  151. package/src/pensieve-sdk/lib/tools.cjs +66 -0
  152. package/src/pensieve-sdk/lib/vault.cjs +122 -0
  153. package/src/pensieve-sdk/lib/vendor/cvss40.LICENSE +22 -0
  154. package/src/pensieve-sdk/lib/vendor/cvss40.js +1208 -0
  155. package/src/pensieve-sdk/package.json +11 -0
  156. package/templates/ACCOUNTS.md +35 -0
  157. package/templates/ATTACK-SURFACE.md +30 -0
  158. package/templates/FINDING.md +43 -0
  159. package/templates/PLAN.md +33 -0
  160. package/templates/PROJECT.md +29 -0
  161. package/templates/ROE.md +50 -0
  162. package/templates/RUN-LOG.md +19 -0
  163. package/templates/SCOPE.md +32 -0
  164. package/templates/VALIDATION.md +62 -0
  165. package/templates/report/REPORT.md +49 -0
  166. package/workflows/close.md +34 -0
  167. package/workflows/map.md +39 -0
  168. package/workflows/new-engagement.md +37 -0
  169. package/workflows/plan.md +45 -0
  170. package/workflows/progress.md +28 -0
  171. package/workflows/quick.md +67 -0
  172. package/workflows/recon.md +87 -0
  173. package/workflows/report.md +51 -0
  174. package/workflows/resume.md +26 -0
  175. package/workflows/retest.md +53 -0
  176. package/workflows/scope.md +47 -0
  177. package/workflows/surface.md +29 -0
  178. package/workflows/test.md +95 -0
  179. package/workflows/validate.md +34 -0
@@ -0,0 +1,1208 @@
1
+ // Copyright FIRST, Red Hat, and contributors
2
+ // SPDX-License-Identifier: BSD-2-Clause
3
+
4
+ /**
5
+ * Rounds a number to a specified number of decimal places using the "Round Half Up" method.
6
+ *
7
+ * This function shifts the decimal point to the right by the specified number of decimal places,
8
+ * rounds the shifted value to the nearest integer using the "Round Half Up" method, and then shifts
9
+ * the decimal point back to its original position. The final result is returned as a floating-point
10
+ * number with the desired number of decimal places.
11
+ *
12
+ * The "Round Half Up" method rounds a number to the nearest neighbor, rounding .5 away from zero.
13
+ *
14
+ * @param {number} value - The number to be rounded.
15
+ * @return {number} - The number rounded to one decimal place, using an additive adjustment value (EPSILON) to avoid floating-point misrepresentations of values.
16
+ * For example, 8.6 - 7.15 will return 1.4499999999999993 rather than 1.45. When rounded to one decimal place, this will incorrectly produce 1.4, instead of 1.5 (the correct result).
17
+ * The specific EPSILON value is based on interval machine epsilon value for single-precision floating point decimals. See https://en.wikipedia.org/wiki/Machine_epsilon.
18
+ *
19
+ * @example
20
+ * roundToDecimalPlaces(4.945833333333333); // returns 4.9
21
+ * roundToDecimalPlaces(4.25); // returns 4.3
22
+ * roundToDecimalPlaces(1.4499999999999993); // returns 1.5
23
+ */
24
+ function roundToDecimalPlaces(value) {
25
+ const EPSILON = Math.pow(10, -6);
26
+ return Math.round((value + EPSILON) * 10) / 10;
27
+ }
28
+
29
+ /**
30
+ * Class representing a CVSS (Common Vulnerability Scoring System) v4.0 vector.
31
+ *
32
+ * In mathematics and computer science, a vector is a collection of elements (often numbers) that can represent different dimensions of data.
33
+ * Similarly, in CVSS, the vector string represents various dimensions of a vulnerability's characteristics.
34
+ *
35
+ * The Vector class encapsulates the CVSS v4.0 metrics, allowing for the creation,
36
+ * manipulation, and validation of CVSS vectors. It supports generating a vector string
37
+ * dynamically based on current metric values, updating metrics from an input vector string,
38
+ * and computing equivalent classes for higher-level assessments.
39
+ */
40
+ class Vector {
41
+ // CVSS40 metrics with defaults values at first key
42
+ static METRICS = {
43
+ // Base (11 metrics)
44
+ BASE: {
45
+ "AV": ["N", "A", "L", "P"],
46
+ "AC": ["L", "H"],
47
+ "AT": ["N", "P"],
48
+ "PR": ["N", "L", "H"],
49
+ "UI": ["N", "P", "A"],
50
+ "VC": ["N", "L", "H"],
51
+ "VI": ["N", "L", "H"],
52
+ "VA": ["N", "L", "H"],
53
+ "SC": ["N", "L", "H"],
54
+ "SI": ["N", "L", "H"],
55
+ "SA": ["N", "L", "H"]
56
+ },
57
+ // Threat (1 metric)
58
+ THREAT: {
59
+ "E": ["X", "A", "P", "U"]
60
+ },
61
+ // Environmental (14 metrics)
62
+ ENVIRONMENTAL: {
63
+ "CR": ["X", "H", "M", "L"],
64
+ "IR": ["X", "H", "M", "L"],
65
+ "AR": ["X", "H", "M", "L"],
66
+ "MAV": ["X", "N", "A", "L", "P"],
67
+ "MAC": ["X", "L", "H"],
68
+ "MAT": ["X", "N", "P"],
69
+ "MPR": ["X", "N", "L", "H"],
70
+ "MUI": ["X", "N", "P", "A"],
71
+ "MVC": ["X", "H", "L", "N"],
72
+ "MVI": ["X", "H", "L", "N"],
73
+ "MVA": ["X", "H", "L", "N"],
74
+ "MSC": ["X", "H", "L", "N"],
75
+ "MSI": ["X", "S", "H", "L", "N"],
76
+ "MSA": ["X", "S", "H", "L", "N"],
77
+ },
78
+ // Supplemental (6 metrics)
79
+ SUPPLEMENTAL: {
80
+ "S": ["X", "N", "P"],
81
+ "AU": ["X", "N", "Y"],
82
+ "R": ["X", "A", "U", "I"],
83
+ "V": ["X", "D", "C"],
84
+ "RE": ["X", "L", "M", "H"],
85
+ "U": ["X", "Clear", "Green", "Amber", "Red"],
86
+ }
87
+ };
88
+
89
+ static ALL_METRICS = Object.keys(Vector.METRICS).reduce((order, category) => {
90
+ return { ...order, ...Vector.METRICS[category] };
91
+ }, {});
92
+
93
+ // Nomenclature base constant
94
+ static BASE_NOMENCLATURE = "CVSS-B";
95
+
96
+ /**
97
+ * Initializes a new Vector instance with optional CVSS vector string.
98
+ *
99
+ * This constructor initializes the metrics with their default values based on the CVSS v4.0 specification.
100
+ * If a vector string is provided, it parses the string and updates the metrics accordingly.
101
+ *
102
+ * @param {string} [vectorString=""] - Optional CVSS v4.0 vector string to initialize the metrics (e.g., "CVSS:4.0/AV:L/AC:L/PR:N/UI:R/...").
103
+ */
104
+ constructor(vectorString = "") {
105
+ // Initialize the metrics
106
+ const selected = {};
107
+ for (let category in Vector.METRICS) {
108
+ for (let key in Vector.METRICS[category]) {
109
+ // Use the first value in the array of allowed values as the default
110
+ selected[key] = Vector.METRICS[category][key][0];
111
+ }
112
+ }
113
+
114
+ this.metrics = selected;
115
+
116
+ if (vectorString) {
117
+ // Remove any leading '#' symbol
118
+ if (vectorString.startsWith('#')) {
119
+ vectorString = vectorString.slice(1);
120
+ }
121
+ this.updateMetricsFromVectorString(vectorString);
122
+ }
123
+ }
124
+
125
+ /**
126
+ * Dynamically generates the `raw` CVSS vector string based on the current state of `metrics`.
127
+ *
128
+ * This getter constructs the vector string from the `metrics` object, including only those metrics
129
+ * that are not set to "X". The string starts with "CVSS:4.0" followed by each metric and its value.
130
+ *
131
+ * @return {string} - The CVSS vector string in the format "CVSS:4.0/AV:N/AC:L/..."
132
+ */
133
+ get raw() {
134
+ // Construct the vector string dynamically based on the current state of `metrics`
135
+ const baseString = "CVSS:4.0";
136
+ const metricEntries = Object.entries(this.metrics)
137
+ .filter(([, value]) => value !== "X") // Filter out metrics with value "X"
138
+ .map(([key, value]) => `/${key}:${value}`)
139
+ .join('');
140
+ return baseString + metricEntries;
141
+ }
142
+
143
+ /**
144
+ * Computes the equivalent classes for the given CVSS metrics.
145
+ *
146
+ * This method aggregates multiple detailed security metrics into a higher-level
147
+ * equivalent classes that represents the overall security posture.
148
+ *
149
+ * @returns {string} - The equivalent classes (e.g., "002201").
150
+ */
151
+ get equivalentClasses() {
152
+ // Helper function to compute EQ1
153
+ const computeEQ1 = () => {
154
+ const AV = this.getEffectiveMetricValue("AV");
155
+ const PR = this.getEffectiveMetricValue("PR");
156
+ const UI = this.getEffectiveMetricValue("UI");
157
+
158
+ if (AV === "N" && PR === "N" && UI === "N") {
159
+ return "0";
160
+ }
161
+ if ((AV === "N" || PR === "N" || UI === "N") &&
162
+ !(AV === "N" && PR === "N" && UI === "N") &&
163
+ AV !== "P") {
164
+ return "1";
165
+ }
166
+ if (AV === "P" || !(AV === "N" || PR === "N" || UI === "N")) {
167
+ return "2";
168
+ }
169
+ };
170
+
171
+ // Helper function to compute EQ2
172
+ const computeEQ2 = () => {
173
+ const AC = this.getEffectiveMetricValue("AC");
174
+ const AT = this.getEffectiveMetricValue("AT");
175
+
176
+ return (AC === "L" && AT === "N") ? "0" : "1";
177
+ };
178
+
179
+ // Helper function to compute EQ3
180
+ const computeEQ3 = () => {
181
+ const VC = this.getEffectiveMetricValue("VC");
182
+ const VI = this.getEffectiveMetricValue("VI");
183
+ const VA = this.getEffectiveMetricValue("VA");
184
+
185
+ if (VC === "H" && VI === "H") {
186
+ return "0";
187
+ }
188
+ if (!(VC === "H" && VI === "H") && (VC === "H" || VI === "H" || VA === "H")) {
189
+ return "1";
190
+ }
191
+ if (!(VC === "H" || VI === "H" || VA === "H")) {
192
+ return "2";
193
+ }
194
+ };
195
+
196
+ // Helper function to compute EQ4
197
+ const computeEQ4 = () => {
198
+ const MSI = this.getEffectiveMetricValue("MSI");
199
+ const MSA = this.getEffectiveMetricValue("MSA");
200
+ const SC = this.getEffectiveMetricValue("SC");
201
+ const SI = this.getEffectiveMetricValue("SI");
202
+ const SA = this.getEffectiveMetricValue("SA");
203
+
204
+ if (MSI === "S" || MSA === "S") {
205
+ return "0";
206
+ }
207
+ if (!(MSI === "S" || MSA === "S") && (SC === "H" || SI === "H" || SA === "H")) {
208
+ return "1";
209
+ }
210
+ return "2";
211
+ };
212
+
213
+ // Helper function to compute EQ5
214
+ const computeEQ5 = () => {
215
+ const E = this.getEffectiveMetricValue("E");
216
+ if (E === "A") return "0";
217
+ if (E === "P") return "1";
218
+ if (E === "U") return "2";
219
+ };
220
+
221
+ // Helper function to compute EQ6
222
+ const computeEQ6 = () => {
223
+ const CR = this.getEffectiveMetricValue("CR");
224
+ const VC = this.getEffectiveMetricValue("VC");
225
+ const IR = this.getEffectiveMetricValue("IR");
226
+ const VI = this.getEffectiveMetricValue("VI");
227
+ const AR = this.getEffectiveMetricValue("AR");
228
+ const VA = this.getEffectiveMetricValue("VA");
229
+
230
+ if ((CR === "H" && VC === "H") || (IR === "H" && VI === "H") || (AR === "H" && VA === "H")) {
231
+ return "0";
232
+ }
233
+ return "1";
234
+ };
235
+
236
+ // Compute all equivalency values
237
+ const eq1 = computeEQ1();
238
+ const eq2 = computeEQ2();
239
+ const eq3 = computeEQ3();
240
+ const eq4 = computeEQ4();
241
+ const eq5 = computeEQ5();
242
+ const eq6 = computeEQ6();
243
+
244
+ // Combine all EQ values into the equivalent classes
245
+ return eq1 + eq2 + eq3 + eq4 + eq5 + eq6;
246
+ }
247
+
248
+ /**
249
+ * Determines the CVSS nomenclature based on the metrics used in the vector.
250
+ *
251
+ * This method generates the nomenclature string by evaluating whether the vector includes
252
+ * threat and/or environmental metrics. The nomenclature helps to categorize the type of vector
253
+ * (e.g., "CVSS-B", "CVSS-BE", "CVSS-BT", "CVSS-BTE").
254
+ *
255
+ * @returns {string} - The CVSS nomenclature string.
256
+ */
257
+ get nomenclature() {
258
+ let nomenclature = Vector.BASE_NOMENCLATURE;
259
+
260
+ const hasThreatMetrics = Object.keys(Vector.METRICS.THREAT).some(key => this.metrics[key] !== "X");
261
+ const hasEnvironmentalMetrics = Object.keys(Vector.METRICS.ENVIRONMENTAL).some(key => this.metrics[key] !== "X");
262
+
263
+ if (hasThreatMetrics) {
264
+ nomenclature += "T";
265
+ }
266
+
267
+ if (hasEnvironmentalMetrics) {
268
+ nomenclature += "E";
269
+ }
270
+
271
+ return nomenclature;
272
+ }
273
+
274
+ /**
275
+ * Generates a detailed breakdown of equivalent classes with their associated severity levels.
276
+ *
277
+ * This method analyzes a vector string representing various dimensions of a vulnerability
278
+ * (known as macrovectors) and maps them to their corresponding human-readable severity levels
279
+ * ("High", "Medium", "Low").
280
+ *
281
+ * @example
282
+ * const breakdown = vectorInstance.severityBreakdown();
283
+ * console.log(breakdown["Exploitability"]); // Outputs: "Medium"
284
+ * console.log(breakdown["Complexity"]); // Outputs: "High"
285
+ *
286
+ * @returns {Object} An object where each key is a metric description and each value is the corresponding severity level.
287
+ */
288
+ get severityBreakdown() {
289
+ const macroVector = this.equivalentClasses;
290
+
291
+ // Define the macrovectors and their positions
292
+ const macroVectorDetails = [
293
+ "Exploitability",
294
+ "Complexity",
295
+ "Vulnerable system",
296
+ "Subsequent system",
297
+ "Exploitation",
298
+ "Security requirements"
299
+ ];
300
+
301
+ // Define which macrovectors have only two severity options
302
+ const macroVectorsWithTwoSeverities = ["Complexity", "Security requirements"];
303
+
304
+ // Lookup tables for macrovectors with two and three possible severity levels
305
+ const threeSeverities = ["High", "Medium", "Low"];
306
+ const twoSeverities = ["High", "Low"];
307
+
308
+ // Construct the detailed breakdown
309
+ return Object.fromEntries(
310
+ macroVectorDetails.map((description, index) => {
311
+ // Determine which lookup table to use based on the macrovector description
312
+ const macroVectorValueOptions = macroVectorsWithTwoSeverities.includes(description)
313
+ ? twoSeverities
314
+ : threeSeverities;
315
+
316
+ return [description, macroVectorValueOptions[macroVector[index]]];
317
+ })
318
+ );
319
+ }
320
+
321
+
322
+ /**
323
+ * Gets the effective value for a given CVSS metric.
324
+ *
325
+ * This method determines the effective value of a metric, considering any
326
+ * modifications and defaults to the worst-case scenario for certain metrics.
327
+ * It checks if the metric has been overridden by an environmental metric and
328
+ * returns the appropriate value.
329
+ *
330
+ * @param {string} metric - The metric for which to get the effective value (e.g., "AV", "PR").
331
+ * @returns {string} - The effective metric value.
332
+ */
333
+ getEffectiveMetricValue(metric) {
334
+ // Default worst-case scenarios for specific metrics
335
+ const worstCaseDefaults = {
336
+ "E": "A", // If E=X, it defaults to E=A
337
+ "CR": "H", // If CR=X, it defaults to CR=H
338
+ "IR": "H", // If IR=X, it defaults to IR=H
339
+ "AR": "H" // If AR=X, it defaults to AR=H
340
+ };
341
+
342
+ // Check if the metric has a worst-case default
343
+ if (this.metrics[metric] === "X" && Object.prototype.hasOwnProperty.call(worstCaseDefaults, metric)) {
344
+ return worstCaseDefaults[metric];
345
+ }
346
+
347
+ // Check for environmental metrics that overwrite score values
348
+ const modifiedMetric = "M" + metric;
349
+ if (Object.prototype.hasOwnProperty.call(this.metrics, modifiedMetric) && this.metrics[modifiedMetric] !== "X") {
350
+ return this.metrics[modifiedMetric];
351
+ }
352
+
353
+ // Return the selected value for the metric
354
+ return this.metrics[metric];
355
+ }
356
+
357
+
358
+ /**
359
+ * Validates a CVSS v4.0 vector string.
360
+ *
361
+ * This method checks the structure of a given CVSS v4.0 vector string to ensure it adheres to the expected format and values.
362
+ * It verifies the presence of the "CVSS:4.0" prefix, the mandatory metrics, and their valid values.
363
+ *
364
+ * @param {string} vector - The CVSS v4.0 vector string to validate (e.g., "CVSS:4.0/AV:L/AC:L/AT:P/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N/E:A/MAV:A/AU:N/R:A").
365
+ * @returns {boolean} - Returns true if the vector is valid, otherwise false.
366
+ */
367
+ validateStringVector(vector) {
368
+
369
+ const metrics = vector.split('/');
370
+
371
+ // Check if the prefix is correct
372
+ if (metrics.shift() !== "CVSS:4.0") {
373
+ console.error("Error: invalid vector, missing CVSS v4.0 prefix from vector: " + vector);
374
+ return false;
375
+ }
376
+
377
+ const expectedMetrics = Object.entries(Vector.ALL_METRICS);
378
+ const baseMetricsCount = Object.keys(Vector.METRICS.BASE).length;
379
+ let mandatoryMetricIndex = 0;
380
+
381
+ for (let metric of metrics) {
382
+ const [key, value] = metric.split(':');
383
+
384
+ // Check if there are too many metric values
385
+ if (!expectedMetrics[mandatoryMetricIndex]) {
386
+ console.error("Error: invalid vector, too many metric values");
387
+ return false;
388
+ }
389
+
390
+ // Find the current expected metric
391
+ while (expectedMetrics[mandatoryMetricIndex] && expectedMetrics[mandatoryMetricIndex][0] !== key) {
392
+ // Check for missing mandatory metrics
393
+ if (mandatoryMetricIndex < baseMetricsCount) {
394
+ console.error("Error: invalid vector, missing mandatory metrics");
395
+ return false;
396
+ }
397
+ mandatoryMetricIndex++;
398
+ }
399
+
400
+ // Check if the value is valid for the given metric
401
+ if (!expectedMetrics[mandatoryMetricIndex][1].includes(value)) {
402
+ console.error(`Error: invalid vector, for key ${key}, value ${value} is not in ${expectedMetrics[mandatoryMetricIndex][1]}`);
403
+ return false;
404
+ }
405
+
406
+ mandatoryMetricIndex++;
407
+ }
408
+
409
+ // A vector that ends before naming all mandatory metrics never reaches the
410
+ // missing-metric check inside the loop, so enforce the count once the input is spent.
411
+ if (mandatoryMetricIndex < baseMetricsCount) {
412
+ console.error("Error: invalid vector, missing mandatory metrics");
413
+ return false;
414
+ }
415
+
416
+ return true;
417
+ }
418
+
419
+ /**
420
+ * Updates the `metrics` object with values from a provided CVSS v4.0 vector string.
421
+ *
422
+ * This method parses a CVSS v4.0 vector string and updates the `metrics` object
423
+ * with the corresponding metric values. The method validates the vector string
424
+ * to ensure it adheres to the expected CVSS v4.0 format before processing.
425
+ *
426
+ * Example usage:
427
+ * ```
428
+ * vector.updateMetricsFromVectorString("CVSS:4.0/AV:L/AC:L/PR:N/UI:R/...");
429
+ * ```
430
+ *
431
+ * @param {string} vectorString - The CVSS v4.0 vector string to be parsed and applied
432
+ * (e.g., "CVSS:4.0/AV:L/AC:L/PR:N/UI:N/...").
433
+ * @throws {Error} - Throws an error if the vector string is invalid or does not conform to the expected format.
434
+ */
435
+ updateMetricsFromVectorString(vector) {
436
+ if (!vector) {
437
+ throw new Error("The vector string cannot be null, undefined, or empty.");
438
+ }
439
+
440
+ // Validate the CVSS v4.0 string vector
441
+ if (!this.validateStringVector(vector)) {
442
+ throw new Error("Invalid CVSS v4.0 vector: " + vector);
443
+ }
444
+
445
+ let metrics = vector.split('/');
446
+
447
+ // Remove the "CVSS:4.0" prefix
448
+ metrics.shift();
449
+
450
+ // Iterate through each metric component and update the corresponding metric in the `metrics` object
451
+ for (let metric of metrics) {
452
+ let [key, value] = metric.split(':');
453
+ this.metrics[key] = value;
454
+ }
455
+ }
456
+
457
+ /**
458
+ * Updates the value of a specific CVSS metric and automatically refreshes the `raw` vector string.
459
+ *
460
+ * This method updates the value of the specified metric in the `metrics` object.
461
+ * After updating the metric, it updates the `raw` string by replacing the corresponding
462
+ * metric value in the existing string without reconstructing the entire string.
463
+ *
464
+ * Example usage:
465
+ * ```
466
+ * vector.updateMetric("AV", "L");
467
+ * console.log(vector.raw); // Output: "CVSS:4.0/AV:L/AC:L/..."
468
+ * ```
469
+ *
470
+ * @param {string} metric - The abbreviation of the metric to be updated (e.g., "AV", "AC").
471
+ * @param {string} value - The new value to assign to the metric (e.g., "L", "H").
472
+ */
473
+ updateMetric(metric, value) {
474
+ if (Object.prototype.hasOwnProperty.call(this.metrics, metric)) {
475
+ this.metrics[metric] = value;
476
+ } else {
477
+ console.error(`Metric ${metric} not found.`);
478
+ }
479
+ }
480
+
481
+
482
+ }
483
+
484
+ /**
485
+ * Class representing the CVSS (Common Vulnerability Scoring System) version 4.0.
486
+ *
487
+ * This class encapsulates the CVSS v4.0 scoring logic, enabling the calculation of a score based on a vector string.
488
+ * It manages an internal `Vector` object, which represents the individual CVSS metrics and their values.
489
+ * The `CVSS40` class leverages this `Vector` object to compute the overall score and severity rating.
490
+ *
491
+ *
492
+ * @example
493
+ * let vuln = new CVSS40("CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:L/SC:N/SI:N/SA:N/E:A/MAV:A");
494
+ * console.log(vuln.score); // Output the computed CVSS score (8.7)
495
+ * console.log(vuln.severity); // Output the severity rating (High)
496
+ * console.log(vuln.vector.nomenclature); // Output the corresponding nomenclature (CVSS-BTE)
497
+ * console.log(vuln.vector.raw); // Output the raw vector
498
+ * @class
499
+ */
500
+ class CVSS40 {
501
+
502
+ // Lookup table of macro vectors and their pre-computed equivalent classes value.
503
+ static LOOKUP_TABLE = {
504
+ "000000": 10,
505
+ "000001": 9.9,
506
+ "000010": 9.8,
507
+ "000011": 9.5,
508
+ "000020": 9.5,
509
+ "000021": 9.2,
510
+ "000100": 10,
511
+ "000101": 9.6,
512
+ "000110": 9.3,
513
+ "000111": 8.7,
514
+ "000120": 9.1,
515
+ "000121": 8.1,
516
+ "000200": 9.3,
517
+ "000201": 9,
518
+ "000210": 8.9,
519
+ "000211": 8,
520
+ "000220": 8.1,
521
+ "000221": 6.8,
522
+ "001000": 9.8,
523
+ "001001": 9.5,
524
+ "001010": 9.5,
525
+ "001011": 9.2,
526
+ "001020": 9,
527
+ "001021": 8.4,
528
+ "001100": 9.3,
529
+ "001101": 9.2,
530
+ "001110": 8.9,
531
+ "001111": 8.1,
532
+ "001120": 8.1,
533
+ "001121": 6.5,
534
+ "001200": 8.8,
535
+ "001201": 8,
536
+ "001210": 7.8,
537
+ "001211": 7,
538
+ "001220": 6.9,
539
+ "001221": 4.8,
540
+ "002001": 9.2,
541
+ "002011": 8.2,
542
+ "002021": 7.2,
543
+ "002101": 7.9,
544
+ "002111": 6.9,
545
+ "002121": 5,
546
+ "002201": 6.9,
547
+ "002211": 5.5,
548
+ "002221": 2.7,
549
+ "010000": 9.9,
550
+ "010001": 9.7,
551
+ "010010": 9.5,
552
+ "010011": 9.2,
553
+ "010020": 9.2,
554
+ "010021": 8.5,
555
+ "010100": 9.5,
556
+ "010101": 9.1,
557
+ "010110": 9,
558
+ "010111": 8.3,
559
+ "010120": 8.4,
560
+ "010121": 7.1,
561
+ "010200": 9.2,
562
+ "010201": 8.1,
563
+ "010210": 8.2,
564
+ "010211": 7.1,
565
+ "010220": 7.2,
566
+ "010221": 5.3,
567
+ "011000": 9.5,
568
+ "011001": 9.3,
569
+ "011010": 9.2,
570
+ "011011": 8.5,
571
+ "011020": 8.5,
572
+ "011021": 7.3,
573
+ "011100": 9.2,
574
+ "011101": 8.2,
575
+ "011110": 8,
576
+ "011111": 7.2,
577
+ "011120": 7,
578
+ "011121": 5.9,
579
+ "011200": 8.4,
580
+ "011201": 7,
581
+ "011210": 7.1,
582
+ "011211": 5.2,
583
+ "011220": 5,
584
+ "011221": 3,
585
+ "012001": 8.6,
586
+ "012011": 7.5,
587
+ "012021": 5.2,
588
+ "012101": 7.1,
589
+ "012111": 5.2,
590
+ "012121": 2.9,
591
+ "012201": 6.3,
592
+ "012211": 2.9,
593
+ "012221": 1.7,
594
+ "100000": 9.8,
595
+ "100001": 9.5,
596
+ "100010": 9.4,
597
+ "100011": 8.7,
598
+ "100020": 9.1,
599
+ "100021": 8.1,
600
+ "100100": 9.4,
601
+ "100101": 8.9,
602
+ "100110": 8.6,
603
+ "100111": 7.4,
604
+ "100120": 7.7,
605
+ "100121": 6.4,
606
+ "100200": 8.7,
607
+ "100201": 7.5,
608
+ "100210": 7.4,
609
+ "100211": 6.3,
610
+ "100220": 6.3,
611
+ "100221": 4.9,
612
+ "101000": 9.4,
613
+ "101001": 8.9,
614
+ "101010": 8.8,
615
+ "101011": 7.7,
616
+ "101020": 7.6,
617
+ "101021": 6.7,
618
+ "101100": 8.6,
619
+ "101101": 7.6,
620
+ "101110": 7.4,
621
+ "101111": 5.8,
622
+ "101120": 5.9,
623
+ "101121": 5,
624
+ "101200": 7.2,
625
+ "101201": 5.7,
626
+ "101210": 5.7,
627
+ "101211": 5.2,
628
+ "101220": 5.2,
629
+ "101221": 2.5,
630
+ "102001": 8.3,
631
+ "102011": 7,
632
+ "102021": 5.4,
633
+ "102101": 6.5,
634
+ "102111": 5.8,
635
+ "102121": 2.6,
636
+ "102201": 5.3,
637
+ "102211": 2.1,
638
+ "102221": 1.3,
639
+ "110000": 9.5,
640
+ "110001": 9,
641
+ "110010": 8.8,
642
+ "110011": 7.6,
643
+ "110020": 7.6,
644
+ "110021": 7,
645
+ "110100": 9,
646
+ "110101": 7.7,
647
+ "110110": 7.5,
648
+ "110111": 6.2,
649
+ "110120": 6.1,
650
+ "110121": 5.3,
651
+ "110200": 7.7,
652
+ "110201": 6.6,
653
+ "110210": 6.8,
654
+ "110211": 5.9,
655
+ "110220": 5.2,
656
+ "110221": 3,
657
+ "111000": 8.9,
658
+ "111001": 7.8,
659
+ "111010": 7.6,
660
+ "111011": 6.7,
661
+ "111020": 6.2,
662
+ "111021": 5.8,
663
+ "111100": 7.4,
664
+ "111101": 5.9,
665
+ "111110": 5.7,
666
+ "111111": 5.7,
667
+ "111120": 4.7,
668
+ "111121": 2.3,
669
+ "111200": 6.1,
670
+ "111201": 5.2,
671
+ "111210": 5.7,
672
+ "111211": 2.9,
673
+ "111220": 2.4,
674
+ "111221": 1.6,
675
+ "112001": 7.1,
676
+ "112011": 5.9,
677
+ "112021": 3,
678
+ "112101": 5.8,
679
+ "112111": 2.6,
680
+ "112121": 1.5,
681
+ "112201": 2.3,
682
+ "112211": 1.3,
683
+ "112221": 0.6,
684
+ "200000": 9.3,
685
+ "200001": 8.7,
686
+ "200010": 8.6,
687
+ "200011": 7.2,
688
+ "200020": 7.5,
689
+ "200021": 5.8,
690
+ "200100": 8.6,
691
+ "200101": 7.4,
692
+ "200110": 7.4,
693
+ "200111": 6.1,
694
+ "200120": 5.6,
695
+ "200121": 3.4,
696
+ "200200": 7,
697
+ "200201": 5.4,
698
+ "200210": 5.2,
699
+ "200211": 4,
700
+ "200220": 4,
701
+ "200221": 2.2,
702
+ "201000": 8.5,
703
+ "201001": 7.5,
704
+ "201010": 7.4,
705
+ "201011": 5.5,
706
+ "201020": 6.2,
707
+ "201021": 5.1,
708
+ "201100": 7.2,
709
+ "201101": 5.7,
710
+ "201110": 5.5,
711
+ "201111": 4.1,
712
+ "201120": 4.6,
713
+ "201121": 1.9,
714
+ "201200": 5.3,
715
+ "201201": 3.6,
716
+ "201210": 3.4,
717
+ "201211": 1.9,
718
+ "201220": 1.9,
719
+ "201221": 0.8,
720
+ "202001": 6.4,
721
+ "202011": 5.1,
722
+ "202021": 2,
723
+ "202101": 4.7,
724
+ "202111": 2.1,
725
+ "202121": 1.1,
726
+ "202201": 2.4,
727
+ "202211": 0.9,
728
+ "202221": 0.4,
729
+ "210000": 8.8,
730
+ "210001": 7.5,
731
+ "210010": 7.3,
732
+ "210011": 5.3,
733
+ "210020": 6,
734
+ "210021": 5,
735
+ "210100": 7.3,
736
+ "210101": 5.5,
737
+ "210110": 5.9,
738
+ "210111": 4,
739
+ "210120": 4.1,
740
+ "210121": 2,
741
+ "210200": 5.4,
742
+ "210201": 4.3,
743
+ "210210": 4.5,
744
+ "210211": 2.2,
745
+ "210220": 2,
746
+ "210221": 1.1,
747
+ "211000": 7.5,
748
+ "211001": 5.5,
749
+ "211010": 5.8,
750
+ "211011": 4.5,
751
+ "211020": 4,
752
+ "211021": 2.1,
753
+ "211100": 6.1,
754
+ "211101": 5.1,
755
+ "211110": 4.8,
756
+ "211111": 1.8,
757
+ "211120": 2,
758
+ "211121": 0.9,
759
+ "211200": 4.6,
760
+ "211201": 1.8,
761
+ "211210": 1.7,
762
+ "211211": 0.7,
763
+ "211220": 0.8,
764
+ "211221": 0.2,
765
+ "212001": 5.3,
766
+ "212011": 2.4,
767
+ "212021": 1.4,
768
+ "212101": 2.4,
769
+ "212111": 1.2,
770
+ "212121": 0.5,
771
+ "212201": 1,
772
+ "212211": 0.3,
773
+ "212221": 0.1
774
+ };
775
+
776
+ // The following defines the index of each metric's values.
777
+ // It is used when looking for the highest vector part of the
778
+ // combinations produced by the MacroVector respective highest
779
+ static METRIC_LEVELS = {
780
+ "AV": {"N": 0.0, "A": 0.1, "L": 0.2, "P": 0.3},
781
+ "PR": {"N": 0.0, "L": 0.1, "H": 0.2},
782
+ "UI": {"N": 0.0, "P": 0.1, "A": 0.2},
783
+ "AC": {'L': 0.0, 'H': 0.1},
784
+ "AT": {'N': 0.0, 'P': 0.1},
785
+ "VC": {'H': 0.0, 'L': 0.1, 'N': 0.2},
786
+ "VI": {'H': 0.0, 'L': 0.1, 'N': 0.2},
787
+ "VA": {'H': 0.0, 'L': 0.1, 'N': 0.2},
788
+ "SC": {'H': 0.1, 'L': 0.2, 'N': 0.3},
789
+ "SI": {'S': 0.0, 'H': 0.1, 'L': 0.2, 'N': 0.3},
790
+ "SA": {'S': 0.0, 'H': 0.1, 'L': 0.2, 'N': 0.3},
791
+ "CR": {'H': 0.0, 'M': 0.1, 'L': 0.2},
792
+ "IR": {'H': 0.0, 'M': 0.1, 'L': 0.2},
793
+ "AR": {'H': 0.0, 'M': 0.1, 'L': 0.2},
794
+ "E": {'U': 0.2, 'P': 0.1, 'A': 0}
795
+ };
796
+
797
+ static MAX_COMPOSED = {
798
+ // EQ1
799
+ "eq1": {
800
+ 0: ["AV:N/PR:N/UI:N/"],
801
+ 1: ["AV:A/PR:N/UI:N/", "AV:N/PR:L/UI:N/", "AV:N/PR:N/UI:P/"],
802
+ 2: ["AV:P/PR:N/UI:N/", "AV:A/PR:L/UI:P/"]
803
+ },
804
+ // EQ2
805
+ "eq2": {
806
+ 0: ["AC:L/AT:N/"],
807
+ 1: ["AC:H/AT:N/", "AC:L/AT:P/"]
808
+ },
809
+ // EQ3+EQ6
810
+ "eq3": {
811
+ 0: { "0": ["VC:H/VI:H/VA:H/CR:H/IR:H/AR:H/"], "1": ["VC:H/VI:H/VA:L/CR:M/IR:M/AR:H/", "VC:H/VI:H/VA:H/CR:M/IR:M/AR:M/"] },
812
+ 1: { "0": ["VC:L/VI:H/VA:H/CR:H/IR:H/AR:H/", "VC:H/VI:L/VA:H/CR:H/IR:H/AR:H/"], "1": ["VC:L/VI:H/VA:L/CR:H/IR:M/AR:H/", "VC:L/VI:H/VA:H/CR:H/IR:M/AR:M/", "VC:H/VI:L/VA:H/CR:M/IR:H/AR:M/", "VC:H/VI:L/VA:L/CR:M/IR:H/AR:H/", "VC:L/VI:L/VA:H/CR:H/IR:H/AR:M/"] },
813
+ 2: { "1": ["VC:L/VI:L/VA:L/CR:H/IR:H/AR:H/"] },
814
+ },
815
+ // EQ4
816
+ "eq4": {
817
+ 0: ["SC:H/SI:S/SA:S/"],
818
+ 1: ["SC:H/SI:H/SA:H/"],
819
+ 2: ["SC:L/SI:L/SA:L/"]
820
+
821
+ },
822
+ // EQ5
823
+ "eq5": {
824
+ 0: ["E:A/"],
825
+ 1: ["E:P/"],
826
+ 2: ["E:U/"],
827
+ },
828
+ };
829
+
830
+ // max severity distances in EQs MacroVectors (+1)
831
+ static MAX_SEVERITY = {
832
+ "eq1": {
833
+ 0: 1,
834
+ 1: 4,
835
+ 2: 5
836
+ },
837
+ "eq2": {
838
+ 0: 1,
839
+ 1: 2
840
+ },
841
+ "eq3eq6": {
842
+ 0: { 0: 7, 1: 6 },
843
+ 1: { 0: 8, 1: 8 },
844
+ 2: { 1: 10 }
845
+ },
846
+ "eq4": {
847
+ 0: 6,
848
+ 1: 5,
849
+ 2: 4
850
+ },
851
+ "eq5": {
852
+ 0: 1,
853
+ 1: 1,
854
+ 2: 1
855
+ },
856
+ };
857
+
858
+ /**
859
+ * Constructs a CVSS40 object and initializes its properties.
860
+ *
861
+ * This constructor validates the provided CVSS v4.0 vector string against the CVSS v4.0 specification,
862
+ * extracts the metrics from the vector string, computes the equivalent classes,
863
+ * and calculates the score.
864
+ *
865
+ * For detailed information on the CVSS v4.0 specification, refer to:
866
+ * https://www.first.org/cvss/v4.0/specification-document
867
+ *
868
+ * @param {string} vectorString - The CVSS v4.0 vector string (e.g., "CVSS:4.0/AV:L/AC:L/AT:P/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N/E:A/MAV:A/AU:N/R:A").
869
+ * Defaults to an empty string if not provided.
870
+ * @throws {Error} - Throws an error if the vector string is invalid according to the CVSS v4.0 schema.
871
+ */
872
+ constructor(input = "") {
873
+ if (input instanceof Vector) {
874
+ // If the input is a Vector object, use it directly
875
+ this.vector = input;
876
+ } else if (typeof input === 'string') {
877
+ // If the input is a string, create a new Vector object from the string
878
+ this.vector = new Vector(input);
879
+ } else {
880
+ throw new Error(`Invalid input type for CVSSv4.0 constructor. Expected a string or a Vector object in ${this.vector}`);
881
+ }
882
+
883
+ // Calculate the score
884
+ this.score = this.calculateScore();
885
+
886
+ // Save the severity
887
+ this.severity = this.calculateSeverityRating(this.score);
888
+ }
889
+
890
+
891
+ /**
892
+ * Calculates the qualitative severity rating based on the CVSS score.
893
+ *
894
+ * The rating is determined according to the following scale:
895
+ * - None: 0.0
896
+ * - Low: 0.1 - 3.9
897
+ * - Medium: 4.0 - 6.9
898
+ * - High: 7.0 - 8.9
899
+ * - Critical: 9.0 - 10.0
900
+ *
901
+ * @param {number} score - The CVSS score.
902
+ * @returns {string} - The qualitative severity rating.
903
+ */
904
+ calculateSeverityRating(score) {
905
+ if (score === 0.0) {
906
+ return "None";
907
+ } else if (score >= 0.1 && score <= 3.9) {
908
+ return "Low";
909
+ } else if (score >= 4.0 && score <= 6.9) {
910
+ return "Medium";
911
+ } else if (score >= 7.0 && score <= 8.9) {
912
+ return "High";
913
+ } else if (score >= 9.0 && score <= 10.0) {
914
+ return "Critical";
915
+ }
916
+ return "Unknown"; // In case of an unexpected score value
917
+ }
918
+
919
+
920
+ /**
921
+ * Calculates the severity distances between the effective metric values and the extracted metric values
922
+ * for a given maximum vector.
923
+ *
924
+ * This method computes the difference between the effective value of each metric in the CVSS vector and
925
+ * the corresponding value in the provided maximum vector. The differences are stored in an object where
926
+ * the keys are the metric names and the values are the calculated distances.
927
+ *
928
+ * @param {string} maxVector - The maximum vector string representing the highest severity levels.
929
+ * @returns {object} - An object with keys as metric names and values as the calculated severity distances.
930
+ */
931
+ calculateSeverityDistances(maxVector) {
932
+ const distances = {};
933
+ for (const metric in CVSS40.METRIC_LEVELS) {
934
+ const effectiveMetricValue = this.vector.getEffectiveMetricValue(metric);
935
+ const extractedMetricValue = this.extractValueMetric(metric, maxVector);
936
+ distances[metric] = CVSS40.METRIC_LEVELS[metric][effectiveMetricValue] - CVSS40.METRIC_LEVELS[metric][extractedMetricValue];
937
+ }
938
+ return distances;
939
+ }
940
+
941
+
942
+
943
+ /**
944
+ * Calculates the CVSS v4.0 score for the given vector.
945
+ *
946
+ * This method follows the CVSS v4.0 specification to determine the score for a given vector.
947
+ * It handles the case where there is no impact on the system by returning a score of 0.0.
948
+ * Otherwise, it calculates the score based on the maximal scoring differences (MSD) for each EQ
949
+ * (Equivalency) and the severity distances from the highest severity vector in the same MacroVector.
950
+ *
951
+ * The process involves the following steps:
952
+ * 1. Determine the maximal scoring difference (MSD) for each EQ by computing the difference between
953
+ * the current MacroVector and the next lower MacroVector.
954
+ * 2. Retrieve the highest severity vectors for each EQ and compute the severity distances from the
955
+ * to-be-scored vector.
956
+ * 3. Calculate the current severity distances for each EQ and determine the proportion of the distance.
957
+ * 4. Compute the mean of the proportional distances.
958
+ * 5. Subtract the mean distance from the score of the highest severity vector to obtain the final score.
959
+ *
960
+ * For detailed information on the CVSS v4.0 specification, refer to:
961
+ * https://www.first.org/cvss/v4.0/specification-document
962
+ *
963
+ * @returns {number} - The calculated CVSS v4.0 score, rounded to one decimal place.
964
+ */
965
+ calculateScore() {
966
+ // Constants
967
+ // When CIA triad is None
968
+ const NO_IMPACT_METRICS = ["VC", "VI", "VA", "SC", "SI", "SA"];
969
+ const STEP = 0.1;
970
+
971
+ // Exception for no impact on system
972
+ if (NO_IMPACT_METRICS.every((metric) => this.vector.getEffectiveMetricValue(metric) === "N")) {
973
+ return 0.0;
974
+ }
975
+
976
+ // Ensure to retrieve up-to-date equivalent classes and store-it inside a variable
977
+ const equivalentClasses = this.vector.equivalentClasses;
978
+
979
+ let value = CVSS40.LOOKUP_TABLE[equivalentClasses];
980
+
981
+ // 1. For each of the EQs:
982
+ // a. The maximal scoring difference is determined as the difference
983
+ // between the current MacroVector and the lower MacroVector.
984
+ // i. If there is no lower MacroVector the available distance is
985
+ // set to NaN and then ignored in the further calculations.
986
+
987
+ // EQ values
988
+ const [eq1, eq2, eq3, eq4, eq5, eq6] = equivalentClasses.split('').map(Number);
989
+
990
+
991
+ // Compute the next lower macro; it may also not exist.
992
+ const eq1_next_lower_macro = `${eq1 + 1}${eq2}${eq3}${eq4}${eq5}${eq6}`;
993
+ const eq2_next_lower_macro = `${eq1}${eq2 + 1}${eq3}${eq4}${eq5}${eq6}`;
994
+
995
+ let eq3eq6_next_lower_macro;
996
+ let eq3eq6_next_lower_macro_left;
997
+ let eq3eq6_next_lower_macro_right;
998
+
999
+ // eq3 and eq6 are related
1000
+ if (eq3 === 1 && eq6 === 1) {
1001
+ // 11 --> 21
1002
+ eq3eq6_next_lower_macro = `${eq1}${eq2}${eq3 + 1}${eq4}${eq5}${eq6}`;
1003
+ } else if (eq3 === 0 && eq6 === 1) {
1004
+ // 01 --> 11
1005
+ eq3eq6_next_lower_macro = `${eq1}${eq2}${eq3 + 1}${eq4}${eq5}${eq6}`;
1006
+ } else if (eq3 === 1 && eq6 === 0) {
1007
+ // 10 --> 11
1008
+ eq3eq6_next_lower_macro = `${eq1}${eq2}${eq3}${eq4}${eq5}${eq6 + 1}`;
1009
+ } else if (eq3 === 0 && eq6 === 0) {
1010
+ // 00 --> 01
1011
+ // 00 --> 10
1012
+ eq3eq6_next_lower_macro_left = `${eq1}${eq2}${eq3}${eq4}${eq5}${eq6 + 1}`;
1013
+ eq3eq6_next_lower_macro_right = `${eq1}${eq2}${eq3 + 1}${eq4}${eq5}${eq6}`;
1014
+ } else {
1015
+ // 21 --> 32 (does not exist)
1016
+ eq3eq6_next_lower_macro = `${eq1}${eq2}${eq3 + 1}${eq4}${eq5}${eq6 + 1}`;
1017
+ }
1018
+
1019
+
1020
+ const eq4_next_lower_macro = `${eq1}${eq2}${eq3}${eq4 + 1}${eq5}${eq6}`;
1021
+ const eq5_next_lower_macro = `${eq1}${eq2}${eq3}${eq4}${eq5 + 1}${eq6}`;
1022
+
1023
+ // get their score, if the next lower macro score do not exist the result is NaN
1024
+ const score_eq1_next_lower_macro = CVSS40.LOOKUP_TABLE[eq1_next_lower_macro];
1025
+ const score_eq2_next_lower_macro = CVSS40.LOOKUP_TABLE[eq2_next_lower_macro];
1026
+
1027
+ let score_eq3eq6_next_lower_macro;
1028
+ if (eq3 == 0 && eq6 == 0) {
1029
+ // multiple path take the one with higher score
1030
+ const score_eq3eq6_next_lower_macro_left = CVSS40.LOOKUP_TABLE[eq3eq6_next_lower_macro_left];
1031
+ const score_eq3eq6_next_lower_macro_right = CVSS40.LOOKUP_TABLE[eq3eq6_next_lower_macro_right];
1032
+
1033
+ score_eq3eq6_next_lower_macro = Math.max(score_eq3eq6_next_lower_macro_left, score_eq3eq6_next_lower_macro_right);
1034
+ } else {
1035
+ score_eq3eq6_next_lower_macro = CVSS40.LOOKUP_TABLE[eq3eq6_next_lower_macro];
1036
+ }
1037
+
1038
+
1039
+ const score_eq4_next_lower_macro = CVSS40.LOOKUP_TABLE[eq4_next_lower_macro];
1040
+ const score_eq5_next_lower_macro = CVSS40.LOOKUP_TABLE[eq5_next_lower_macro];
1041
+
1042
+ // b. The severity distance of the to-be scored vector from a
1043
+ // highest severity vector in the same MacroVector is determined.
1044
+ const eqMaxes = [
1045
+ this.getMaxSeverityVectorsForEQ(equivalentClasses, 1),
1046
+ this.getMaxSeverityVectorsForEQ(equivalentClasses, 2),
1047
+ this.getMaxSeverityVectorsForEQ(equivalentClasses, 3)[eq6],
1048
+ this.getMaxSeverityVectorsForEQ(equivalentClasses, 4),
1049
+ this.getMaxSeverityVectorsForEQ(equivalentClasses, 5)
1050
+ ];
1051
+
1052
+
1053
+ // Compose maximum vectors
1054
+ const maxVectors = [];
1055
+ for (const eq1Max of eqMaxes[0]) {
1056
+ for (const eq2Max of eqMaxes[1]) {
1057
+ for (const eq3Max of eqMaxes[2]) {
1058
+ for (const eq4Max of eqMaxes[3]) {
1059
+ for (const eq5Max of eqMaxes[4]) {
1060
+ maxVectors.push(eq1Max + eq2Max + eq3Max + eq4Max + eq5Max);
1061
+ }
1062
+ }
1063
+ }
1064
+ }
1065
+ }
1066
+
1067
+
1068
+ // Find the max vector to use i.e. one in the combination of all the highest
1069
+ // that is greater or equal (severity distance) than the to-be scored vector.
1070
+ let maxVector, distances;
1071
+ for (const vector of maxVectors) {
1072
+ distances = this.calculateSeverityDistances(vector);
1073
+ if (Object.values(distances).every(distance => distance >= 0)) {
1074
+ maxVector = vector;
1075
+ break;
1076
+ }
1077
+ }
1078
+
1079
+ // Calculate the current severity distances
1080
+ const current_severity_distance_eq1 = distances["AV"] + distances["PR"] + distances["UI"];
1081
+ const current_severity_distance_eq2 = distances["AC"] + distances["AT"];
1082
+ const current_severity_distance_eq3eq6 = distances["VC"] + distances["VI"] + distances["VA"] + distances["CR"] + distances["IR"] + distances["AR"];
1083
+ const current_severity_distance_eq4 = distances["SC"] + distances["SI"] + distances["SA"];
1084
+ // const current_severity_distance_eq5 = 0; // EQ5 is always 0 in this context
1085
+
1086
+
1087
+ // if the next lower macro score do not exist the result is Nan
1088
+ // Rename to maximal scoring difference (aka MSD)
1089
+ const available_distance_eq1 = value - score_eq1_next_lower_macro;
1090
+ const available_distance_eq2 = value - score_eq2_next_lower_macro;
1091
+ const available_distance_eq3eq6 = value - score_eq3eq6_next_lower_macro;
1092
+ const available_distance_eq4 = value - score_eq4_next_lower_macro;
1093
+ const available_distance_eq5 = value - score_eq5_next_lower_macro;
1094
+
1095
+ let percent_to_next_eq1_severity = 0;
1096
+ let percent_to_next_eq2_severity = 0;
1097
+ let percent_to_next_eq3eq6_severity = 0;
1098
+ let percent_to_next_eq4_severity = 0;
1099
+ let percent_to_next_eq5_severity = 0;
1100
+
1101
+ // some of them do not exist, we will find them by retrieving the score. If score null then do not exist
1102
+ let n_existing_lower = 0;
1103
+
1104
+ let normalized_severity_eq1 = 0;
1105
+ let normalized_severity_eq2 = 0;
1106
+ let normalized_severity_eq3eq6 = 0;
1107
+ let normalized_severity_eq4 = 0;
1108
+ let normalized_severity_eq5 = 0;
1109
+
1110
+ // multiply by step because distance is pure
1111
+ const maxSeverity_eq1 = CVSS40.MAX_SEVERITY["eq1"][eq1] * STEP;
1112
+ const maxSeverity_eq2 = CVSS40.MAX_SEVERITY["eq2"][eq2] * STEP;
1113
+ const maxSeverity_eq3eq6 = CVSS40.MAX_SEVERITY["eq3eq6"][eq3][eq6] * STEP;
1114
+ const maxSeverity_eq4 = CVSS40.MAX_SEVERITY["eq4"][eq4] * STEP;
1115
+
1116
+ // c. The proportion of the distance is determined by dividing
1117
+ // the severity distance of the to-be-scored vector by the depth
1118
+ // of the MacroVector.
1119
+ // d. The maximal scoring difference is multiplied by the proportion of
1120
+ // distance.
1121
+ if (!isNaN(available_distance_eq1)) {
1122
+ n_existing_lower = n_existing_lower + 1
1123
+ percent_to_next_eq1_severity = (current_severity_distance_eq1) / maxSeverity_eq1
1124
+ normalized_severity_eq1 = available_distance_eq1 * percent_to_next_eq1_severity
1125
+ }
1126
+
1127
+ if (!isNaN(available_distance_eq2)) {
1128
+ n_existing_lower = n_existing_lower + 1
1129
+ percent_to_next_eq2_severity = (current_severity_distance_eq2) / maxSeverity_eq2
1130
+ normalized_severity_eq2 = available_distance_eq2 * percent_to_next_eq2_severity
1131
+ }
1132
+
1133
+ if (!isNaN(available_distance_eq3eq6)) {
1134
+ n_existing_lower = n_existing_lower + 1
1135
+ percent_to_next_eq3eq6_severity = (current_severity_distance_eq3eq6) / maxSeverity_eq3eq6
1136
+ normalized_severity_eq3eq6 = available_distance_eq3eq6 * percent_to_next_eq3eq6_severity
1137
+ }
1138
+
1139
+ if (!isNaN(available_distance_eq4)) {
1140
+ n_existing_lower = n_existing_lower + 1
1141
+ percent_to_next_eq4_severity = (current_severity_distance_eq4) / maxSeverity_eq4
1142
+ normalized_severity_eq4 = available_distance_eq4 * percent_to_next_eq4_severity
1143
+ }
1144
+
1145
+ if (!isNaN(available_distance_eq5)) {
1146
+ // for eq5 is always 0 the percentage
1147
+ n_existing_lower = n_existing_lower + 1
1148
+ percent_to_next_eq5_severity = 0
1149
+ normalized_severity_eq5 = available_distance_eq5 * percent_to_next_eq5_severity
1150
+ }
1151
+
1152
+ // 2. The mean of the above computed proportional distances is computed.
1153
+ let meanDistance;
1154
+ if (n_existing_lower == 0) {
1155
+ meanDistance = 0
1156
+ } else { // sometimes we need to go up but there is nothing there, or down but there is nothing there so it's a change of 0.
1157
+ meanDistance = (normalized_severity_eq1 + normalized_severity_eq2 + normalized_severity_eq3eq6 + normalized_severity_eq4 + normalized_severity_eq5) / n_existing_lower
1158
+ }
1159
+
1160
+ // 3. The score of the vector is the score of the MacroVector
1161
+ // (i.e. the score of the highest severity vector) minus the mean
1162
+ // distance so computed. This score is rounded to one decimal place.
1163
+ return roundToDecimalPlaces(Math.max(0, Math.min(10, value - meanDistance)), 1);
1164
+ }
1165
+
1166
+
1167
+ /**
1168
+ * Utility method to get the maximum vectors for a given equivalency (EQ) number.
1169
+ *
1170
+ * This method retrieves the highest severity vectors corresponding to the provided
1171
+ * EQ number based on the lookup table.
1172
+ *
1173
+ * @param {string} macroVector - The macro vector string representing the equivalent classes.
1174
+ * @param {number} eqNumber - The EQ number to look up (1-based index).
1175
+ * @returns {Array} - An array of highest severity vectors for the given EQ number.
1176
+ * @throws {Error} - Throws an error if the lookup key is not found for the given EQ number.
1177
+ */
1178
+ getMaxSeverityVectorsForEQ(macroVector, eqNumber) {
1179
+ return CVSS40.MAX_COMPOSED["eq" + eqNumber][macroVector[eqNumber - 1]];
1180
+ }
1181
+
1182
+
1183
+ /**
1184
+ * Extracts the value of a specified metric from a given string.
1185
+ *
1186
+ * This method finds the value of the specified metric within the provided string.
1187
+ * The metric value is expected to be followed by a colon and may be terminated by a slash.
1188
+ *
1189
+ * @param {string} metric - The metric to extract the value for.
1190
+ * @param {string} str - The string containing the metric and its value.
1191
+ * @returns {string} - The extracted metric value.
1192
+ */
1193
+ extractValueMetric(metric, str) {
1194
+ const metricIndex = str.indexOf(metric) + metric.length + 1;
1195
+ const extracted = str.slice(metricIndex);
1196
+ return extracted.indexOf('/') > 0 ? extracted.substring(0, extracted.indexOf('/')) : extracted;
1197
+ }
1198
+ }
1199
+
1200
+ // Check if the environment is Node.js
1201
+ if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
1202
+ module.exports = { CVSS40, Vector };
1203
+ } else {
1204
+ // In a browser environment, attach to the window object
1205
+ window.CVSS40 = CVSS40;
1206
+ window.Vector = Vector;
1207
+ }
1208
+