@openai/codex-security 0.1.19 → 0.1.20

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 (42) hide show
  1. package/README.md +52 -7
  2. package/_bundled_plugin/.codex-plugin/plugin.json +1 -1
  3. package/_bundled_plugin/scripts/workbench_cli.py +5 -1
  4. package/_bundled_plugin/scripts/workbench_db.py +53 -0
  5. package/dist/bulk-scan-discovery.d.ts +8 -0
  6. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  7. package/dist/bulk-scan-discovery.js +15 -1
  8. package/dist/bulk-scan-discovery.js.map +1 -1
  9. package/dist/cli.d.ts +3 -2
  10. package/dist/cli.d.ts.map +1 -1
  11. package/dist/cli.js +166 -82
  12. package/dist/cli.js.map +1 -1
  13. package/dist/cloud-publish.js +16 -12
  14. package/dist/cloud-publish.js.map +1 -1
  15. package/dist/index.d.ts +2 -2
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +1 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/linear.d.ts +5 -0
  20. package/dist/linear.d.ts.map +1 -1
  21. package/dist/linear.js +8 -3
  22. package/dist/linear.js.map +1 -1
  23. package/dist/publication-events.d.ts +40 -13
  24. package/dist/publication-events.d.ts.map +1 -1
  25. package/dist/publication-events.js +218 -104
  26. package/dist/publication-events.js.map +1 -1
  27. package/dist/publication-store.d.ts +1 -0
  28. package/dist/publication-store.d.ts.map +1 -1
  29. package/dist/publication-store.js +73 -26
  30. package/dist/publication-store.js.map +1 -1
  31. package/dist/publication.d.ts +5 -0
  32. package/dist/publication.d.ts.map +1 -1
  33. package/dist/publication.js +12 -0
  34. package/dist/publication.js.map +1 -1
  35. package/dist/publish.d.ts +40 -1
  36. package/dist/publish.d.ts.map +1 -1
  37. package/dist/publish.js +611 -251
  38. package/dist/publish.js.map +1 -1
  39. package/dist/version.d.ts +1 -1
  40. package/dist/version.js +1 -1
  41. package/dist/version.js.map +1 -1
  42. package/package.json +2 -2
package/dist/cli.js CHANGED
@@ -22,7 +22,7 @@ import { importLinearIssues, resolveLinearApiKey, } from "./linear.js";
22
22
  import { runMultiscan } from "./multiscan.js";
23
23
  import { componentPlanSchema } from "./component-plan.js";
24
24
  import { runComponentScans } from "./component-scan.js";
25
- import { publishScan, } from "./publish.js";
25
+ import { checkScanPublication, publishScan, } from "./publish.js";
26
26
  import { bundledPluginRoot, canonicalizeModelSafePath, codexSecurityCredentialHome, codexSecurityStateDirectory, expandHome, prepareCodexSecurityCredentialHome, pythonUtf8Environment, resolveCodexCommand, resolvePluginPython, runWorkbench, setCodexSecurityCredentialLogout, } from "./runtime.js";
27
27
  import { matchScanFindingsInternal, } from "./scan-comparison.js";
28
28
  import { scanActivitiesFromEvent } from "./scan-activity.js";
@@ -142,6 +142,58 @@ function linearApiKeyOption() {
142
142
  .optional()
143
143
  .describe("Linear personal API key; defaults to CODEX_SECURITY_LINEAR_API_KEY.");
144
144
  }
145
+ const PUBLICATION_DESTINATION_OPTIONS = z.object({
146
+ to: z.literal("linear").describe("Publication destination."),
147
+ linearTeam: optionValue("--linear-team")
148
+ .optional()
149
+ .describe("Linear team ID; defaults to CODEX_SECURITY_LINEAR_TEAM."),
150
+ linearApiKey: linearApiKeyOption(),
151
+ linearProject: optionValue("--linear-project")
152
+ .optional()
153
+ .describe("Optional Linear project ID; defaults to CODEX_SECURITY_LINEAR_PROJECT."),
154
+ project: optionValue("--project")
155
+ .optional()
156
+ .describe("Alias for --linear-project.")
157
+ .meta({ deprecated: true }),
158
+ linearAssignee: optionValue("--linear-assignee")
159
+ .optional()
160
+ .describe("Linear assignee email or user ID; omit to leave issues unassigned."),
161
+ });
162
+ function publicationDestination(options, environment) {
163
+ const linearApiKey = resolveLinearApiKey(environment, options.linearApiKey);
164
+ const assigneeId = options.linearAssignee?.trim();
165
+ if (options.linearAssignee !== undefined && !assigneeId) {
166
+ throw new CodexSecurityError("--linear-assignee must not be empty.");
167
+ }
168
+ if (assigneeId !== undefined && linearApiKey === undefined) {
169
+ throw new CodexSecurityError("--linear-assignee requires --linear-api-key or CODEX_SECURITY_LINEAR_API_KEY.");
170
+ }
171
+ const teamId = options.linearTeam?.trim() ||
172
+ environment["CODEX_SECURITY_LINEAR_TEAM"]?.trim();
173
+ if (!teamId) {
174
+ throw new CodexSecurityError("--linear-team or CODEX_SECURITY_LINEAR_TEAM is required.");
175
+ }
176
+ if (options.linearProject !== undefined &&
177
+ options.project !== undefined &&
178
+ options.linearProject.trim() !== options.project.trim()) {
179
+ throw new CodexSecurityError("--linear-project and --project must select the same project.");
180
+ }
181
+ const projectOption = options.linearProject ?? options.project;
182
+ const selectedProject = projectOption?.trim();
183
+ if (projectOption !== undefined && !selectedProject) {
184
+ throw new CodexSecurityError(`${options.linearProject === undefined ? "--project" : "--linear-project"} must not be empty.`);
185
+ }
186
+ const projectId = selectedProject ||
187
+ environment["CODEX_SECURITY_LINEAR_PROJECT"]?.trim() ||
188
+ undefined;
189
+ return {
190
+ destination: options.to,
191
+ teamId,
192
+ ...(projectId === undefined ? {} : { projectId }),
193
+ ...(linearApiKey === undefined ? {} : { linearApiKey }),
194
+ ...(assigneeId === undefined ? {} : { assigneeId }),
195
+ };
196
+ }
145
197
  function publicationScanAge(timestamp, now) {
146
198
  const completedAt = Date.parse(timestamp);
147
199
  if (!Number.isFinite(completedAt))
@@ -229,6 +281,10 @@ function renderPublicationSummary(result, color) {
229
281
  if (created > 0)
230
282
  lines.push("");
231
283
  lines.push(`${created} total issue${created === 1 ? "" : "s"} created`, `${failed} total issue${failed === 1 ? "" : "s"} failed`);
284
+ if (result.skipped !== undefined) {
285
+ const skipped = result.skipped.length;
286
+ lines.push(`${skipped} previously recorded issue${skipped === 1 ? "" : "s"} skipped`);
287
+ }
232
288
  return `${lines.join("\n")}\n`;
233
289
  }
234
290
  class PublicationProgressPresenter {
@@ -325,6 +381,14 @@ class PublicationProgressPresenter {
325
381
  }
326
382
  return;
327
383
  }
384
+ if (event.type === "handoff_recorded") {
385
+ const message = `[${event.recorded}/${event.total}] Saved Linear publication evidence.`;
386
+ if (this.#dashboard === null)
387
+ this.#write(message, true);
388
+ else
389
+ this.#dashboard.setStage(message);
390
+ return;
391
+ }
328
392
  if (event.type === "issue_completed") {
329
393
  const detail = event.error === undefined
330
394
  ? `Created ${event.issueIdentifier ?? event.findingId}`
@@ -1318,6 +1382,20 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1318
1382
  return presentHistory(await matchScanPair(beforeId, afterId), "compare", format);
1319
1383
  },
1320
1384
  });
1385
+ const reportPublicationError = (error, signal) => {
1386
+ if (signal === "SIGINT" || signal === "SIGTERM") {
1387
+ const reason = signal === "SIGINT"
1388
+ ? "Publication canceled by Ctrl-C."
1389
+ : "Publication terminated by SIGTERM.";
1390
+ const recovery = error === signal ? "" : ` ${diagnosticValue(safeErrorMessage(error))}`;
1391
+ errorOutput.write(`codex-security: ${reason}${recovery}\n`);
1392
+ exitCode = signal === "SIGINT" ? 130 : 143;
1393
+ }
1394
+ else {
1395
+ errorOutput.write(`codex-security: ${errorMessage(error)}\n`);
1396
+ exitCode = 2;
1397
+ }
1398
+ };
1321
1399
  const publication = Cli.create("publish", {
1322
1400
  description: "Publish completed Codex Security scan findings.",
1323
1401
  }).command("scan", {
@@ -1330,7 +1408,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1330
1408
  .optional()
1331
1409
  .describe("Completed scan directory; omit to select a saved scan."),
1332
1410
  }),
1333
- options: z.object({
1411
+ options: PUBLICATION_DESTINATION_OPTIONS.extend({
1334
1412
  scan: z
1335
1413
  .array(optionValue("--scan"))
1336
1414
  .default([])
@@ -1346,32 +1424,40 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1346
1424
  message: "Unsupported publication destination. Use --to linear.",
1347
1425
  })
1348
1426
  .describe("Publication destination (linear)."),
1349
- linearTeam: optionValue("--linear-team")
1350
- .optional()
1351
- .describe("Linear team ID; defaults to CODEX_SECURITY_LINEAR_TEAM."),
1352
- linearApiKey: linearApiKeyOption(),
1353
- linearProject: optionValue("--linear-project")
1354
- .optional()
1355
- .describe("Optional Linear project ID; defaults to CODEX_SECURITY_LINEAR_PROJECT."),
1356
- project: optionValue("--project")
1357
- .optional()
1358
- .describe("Alias for --linear-project.")
1359
- .meta({ deprecated: true }),
1360
- linearAssignee: optionValue("--linear-assignee")
1361
- .optional()
1362
- .describe("Linear assignee email or user ID; omit to leave issues unassigned."),
1363
1427
  dryRun: z
1364
1428
  .boolean()
1365
1429
  .default(false)
1366
1430
  .describe("Preview the findings without creating Linear issues."),
1431
+ skipExisting: z
1432
+ .boolean()
1433
+ .default(false)
1434
+ .describe("Skip findings already recorded for this exact Linear destination."),
1367
1435
  }),
1368
1436
  output: z.record(z.string(), z.unknown()).optional(),
1369
1437
  async run({ args, format, formatExplicit, options }) {
1370
1438
  const controller = new AbortController();
1371
1439
  let presentation;
1440
+ let directApiPublication = false;
1441
+ let firstSignalAt = 0;
1442
+ let observingSignals = false;
1372
1443
  let cloudBatch;
1373
1444
  const cancel = (signal) => {
1374
1445
  presentation?.stop();
1446
+ if (controller.signal.aborted) {
1447
+ if (!directApiPublication ||
1448
+ (controller.signal.reason === signal &&
1449
+ dependencies.now() - firstSignalAt < 500)) {
1450
+ return;
1451
+ }
1452
+ try {
1453
+ dependencies.writeSynchronously(errorOutput, "codex-security: Publication force-stopped; reconcile retained Linear publication evidence before retrying.\n");
1454
+ }
1455
+ catch { }
1456
+ removeSignalListeners();
1457
+ dependencies.forceExit(signal);
1458
+ return;
1459
+ }
1460
+ firstSignalAt = dependencies.now();
1375
1461
  controller.abort(signal);
1376
1462
  };
1377
1463
  const finishCancellation = (error) => {
@@ -1390,7 +1476,13 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1390
1476
  };
1391
1477
  const onInterrupt = () => cancel("SIGINT");
1392
1478
  const onTerminate = () => cancel("SIGTERM");
1393
- let observingSignals = false;
1479
+ const removeSignalListeners = () => {
1480
+ if (!observingSignals)
1481
+ return;
1482
+ dependencies.removeSignalListener("SIGINT", onInterrupt);
1483
+ dependencies.removeSignalListener("SIGTERM", onTerminate);
1484
+ observingSignals = false;
1485
+ };
1394
1486
  try {
1395
1487
  const currentDirectory = dependencies.currentDirectory();
1396
1488
  const directories = [
@@ -1409,44 +1501,21 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1409
1501
  throw new CodexSecurityError("Multiple scans are only supported with --to cloud.");
1410
1502
  }
1411
1503
  if (options.to === "cloud" &&
1412
- [
1413
- options.linearTeam,
1414
- options.linearApiKey,
1415
- options.linearProject,
1416
- options.project,
1417
- options.linearAssignee,
1418
- ].some((value) => value !== undefined)) {
1504
+ (options.skipExisting ||
1505
+ [
1506
+ options.linearTeam,
1507
+ options.linearApiKey,
1508
+ options.linearProject,
1509
+ options.project,
1510
+ options.linearAssignee,
1511
+ ].some((value) => value !== undefined))) {
1419
1512
  throw new CodexSecurityError("Cloud publication cannot be combined with Linear options.");
1420
1513
  }
1421
- const linearApiKey = options.to === "linear"
1422
- ? resolveLinearApiKey(dependencies.environment, options.linearApiKey)
1514
+ const destination = options.to === "linear"
1515
+ ? publicationDestination({ ...options, to: "linear" }, dependencies.environment)
1423
1516
  : undefined;
1424
- const assigneeId = options.linearAssignee?.trim();
1425
- if (options.linearAssignee !== undefined && !assigneeId) {
1426
- throw new CodexSecurityError("--linear-assignee must not be empty.");
1427
- }
1428
- if (assigneeId !== undefined && linearApiKey === undefined) {
1429
- throw new CodexSecurityError("--linear-assignee requires --linear-api-key or CODEX_SECURITY_LINEAR_API_KEY.");
1430
- }
1431
- const teamId = options.linearTeam?.trim() ||
1432
- dependencies.environment["CODEX_SECURITY_LINEAR_TEAM"]?.trim() ||
1433
- "";
1434
- if (options.to === "linear" && !teamId) {
1435
- throw new CodexSecurityError("--linear-team or CODEX_SECURITY_LINEAR_TEAM is required.");
1436
- }
1437
- if (options.linearProject !== undefined &&
1438
- options.project !== undefined &&
1439
- options.linearProject.trim() !== options.project.trim()) {
1440
- throw new CodexSecurityError("--linear-project and --project must select the same project.");
1441
- }
1442
- const projectOption = options.linearProject ?? options.project;
1443
- const selectedProject = projectOption?.trim();
1444
- if (projectOption !== undefined && !selectedProject) {
1445
- throw new CodexSecurityError(`${options.linearProject === undefined ? "--project" : "--linear-project"} must not be empty.`);
1446
- }
1447
- const projectId = selectedProject ||
1448
- dependencies.environment["CODEX_SECURITY_LINEAR_PROJECT"]?.trim() ||
1449
- undefined;
1517
+ directApiPublication =
1518
+ !options.dryRun && destination?.linearApiKey !== undefined;
1450
1519
  if (options.to === "cloud") {
1451
1520
  dependencies.addSignalListener("SIGINT", onInterrupt);
1452
1521
  dependencies.addSignalListener("SIGTERM", onTerminate);
@@ -1586,24 +1655,13 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1586
1655
  };
1587
1656
  });
1588
1657
  if (options.to === "cloud") {
1589
- let remaining = choices;
1590
- while (true) {
1591
- controller.signal.throwIfAborted();
1592
- const selected = await prompt.select("Which completed scans would you like to publish?", selectedScans.length === 0
1593
- ? remaining
1594
- : [
1595
- {
1596
- label: `Done (${selectedScans.length} selected)`,
1597
- value: "",
1598
- },
1599
- ...remaining,
1600
- ], { header }, controller.signal);
1601
- controller.signal.throwIfAborted();
1602
- if (selected === "")
1603
- break;
1604
- selectedScans.push(scansById.get(selected));
1605
- remaining = remaining.filter(({ value }) => value !== selected);
1658
+ if (prompt.checkbox === undefined) {
1659
+ throw new CodexSecurityError("Interactive scan selection is unavailable.");
1606
1660
  }
1661
+ controller.signal.throwIfAborted();
1662
+ const selected = await prompt.checkbox("Which completed scans would you like to publish?", choices, { header, required: true }, controller.signal);
1663
+ controller.signal.throwIfAborted();
1664
+ selectedScans.push(...selected.map((scanId) => scansById.get(scanId)));
1607
1665
  scanDir = selectedScans[0].scanDir;
1608
1666
  }
1609
1667
  else {
@@ -1681,28 +1739,25 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1681
1739
  }
1682
1740
  const progress = new PublicationProgressPresenter(errorOutput, dependencies, publicationRepository);
1683
1741
  presentation = progress;
1742
+ dependencies.addSignalListener("SIGINT", onInterrupt);
1743
+ dependencies.addSignalListener("SIGTERM", onTerminate);
1744
+ observingSignals = true;
1684
1745
  if (!options.dryRun) {
1685
- dependencies.addSignalListener("SIGINT", onInterrupt);
1686
- dependencies.addSignalListener("SIGTERM", onTerminate);
1687
- observingSignals = true;
1688
1746
  progress.start();
1689
1747
  }
1690
1748
  let result;
1691
1749
  try {
1692
1750
  result = await (dependencies.publishScan ?? publishScan)(resolveCliPath(currentDirectory, scanDir), {
1693
- destination: "linear",
1751
+ ...destination,
1694
1752
  ...(selectedScans[0]?.scanId === undefined
1695
1753
  ? {}
1696
1754
  : { expectedScanId: selectedScans[0].scanId }),
1697
- teamId,
1698
- ...(projectId === undefined ? {} : { projectId }),
1699
1755
  dryRun: options.dryRun,
1700
- ...(linearApiKey === undefined ? {} : { linearApiKey }),
1701
- ...(assigneeId === undefined ? {} : { assigneeId }),
1756
+ signal: controller.signal,
1757
+ ...(options.skipExisting ? { skipExisting: true } : {}),
1702
1758
  ...(options.dryRun
1703
1759
  ? {}
1704
1760
  : {
1705
- signal: controller.signal,
1706
1761
  onProgress: (event) => progress.observe(event),
1707
1762
  }),
1708
1763
  });
@@ -1738,10 +1793,39 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1738
1793
  return cloudBatch;
1739
1794
  }
1740
1795
  finally {
1741
- if (observingSignals) {
1742
- dependencies.removeSignalListener("SIGINT", onInterrupt);
1743
- dependencies.removeSignalListener("SIGTERM", onTerminate);
1744
- }
1796
+ removeSignalListeners();
1797
+ }
1798
+ },
1799
+ });
1800
+ publication.command("check", {
1801
+ description: "Check saved scan history and Linear access without creating issues.",
1802
+ mcp: false,
1803
+ args: z.object({
1804
+ scanDir: z.string().describe("Completed scan directory."),
1805
+ }),
1806
+ options: PUBLICATION_DESTINATION_OPTIONS,
1807
+ output: z.record(z.string(), z.unknown()).optional(),
1808
+ async run({ args, options }) {
1809
+ const controller = new AbortController();
1810
+ const onInterrupt = () => controller.abort("SIGINT");
1811
+ const onTerminate = () => controller.abort("SIGTERM");
1812
+ dependencies.addSignalListener("SIGINT", onInterrupt);
1813
+ dependencies.addSignalListener("SIGTERM", onTerminate);
1814
+ try {
1815
+ const result = await (dependencies.checkScanPublication ?? checkScanPublication)(resolve(dependencies.currentDirectory(), args.scanDir), {
1816
+ ...publicationDestination(options, dependencies.environment),
1817
+ signal: controller.signal,
1818
+ });
1819
+ controller.signal.throwIfAborted();
1820
+ return { ...result };
1821
+ }
1822
+ catch (error) {
1823
+ reportPublicationError(error, controller.signal.reason);
1824
+ return undefined;
1825
+ }
1826
+ finally {
1827
+ dependencies.removeSignalListener("SIGINT", onInterrupt);
1828
+ dependencies.removeSignalListener("SIGTERM", onTerminate);
1745
1829
  }
1746
1830
  },
1747
1831
  });