@cdxgen/cdxgen 13.0.0 → 13.1.0

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 (170) hide show
  1. package/README.md +26 -13
  2. package/bin/audit.js +6 -0
  3. package/bin/cdxgen.js +140 -8
  4. package/bin/convert.js +7 -0
  5. package/bin/evinse.js +1 -1
  6. package/bin/hbom.js +13 -4
  7. package/bin/tracebom.js +9 -1
  8. package/data/component-tags.json +8 -1
  9. package/data/frameworks-list.json +86 -1
  10. package/data/remediations.json +1144 -0
  11. package/data/rules/asar-archives.yaml +1 -1
  12. package/data/rules/build-fidelity.yaml +782 -0
  13. package/data/rules/package-integrity.yaml +13 -13
  14. package/lib/audit/index.js +48 -4
  15. package/lib/audit/scoring.js +2 -8
  16. package/lib/cli/bomAssembly.js +83 -15
  17. package/lib/cli/cliOptions.js +124 -7
  18. package/lib/cli/index.js +76 -48
  19. package/lib/cli/jsBom.js +32 -3
  20. package/lib/cli/jvmBom.js +1494 -1315
  21. package/lib/cli/managedBom.js +97 -28
  22. package/lib/cli/nativeBom.js +189 -10
  23. package/lib/core/buildLedger.js +799 -0
  24. package/lib/core/deferredExit.js +141 -0
  25. package/lib/core/env.js +69 -1
  26. package/lib/core/fs.js +60 -0
  27. package/lib/core/logger.js +76 -25
  28. package/lib/core/severity.js +16 -0
  29. package/lib/core/syncFileWriter.js +106 -0
  30. package/lib/core/ui.js +46 -2
  31. package/lib/ecosystems/ecosystems.js +9 -0
  32. package/lib/ecosystems/gradleutils.js +355 -15
  33. package/lib/ecosystems/parsers-js.js +26 -0
  34. package/lib/ecosystems/parsers-jvm.js +9 -0
  35. package/lib/ecosystems/parsers-misc.js +16 -2
  36. package/lib/ecosystems/parsers-python.js +171 -35
  37. package/lib/ecosystems/parsers-rust.js +12 -2
  38. package/lib/ecosystems/pypiClassifiers.js +326 -0
  39. package/lib/ecosystems/pythonutils.js +49 -1
  40. package/lib/ecosystems/rubyutils.js +13 -0
  41. package/lib/ecosystems/utils.js +9 -1
  42. package/lib/evinser/evinser.js +30 -12
  43. package/lib/helpers/remote/dependency-track.js +81 -14
  44. package/lib/inventory/auditCategories.js +9 -0
  45. package/lib/inventory/depsUtils.js +72 -0
  46. package/lib/inventory/dosai.js +431 -22
  47. package/lib/inventory/envcontext.js +211 -19
  48. package/lib/inventory/fetchBatch.js +57 -0
  49. package/lib/inventory/jvmToolEnv.js +593 -0
  50. package/lib/inventory/osinfo.js +123 -16
  51. package/lib/inventory/table.js +9 -5
  52. package/lib/inventory/toolRequirements.js +903 -0
  53. package/lib/managers/binary.js +216 -40
  54. package/lib/managers/docker.js +26 -5
  55. package/lib/server/server.js +1 -1
  56. package/lib/stages/postgen/annotationBuilder.js +97 -0
  57. package/lib/stages/postgen/auditBom.js +76 -79
  58. package/lib/stages/postgen/introspection/annotate.js +320 -0
  59. package/lib/stages/postgen/introspection/commandFacts.js +293 -0
  60. package/lib/stages/postgen/introspection/emit.js +192 -0
  61. package/lib/stages/postgen/introspection/formulationEvidence.js +427 -0
  62. package/lib/stages/postgen/introspection/markers.js +227 -0
  63. package/lib/stages/postgen/introspection/reflect.js +1214 -0
  64. package/lib/stages/postgen/introspection/report.js +1208 -0
  65. package/lib/stages/postgen/introspection/score.js +1067 -0
  66. package/lib/stages/postgen/introspection/shapeCommand.js +304 -0
  67. package/lib/stages/postgen/parentEdge.js +156 -0
  68. package/lib/stages/postgen/postgen.js +115 -2
  69. package/lib/stages/postgen/ruleEngine.js +17 -0
  70. package/lib/stages/pregen/envAudit.js +42 -2
  71. package/lib/stages/pregen/pregen.js +1171 -6
  72. package/lib/validator/bomValidator.js +11 -2
  73. package/package.json +14 -14
  74. package/types/lib/audit/index.d.ts.map +1 -1
  75. package/types/lib/audit/scoring.d.ts +2 -8
  76. package/types/lib/audit/scoring.d.ts.map +1 -1
  77. package/types/lib/cli/bomAssembly.d.ts +14 -0
  78. package/types/lib/cli/bomAssembly.d.ts.map +1 -1
  79. package/types/lib/cli/cliOptions.d.ts +2 -1
  80. package/types/lib/cli/cliOptions.d.ts.map +1 -1
  81. package/types/lib/cli/index.d.ts +8 -0
  82. package/types/lib/cli/index.d.ts.map +1 -1
  83. package/types/lib/cli/jsBom.d.ts.map +1 -1
  84. package/types/lib/cli/jvmBom.d.ts.map +1 -1
  85. package/types/lib/cli/managedBom.d.ts.map +1 -1
  86. package/types/lib/cli/nativeBom.d.ts.map +1 -1
  87. package/types/lib/core/buildLedger.d.ts +272 -0
  88. package/types/lib/core/buildLedger.d.ts.map +1 -0
  89. package/types/lib/core/deferredExit.d.ts +84 -0
  90. package/types/lib/core/deferredExit.d.ts.map +1 -0
  91. package/types/lib/core/env.d.ts +22 -1
  92. package/types/lib/core/env.d.ts.map +1 -1
  93. package/types/lib/core/fs.d.ts +21 -0
  94. package/types/lib/core/fs.d.ts.map +1 -1
  95. package/types/lib/core/logger.d.ts +14 -1
  96. package/types/lib/core/logger.d.ts.map +1 -1
  97. package/types/lib/core/severity.d.ts +11 -0
  98. package/types/lib/core/severity.d.ts.map +1 -0
  99. package/types/lib/core/syncFileWriter.d.ts +33 -0
  100. package/types/lib/core/syncFileWriter.d.ts.map +1 -0
  101. package/types/lib/core/ui.d.ts +1 -1
  102. package/types/lib/core/ui.d.ts.map +1 -1
  103. package/types/lib/ecosystems/ecosystems.d.ts.map +1 -1
  104. package/types/lib/ecosystems/gradleutils.d.ts +33 -0
  105. package/types/lib/ecosystems/gradleutils.d.ts.map +1 -1
  106. package/types/lib/ecosystems/parsers-js.d.ts.map +1 -1
  107. package/types/lib/ecosystems/parsers-jvm.d.ts.map +1 -1
  108. package/types/lib/ecosystems/parsers-misc.d.ts.map +1 -1
  109. package/types/lib/ecosystems/parsers-python.d.ts.map +1 -1
  110. package/types/lib/ecosystems/parsers-rust.d.ts.map +1 -1
  111. package/types/lib/ecosystems/pypiClassifiers.d.ts +80 -0
  112. package/types/lib/ecosystems/pypiClassifiers.d.ts.map +1 -0
  113. package/types/lib/ecosystems/pythonutils.d.ts +20 -0
  114. package/types/lib/ecosystems/pythonutils.d.ts.map +1 -1
  115. package/types/lib/ecosystems/rubyutils.d.ts.map +1 -1
  116. package/types/lib/ecosystems/utils.d.ts +4 -4
  117. package/types/lib/ecosystems/utils.d.ts.map +1 -1
  118. package/types/lib/evinser/evinser.d.ts +5 -1
  119. package/types/lib/evinser/evinser.d.ts.map +1 -1
  120. package/types/lib/helpers/remote/dependency-track.d.ts +45 -4
  121. package/types/lib/helpers/remote/dependency-track.d.ts.map +1 -1
  122. package/types/lib/inventory/auditCategories.d.ts +8 -0
  123. package/types/lib/inventory/auditCategories.d.ts.map +1 -1
  124. package/types/lib/inventory/depsUtils.d.ts.map +1 -1
  125. package/types/lib/inventory/dosai.d.ts +20 -0
  126. package/types/lib/inventory/dosai.d.ts.map +1 -1
  127. package/types/lib/inventory/envcontext.d.ts.map +1 -1
  128. package/types/lib/inventory/fetchBatch.d.ts +9 -0
  129. package/types/lib/inventory/fetchBatch.d.ts.map +1 -1
  130. package/types/lib/inventory/jvmToolEnv.d.ts +189 -0
  131. package/types/lib/inventory/jvmToolEnv.d.ts.map +1 -0
  132. package/types/lib/inventory/osinfo.d.ts +83 -2
  133. package/types/lib/inventory/osinfo.d.ts.map +1 -1
  134. package/types/lib/inventory/table.d.ts +4 -1
  135. package/types/lib/inventory/table.d.ts.map +1 -1
  136. package/types/lib/inventory/toolRequirements.d.ts +216 -0
  137. package/types/lib/inventory/toolRequirements.d.ts.map +1 -0
  138. package/types/lib/managers/binary.d.ts +71 -0
  139. package/types/lib/managers/binary.d.ts.map +1 -1
  140. package/types/lib/managers/docker.d.ts.map +1 -1
  141. package/types/lib/stages/postgen/annotationBuilder.d.ts +53 -0
  142. package/types/lib/stages/postgen/annotationBuilder.d.ts.map +1 -0
  143. package/types/lib/stages/postgen/auditBom.d.ts.map +1 -1
  144. package/types/lib/stages/postgen/introspection/annotate.d.ts +81 -0
  145. package/types/lib/stages/postgen/introspection/annotate.d.ts.map +1 -0
  146. package/types/lib/stages/postgen/introspection/commandFacts.d.ts +32 -0
  147. package/types/lib/stages/postgen/introspection/commandFacts.d.ts.map +1 -0
  148. package/types/lib/stages/postgen/introspection/emit.d.ts +71 -0
  149. package/types/lib/stages/postgen/introspection/emit.d.ts.map +1 -0
  150. package/types/lib/stages/postgen/introspection/formulationEvidence.d.ts +154 -0
  151. package/types/lib/stages/postgen/introspection/formulationEvidence.d.ts.map +1 -0
  152. package/types/lib/stages/postgen/introspection/markers.d.ts +57 -0
  153. package/types/lib/stages/postgen/introspection/markers.d.ts.map +1 -0
  154. package/types/lib/stages/postgen/introspection/reflect.d.ts +77 -0
  155. package/types/lib/stages/postgen/introspection/reflect.d.ts.map +1 -0
  156. package/types/lib/stages/postgen/introspection/report.d.ts +85 -0
  157. package/types/lib/stages/postgen/introspection/report.d.ts.map +1 -0
  158. package/types/lib/stages/postgen/introspection/score.d.ts +149 -0
  159. package/types/lib/stages/postgen/introspection/score.d.ts.map +1 -0
  160. package/types/lib/stages/postgen/introspection/shapeCommand.d.ts +79 -0
  161. package/types/lib/stages/postgen/introspection/shapeCommand.d.ts.map +1 -0
  162. package/types/lib/stages/postgen/parentEdge.d.ts +35 -0
  163. package/types/lib/stages/postgen/parentEdge.d.ts.map +1 -0
  164. package/types/lib/stages/postgen/postgen.d.ts +2 -2
  165. package/types/lib/stages/postgen/postgen.d.ts.map +1 -1
  166. package/types/lib/stages/postgen/ruleEngine.d.ts.map +1 -1
  167. package/types/lib/stages/pregen/envAudit.d.ts.map +1 -1
  168. package/types/lib/stages/pregen/pregen.d.ts +131 -0
  169. package/types/lib/stages/pregen/pregen.d.ts.map +1 -1
  170. package/types/lib/validator/bomValidator.d.ts.map +1 -1
@@ -0,0 +1,427 @@
1
+ /**
2
+ * Reader over the BOM's formulation section, the third evidence source.
3
+ *
4
+ * Formulation is two things at once, and the distinction governs every
5
+ * confidence decision downstream. The run-derived half — `type: "platform"`
6
+ * components recording the toolchain the generating run probed, and the git
7
+ * provenance components — is what cdxgen itself did. The config-parsed half —
8
+ * the commands, environment variables and task types under
9
+ * `formulation[].workflows[]` — is what the repo's CI declares, and a
10
+ * declared command was never observed to run.
11
+ *
12
+ * This module is pure: the BOM and the caller's run facts are the only
13
+ * inputs, nothing is read from the filesystem, and environment variable
14
+ * *values* are never touched — names only, because the BOM stores them and
15
+ * copying them into a report would republish them.
16
+ */
17
+
18
+ /**
19
+ * Property carrying the id of the run that stamped a BOM's formulation, set
20
+ * on every formulation workflow by the introspection pass that reflected
21
+ * over the freshly generated document.
22
+ *
23
+ * @type {string}
24
+ */
25
+ export const FORMULATION_RUN_ID_PROPERTY = "cdx:introspection:runId";
26
+
27
+ /** The formulation record was produced by the run being reflected. */
28
+ export const FORMULATION_ORIGIN_SAME_RUN = "same-run";
29
+
30
+ /** The formulation record came with a BOM this run did not generate. */
31
+ export const FORMULATION_ORIGIN_FOREIGN = "foreign";
32
+
33
+ /**
34
+ * The BOM carries no formulation record at all, which is neither of the
35
+ * above: there is no evidence to weigh, so every consumer must behave as it
36
+ * did before formulation became a source.
37
+ */
38
+ export const FORMULATION_ORIGIN_ABSENT = "absent";
39
+
40
+ /**
41
+ * Platform component names, as the environment probes emit them, mapped to
42
+ * the canonical ecosystem whose row their facts can corroborate. Names not
43
+ * listed here have no fidelity row to join.
44
+ *
45
+ * @type {Readonly<Record<string, string>>}
46
+ */
47
+ export const TOOL_NAME_ECOSYSTEMS = Object.freeze({
48
+ java: "java",
49
+ dotnet: "csharp",
50
+ python: "python",
51
+ "Node.js": "npm",
52
+ gcc: "c",
53
+ rustc: "rust",
54
+ go: "go",
55
+ ruby: "ruby",
56
+ swift: "swift",
57
+ });
58
+
59
+ /**
60
+ * Resolver executables whose presence in a declared command says a build or
61
+ * dependency resolution was attempted, mapped to the ecosystem they drive.
62
+ * Keys are executable basenames, lower-cased.
63
+ *
64
+ * @type {Readonly<Record<string, string>>}
65
+ */
66
+ export const RESOLVER_EXECUTABLES = Object.freeze({
67
+ mvn: "java",
68
+ mvnw: "java",
69
+ gradle: "java",
70
+ gradlew: "java",
71
+ java: "java",
72
+ ant: "java",
73
+ sbt: "java",
74
+ npm: "npm",
75
+ pnpm: "npm",
76
+ yarn: "npm",
77
+ bun: "npm",
78
+ deno: "npm",
79
+ npx: "npm",
80
+ node: "npm",
81
+ pip: "python",
82
+ pip3: "python",
83
+ poetry: "python",
84
+ uv: "python",
85
+ pdm: "python",
86
+ pipenv: "python",
87
+ conda: "python",
88
+ python: "python",
89
+ python3: "python",
90
+ cargo: "rust",
91
+ go: "go",
92
+ bundle: "ruby",
93
+ gem: "ruby",
94
+ rake: "ruby",
95
+ ruby: "ruby",
96
+ dotnet: "csharp",
97
+ nuget: "csharp",
98
+ composer: "php",
99
+ dart: "dart",
100
+ flutter: "dart",
101
+ helm: "helm",
102
+ lein: "clojure",
103
+ clj: "clojure",
104
+ clojure: "clojure",
105
+ swift: "swift",
106
+ pod: "cocoa",
107
+ mix: "elixir",
108
+ cmake: "c",
109
+ make: "c",
110
+ meson: "c",
111
+ ninja: "c",
112
+ cabal: "haskell",
113
+ stack: "haskell",
114
+ });
115
+
116
+ /**
117
+ * The runtime names the reflection reports, mapped to the family a
118
+ * formulation platform component records for the same runtime.
119
+ *
120
+ * @type {Readonly<Record<string, string>>}
121
+ */
122
+ const RUNTIME_FAMILIES = Object.freeze({
123
+ "Node.js": "Node.js",
124
+ node: "Node.js",
125
+ Deno: "Deno",
126
+ deno: "Deno",
127
+ Bun: "Bun",
128
+ bun: "Bun",
129
+ });
130
+
131
+ /**
132
+ * The final path segment of a POSIX- or Windows-flavoured command token.
133
+ *
134
+ * @param {string} token Command token.
135
+ * @returns {string} The token after its last path separator.
136
+ */
137
+ function basenameOfToken(token) {
138
+ const slash = token.lastIndexOf("/");
139
+ const backslash = token.lastIndexOf("\\");
140
+ return token.slice(Math.max(slash, backslash) + 1);
141
+ }
142
+
143
+ /**
144
+ * Whether an `executed` value is a CI action reference such as
145
+ * `actions/checkout@<sha>` rather than a shell command. An action reference
146
+ * carries no whitespace, contains a path separator, and names a revision of
147
+ * the repository after the last separator; a relative-path executable such
148
+ * as `./mvnw` carries no revision and stays a command.
149
+ *
150
+ * @param {string|undefined} executed The executed value of a step command.
151
+ * @returns {boolean} True when the value names an action, not a command.
152
+ */
153
+ export function isActionReference(executed) {
154
+ if (typeof executed !== "string" || !executed.trim()) {
155
+ return false;
156
+ }
157
+ const text = executed.trim();
158
+ if (/\s/.test(text) || !text.includes("/")) {
159
+ return false;
160
+ }
161
+ const lastSegment = text.slice(text.lastIndexOf("/") + 1);
162
+ return lastSegment.includes("@");
163
+ }
164
+
165
+ /**
166
+ * The executable a command line starts with, as a bare name.
167
+ *
168
+ * @param {string|undefined} commandLine Command line.
169
+ * @returns {string} Basename of the first whitespace token, or "" when absent.
170
+ */
171
+ export function executableOf(commandLine) {
172
+ const first = `${commandLine || ""}`.trim().split(/\s+/)[0] || "";
173
+ return basenameOfToken(first);
174
+ }
175
+
176
+ /**
177
+ * The revision a git provenance component records, from its OmniBOR or
178
+ * Software Heritage identifier. Identifiers prefix the raw hash with a
179
+ * scheme and type, so the value after the last colon is the revision itself.
180
+ *
181
+ * @param {Object} component Formulation component of type `file`.
182
+ * @returns {string|undefined} The raw revision, when the component names one.
183
+ */
184
+ function revisionOf(component) {
185
+ const identifiers = [
186
+ ...(Array.isArray(component?.omniborId) ? component.omniborId : []),
187
+ ...(Array.isArray(component?.swhid) ? component.swhid : []),
188
+ ];
189
+ for (const identifier of identifiers) {
190
+ if (typeof identifier === "string" && identifier.includes(":")) {
191
+ return identifier.slice(identifier.lastIndexOf(":") + 1) || undefined;
192
+ }
193
+ }
194
+ return undefined;
195
+ }
196
+
197
+ /**
198
+ * Whether a formulation component is one of the git graph nodes the
199
+ * provenance builder adds beside the per-file blobs.
200
+ *
201
+ * @param {Object} component Formulation component.
202
+ * @returns {boolean} True for `git-parent` and `git-tree`.
203
+ */
204
+ function isGitGraphNode(component) {
205
+ return component?.name === "git-parent" || component?.name === "git-tree";
206
+ }
207
+
208
+ /**
209
+ * The run id a formulation workflow carries, when the introspection pass
210
+ * that reflected over it stamped one.
211
+ *
212
+ * @param {Object} workflow Formulation workflow.
213
+ * @returns {string|undefined} The stamped run id, when present.
214
+ */
215
+ function stampedRunId(workflow) {
216
+ for (const property of Array.isArray(workflow?.properties)
217
+ ? workflow.properties
218
+ : []) {
219
+ if (property?.name === FORMULATION_RUN_ID_PROPERTY) {
220
+ return property?.value;
221
+ }
222
+ }
223
+ return undefined;
224
+ }
225
+
226
+ /**
227
+ * Whether a formulation workflow carries this run's id marker.
228
+ *
229
+ * @param {Object} bomJson CycloneDX BOM.
230
+ * @param {string} runId The reflection's own run id.
231
+ * @returns {boolean} True when the formulation names this run.
232
+ */
233
+ function carriesRunIdMarker(bomJson, runId) {
234
+ if (!runId) {
235
+ return false;
236
+ }
237
+ for (const entry of Array.isArray(bomJson?.formulation)
238
+ ? bomJson.formulation
239
+ : []) {
240
+ for (const workflow of Array.isArray(entry?.workflows)
241
+ ? entry.workflows
242
+ : []) {
243
+ if (stampedRunId(workflow) === runId) {
244
+ return true;
245
+ }
246
+ }
247
+ }
248
+ return false;
249
+ }
250
+
251
+ /**
252
+ * Read the provenance-aware formulation evidence out of a BOM.
253
+ *
254
+ * `origin` decides how the evidence may be used. `same-run` means the
255
+ * formulation describes this very run — its platform components are the same
256
+ * probes the ledger recorded as `tool.resolved`, so the evidence corroborates
257
+ * and must never add confidence. `foreign` means the BOM arrived without a
258
+ * ledger for its generation, and the formulation is the only surviving trace
259
+ * of what that build attempted. `absent` is neither: the BOM carries no
260
+ * formulation record, so there is nothing to weigh and every consumer keeps
261
+ * the behaviour it had before formulation became a source.
262
+ *
263
+ * Environment variable names are collected; values are never read.
264
+ *
265
+ * @param {Object} bomJson CycloneDX BOM.
266
+ * @param {Object} [context] Run facts the caller already knows.
267
+ * @param {string} [context.runId] This reflection's run id, matched against the formulation's marker.
268
+ * @param {number} [context.ledgerEventCount] Events the ledger holds for this BOM's generation.
269
+ * @returns {{
270
+ * origin: "same-run"|"foreign"|"absent",
271
+ * tools: {name: string|undefined, version: string|undefined, source: "probe"}[],
272
+ * declaredCommands: {executable: string, commandLine: string, workflow: string|undefined, task: string|undefined, step: string|undefined, source: "ci-config"}[],
273
+ * sourceTree: {commit: string|undefined, treeId: string|undefined, fileCount: number}|undefined,
274
+ * environmentKeys: string[]
275
+ * }} The formulation evidence.
276
+ */
277
+ export function readFormulationEvidence(bomJson, context = {}) {
278
+ const entries = Array.isArray(bomJson?.formulation)
279
+ ? bomJson.formulation
280
+ : [];
281
+ const sameRun =
282
+ Number(context.ledgerEventCount) > 0 ||
283
+ carriesRunIdMarker(bomJson, context.runId);
284
+ const tools = [];
285
+ let commit;
286
+ let treeId;
287
+ let fileCount = 0;
288
+ const environmentKeys = [];
289
+ const declaredCommands = [];
290
+ for (const entry of entries) {
291
+ for (const component of Array.isArray(entry?.components)
292
+ ? entry.components
293
+ : []) {
294
+ if (component?.type === "platform") {
295
+ tools.push({
296
+ name: component.name,
297
+ version: component.version,
298
+ source: "probe",
299
+ });
300
+ } else if (component?.type === "file") {
301
+ if (isGitGraphNode(component)) {
302
+ if (component.name === "git-parent") {
303
+ commit = revisionOf(component) || commit;
304
+ } else {
305
+ treeId = revisionOf(component) || treeId;
306
+ }
307
+ } else {
308
+ fileCount += 1;
309
+ }
310
+ }
311
+ }
312
+ for (const workflow of Array.isArray(entry?.workflows)
313
+ ? entry.workflows
314
+ : []) {
315
+ for (const input of Array.isArray(workflow?.inputs)
316
+ ? workflow.inputs
317
+ : []) {
318
+ for (const variable of Array.isArray(input?.environmentVars)
319
+ ? input.environmentVars
320
+ : []) {
321
+ if (variable?.name && !environmentKeys.includes(variable.name)) {
322
+ environmentKeys.push(variable.name);
323
+ }
324
+ }
325
+ }
326
+ for (const task of Array.isArray(workflow?.tasks) ? workflow.tasks : []) {
327
+ for (const step of Array.isArray(task?.steps) ? task.steps : []) {
328
+ for (const command of Array.isArray(step?.commands)
329
+ ? step.commands
330
+ : []) {
331
+ const executed = command?.executed;
332
+ if (
333
+ typeof executed !== "string" ||
334
+ !executed.trim() ||
335
+ isActionReference(executed)
336
+ ) {
337
+ continue;
338
+ }
339
+ declaredCommands.push({
340
+ executable: executableOf(executed),
341
+ commandLine: executed.trim(),
342
+ workflow: workflow?.name || workflow?.uid,
343
+ task: task?.name || task?.uid,
344
+ step: step?.name,
345
+ source: "ci-config",
346
+ });
347
+ }
348
+ }
349
+ }
350
+ }
351
+ }
352
+ return {
353
+ origin: !entries.length
354
+ ? FORMULATION_ORIGIN_ABSENT
355
+ : sameRun
356
+ ? FORMULATION_ORIGIN_SAME_RUN
357
+ : FORMULATION_ORIGIN_FOREIGN,
358
+ tools,
359
+ declaredCommands,
360
+ sourceTree:
361
+ commit || treeId || fileCount > 0
362
+ ? { commit, treeId, fileCount }
363
+ : undefined,
364
+ environmentKeys,
365
+ };
366
+ }
367
+
368
+ /**
369
+ * The formulation platform entry for the runtime family this reflection runs
370
+ * on, when the record cannot describe the machine in use: either the
371
+ * recorded version of the family differs from the runtime actually running,
372
+ * or the record names other toolchains but no entry of this family at all. A
373
+ * difference proves the formulation's toolchain record describes a machine
374
+ * other than the one re-scanning the BOM; it says nothing about the other
375
+ * toolchains the record names, which no probe of this run observed.
376
+ *
377
+ * @param {Object} evidence Formulation evidence from readFormulationEvidence.
378
+ * @param {Object} runtime The reflection's runtime facts.
379
+ * @param {string} runtime.name Runtime name, such as "Node.js".
380
+ * @param {string} runtime.version Runtime version.
381
+ * @returns {{tool: string, recordedVersion: string|undefined, runtimeVersion: string}|undefined} The mismatching formulation entry, when one exists.
382
+ */
383
+ export function findRuntimeToolMismatch(evidence, runtime) {
384
+ const family = RUNTIME_FAMILIES[`${runtime?.name}`];
385
+ const tools = Array.isArray(evidence?.tools) ? evidence.tools : [];
386
+ if (!family || !runtime?.version || !tools.length) {
387
+ return undefined;
388
+ }
389
+ for (const tool of tools) {
390
+ if (tool?.name === family) {
391
+ if (tool.version !== runtime.version) {
392
+ return {
393
+ tool: family,
394
+ recordedVersion: tool.version,
395
+ runtimeVersion: runtime.version,
396
+ };
397
+ }
398
+ return undefined;
399
+ }
400
+ }
401
+ return {
402
+ tool: family,
403
+ recordedVersion: undefined,
404
+ runtimeVersion: runtime.version,
405
+ };
406
+ }
407
+
408
+ /**
409
+ * The declared CI command a formulation records for one ecosystem, if any.
410
+ * Commands carry no ecosystem tag, so the executable answers: the first
411
+ * declared command whose executable drives that ecosystem's resolver wins,
412
+ * in the order the formulation records them.
413
+ *
414
+ * @param {Object} evidence Formulation evidence from readFormulationEvidence.
415
+ * @param {string} ecosystem Canonical ecosystem name.
416
+ * @returns {Object|undefined} The matching declared command entry.
417
+ */
418
+ export function declaredCommandForEcosystem(evidence, ecosystem) {
419
+ for (const command of Array.isArray(evidence?.declaredCommands)
420
+ ? evidence.declaredCommands
421
+ : []) {
422
+ if (RESOLVER_EXECUTABLES[command.executable] === ecosystem) {
423
+ return command;
424
+ }
425
+ }
426
+ return undefined;
427
+ }
@@ -0,0 +1,227 @@
1
+ /**
2
+ * Ecosystem marker detection for the build-introspection reflection step.
3
+ *
4
+ * A marker is a manifest or lockfile whose presence on disk proves an
5
+ * ecosystem was part of the scanned project. Markers drive two verdicts:
6
+ * an ecosystem with markers but zero components in the BOM is `absent`, and
7
+ * an ecosystem that has markers but no cdxgen project type at all is a
8
+ * coverage gap (`unsupported`), which is cdxgen's backlog rather than the
9
+ * user's problem.
10
+ *
11
+ * Detection is deliberately bounded: the scanned directory itself plus one
12
+ * level of immediate subdirectories. postProcess runs after generation, so a
13
+ * second full tree walk would be a measurable cost on large repos; markers
14
+ * deeper than one directory level are simply not seen, which is acceptable
15
+ * because tier assignment never relies on markers alone when the BOM already
16
+ * carries the ecosystem's components.
17
+ */
18
+
19
+ import { readdirSync } from "node:fs";
20
+ import { join } from "node:path";
21
+
22
+ import { safeExistsSync } from "../../../core/fs.js";
23
+
24
+ /** Largest number of immediate subdirectories inspected at depth one. */
25
+ const MAX_SUBDIRS_SCANNED = 100;
26
+
27
+ /**
28
+ * Marker table: ecosystem → manifest and lockfile names that indicate the
29
+ * ecosystem is part of the project. Entries are either exact file names or
30
+ * `{ suffix }` matchers for extension-driven ecosystems. Ecosystems cdxgen
31
+ * cannot parse (elm, crystal, nim, perl, r) are included so the reflection
32
+ * can report them as coverage gaps instead of silently ignoring them.
33
+ *
34
+ * @type {Readonly<Record<string, {names: string[], suffixes: string[]}>>}
35
+ */
36
+ export const ECOSYSTEM_MARKERS = Object.freeze({
37
+ java: {
38
+ names: [
39
+ "pom.xml",
40
+ "build.gradle",
41
+ "build.gradle.kts",
42
+ "settings.gradle",
43
+ "settings.gradle.kts",
44
+ "gradlew",
45
+ "build.sbt",
46
+ "mill-version",
47
+ ".sdkmanrc",
48
+ ],
49
+ suffixes: [],
50
+ },
51
+ npm: {
52
+ names: [
53
+ "package.json",
54
+ "package-lock.json",
55
+ "pnpm-lock.yaml",
56
+ "pnpm-workspace.yaml",
57
+ "yarn.lock",
58
+ "bun.lockb",
59
+ "bun.lock",
60
+ "deno.lock",
61
+ ],
62
+ suffixes: [],
63
+ },
64
+ python: {
65
+ names: [
66
+ "requirements.txt",
67
+ "Pipfile",
68
+ "Pipfile.lock",
69
+ "pyproject.toml",
70
+ "poetry.lock",
71
+ "pdm.lock",
72
+ "uv.lock",
73
+ "setup.py",
74
+ "setup.cfg",
75
+ ".python-version",
76
+ "environment.yml",
77
+ ],
78
+ suffixes: [],
79
+ },
80
+ go: { names: ["go.mod", "go.sum", "go.work"], suffixes: [] },
81
+ rust: {
82
+ names: [
83
+ "Cargo.toml",
84
+ "Cargo.lock",
85
+ "rust-toolchain.toml",
86
+ "rust-toolchain",
87
+ ],
88
+ suffixes: [],
89
+ },
90
+ ruby: { names: ["Gemfile", "Gemfile.lock", ".ruby-version"], suffixes: [] },
91
+ csharp: {
92
+ names: ["global.json", "nuget.config"],
93
+ suffixes: [".csproj", ".vbproj", ".fsproj", ".sln"],
94
+ },
95
+ php: { names: ["composer.json", "composer.lock"], suffixes: [] },
96
+ dart: { names: ["pubspec.yaml", "pubspec.lock"], suffixes: [] },
97
+ helm: { names: ["Chart.yaml", "Chart.yml"], suffixes: [] },
98
+ clojure: {
99
+ names: ["project.clj", "deps.edn", "bb.edn", "shadow-cljs.edn"],
100
+ suffixes: [],
101
+ },
102
+ cocoa: { names: ["Podfile", "Podfile.lock"], suffixes: [] },
103
+ swift: { names: ["Package.swift", "Package.resolved"], suffixes: [] },
104
+ elixir: { names: ["mix.exs", "mix.lock"], suffixes: [] },
105
+ c: {
106
+ names: ["CMakeLists.txt", "conanfile.txt", "conanfile.py"],
107
+ suffixes: [],
108
+ },
109
+ haskell: { names: ["cabal.project", "stack.yaml"], suffixes: [".cabal"] },
110
+ elm: { names: ["elm.json"], suffixes: [] },
111
+ crystal: { names: ["shard.yml", "shard.lock"], suffixes: [] },
112
+ nim: { names: ["nim.cfg", "config.nims"], suffixes: [".nimble"] },
113
+ perl: {
114
+ names: ["cpanfile", "META.json", "META.yml", "Makefile.PL", "Build.PL"],
115
+ suffixes: [],
116
+ },
117
+ r: { names: ["DESCRIPTION", "renv.lock"], suffixes: [] },
118
+ });
119
+
120
+ /** Marker file name → ecosystems claiming it, and marker suffix → claiming
121
+ * ecosystems, both built once from the table. The name index is exported so
122
+ * the reflection can recognize marker paths carried in ledger events without
123
+ * rescanning the filesystem. */
124
+ export const MARKERS_BY_NAME = new Map();
125
+ /** Marker suffix → ecosystems claiming it. */
126
+ const MARKERS_BY_SUFFIX = [];
127
+ for (const [ecosystem, markers] of Object.entries(ECOSYSTEM_MARKERS)) {
128
+ for (const name of markers.names) {
129
+ const ecosystems = MARKERS_BY_NAME.get(name) || [];
130
+ ecosystems.push(ecosystem);
131
+ MARKERS_BY_NAME.set(name, ecosystems);
132
+ }
133
+ for (const suffix of markers.suffixes) {
134
+ MARKERS_BY_SUFFIX.push({ ecosystem, suffix });
135
+ }
136
+ }
137
+
138
+ /**
139
+ * List a directory's entries as plain `{ name, directory }` records, sorted by
140
+ * name so detection results are deterministic. Callers may inject this helper
141
+ * to bound or observe the scan in tests.
142
+ *
143
+ * @param {string} dirPath Directory to list.
144
+ * @returns {{name: string, directory: boolean}[]} Sorted entries.
145
+ */
146
+ function listDirectory(dirPath) {
147
+ return readdirSync(dirPath, { withFileTypes: true })
148
+ .map((entry) => ({ name: entry.name, directory: entry.isDirectory() }))
149
+ .sort((a, b) => a.name.localeCompare(b.name));
150
+ }
151
+
152
+ /**
153
+ * Match one directory's entries against the marker index and record the full
154
+ * paths of every marker found.
155
+ *
156
+ * @param {string} dirPath Directory the entries belong to.
157
+ * @param {{name: string, directory: boolean}[]} entries Directory entries.
158
+ * @param {Map<string, Set<string>>} markersByEcosystem Accumulator keyed by ecosystem.
159
+ * @returns {void}
160
+ */
161
+ function scanEntries(dirPath, entries, markersByEcosystem) {
162
+ for (const entry of entries) {
163
+ const nameEcosystems = MARKERS_BY_NAME.get(entry.name);
164
+ if (nameEcosystems) {
165
+ for (const ecosystem of nameEcosystems) {
166
+ const paths = markersByEcosystem.get(ecosystem) || new Set();
167
+ paths.add(join(dirPath, entry.name));
168
+ markersByEcosystem.set(ecosystem, paths);
169
+ }
170
+ }
171
+ for (const { ecosystem, suffix } of MARKERS_BY_SUFFIX) {
172
+ if (entry.name.length > suffix.length && entry.name.endsWith(suffix)) {
173
+ const paths = markersByEcosystem.get(ecosystem) || new Set();
174
+ paths.add(join(dirPath, entry.name));
175
+ markersByEcosystem.set(ecosystem, paths);
176
+ }
177
+ }
178
+ }
179
+ }
180
+
181
+ /**
182
+ * Detect ecosystem markers under a project directory without walking the full
183
+ * tree: the directory itself is inspected, then up to
184
+ * {@link MAX_SUBDIRS_SCANNED} immediate subdirectories in sorted order. Paths
185
+ * are built with `node:path`, so `markersOnDisk` carries platform-correct
186
+ * separators.
187
+ *
188
+ * @param {string} projectPath Directory that was scanned, when known.
189
+ * @param {Object} [hooks] Test hooks.
190
+ * @param {(dirPath: string) => {name: string, directory: boolean}[]} [hooks.listDir] Directory lister override.
191
+ * @returns {{markersByEcosystem: Map<string, string[]>, scannedDirectories: number}} Marker paths per ecosystem and the number of directories inspected.
192
+ */
193
+ export function detectEcosystemMarkers(projectPath, hooks = {}) {
194
+ const markersByEcosystem = new Map();
195
+ if (!projectPath || !safeExistsSync(projectPath)) {
196
+ return { markersByEcosystem, scannedDirectories: 0 };
197
+ }
198
+ const listDir = hooks.listDir || listDirectory;
199
+ let rootEntries;
200
+ try {
201
+ rootEntries = listDir(projectPath);
202
+ } catch {
203
+ return { markersByEcosystem, scannedDirectories: 0 };
204
+ }
205
+ scanEntries(projectPath, rootEntries, markersByEcosystem);
206
+ const subdirs = rootEntries
207
+ .filter((entry) => entry.directory)
208
+ .map((entry) => entry.name)
209
+ .slice(0, MAX_SUBDIRS_SCANNED);
210
+ let scannedDirectories = 1;
211
+ for (const subdir of subdirs) {
212
+ const subdirPath = join(projectPath, subdir);
213
+ try {
214
+ scanEntries(subdirPath, listDir(subdirPath), markersByEcosystem);
215
+ } catch {
216
+ continue;
217
+ }
218
+ scannedDirectories += 1;
219
+ }
220
+ const sorted = new Map(
221
+ [...markersByEcosystem.entries()].map(([ecosystem, paths]) => [
222
+ ecosystem,
223
+ [...paths].sort(),
224
+ ]),
225
+ );
226
+ return { markersByEcosystem: sorted, scannedDirectories };
227
+ }