@openai/codex-security 0.1.20 → 0.1.23

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 (187) hide show
  1. package/README.md +1101 -865
  2. package/_bundled_plugin/.codex-plugin/plugin.json +1 -1
  3. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  4. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  5. package/_bundled_plugin/references/config-preflight.md +2 -30
  6. package/_bundled_plugin/references/core-scan.md +1 -1
  7. package/_bundled_plugin/references/final-report.md +1 -1
  8. package/_bundled_plugin/references/finding-detail-fields.md +5 -10
  9. package/_bundled_plugin/references/sarif-adapter.md +1 -1
  10. package/_bundled_plugin/schemas/patch-risk-assessment.schema.json +243 -0
  11. package/_bundled_plugin/scripts/config_preflight.py +2 -8
  12. package/_bundled_plugin/scripts/finalize_scan_contract.py +206 -35
  13. package/_bundled_plugin/scripts/finding_preview.py +22 -4
  14. package/_bundled_plugin/scripts/generate_in_scope_files.py +27 -1
  15. package/_bundled_plugin/scripts/launch_codex_security_mcp +0 -0
  16. package/_bundled_plugin/scripts/windows_scan_local_files.py +101 -7
  17. package/_bundled_plugin/scripts/workbench_cli.py +21 -1
  18. package/_bundled_plugin/scripts/workbench_dashboard.py +115 -0
  19. package/_bundled_plugin/scripts/workbench_db.py +29 -2
  20. package/_bundled_plugin/scripts/workbench_finding_index.py +42 -24
  21. package/_bundled_plugin/scripts/workbench_finding_workflows.py +190 -0
  22. package/_bundled_plugin/scripts/workbench_findings.py +216 -0
  23. package/_bundled_plugin/scripts/workbench_saved_results.py +30 -5
  24. package/_bundled_plugin/scripts/workbench_schema.py +263 -58
  25. package/_bundled_plugin/scripts/workbench_target.py +22 -3
  26. package/_bundled_plugin/skills/assess-patch-risk/SKILL.md +88 -0
  27. package/_bundled_plugin/skills/assess-patch-risk/agents/openai.yaml +4 -0
  28. package/_bundled_plugin/skills/assess-patch-risk/references/risk-rubric.md +76 -0
  29. package/_bundled_plugin/skills/assess-patch-risk/scripts/validate_patch_risk_assessment.py +163 -0
  30. package/_bundled_plugin/skills/fix-finding/SKILL.md +16 -48
  31. package/_bundled_plugin/skills/track-findings/SKILL.md +2 -0
  32. package/dist/api.d.ts +3 -1
  33. package/dist/api.d.ts.map +1 -1
  34. package/dist/api.js +129 -45
  35. package/dist/api.js.map +1 -1
  36. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  37. package/dist/bulk-scan-discovery.js +6 -24
  38. package/dist/bulk-scan-discovery.js.map +1 -1
  39. package/dist/cli.d.ts +28 -2
  40. package/dist/cli.d.ts.map +1 -1
  41. package/dist/cli.js +525 -96
  42. package/dist/cli.js.map +1 -1
  43. package/dist/cloud-publish.d.ts +5 -1
  44. package/dist/cloud-publish.d.ts.map +1 -1
  45. package/dist/cloud-publish.js +312 -10
  46. package/dist/cloud-publish.js.map +1 -1
  47. package/dist/component-plan.d.ts.map +1 -1
  48. package/dist/component-plan.js +5 -1
  49. package/dist/component-plan.js.map +1 -1
  50. package/dist/contract.d.ts +1 -0
  51. package/dist/contract.d.ts.map +1 -1
  52. package/dist/contract.js +9 -0
  53. package/dist/contract.js.map +1 -1
  54. package/dist/custom-publish.d.ts +22 -0
  55. package/dist/custom-publish.d.ts.map +1 -0
  56. package/dist/custom-publish.js +47 -0
  57. package/dist/custom-publish.js.map +1 -0
  58. package/dist/custom-validation-prompt.js +1 -1
  59. package/dist/custom-validation-prompt.js.map +1 -1
  60. package/dist/deduplication/checkpointed-review.d.ts +16 -0
  61. package/dist/deduplication/checkpointed-review.d.ts.map +1 -0
  62. package/dist/deduplication/checkpointed-review.js +76 -0
  63. package/dist/deduplication/checkpointed-review.js.map +1 -0
  64. package/dist/deduplication/codex-review.d.ts +21 -0
  65. package/dist/deduplication/codex-review.d.ts.map +1 -0
  66. package/dist/deduplication/codex-review.js +252 -0
  67. package/dist/deduplication/codex-review.js.map +1 -0
  68. package/dist/deduplication/deduplication-prompts.d.ts +8 -0
  69. package/dist/deduplication/deduplication-prompts.d.ts.map +1 -0
  70. package/dist/deduplication/deduplication-prompts.js +35 -0
  71. package/dist/deduplication/deduplication-prompts.js.map +1 -0
  72. package/dist/deduplication/deduplication-reviewer.d.ts +46 -0
  73. package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -0
  74. package/dist/deduplication/deduplication-reviewer.js +105 -0
  75. package/dist/deduplication/deduplication-reviewer.js.map +1 -0
  76. package/dist/deduplication/deduplication.d.ts +6 -0
  77. package/dist/deduplication/deduplication.d.ts.map +1 -0
  78. package/dist/deduplication/deduplication.js +85 -0
  79. package/dist/deduplication/deduplication.js.map +1 -0
  80. package/dist/deduplication/scan.d.ts +16 -0
  81. package/dist/deduplication/scan.d.ts.map +1 -0
  82. package/dist/deduplication/scan.js +101 -0
  83. package/dist/deduplication/scan.js.map +1 -0
  84. package/dist/finding-dedupe-groups.d.ts +7 -0
  85. package/dist/finding-dedupe-groups.d.ts.map +1 -0
  86. package/dist/finding-dedupe-groups.js +2 -0
  87. package/dist/finding-dedupe-groups.js.map +1 -0
  88. package/dist/finding-retrieval.d.ts +13 -0
  89. package/dist/finding-retrieval.d.ts.map +1 -0
  90. package/dist/finding-retrieval.js +2 -0
  91. package/dist/finding-retrieval.js.map +1 -0
  92. package/dist/finding-workflow.d.ts +53 -0
  93. package/dist/finding-workflow.d.ts.map +1 -0
  94. package/dist/finding-workflow.js +117 -0
  95. package/dist/finding-workflow.js.map +1 -0
  96. package/dist/findings-client.d.ts +15 -0
  97. package/dist/findings-client.d.ts.map +1 -0
  98. package/dist/findings-client.js +60 -0
  99. package/dist/findings-client.js.map +1 -0
  100. package/dist/github.d.ts +161 -0
  101. package/dist/github.d.ts.map +1 -0
  102. package/dist/github.js +158 -0
  103. package/dist/github.js.map +1 -0
  104. package/dist/index.d.ts +6 -0
  105. package/dist/index.d.ts.map +1 -1
  106. package/dist/index.js +3 -0
  107. package/dist/index.js.map +1 -1
  108. package/dist/publication.js +28 -40
  109. package/dist/publication.js.map +1 -1
  110. package/dist/publish.d.ts +10 -0
  111. package/dist/publish.d.ts.map +1 -1
  112. package/dist/publish.js +29 -5
  113. package/dist/publish.js.map +1 -1
  114. package/dist/runtime.d.ts +5 -4
  115. package/dist/runtime.d.ts.map +1 -1
  116. package/dist/runtime.js +371 -197
  117. package/dist/runtime.js.map +1 -1
  118. package/dist/saved-scan.d.ts +15 -0
  119. package/dist/saved-scan.d.ts.map +1 -0
  120. package/dist/saved-scan.js +68 -0
  121. package/dist/saved-scan.js.map +1 -0
  122. package/dist/scan-comparison.d.ts +6 -2
  123. package/dist/scan-comparison.d.ts.map +1 -1
  124. package/dist/scan-comparison.js +7 -2
  125. package/dist/scan-comparison.js.map +1 -1
  126. package/dist/server/dashboard/THIRD_PARTY_NOTICES.txt +276 -0
  127. package/dist/server/dashboard/app.css +2 -0
  128. package/dist/server/dashboard/app.js +66 -0
  129. package/dist/server/dashboard/index.html +18 -0
  130. package/dist/server/dashboard-types.d.ts +45 -0
  131. package/dist/server/dashboard-types.d.ts.map +1 -0
  132. package/dist/server/dashboard-types.js +2 -0
  133. package/dist/server/dashboard-types.js.map +1 -0
  134. package/dist/server/dashboard.d.ts +6 -0
  135. package/dist/server/dashboard.d.ts.map +1 -0
  136. package/dist/server/dashboard.js +45 -0
  137. package/dist/server/dashboard.js.map +1 -0
  138. package/dist/server/embeddings.d.ts +16 -0
  139. package/dist/server/embeddings.d.ts.map +1 -0
  140. package/dist/server/embeddings.js +114 -0
  141. package/dist/server/embeddings.js.map +1 -0
  142. package/dist/server/errors.d.ts +5 -0
  143. package/dist/server/errors.d.ts.map +1 -0
  144. package/dist/server/errors.js +8 -0
  145. package/dist/server/errors.js.map +1 -0
  146. package/dist/server/findings-service.d.ts +20 -0
  147. package/dist/server/findings-service.d.ts.map +1 -0
  148. package/dist/server/findings-service.js +31 -0
  149. package/dist/server/findings-service.js.map +1 -0
  150. package/dist/server/index.d.ts +2 -0
  151. package/dist/server/index.d.ts.map +1 -0
  152. package/dist/server/index.js +32 -0
  153. package/dist/server/index.js.map +1 -0
  154. package/dist/server/routes.d.ts +6 -0
  155. package/dist/server/routes.d.ts.map +1 -0
  156. package/dist/server/routes.js +86 -0
  157. package/dist/server/routes.js.map +1 -0
  158. package/dist/server/server.d.ts +10 -0
  159. package/dist/server/server.d.ts.map +1 -0
  160. package/dist/server/server.js +17 -0
  161. package/dist/server/server.js.map +1 -0
  162. package/dist/server/sqlite-store.d.ts +22 -0
  163. package/dist/server/sqlite-store.d.ts.map +1 -0
  164. package/dist/server/sqlite-store.js +82 -0
  165. package/dist/server/sqlite-store.js.map +1 -0
  166. package/dist/server/storage.d.ts +32 -0
  167. package/dist/server/storage.d.ts.map +1 -0
  168. package/dist/server/storage.js +2 -0
  169. package/dist/server/storage.js.map +1 -0
  170. package/dist/server/validation.d.ts +17 -0
  171. package/dist/server/validation.d.ts.map +1 -0
  172. package/dist/server/validation.js +53 -0
  173. package/dist/server/validation.js.map +1 -0
  174. package/dist/targets.d.ts.map +1 -1
  175. package/dist/targets.js +1 -0
  176. package/dist/targets.js.map +1 -1
  177. package/dist/thread-source.d.ts +8 -0
  178. package/dist/thread-source.d.ts.map +1 -0
  179. package/dist/thread-source.js +7 -0
  180. package/dist/thread-source.js.map +1 -0
  181. package/dist/trusted-executable.d.ts.map +1 -1
  182. package/dist/trusted-executable.js +2 -1
  183. package/dist/trusted-executable.js.map +1 -1
  184. package/dist/version.d.ts +1 -1
  185. package/dist/version.js +1 -1
  186. package/dist/version.js.map +1 -1
  187. package/package.json +23 -8
@@ -0,0 +1,6 @@
1
+ export interface DeduplicationResult {
2
+ uniqueFindingIds: string[];
3
+ duplicateGroups: string[][];
4
+ deduplicationStatus: "completed";
5
+ }
6
+ //# sourceMappingURL=deduplication.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deduplication.d.ts","sourceRoot":"","sources":["../../src/deduplication/deduplication.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,mBAAmB;IAClC,gBAAgB,EAAE,MAAM,EAAE,CAAC;IAC3B,eAAe,EAAE,MAAM,EAAE,EAAE,CAAC;IAC5B,mBAAmB,EAAE,WAAW,CAAC;CAClC"}
@@ -0,0 +1,85 @@
1
+ import { pairKey, } from "./deduplication-reviewer.js";
2
+ const severityOrder = {
3
+ critical: 0,
4
+ high: 1,
5
+ medium: 2,
6
+ low: 3,
7
+ informational: 4,
8
+ };
9
+ /** @internal */
10
+ export class FindingDeduplicator {
11
+ candidates;
12
+ reviewer;
13
+ signal;
14
+ constructor(candidates, reviewer, signal) {
15
+ this.candidates = candidates;
16
+ this.reviewer = reviewer;
17
+ this.signal = signal;
18
+ }
19
+ async run(findingIds) {
20
+ this.signal?.throwIfAborted();
21
+ const ids = [...new Set(findingIds)];
22
+ const findings = new Map();
23
+ const nominated = new Map();
24
+ for (const id of ids) {
25
+ this.signal?.throwIfAborted();
26
+ const result = await this.candidates.potentialDuplicates(id);
27
+ const neighborhood = [result.finding, ...result.potentialDuplicates];
28
+ for (const finding of neighborhood)
29
+ findings.set(finding.findingId, finding);
30
+ if (neighborhood.length < 2)
31
+ continue;
32
+ const screening = await this.reviewer.screen(neighborhood);
33
+ for (const decision of screening.decisions) {
34
+ if (decision.decision === "SAME") {
35
+ nominated.set(pairKey(decision.findingIds), decision.findingIds);
36
+ }
37
+ }
38
+ }
39
+ const adjacent = new Map();
40
+ for (const pair of nominated.values()) {
41
+ this.signal?.throwIfAborted();
42
+ const originals = pair.map((id) => findings.get(id));
43
+ if ((await this.reviewer.reviewPair(originals)).decision !== "SAME")
44
+ continue;
45
+ for (const [left, right] of [pair, [pair[1], pair[0]]]) {
46
+ const neighbors = adjacent.get(left) ?? new Set();
47
+ neighbors.add(right);
48
+ adjacent.set(left, neighbors);
49
+ }
50
+ }
51
+ const selected = new Set(ids);
52
+ const visited = new Set();
53
+ const duplicateGroups = [];
54
+ const canonical = new Map();
55
+ for (const id of findings.keys()) {
56
+ this.signal?.throwIfAborted();
57
+ if (!adjacent.has(id) || visited.has(id))
58
+ continue;
59
+ const members = [];
60
+ const pending = [id];
61
+ while (pending.length > 0) {
62
+ const member = pending.pop();
63
+ if (visited.has(member))
64
+ continue;
65
+ visited.add(member);
66
+ members.push(member);
67
+ pending.push(...adjacent.get(member));
68
+ }
69
+ if (!members.some((member) => selected.has(member)))
70
+ continue;
71
+ members.sort((left, right) => severityOrder[findings.get(left).severity.level] -
72
+ severityOrder[findings.get(right).severity.level] ||
73
+ (left < right ? -1 : left > right ? 1 : 0));
74
+ duplicateGroups.push(members);
75
+ for (const member of members)
76
+ canonical.set(member, members[0]);
77
+ }
78
+ return {
79
+ uniqueFindingIds: [...new Set(ids.map((id) => canonical.get(id) ?? id))],
80
+ duplicateGroups,
81
+ deduplicationStatus: "completed",
82
+ };
83
+ }
84
+ }
85
+ //# sourceMappingURL=deduplication.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deduplication.js","sourceRoot":"","sources":["../../src/deduplication/deduplication.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,OAAO,GAER,MAAM,6BAA6B,CAAC;AAQrC,MAAM,aAAa,GAAiD;IAClE,QAAQ,EAAE,CAAC;IACX,IAAI,EAAE,CAAC;IACP,MAAM,EAAE,CAAC;IACT,GAAG,EAAE,CAAC;IACN,aAAa,EAAE,CAAC;CACjB,CAAC;AAEF,gBAAgB;AAChB,MAAM,OAAO,mBAAmB;IAEX;IAGA;IACA;IALnB,YACmB,UAEhB,EACgB,QAA+B,EAC/B,MAAoB;QAJpB,eAAU,GAAV,UAAU,CAE1B;QACgB,aAAQ,GAAR,QAAQ,CAAuB;QAC/B,WAAM,GAAN,MAAM,CAAc;IACpC,CAAC;IAEJ,KAAK,CAAC,GAAG,CAAC,UAA6B;QACrC,IAAI,CAAC,MAAM,EAAE,cAAc,EAAE,CAAC;QAC9B,MAAM,GAAG,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;QACrC,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAm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,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAE,CAAC,CAAC;YACzC,CAAC;YACD,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;gBAAE,SAAS;YAC9D,OAAO,CAAC,IAAI,CACV,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CACd,aAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAE,CAAC,QAAQ,CAAC,KAAK,CAAC;gBAC/C,aAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAE,CAAC,QAAQ,CAAC,KAAK,CAAC;gBACpD,CAAC,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAC7C,CAAC;YACF,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAC9B,KAAK,MAAM,MAAM,IAAI,OAAO;gBAAE,SAAS,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAE,CAAC,CAAC;QACnE,CAAC;QACD,OAAO;YACL,gBAAgB,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;YACxE,eAAe;YACf,mBAAmB,EAAE,WAAW;SACjC,CAAC;IACJ,CAAC;CACF","sourcesContent":["import type { Finding } from \"../models.js\";\nimport type { FindingNeighborhood } from \"../finding-retrieval.js\";\nimport {\n pairKey,\n type DeduplicationReviewer,\n} from \"./deduplication-reviewer.js\";\n\nexport interface DeduplicationResult {\n uniqueFindingIds: string[];\n duplicateGroups: string[][];\n deduplicationStatus: \"completed\";\n}\n\nconst severityOrder: Record<Finding[\"severity\"][\"level\"], number> = {\n critical: 0,\n high: 1,\n medium: 2,\n low: 3,\n informational: 4,\n};\n\n/** @internal */\nexport class FindingDeduplicator {\n constructor(\n private readonly candidates: {\n potentialDuplicates(findingId: string): Promise<FindingNeighborhood>;\n },\n private readonly reviewer: DeduplicationReviewer,\n private readonly signal?: AbortSignal,\n ) {}\n\n async run(findingIds: readonly string[]): Promise<DeduplicationResult> {\n this.signal?.throwIfAborted();\n const ids = [...new Set(findingIds)];\n const findings = new Map<string, Finding>();\n const nominated = new Map<string, [string, string]>();\n for (const id of ids) {\n this.signal?.throwIfAborted();\n const result = await this.candidates.potentialDuplicates(id);\n const neighborhood = [result.finding, ...result.potentialDuplicates];\n for (const finding of neighborhood)\n findings.set(finding.findingId, finding);\n if (neighborhood.length < 2) continue;\n const screening = await this.reviewer.screen(neighborhood);\n for (const decision of screening.decisions) {\n if (decision.decision === \"SAME\") {\n nominated.set(pairKey(decision.findingIds), decision.findingIds);\n }\n }\n }\n\n const adjacent = new Map<string, Set<string>>();\n for (const pair of nominated.values()) {\n this.signal?.throwIfAborted();\n const originals = pair.map((id) => findings.get(id)!);\n if ((await this.reviewer.reviewPair(originals)).decision !== \"SAME\")\n continue;\n for (const [left, right] of [pair, [pair[1], pair[0]]] as const) {\n const neighbors = adjacent.get(left) ?? new Set<string>();\n neighbors.add(right);\n adjacent.set(left, neighbors);\n }\n }\n\n const selected = new Set(ids);\n const visited = new Set<string>();\n const duplicateGroups: string[][] = [];\n const canonical = new Map<string, string>();\n for (const id of findings.keys()) {\n this.signal?.throwIfAborted();\n if (!adjacent.has(id) || visited.has(id)) continue;\n const members: string[] = [];\n const pending = [id];\n while (pending.length > 0) {\n const member = pending.pop()!;\n if (visited.has(member)) continue;\n visited.add(member);\n members.push(member);\n pending.push(...adjacent.get(member)!);\n }\n if (!members.some((member) => selected.has(member))) continue;\n members.sort(\n (left, right) =>\n severityOrder[findings.get(left)!.severity.level] -\n severityOrder[findings.get(right)!.severity.level] ||\n (left < right ? -1 : left > right ? 1 : 0),\n );\n duplicateGroups.push(members);\n for (const member of members) canonical.set(member, members[0]!);\n }\n return {\n uniqueFindingIds: [...new Set(ids.map((id) => canonical.get(id) ?? id))],\n duplicateGroups,\n deduplicationStatus: \"completed\",\n };\n }\n}\n"]}
@@ -0,0 +1,16 @@
1
+ import { type DeduplicationResult } from "./deduplication.js";
2
+ export interface DeduplicateScanOptions {
3
+ /** Resume the named local findings workflow, including custom publication. */
4
+ workflowId?: string;
5
+ /** Findings API base URL. The scan's findings must already be indexed there. */
6
+ findingsUrl: string;
7
+ /** Search all repositories instead of the saved scan's targetId. Defaults to false. */
8
+ allRepositories?: boolean;
9
+ signal?: AbortSignal;
10
+ }
11
+ export interface DeduplicateScanResult extends DeduplicationResult {
12
+ scanId: string;
13
+ }
14
+ /** Review a saved scan against embedding candidates and persist accepted duplicate groups. */
15
+ export declare function deduplicateScan(scanId: string, options: DeduplicateScanOptions): Promise<DeduplicateScanResult>;
16
+ //# sourceMappingURL=scan.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scan.d.ts","sourceRoot":"","sources":["../../src/deduplication/scan.ts"],"names":[],"mappings":"AAYA,OAAO,EAEL,KAAK,mBAAmB,EACzB,MAAM,oBAAoB,CAAC;AAkB5B,MAAM,WAAW,sBAAsB;IACrC,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,gFAAgF;IAChF,WAAW,EAAE,MAAM,CAAC;IACpB,uFAAuF;IACvF,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,WAAW,qBAAsB,SAAQ,mBAAmB;IAChE,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,8FAA8F;AAC9F,wBAAsB,eAAe,CACnC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,qBAAqB,CAAC,CAEhC"}
@@ -0,0 +1,101 @@
1
+ import { loadContractWithScanDirectory } from "../contract.js";
2
+ import { bundledPluginRoot, codexSecurityStateDirectory, resolvePluginPython, runWorkbench, } from "../runtime.js";
3
+ import { resolveCompletedScan, } from "../saved-scan.js";
4
+ import { CodexReviewRunner } from "./codex-review.js";
5
+ import { FindingDeduplicator, } from "./deduplication.js";
6
+ import { CodexDeduplicationReviewer, } from "./deduplication-reviewer.js";
7
+ import { FindingsClient } from "../findings-client.js";
8
+ import { FindingWorkflow, workflowDestination, workflowDigest, } from "../finding-workflow.js";
9
+ import { publishScanToCustomInternal } from "../custom-publish.js";
10
+ import { CheckpointedReviewRunner, reviewSettingsDigest, } from "./checkpointed-review.js";
11
+ /** Review a saved scan against embedding candidates and persist accepted duplicate groups. */
12
+ export async function deduplicateScan(scanId, options) {
13
+ return await deduplicateScanInternal(scanId, options);
14
+ }
15
+ /** @internal */
16
+ export async function deduplicateScanInternal(scanId, options, dependencies = {}) {
17
+ options.signal?.throwIfAborted();
18
+ const environment = dependencies.environment ?? process.env;
19
+ const pluginRoot = await bundledPluginRoot();
20
+ const scan = await resolveCompletedScan(scanId, {
21
+ currentDirectory: dependencies.currentDirectory ?? (() => process.cwd()),
22
+ runWorkbench: dependencies.runWorkbench ??
23
+ (async (args) => {
24
+ const stateEnvironment = {
25
+ ...environment,
26
+ CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(environment),
27
+ };
28
+ return await runWorkbench({
29
+ environment: stateEnvironment,
30
+ pluginRoot,
31
+ python: await resolvePluginPython({
32
+ environment: stateEnvironment,
33
+ }),
34
+ signal: options.signal,
35
+ failureMessage: "Could not read Codex Security scan history",
36
+ }, args);
37
+ }),
38
+ });
39
+ const { contract, scanDirectory } = await loadContractWithScanDirectory(scan.scanDir, {
40
+ pluginRoot,
41
+ expectedScanId: scan.scanId,
42
+ signal: options.signal,
43
+ });
44
+ const client = new FindingsClient(options.findingsUrl, options.signal, dependencies.fetch);
45
+ const scope = options.allRepositories === true
46
+ ? { allRepositories: true }
47
+ : { repositoryId: contract.manifest.scan.target.targetId };
48
+ const workflow = options.workflowId === undefined
49
+ ? undefined
50
+ : new FindingWorkflow(options.workflowId, environment, dependencies.runWorkbench === undefined
51
+ ? undefined
52
+ : (_options, args, input) => dependencies.runWorkbench(args, input));
53
+ if (workflow) {
54
+ await workflow.protectArtifacts(scanDirectory);
55
+ await workflow.bind({
56
+ scanId: scan.scanId,
57
+ scanDir: scanDirectory,
58
+ artifactDigest: workflowDigest(contract),
59
+ destination: workflowDestination(options.findingsUrl),
60
+ scope,
61
+ });
62
+ await workflow.complete("scan", null);
63
+ await publishScanToCustomInternal(scanDirectory, {
64
+ findingsUrl: options.findingsUrl,
65
+ workflowId: options.workflowId,
66
+ expectedScanId: scan.scanId,
67
+ signal: options.signal,
68
+ }, {
69
+ environment,
70
+ fetch: dependencies.fetch,
71
+ runWorkbench: dependencies.runWorkbench === undefined
72
+ ? undefined
73
+ : (_options, args, input) => dependencies.runWorkbench(args, input),
74
+ });
75
+ }
76
+ const dedupe = async () => {
77
+ const saved = (await workflow?.get())?.stages.dedupe;
78
+ if (saved?.pendingWrite) {
79
+ await client.storeDedupeGroups(saved.pendingWrite.groups);
80
+ return saved.result;
81
+ }
82
+ const runner = dependencies.reviewRunner ??
83
+ new CodexReviewRunner(environment, undefined, options.signal, scan["targetPath"]);
84
+ const checkpoints = workflow
85
+ ? new CheckpointedReviewRunner(workflow, runner, await workflow.sourceSnapshot(scan["targetPath"]), scope, await reviewSettingsDigest(environment))
86
+ : undefined;
87
+ const deduplicator = new FindingDeduplicator({
88
+ potentialDuplicates: (findingId) => client.potentialDuplicates(findingId, scope),
89
+ }, dependencies.reviewer ??
90
+ new CodexDeduplicationReviewer(checkpoints ?? runner), options.signal);
91
+ const reviewed = await deduplicator.run(contract.findings.findings.map((finding) => finding.findingId));
92
+ await checkpoints?.assertSourceUnchanged();
93
+ options.signal?.throwIfAborted();
94
+ const result = { scanId: scan.scanId, ...reviewed };
95
+ await workflow?.prepareDedupe(result, { groups: result.duplicateGroups });
96
+ await client.storeDedupeGroups(result.duplicateGroups);
97
+ return result;
98
+ };
99
+ return workflow ? await workflow.run("dedupe", dedupe) : await dedupe();
100
+ }
101
+ //# sourceMappingURL=scan.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scan.js","sourceRoot":"","sources":["../../src/deduplication/scan.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EACL,iBAAiB,EACjB,2BAA2B,EAC3B,mBAAmB,EACnB,YAAY,GACb,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,oBAAoB,GAErB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EACL,mBAAmB,GAEpB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,0BAA0B,GAE3B,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,cAAc,EAAwB,MAAM,uBAAuB,CAAC;AAE7E,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,cAAc,GACf,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,2BAA2B,EAAE,MAAM,sBAAsB,CAAC;AACnE,OAAO,EACL,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,0BAA0B,CAAC;AAgBlC,8FAA8F;AAC9F,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,MAAc,EACd,OAA+B;IAE/B,OAAO,MAAM,uBAAuB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACxD,CAAC;AAED,gBAAgB;AAChB,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,MAAc,EACd,OAA+B,EAC/B,eAKI,EAAE;IAEN,OAAO,CAAC,MAAM,EAAE,cAAc,EAAE,CAAC;IACjC,MAAM,WAAW,GAAG,YAAY,CAAC,WAAW,IAAI,OAAO,CAAC,GAAG,CAAC;IAC5D,MAAM,UAAU,GAAG,M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EAAE,aAAa,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,CAAC,eAAe,EAAE,CAAC,CAAC;QAC1E,MAAM,MAAM,CAAC,iBAAiB,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;QACvD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IACF,OAAO,QAAQ,CAAC,CAAC,CAAC,MAAM,QAAQ,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,MAAM,EAAE,CAAC;AAC1E,CAAC","sourcesContent":["import { loadContractWithScanDirectory } from \"../contract.js\";\nimport {\n bundledPluginRoot,\n codexSecurityStateDirectory,\n resolvePluginPython,\n runWorkbench,\n} from \"../runtime.js\";\nimport {\n resolveCompletedScan,\n type SavedScanDependencies,\n} from \"../saved-scan.js\";\nimport { CodexReviewRunner } from \"./codex-review.js\";\nimport {\n FindingDeduplicator,\n type DeduplicationResult,\n} from \"./deduplication.js\";\nimport {\n CodexDeduplicationReviewer,\n type DeduplicationReviewer,\n} from \"./deduplication-reviewer.js\";\nimport { FindingsClient, type FindingsRequest } from \"../findings-client.js\";\nimport type { FindingSearchScope } from \"../finding-retrieval.js\";\nimport {\n FindingWorkflow,\n workflowDestination,\n workflowDigest,\n} from \"../finding-workflow.js\";\nimport { publishScanToCustomInternal } from \"../custom-publish.js\";\nimport {\n CheckpointedReviewRunner,\n reviewSettingsDigest,\n} from \"./checkpointed-review.js\";\n\nexport interface DeduplicateScanOptions {\n /** Resume the named local findings workflow, including custom publication. */\n workflowId?: string;\n /** Findings API base URL. The scan's findings must already be indexed there. */\n findingsUrl: string;\n /** Search all repositories instead of the saved scan's targetId. Defaults to false. */\n allRepositories?: boolean;\n signal?: AbortSignal;\n}\n\nexport interface DeduplicateScanResult extends DeduplicationResult {\n scanId: string;\n}\n\n/** Review a saved scan against embedding candidates and persist accepted duplicate groups. */\nexport async function deduplicateScan(\n scanId: string,\n options: DeduplicateScanOptions,\n): Promise<DeduplicateScanResult> {\n return await deduplicateScanInternal(scanId, options);\n}\n\n/** @internal */\nexport async function deduplicateScanInternal(\n scanId: string,\n options: DeduplicateScanOptions,\n dependencies: Partial<SavedScanDependencies> & {\n environment?: NodeJS.ProcessEnv;\n reviewer?: DeduplicationReviewer;\n reviewRunner?: Pick<CodexReviewRunner, \"run\">;\n fetch?: FindingsRequest;\n } = {},\n): Promise<DeduplicateScanResult> {\n options.signal?.throwIfAborted();\n const environment = dependencies.environment ?? process.env;\n const pluginRoot = await bundledPluginRoot();\n const scan = await resolveCompletedScan(scanId, {\n currentDirectory: dependencies.currentDirectory ?? (() => process.cwd()),\n runWorkbench:\n dependencies.runWorkbench ??\n (async (args) => {\n const stateEnvironment = {\n ...environment,\n CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(environment),\n };\n return await runWorkbench(\n {\n environment: stateEnvironment,\n pluginRoot,\n python: await resolvePluginPython({\n environment: stateEnvironment,\n }),\n signal: options.signal,\n failureMessage: \"Could not read Codex Security scan history\",\n },\n args,\n );\n }),\n });\n const { contract, scanDirectory } = await loadContractWithScanDirectory(\n scan.scanDir,\n {\n pluginRoot,\n expectedScanId: scan.scanId,\n signal: options.signal,\n },\n );\n const client = new FindingsClient(\n options.findingsUrl,\n options.signal,\n dependencies.fetch,\n );\n const scope: FindingSearchScope =\n options.allRepositories === true\n ? { allRepositories: true }\n : { repositoryId: contract.manifest.scan.target.targetId };\n const workflow =\n options.workflowId === undefined\n ? undefined\n : new FindingWorkflow(\n options.workflowId,\n environment,\n dependencies.runWorkbench === undefined\n ? undefined\n : (_options, args, input) =>\n dependencies.runWorkbench!(args, input),\n );\n if (workflow) {\n await workflow.protectArtifacts(scanDirectory);\n await workflow.bind({\n scanId: scan.scanId,\n scanDir: scanDirectory,\n artifactDigest: workflowDigest(contract),\n destination: workflowDestination(options.findingsUrl),\n scope,\n });\n await workflow.complete(\"scan\", null);\n await publishScanToCustomInternal(\n scanDirectory,\n {\n findingsUrl: options.findingsUrl,\n workflowId: options.workflowId,\n expectedScanId: scan.scanId,\n signal: options.signal,\n },\n {\n environment,\n fetch: dependencies.fetch,\n runWorkbench:\n dependencies.runWorkbench === undefined\n ? undefined\n : (_options, args, input) =>\n dependencies.runWorkbench!(args, input),\n },\n );\n }\n const dedupe = async (): Promise<DeduplicateScanResult> => {\n const saved = (await workflow?.get())?.stages.dedupe;\n if (saved?.pendingWrite) {\n await client.storeDedupeGroups(saved.pendingWrite.groups);\n return saved.result as DeduplicateScanResult;\n }\n const runner =\n dependencies.reviewRunner ??\n new CodexReviewRunner(\n environment,\n undefined,\n options.signal,\n scan[\"targetPath\"] as string,\n );\n const checkpoints = workflow\n ? new CheckpointedReviewRunner(\n workflow,\n runner,\n await workflow.sourceSnapshot(scan[\"targetPath\"] as string),\n scope,\n await reviewSettingsDigest(environment),\n )\n : undefined;\n const deduplicator = new FindingDeduplicator(\n {\n potentialDuplicates: (findingId) =>\n client.potentialDuplicates(findingId, scope),\n },\n dependencies.reviewer ??\n new CodexDeduplicationReviewer(checkpoints ?? runner),\n options.signal,\n );\n const reviewed = await deduplicator.run(\n contract.findings.findings.map((finding) => finding.findingId),\n );\n await checkpoints?.assertSourceUnchanged();\n options.signal?.throwIfAborted();\n const result: DeduplicateScanResult = { scanId: scan.scanId, ...reviewed };\n await workflow?.prepareDedupe(result, { groups: result.duplicateGroups });\n await client.storeDedupeGroups(result.duplicateGroups);\n return result;\n };\n return workflow ? await workflow.run(\"dedupe\", dedupe) : await dedupe();\n}\n"]}
@@ -0,0 +1,7 @@
1
+ /** A reviewed set of duplicate findings. A finding may belong to multiple groups. */
2
+ export interface FindingDedupeGroup {
3
+ groupId: string;
4
+ findingIds: string[];
5
+ createdAt: string;
6
+ }
7
+ //# sourceMappingURL=finding-dedupe-groups.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finding-dedupe-groups.d.ts","sourceRoot":"","sources":["../src/finding-dedupe-groups.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=finding-dedupe-groups.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finding-dedupe-groups.js","sourceRoot":"","sources":["../src/finding-dedupe-groups.ts"],"names":[],"mappings":"","sourcesContent":["/** A reviewed set of duplicate findings. A finding may belong to multiple groups. */\nexport interface FindingDedupeGroup {\n groupId: string;\n findingIds: string[];\n createdAt: string;\n}\n"]}
@@ -0,0 +1,13 @@
1
+ import type { Finding } from "./models.js";
2
+ export type FindingSearchScope = {
3
+ repositoryId: string;
4
+ allRepositories?: never;
5
+ } | {
6
+ allRepositories: true;
7
+ repositoryId?: never;
8
+ };
9
+ export interface FindingNeighborhood {
10
+ finding: Finding;
11
+ potentialDuplicates: Finding[];
12
+ }
13
+ //# sourceMappingURL=finding-retrieval.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finding-retrieval.d.ts","sourceRoot":"","sources":["../src/finding-retrieval.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAE3C,MAAM,MAAM,kBAAkB,GAC1B;IAAE,YAAY,EAAE,MAAM,CAAC;IAAC,eAAe,CAAC,EAAE,KAAK,CAAA;CAAE,GACjD;IAAE,eAAe,EAAE,IAAI,CAAC;IAAC,YAAY,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC;AAEpD,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,OAAO,CAAC;IACjB,mBAAmB,EAAE,OAAO,EAAE,CAAC;CAChC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=finding-retrieval.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finding-retrieval.js","sourceRoot":"","sources":["../src/finding-retrieval.ts"],"names":[],"mappings":"","sourcesContent":["import type { Finding } from \"./models.js\";\n\nexport type FindingSearchScope =\n | { repositoryId: string; allRepositories?: never }\n | { allRepositories: true; repositoryId?: never };\n\nexport interface FindingNeighborhood {\n finding: Finding;\n potentialDuplicates: Finding[];\n}\n"]}
@@ -0,0 +1,53 @@
1
+ import type { JsonObject } from "./config.js";
2
+ import type { FindingSearchScope } from "./finding-retrieval.js";
3
+ import { runWorkbench } from "./runtime.js";
4
+ export type WorkflowStage = "scan" | "publish" | "dedupe";
5
+ export interface WorkflowBinding {
6
+ repositoryPath?: string;
7
+ scanRequestDigest?: string;
8
+ scanId?: string;
9
+ scanDir?: string;
10
+ artifactDigest?: string;
11
+ destination?: string;
12
+ scope?: FindingSearchScope;
13
+ }
14
+ export interface WorkflowState extends WorkflowBinding {
15
+ id: string;
16
+ stages: Record<WorkflowStage, {
17
+ status: "pending" | "running" | "failed" | "completed";
18
+ result?: unknown;
19
+ error?: string;
20
+ pendingWrite?: {
21
+ groups: string[][];
22
+ };
23
+ }>;
24
+ }
25
+ export declare function workflowDigest(value: unknown): string;
26
+ export declare function workflowDestination(url: string): string;
27
+ /** State lives in the workbench database, never in sealed scan artifacts. */
28
+ export declare class FindingWorkflow {
29
+ readonly id: string;
30
+ private readonly environment;
31
+ private readonly workbench;
32
+ private readonly pythonPath?;
33
+ private options?;
34
+ constructor(id: string, environment?: NodeJS.ProcessEnv, workbench?: typeof runWorkbench, pythonPath?: string | undefined);
35
+ protectArtifacts(scanDir: string): Promise<void>;
36
+ get(): Promise<WorkflowState | null>;
37
+ bind(binding: WorkflowBinding): Promise<WorkflowState>;
38
+ begin(stage: WorkflowStage): Promise<WorkflowState>;
39
+ complete(stage: WorkflowStage, result: unknown): Promise<WorkflowState>;
40
+ fail(stage: WorkflowStage, error: unknown): Promise<void>;
41
+ run<T>(stage: WorkflowStage, operation: () => Promise<T>): Promise<T>;
42
+ registeredScan(scanId: string): Promise<JsonObject>;
43
+ sourceSnapshot(repository: string): Promise<JsonObject>;
44
+ getReview(key: string): Promise<unknown>;
45
+ saveReview(key: string, binding: object, result: unknown): Promise<void>;
46
+ prepareDedupe(result: unknown, pendingWrite: {
47
+ groups: string[][];
48
+ }): Promise<void>;
49
+ private command;
50
+ private request;
51
+ private call;
52
+ }
53
+ //# sourceMappingURL=finding-workflow.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finding-workflow.d.ts","sourceRoot":"","sources":["../src/finding-workflow.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AACjE,OAAO,EAKL,YAAY,EAEb,MAAM,cAAc,CAAC;AAEtB,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,SAAS,GAAG,QAAQ,CAAC;AAC1D,MAAM,WAAW,eAAe;IAC9B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,kBAAkB,CAAC;CAC5B;AACD,MAAM,WAAW,aAAc,SAAQ,eAAe;IACpD,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CACZ,aAAa,EACb;QACE,MAAM,EAAE,SAAS,GAAG,SAAS,GAAG,QAAQ,GAAG,WAAW,CAAC;QACvD,MAAM,CAAC,EAAE,OAAO,CAAC;QACjB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,YAAY,CAAC,EAAE;YAAE,MAAM,EAAE,MAAM,EAAE,EAAE,CAAA;SAAE,CAAC;KACvC,CACF,CAAC;CACH;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CASrD;A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,EACX,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,OAAO,GACd,OAAO,CAAC,IAAI,CAAC;IAIV,aAAa,CACjB,MAAM,EAAE,OAAO,EACf,YAAY,EAAE;QAAE,MAAM,EAAE,MAAM,EAAE,EAAE,CAAA;KAAE,GACnC,OAAO,CAAC,IAAI,CAAC;YASF,OAAO;YAKP,OAAO;YAOP,IAAI;CAkBnB"}
@@ -0,0 +1,117 @@
1
+ import { createHash } from "node:crypto";
2
+ import { isAbsolute, relative, sep } from "node:path";
3
+ import { CodexSecurityError, safeErrorMessage } from "./errors.js";
4
+ import { bundledPluginRoot, canonicalizeModelSafePath, codexSecurityStateDirectory, resolvePluginPython, runWorkbench, } from "./runtime.js";
5
+ export function workflowDigest(value) {
6
+ const json = JSON.stringify(value, (_key, item) => item !== null && typeof item === "object" && !Array.isArray(item)
7
+ ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)))
8
+ : item);
9
+ return createHash("sha256").update(json).digest("hex");
10
+ }
11
+ export function workflowDestination(url) {
12
+ const route = "v1/bulk/findings";
13
+ const destination = new URL(route, url.endsWith("/") ? url : `${url}/`);
14
+ destination.username = "";
15
+ destination.password = "";
16
+ return destination.href.slice(0, -route.length);
17
+ }
18
+ /** State lives in the workbench database, never in sealed scan artifacts. */
19
+ export class FindingWorkflow {
20
+ id;
21
+ environment;
22
+ workbench;
23
+ pythonPath;
24
+ options;
25
+ constructor(id, environment = process.env, workbench = runWorkbench, pythonPath) {
26
+ this.id = id;
27
+ this.environment = environment;
28
+ this.workbench = workbench;
29
+ this.pythonPath = pythonPath;
30
+ if (!id.trim())
31
+ throw new CodexSecurityError("workflowId must be a nonempty string.");
32
+ }
33
+ async protectArtifacts(scanDir) {
34
+ const path = relative(await canonicalizeModelSafePath(scanDir), await canonicalizeModelSafePath(codexSecurityStateDirectory(this.environment)));
35
+ if (path === "" ||
36
+ (!isAbsolute(path) && path !== ".." && !path.startsWith(`..${sep}`))) {
37
+ throw new CodexSecurityError("Workflow state must be outside the sealed scan artifacts.");
38
+ }
39
+ }
40
+ async get() {
41
+ return (await this.command({ action: "get" }));
42
+ }
43
+ async bind(binding) {
44
+ return (await this.command({ action: "bind", binding }));
45
+ }
46
+ async begin(stage) {
47
+ return (await this.command({ action: "begin", stage }));
48
+ }
49
+ async complete(stage, result) {
50
+ return (await this.command({ action: "complete", stage, result }));
51
+ }
52
+ async fail(stage, error) {
53
+ await this.command({
54
+ action: "fail",
55
+ stage,
56
+ error: safeErrorMessage(error),
57
+ }).catch(() => undefined);
58
+ }
59
+ async run(stage, operation) {
60
+ const state = await this.begin(stage);
61
+ if (state.stages[stage].status === "completed")
62
+ return state.stages[stage].result;
63
+ try {
64
+ const result = await operation();
65
+ await this.complete(stage, result);
66
+ return result;
67
+ }
68
+ catch (error) {
69
+ await this.fail(stage, error);
70
+ throw error;
71
+ }
72
+ }
73
+ async registeredScan(scanId) {
74
+ const context = await this.call(["get-scan", "--scan-id", scanId]);
75
+ return context["scan"];
76
+ }
77
+ async sourceSnapshot(repository) {
78
+ return (await this.request({ action: "source", repository }))["source"];
79
+ }
80
+ async getReview(key) {
81
+ return (await this.request({ action: "get-review", key }))["review"];
82
+ }
83
+ async saveReview(key, binding, result) {
84
+ await this.request({ action: "save-review", key, binding, result });
85
+ }
86
+ async prepareDedupe(result, pendingWrite) {
87
+ await this.command({
88
+ action: "prepare-dedupe",
89
+ stage: "dedupe",
90
+ result,
91
+ pendingWrite,
92
+ });
93
+ }
94
+ async command(payload) {
95
+ const result = await this.request(payload);
96
+ return result["workflow"];
97
+ }
98
+ async request(payload) {
99
+ return await this.call(["finding-workflow"], JSON.stringify({ id: this.id, ...payload }));
100
+ }
101
+ async call(args, input) {
102
+ this.options ??= (async () => ({
103
+ pluginRoot: await bundledPluginRoot(),
104
+ python: await resolvePluginPython({
105
+ environment: this.environment,
106
+ configuredPath: this.pythonPath,
107
+ }),
108
+ environment: {
109
+ ...this.environment,
110
+ CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(this.environment),
111
+ },
112
+ failureMessage: "Could not save or resume the findings workflow",
113
+ }))();
114
+ return await this.workbench(await this.options, args, input);
115
+ }
116
+ }
117
+ //# sourceMappingURL=finding-workflow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finding-workflow.js","sourceRoot":"","sources":["../src/finding-workflow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAEtD,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEnE,OAAO,EACL,iBAAiB,EACjB,yBAAyB,EACzB,2BAA2B,EAC3B,mBAAmB,EACnB,YAAY,GAEb,MAAM,cAAc,CAAC;AAyBtB,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,IAAa,EAAE,EAAE,CACzD,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QAC/D,CAAC,CAAC,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CACtE;QACH,CAAC,CAAC,IAAI,CACT,CAAC;IACF,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC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C,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAC3C,OAAO,MAAM,CAAC,UAAU,CAAoC,CAAC;IAC/D,CAAC;IAEO,KAAK,CAAC,OAAO,CAAC,OAAe;QACnC,OAAO,MAAM,IAAI,CAAC,IAAI,CACpB,CAAC,kBAAkB,CAAC,EACpB,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,GAAG,OAAO,EAAE,CAAC,CAC5C,CAAC;IACJ,CAAC;IAEO,KAAK,CAAC,IAAI,CAChB,IAAuB,EACvB,KAAc;QAEd,IAAI,CAAC,OAAO,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;YAC7B,UAAU,EAAE,MAAM,iBAAiB,EAAE;YACrC,MAAM,EAAE,MAAM,mBAAmB,CAAC;gBAChC,WAAW,EAAE,IAAI,CAAC,WAAW;gBAC7B,cAAc,EAAE,IAAI,CAAC,UAAU;aAChC,CAAC;YACF,WAAW,EAAE;gBACX,GAAG,IAAI,CAAC,WAAW;gBACnB,wBAAwB,EAAE,2BAA2B,CAAC,IAAI,CAAC,WAAW,CAAC;aACxE;YACD,cAAc,EAAE,gDAAgD;SACjE,CAAC,CAAC,EAAE,CAAC;QACN,OAAO,MAAM,IAAI,CAAC,SAAS,CAAC,MAAM,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;IAC/D,CAAC;CACF","sourcesContent":["import { createHash } from \"node:crypto\";\nimport { isAbsolute, relative, sep } from \"node:path\";\nimport type { JsonObject } from \"./config.js\";\nimport { CodexSecurityError, safeErrorMessage } from \"./errors.js\";\nimport type { FindingSearchScope } from \"./finding-retrieval.js\";\nimport {\n bundledPluginRoot,\n canonicalizeModelSafePath,\n codexSecurityStateDirectory,\n resolvePluginPython,\n runWorkbench,\n type WorkbenchCommandOptions,\n} from \"./runtime.js\";\n\nexport type WorkflowStage = \"scan\" | \"publish\" | \"dedupe\";\nexport interface WorkflowBinding {\n repositoryPath?: string;\n scanRequestDigest?: string;\n scanId?: string;\n scanDir?: string;\n artifactDigest?: string;\n destination?: string;\n scope?: FindingSearchScope;\n}\nexport interface WorkflowState extends WorkflowBinding {\n id: string;\n stages: Record<\n WorkflowStage,\n {\n status: \"pending\" | \"running\" | \"failed\" | \"completed\";\n result?: unknown;\n error?: string;\n pendingWrite?: { groups: string[][] };\n }\n >;\n}\n\nexport function workflowDigest(value: unknown): string {\n const json = JSON.stringify(value, (_key, item: unknown) =>\n item !== null && typeof item === \"object\" && !Array.isArray(item)\n ? Object.fromEntries(\n Object.entries(item).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)),\n )\n : item,\n );\n return createHash(\"sha256\").update(json).digest(\"hex\");\n}\n\nexport function workflowDestination(url: string): string {\n const route = \"v1/bulk/findings\";\n const destination = new URL(route, url.endsWith(\"/\") ? url : `${url}/`);\n destination.username = \"\";\n destination.password = \"\";\n return destination.href.slice(0, -route.length);\n}\n\n/** State lives in the workbench database, never in sealed scan artifacts. */\nexport class FindingWorkflow {\n private options?: Promise<WorkbenchCommandOptions>;\n\n constructor(\n readonly id: string,\n private readonly environment: NodeJS.ProcessEnv = process.env,\n private readonly workbench: typeof runWorkbench = runWorkbench,\n private readonly pythonPath?: string,\n ) {\n if (!id.trim())\n throw new CodexSecurityError(\"workflowId must be a nonempty string.\");\n }\n\n async protectArtifacts(scanDir: string): Promise<void> {\n const path = relative(\n await canonicalizeModelSafePath(scanDir),\n await canonicalizeModelSafePath(\n codexSecurityStateDirectory(this.environment),\n ),\n );\n if (\n path === \"\" ||\n (!isAbsolute(path) && path !== \"..\" && !path.startsWith(`..${sep}`))\n ) {\n throw new CodexSecurityError(\n \"Workflow state must be outside the sealed scan artifacts.\",\n );\n }\n }\n\n async get(): Promise<WorkflowState | null> {\n return (await this.command({ action: \"get\" })) as WorkflowState | null;\n }\n\n async bind(binding: WorkflowBinding): Promise<WorkflowState> {\n return (await this.command({ action: \"bind\", binding }))!;\n }\n\n async begin(stage: WorkflowStage): Promise<WorkflowState> {\n return (await this.command({ action: \"begin\", stage }))!;\n }\n\n async complete(\n stage: WorkflowStage,\n result: unknown,\n ): Promise<WorkflowState> {\n return (await this.command({ action: \"complete\", stage, result }))!;\n }\n\n async fail(stage: WorkflowStage, error: unknown): Promise<void> {\n await this.command({\n action: \"fail\",\n stage,\n error: safeErrorMessage(error),\n }).catch(() => undefined);\n }\n\n async run<T>(stage: WorkflowStage, operation: () => Promise<T>): Promise<T> {\n const state = await this.begin(stage);\n if (state.stages[stage].status === \"completed\")\n return state.stages[stage].result as T;\n try {\n const result = await operation();\n await this.complete(stage, result);\n return result;\n } catch (error) {\n await this.fail(stage, error);\n throw error;\n }\n }\n\n async registeredScan(scanId: string): Promise<JsonObject> {\n const context = await this.call([\"get-scan\", \"--scan-id\", scanId]);\n return context[\"scan\"] as JsonObject;\n }\n\n async sourceSnapshot(repository: string): Promise<JsonObject> {\n return (await this.request({ action: \"source\", repository }))[\n \"source\"\n ] as JsonObject;\n }\n\n async getReview(key: string): Promise<unknown> {\n return (await this.request({ action: \"get-review\", key }))[\"review\"];\n }\n\n async saveReview(\n key: string,\n binding: object,\n result: unknown,\n ): Promise<void> {\n await this.request({ action: \"save-review\", key, binding, result });\n }\n\n async prepareDedupe(\n result: unknown,\n pendingWrite: { groups: string[][] },\n ): Promise<void> {\n await this.command({\n action: \"prepare-dedupe\",\n stage: \"dedupe\",\n result,\n pendingWrite,\n });\n }\n\n private async command(payload: object): Promise<WorkflowState | null> {\n const result = await this.request(payload);\n return result[\"workflow\"] as unknown as WorkflowState | null;\n }\n\n private async request(payload: object): Promise<JsonObject> {\n return await this.call(\n [\"finding-workflow\"],\n JSON.stringify({ id: this.id, ...payload }),\n );\n }\n\n private async call(\n args: readonly string[],\n input?: string,\n ): Promise<JsonObject> {\n this.options ??= (async () => ({\n pluginRoot: await bundledPluginRoot(),\n python: await resolvePluginPython({\n environment: this.environment,\n configuredPath: this.pythonPath,\n }),\n environment: {\n ...this.environment,\n CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(this.environment),\n },\n failureMessage: \"Could not save or resume the findings workflow\",\n }))();\n return await this.workbench(await this.options, args, input);\n }\n}\n"]}
@@ -0,0 +1,15 @@
1
+ import type { Finding } from "./models.js";
2
+ import type { FindingNeighborhood, FindingSearchScope } from "./finding-retrieval.js";
3
+ export type FindingsRequest = (url: URL, init: RequestInit) => Promise<Response>;
4
+ export declare class FindingsClient {
5
+ private readonly url;
6
+ private readonly signal?;
7
+ private readonly request;
8
+ constructor(url: string, signal?: AbortSignal | undefined, request?: FindingsRequest);
9
+ potentialDuplicates(findingId: string, scope: FindingSearchScope): Promise<FindingNeighborhood>;
10
+ publish(findings: readonly Finding[], repositoryId: string): Promise<string[]>;
11
+ storeDedupeGroups(groups: readonly string[][]): Promise<void>;
12
+ private endpoint;
13
+ private post;
14
+ }
15
+ //# sourceMappingURL=findings-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"findings-client.d.ts","sourceRoot":"","sources":["../src/findings-client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,EACV,mBAAmB,EACnB,kBAAkB,EACnB,MAAM,wBAAwB,CAAC;AAEhC,MAAM,MAAM,eAAe,GAAG,CAC5B,GAAG,EAAE,GAAG,EACR,IAAI,EAAE,WAAW,KACd,OAAO,CAAC,QAAQ,CAAC,CAAC;AAEvB,qBAAa,cAAc;IAEvB,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAFP,GAAG,EAAE,MAAM,EACX,MAAM,CAAC,EAAE,WAAW,YAAA,EACpB,OAAO,GAAE,eAAuB;IAG7C,mBAAmB,CACvB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,kBAAkB,GACxB,OAAO,CAAC,mBAAmB,CAAC;IAoBzB,OAAO,CACX,QAAQ,EAAE,SAAS,OAAO,EAAE,EAC5B,YAAY,EAAE,MAAM,GACnB,OAAO,CAAC,MAAM,EAAE,CAAC;IAmBd,iBAAiB,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAKnE,OAAO,CAAC,QAAQ;YAIF,IAAI;CAcnB"}
@@ -0,0 +1,60 @@
1
+ import { CodexSecurityError } from "./errors.js";
2
+ export class FindingsClient {
3
+ url;
4
+ signal;
5
+ request;
6
+ constructor(url, signal, request = fetch) {
7
+ this.url = url;
8
+ this.signal = signal;
9
+ this.request = request;
10
+ }
11
+ async potentialDuplicates(findingId, scope) {
12
+ const url = this.endpoint(`v1/finding/${encodeURIComponent(findingId)}/potential-duplicates`);
13
+ if (scope.allRepositories === true)
14
+ url.searchParams.set("allRepositories", "true");
15
+ else
16
+ url.searchParams.set("repositoryId", scope.repositoryId);
17
+ const response = await this.request(url, { signal: this.signal });
18
+ if (!response.ok) {
19
+ throw new CodexSecurityError(`Potential-duplicates lookup for ${findingId} failed (HTTP ${response.status}).${response.status === 404
20
+ ? " Import the finding with its repositoryId through POST /v1/bulk/findings before deduplicating."
21
+ : ""}`);
22
+ }
23
+ return (await response.json());
24
+ }
25
+ async publish(findings, repositoryId) {
26
+ const receipt = await this.post("v1/bulk/findings", {
27
+ findings,
28
+ repositoryId,
29
+ });
30
+ const expected = new Set(findings.map((finding) => finding.findingId));
31
+ if (!Array.isArray(receipt) ||
32
+ receipt.length !== findings.length ||
33
+ new Set(receipt).size !== expected.size ||
34
+ receipt.some((id) => !expected.has(id))) {
35
+ throw new CodexSecurityError("The findings API did not acknowledge all published finding IDs. Check the service before retrying.");
36
+ }
37
+ return receipt;
38
+ }
39
+ async storeDedupeGroups(groups) {
40
+ if (groups.length === 0)
41
+ return;
42
+ await this.post("v1/dedupe-groups", { groups });
43
+ }
44
+ endpoint(path) {
45
+ return new URL(path, this.url.endsWith("/") ? this.url : `${this.url}/`);
46
+ }
47
+ async post(path, body) {
48
+ const response = await this.request(this.endpoint(path), {
49
+ method: "POST",
50
+ headers: { "Content-Type": "application/json" },
51
+ body: JSON.stringify(body),
52
+ signal: this.signal,
53
+ });
54
+ if (!response.ok) {
55
+ throw new CodexSecurityError(`Findings API POST /${path} failed (HTTP ${response.status}).`);
56
+ }
57
+ return await response.json();
58
+ }
59
+ }
60
+ //# sourceMappingURL=findings-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"findings-client.js","sourceRoot":"","sources":["../src/findings-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAYjD,MAAM,OAAO,cAAc;IAEN;IACA;IACA;IAHnB,YACmB,GAAW,EACX,MAAoB,EACpB,UAA2B,KAAK;QAFhC,QAAG,GAAH,GAAG,CAAQ;QACX,WAAM,GAAN,MAAM,CAAc;QACpB,YAAO,GAAP,OAAO,CAAyB;IAChD,CAAC;IAEJ,KAAK,CAAC,mBAAmB,CACvB,SAAiB,EACjB,KAAyB;QAEzB,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CACvB,cAAc,kBAAkB,CAAC,SAAS,CAAC,uBAAuB,CACnE,CAAC;QACF,IAAI,KAAK,CAAC,eAAe,KAAK,IAAI;YAChC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC;;YAC7C,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,cAAc,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC;QAC9D,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;QAClE,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,kBAAkB,CAC1B,mCAAmC,SAAS,iBAAiB,QAAQ,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AAG,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC;IAC3E,CAAC;IAEO,KAAK,CAAC,IAAI,CAAC,IAAY,EAAE,IAAa;QAC5C,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;YACvD,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;YAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;YAC1B,MAAM,EAAE,IAAI,CAAC,MAAM;SACpB,CAAC,CAAC;QACH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,kBAAkB,CAC1B,sBAAsB,IAAI,iBAAiB,QAAQ,CAAC,MAAM,IAAI,CAC/D,CAAC;QACJ,CAAC;QACD,OAAO,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IAC/B,CAAC;CACF","sourcesContent":["import { CodexSecurityError } from \"./errors.js\";\nimport type { Finding } from \"./models.js\";\nimport type {\n FindingNeighborhood,\n FindingSearchScope,\n} from \"./finding-retrieval.js\";\n\nexport type FindingsRequest = (\n url: URL,\n init: RequestInit,\n) => Promise<Response>;\n\nexport class FindingsClient {\n constructor(\n private readonly url: string,\n private readonly signal?: AbortSignal,\n private readonly request: FindingsRequest = fetch,\n ) {}\n\n async potentialDuplicates(\n findingId: string,\n scope: FindingSearchScope,\n ): Promise<FindingNeighborhood> {\n const url = this.endpoint(\n `v1/finding/${encodeURIComponent(findingId)}/potential-duplicates`,\n );\n if (scope.allRepositories === true)\n url.searchParams.set(\"allRepositories\", \"true\");\n else url.searchParams.set(\"repositoryId\", scope.repositoryId);\n const response = await this.request(url, { signal: this.signal });\n if (!response.ok) {\n throw new CodexSecurityError(\n `Potential-duplicates lookup for ${findingId} failed (HTTP ${response.status}).${\n response.status === 404\n ? \" Import the finding with its repositoryId through POST /v1/bulk/findings before deduplicating.\"\n : \"\"\n }`,\n );\n }\n return (await response.json()) as FindingNeighborhood;\n }\n\n async publish(\n findings: readonly Finding[],\n repositoryId: string,\n ): Promise<string[]> {\n const receipt = await this.post(\"v1/bulk/findings\", {\n findings,\n repositoryId,\n });\n const expected = new Set(findings.map((finding) => finding.findingId));\n if (\n !Array.isArray(receipt) ||\n receipt.length !== findings.length ||\n new Set(receipt).size !== expected.size ||\n receipt.some((id) => !expected.has(id))\n ) {\n throw new CodexSecurityError(\n \"The findings API did not acknowledge all published finding IDs. Check the service before retrying.\",\n );\n }\n return receipt as string[];\n }\n\n async storeDedupeGroups(groups: readonly string[][]): Promise<void> {\n if (groups.length === 0) return;\n await this.post(\"v1/dedupe-groups\", { groups });\n }\n\n private endpoint(path: string): URL {\n return new URL(path, this.url.endsWith(\"/\") ? this.url : `${this.url}/`);\n }\n\n private async post(path: string, body: unknown): Promise<unknown> {\n const response = await this.request(this.endpoint(path), {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify(body),\n signal: this.signal,\n });\n if (!response.ok) {\n throw new CodexSecurityError(\n `Findings API POST /${path} failed (HTTP ${response.status}).`,\n );\n }\n return await response.json();\n }\n}\n"]}