@openai/codex-security 0.1.8 → 0.1.10

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 (77) hide show
  1. package/README.md +30 -14
  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/preflight/capability-profiles.toml +1 -1
  6. package/_bundled_plugin/references/scan-contract.md +1 -1
  7. package/_bundled_plugin/scripts/deep_scan_workbench.py +22 -0
  8. package/_bundled_plugin/scripts/generate_in_scope_files.py +102 -1
  9. package/_bundled_plugin/scripts/generate_rank_input.py +28 -2
  10. package/_bundled_plugin/scripts/normalize_candidates.py +20 -2
  11. package/_bundled_plugin/scripts/workbench_cli.py +4 -0
  12. package/_bundled_plugin/scripts/workbench_constants.py +0 -1
  13. package/_bundled_plugin/scripts/workbench_db.py +12 -8
  14. package/_bundled_plugin/scripts/workbench_native_indexes.py +100 -43
  15. package/_bundled_plugin/scripts/workbench_scan_history.py +8 -10
  16. package/_bundled_plugin/skills/finding-discovery/SKILL.md +8 -4
  17. package/_bundled_plugin/skills/security-diff-scan/SKILL.md +21 -140
  18. package/dist/api.d.ts +43 -4
  19. package/dist/api.d.ts.map +1 -1
  20. package/dist/api.js +164 -117
  21. package/dist/api.js.map +1 -1
  22. package/dist/auth.d.ts.map +1 -1
  23. package/dist/auth.js +18 -71
  24. package/dist/auth.js.map +1 -1
  25. package/dist/cli.d.ts +2 -2
  26. package/dist/cli.d.ts.map +1 -1
  27. package/dist/cli.js +112 -178
  28. package/dist/cli.js.map +1 -1
  29. package/dist/contract.d.ts.map +1 -1
  30. package/dist/contract.js +7 -228
  31. package/dist/contract.js.map +1 -1
  32. package/dist/cost.d.ts +1 -0
  33. package/dist/cost.d.ts.map +1 -1
  34. package/dist/cost.js +1 -5
  35. package/dist/cost.js.map +1 -1
  36. package/dist/errors.d.ts +4 -2
  37. package/dist/errors.d.ts.map +1 -1
  38. package/dist/errors.js +10 -46
  39. package/dist/errors.js.map +1 -1
  40. package/dist/index.d.ts +2 -2
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +1 -1
  43. package/dist/index.js.map +1 -1
  44. package/dist/knowledge-base.js +4 -4
  45. package/dist/knowledge-base.js.map +1 -1
  46. package/dist/multiscan.d.ts.map +1 -1
  47. package/dist/multiscan.js +33 -7
  48. package/dist/multiscan.js.map +1 -1
  49. package/dist/result.d.ts +13 -1
  50. package/dist/result.d.ts.map +1 -1
  51. package/dist/result.js +3 -0
  52. package/dist/result.js.map +1 -1
  53. package/dist/runtime.d.ts.map +1 -1
  54. package/dist/runtime.js +17 -153
  55. package/dist/runtime.js.map +1 -1
  56. package/dist/scan-comparison.d.ts +14 -0
  57. package/dist/scan-comparison.d.ts.map +1 -1
  58. package/dist/scan-comparison.js +89 -0
  59. package/dist/scan-comparison.js.map +1 -1
  60. package/dist/scan-dashboard.d.ts.map +1 -1
  61. package/dist/scan-dashboard.js +1 -0
  62. package/dist/scan-dashboard.js.map +1 -1
  63. package/dist/scan-history-renderer.d.ts +1 -1
  64. package/dist/scan-history-renderer.d.ts.map +1 -1
  65. package/dist/scan-history-renderer.js +10 -0
  66. package/dist/scan-history-renderer.js.map +1 -1
  67. package/dist/scan-logs.d.ts +17 -0
  68. package/dist/scan-logs.d.ts.map +1 -0
  69. package/dist/scan-logs.js +90 -0
  70. package/dist/scan-logs.js.map +1 -0
  71. package/dist/version.d.ts +1 -1
  72. package/dist/version.js +1 -1
  73. package/dist/version.js.map +1 -1
  74. package/dist/worker-progress.d.ts.map +1 -1
  75. package/dist/worker-progress.js +8 -29
  76. package/dist/worker-progress.js.map +1 -1
  77. package/package.json +3 -3
package/dist/api.js CHANGED
@@ -9,13 +9,14 @@ import { accountStatus, CodexLoginHandle, loginApiKey as persistApiKey, logout a
9
9
  import { EXTERNAL_CODEX_PROVIDERS, isExternalModelProvider, mergedCodexConfig, scanModelConfiguration, scanModelProvider, writeCodexConfig, } from "./config.js";
10
10
  import { estimateScanCost, ScanCostTracker } from "./cost.js";
11
11
  import { loadContract, requireScanFile, } from "./contract.js";
12
- import { AuthenticationRequiredError, CodexSecurityError, IncompleteScanError, OutputDirectoryError, OutputInsideProtectedRootError, redactedErrorMessage, ScanCostLimitExceededError, ScanInterruptedError, } from "./errors.js";
12
+ import { AuthenticationRequiredError, CodexSecurityError, IncompleteScanError, OutputDirectoryError, OutputInsideProtectedRootError, errorMessage, safeErrorMessage, ScanCostLimitExceededError, ScanInterruptedError, } from "./errors.js";
13
13
  import { prepareKnowledgeBase, } from "./knowledge-base.js";
14
- import { ScanResult } from "./result.js";
14
+ import { ScanResult, } from "./result.js";
15
15
  import { scanActivitiesFromEvent } from "./scan-activity.js";
16
+ import { matchCompletedScan, } from "./scan-comparison.js";
16
17
  import { scanProgressUpdatesFromEvent, workerStatusFromEvent, } from "./worker-progress.js";
17
18
  import { CODEX_EXECUTABLE_VERSION, CODEX_SDK_VERSION } from "./version.js";
18
- import { acquireCodexSecurityCredentialHomeLock, bootstrapPlugin, cleanupSdkDirectory, codexSecurityCredentialAllowsAmbientImport, codexSecurityHasStoredFileCredentials, codexSecurityStateDirectory, createIsolatedHome, importAmbientAuth, prepareCodexSecurityCredentialHome, preserveCodexSecurityPluginRegistration, pluginExecutionEnvironment, planOutputArchive, prepareOutputDir, preparePersistentScanRoot, requireModelSafeOutputDir, resolveCodexCommand, resolvePluginPath, resolvePluginPython, runWorkbench, setCodexSecurityCredentialLogout, validateOutputDir, } from "./runtime.js";
19
+ import { acquireCodexSecurityCredentialHomeLock, bootstrapPlugin, cleanupSdkDirectory, codexSecurityCredentialAllowsAmbientImport, codexSecurityCredentialHome, codexSecurityHasStoredFileCredentials, codexSecurityStateDirectory, createIsolatedHome, importAmbientAuth, prepareCodexSecurityCredentialHome, preserveCodexSecurityPluginRegistration, pluginExecutionEnvironment, planOutputArchive, prepareOutputDir, preparePersistentScanRoot, requireModelSafeOutputDir, resolveCodexCommand, resolvePluginPath, resolvePluginPython, runWorkbench, setCodexSecurityCredentialLogout, validateOutputDir, } from "./runtime.js";
19
20
  import { enclosingGitWorktreeRoot, normalizeRepository, normalizeTarget, repositoryRevision, resolveRepositoryPath, validatedGitEnvironment, validateMode, } from "./targets.js";
20
21
  const DEFAULT_DEPENDENCIES = {
21
22
  createCodex: (options) => new Codex(options),
@@ -39,6 +40,7 @@ export class CodexSecurity {
39
40
  executableVersion: CODEX_EXECUTABLE_VERSION,
40
41
  };
41
42
  #dependencies;
43
+ #surface;
42
44
  #loginHandles = new Set();
43
45
  #abortController = new AbortController();
44
46
  #activeOperation = null;
@@ -47,9 +49,10 @@ export class CodexSecurity {
47
49
  #runtimeCredentialSource = null;
48
50
  #closed = false;
49
51
  #closePromise = null;
50
- constructor(config = {}, dependencies = DEFAULT_DEPENDENCIES) {
52
+ constructor(config = {}, dependencies = DEFAULT_DEPENDENCIES, runtimeOptions = { surface: "sdk" }) {
51
53
  this.config = structuredClone(config);
52
54
  this.#dependencies = dependencies;
55
+ this.#surface = runtimeOptions.surface;
53
56
  }
54
57
  async run(repository, options = {}) {
55
58
  return await this.#trackOperation(() => this.#run(repository, options));
@@ -105,6 +108,7 @@ export class CodexSecurity {
105
108
  let scanFailure = false;
106
109
  let completionCost = null;
107
110
  let preparedTargetWarnings = [];
111
+ let runPostScan = null;
108
112
  let activeScan = null;
109
113
  const workbench = this.#dependencies.runWorkbench ?? runWorkbench;
110
114
  try {
@@ -144,21 +148,19 @@ export class CodexSecurity {
144
148
  throw new AuthenticationRequiredError(`Set ${externalProvider.env_key} to run a scan through ${externalProvider.name}.`);
145
149
  }
146
150
  const scanEnvironment = selectedScanEnvironment(this.#dependencies.environment, options.auth, modelProvider);
147
- if (authentication.method === "stored_credentials" &&
148
- this.#dependencies.prepareRuntime === undefined) {
151
+ if (this.#dependencies.prepareRuntime === undefined) {
149
152
  const credentialHome = await prepareCodexSecurityCredentialHome(scanEnvironment, (path) => requireOutputOutsideRepository(protectedRoot, path, "runtime"));
150
153
  releaseCredentialHome = await acquireCodexSecurityCredentialHomeLock(credentialHome, signal);
151
154
  }
152
155
  const previousRuntime = this.#runtime;
153
156
  const runtime = await this.#ensureRuntime(signal, temporaryRoot, (path) => requireOutputOutsideRepository(protectedRoot, path, "runtime"), options.auth, modelProvider);
154
157
  if (runtime === previousRuntime &&
155
- runtime.persistentCredentialHome === true &&
156
158
  this.#dependencies.prepareRuntime === undefined) {
157
159
  await this.#refreshPersistentRuntime(runtime, scanEnvironment, signal);
158
160
  }
159
161
  const effectiveConfig = runtime.effectiveConfig ?? requestedConfig;
160
162
  if (runtime.configPath !== undefined) {
161
- await writeCodexConfig(runtime.configPath, scanPreflightCodexConfig(effectiveConfig, repo));
163
+ await writeCodexConfig(runtime.configPath, scanPreflightCodexConfig(effectiveConfig));
162
164
  }
163
165
  const runtimeHome = await realpath(runtime.codexHome);
164
166
  requireOutputOutsideRepository(protectedRoot, runtimeHome, "runtime");
@@ -198,6 +200,10 @@ export class CodexSecurity {
198
200
  "OPENAI_API_KEY or CODEX_API_KEY for CI.");
199
201
  }
200
202
  authentication = await runtimeScanAuthentication(this.#dependencies.environment, runtime.codexHome, options.auth, modelProvider);
203
+ if (authentication.method !== "stored_credentials") {
204
+ await releaseCredentialHome?.();
205
+ releaseCredentialHome = null;
206
+ }
201
207
  notifyObserver("onAuthentication", options.onAuthentication, options.onObserverError, authentication);
202
208
  const python = await (this.#dependencies.resolvePluginPython ?? resolvePluginPython)({
203
209
  configuredPath: this.config.pythonPath,
@@ -252,26 +258,35 @@ export class CodexSecurity {
252
258
  reviewedFileCount = progress.filesCompleted;
253
259
  notifyObserver("onProgress", options.onProgress, options.onObserverError, { ...progress, filesTotal: scopeFileCount });
254
260
  };
261
+ const reportTrackingError = (error) => {
262
+ if (options.maxCostUsd !== undefined) {
263
+ costAbortController.abort(error);
264
+ return;
265
+ }
266
+ notifyObserver("onWarning", options.onWarning, options.onObserverError, `Could not track scan activity: ${errorMessage(error)}`);
267
+ };
255
268
  const tracker = new ScanCostTracker({
256
269
  codexHome: runtime.codexHome,
257
270
  model,
258
271
  repository: repo,
259
272
  maxCostUsd: options.maxCostUsd,
260
- onActivity: (activity) => {
261
- notifyObserver("onActivity", options.onActivity, options.onObserverError, activity);
262
- },
263
- onProgress: reportProgress,
264
- onCost: (cost) => {
265
- notifyObserver("onCost", options.onCost, options.onObserverError, cost);
266
- if (options.maxCostUsd !== undefined &&
267
- cost.estimatedUsd > options.maxCostUsd) {
268
- costAbortController.abort(new ScanCostLimitExceededError(options.maxCostUsd, cost, scanDir));
269
- }
270
- },
271
- onError: (error) => costAbortController.abort(error),
273
+ onActivity: options.onActivity === undefined
274
+ ? undefined
275
+ : (activity) => notifyObserver("onActivity", options.onActivity, options.onObserverError, activity),
276
+ onProgress: options.onProgress === undefined ? undefined : reportProgress,
277
+ onCost: options.onCost === undefined && options.maxCostUsd === undefined
278
+ ? undefined
279
+ : (cost) => {
280
+ notifyObserver("onCost", options.onCost, options.onObserverError, cost);
281
+ if (options.maxCostUsd !== undefined &&
282
+ cost.estimatedUsd > options.maxCostUsd) {
283
+ costAbortController.abort(new ScanCostLimitExceededError(options.maxCostUsd, cost, scanDir));
284
+ }
285
+ },
286
+ onError: reportTrackingError,
272
287
  });
273
288
  costTracker = tracker;
274
- const recipe = scanRecipe(repo, protectedRoot, normalized, mode, expectation.repositoryRevision, runtime.plugin.version, effectiveConfig, options.failureSeverity, knowledgeBase?.sources, options.maxCostUsd, deepScanOptions(options));
289
+ const recipe = scanRecipe(repo, normalized, mode, expectation.repositoryRevision, runtime.plugin.version, effectiveConfig, options.failureSeverity, knowledgeBase?.sources, options.maxCostUsd, deepScanOptions(options));
275
290
  const workbenchOptions = {
276
291
  python,
277
292
  pluginRoot: runtime.plugin.pluginRoot,
@@ -422,12 +437,18 @@ export class CodexSecurity {
422
437
  CODEX_HOME: runtime.codexHome,
423
438
  ...runtimePaths,
424
439
  };
440
+ const sdkCodexConfig = scanPreflightCodexConfig(effectiveConfig);
441
+ delete sdkCodexConfig["projects"];
425
442
  const codex = this.#dependencies.createCodex({
426
443
  ...(externalProvider !== null || apiKey === null ? {} : { apiKey }),
427
444
  env: definedEnvironment(selectedScanEnvironment(environment, "chatgpt")),
428
445
  config: {
446
+ ...sdkCodexConfig,
429
447
  default_permissions: SCAN_PERMISSION_PROFILE,
430
448
  allow_login_shell: false,
449
+ responses_api_metadata: {
450
+ codex_security_surface: this.#surface,
451
+ },
431
452
  },
432
453
  });
433
454
  const thread = codex.startThread({
@@ -451,6 +472,10 @@ export class CodexSecurity {
451
472
  await chmod(targetPathsFile, 0o400);
452
473
  }
453
474
  checkOpen();
475
+ const postScanPrompt = options.postScanPrompt;
476
+ if (postScanPrompt?.trim()) {
477
+ runPostScan = () => thread.runStreamed(postScanPrompt, { signal });
478
+ }
454
479
  const { events } = await thread.runStreamed(prompt, {
455
480
  signal,
456
481
  });
@@ -465,9 +490,28 @@ export class CodexSecurity {
465
490
  authentication,
466
491
  workbenchValidated: true,
467
492
  model,
468
- onThreadStarted: (threadId) => tracker.start(threadId),
493
+ onThreadStarted: async (threadId) => {
494
+ tracker.start(threadId);
495
+ try {
496
+ await workbench(workbenchOptions, [
497
+ "set-scan-thread",
498
+ "--scan-id",
499
+ scanId,
500
+ "--thread-id",
501
+ threadId,
502
+ ]);
503
+ }
504
+ catch (error) {
505
+ notifyObserver("onWarning", options.onWarning, options.onObserverError, `Could not save scan session: ${safeErrorMessage(error)}`);
506
+ }
507
+ },
469
508
  onFinalize: async (usage) => {
470
- const snapshot = await tracker.stop(usage);
509
+ const snapshot = await tracker.stop(usage).catch((error) => {
510
+ if (options.maxCostUsd !== undefined)
511
+ throw error;
512
+ reportTrackingError(error);
513
+ return { usage, cost: estimateScanCost(model, usage) };
514
+ });
471
515
  throwIfAborted(signal, scanDir);
472
516
  if (options.maxCostUsd !== undefined && snapshot.cost === null) {
473
517
  notifyObserver("onWarning", options.onWarning, options.onObserverError, "Scan completed, but its cost limit could not be verified because model pricing or token usage is unavailable.");
@@ -527,14 +571,12 @@ export class CodexSecurity {
527
571
  }
528
572
  }
529
573
  }
530
- if (options.postScanPrompt?.trim() &&
531
- result.coverage.completeness === "complete") {
532
- const followUp = await thread.runStreamed(options.postScanPrompt, {
533
- signal,
534
- });
574
+ if (runPostScan !== null) {
575
+ const followUp = runPostScan;
576
+ runPostScan = null;
535
577
  await runScanEvents({
536
578
  thread,
537
- events: followUp.events,
579
+ events: (await followUp()).events,
538
580
  signal,
539
581
  scanDir,
540
582
  pluginRoot: runtime.plugin.installedRoot,
@@ -546,6 +588,29 @@ export class CodexSecurity {
546
588
  });
547
589
  checkOpen();
548
590
  }
591
+ try {
592
+ const runWorkbench = (args) => workbench(workbenchOptions, args);
593
+ const previousFindings = await listRepositoryFindings(runWorkbench, targetId, "all");
594
+ if (previousFindings !== undefined) {
595
+ await matchCompletedScan({
596
+ scanId,
597
+ repository: repo,
598
+ previousFindings: previousFindings.filter((finding) => finding["scanId"] !== scanId &&
599
+ finding["targetId"] === targetId),
600
+ falsePositives: falsePositiveExamples,
601
+ findings: result.findings.findings,
602
+ workbench: runWorkbench,
603
+ matchFindings: this.#dependencies.matchFindings,
604
+ environment,
605
+ model,
606
+ signal,
607
+ });
608
+ result.repositoryFindings = (await listRepositoryFindings(runWorkbench, targetId));
609
+ }
610
+ }
611
+ catch (error) {
612
+ notifyObserver("onWarning", options.onWarning, options.onObserverError, `Could not update repository findings: ${errorMessage(error)}`);
613
+ }
549
614
  return result;
550
615
  }
551
616
  catch (error) {
@@ -562,10 +627,9 @@ export class CodexSecurity {
562
627
  "fail-scan",
563
628
  "--scan-id",
564
629
  activeScan.id,
565
- // Redact before truncating: the stored message is read back by
566
- // `scans show` and travels inside the results directory.
630
+ // Scan history can be shared; never persist credential-bearing failures.
567
631
  "--message",
568
- redactedErrorMessage(failure).slice(0, 2400),
632
+ safeErrorMessage(failure).slice(0, 2400),
569
633
  ...(snapshot?.cost
570
634
  ? ["--cost-json", JSON.stringify(snapshot.cost)]
571
635
  : []),
@@ -573,6 +637,18 @@ export class CodexSecurity {
573
637
  }
574
638
  catch { }
575
639
  }
640
+ if (runPostScan !== null && !signal.aborted) {
641
+ try {
642
+ for await (const event of (await runPostScan()).events) {
643
+ if (event.type === "turn.failed") {
644
+ throw new CodexSecurityError(turnFailureMessage(event["error"]));
645
+ }
646
+ }
647
+ }
648
+ catch (postScanError) {
649
+ notifyObserver("onWarning", options.onWarning, options.onObserverError, `Could not run post-scan instructions: ${errorMessage(postScanError)}`);
650
+ }
651
+ }
576
652
  if (this.#closed)
577
653
  this.#requireOpen();
578
654
  if (signal.aborted && !(failure instanceof ScanInterruptedError)) {
@@ -746,20 +822,8 @@ export class CodexSecurity {
746
822
  }
747
823
  async #ensureRuntime(signal, temporaryRoot, validateLocation, auth = "auto", modelProvider) {
748
824
  this.#requireOpen();
749
- if (this.#runtime !== null) {
750
- const usePersistentCredentials = scanAuthentication(this.#dependencies.environment, auth, modelProvider)
751
- .method === "stored_credentials";
752
- if (this.#dependencies.prepareRuntime !== undefined ||
753
- this.#runtime.persistentCredentialHome === undefined ||
754
- this.#runtime.persistentCredentialHome === usePersistentCredentials) {
755
- return this.#runtime;
756
- }
757
- await this.#cleanupRuntime(this.#runtime);
758
- this.#runtime = null;
759
- this.#runtimePromise = null;
760
- this.#runtimeCredentialSource = null;
761
- this.#requireOpen();
762
- }
825
+ if (this.#runtime !== null)
826
+ return this.#runtime;
763
827
  if (this.#runtimePromise === null) {
764
828
  const runtimePromise = this.#prepareRuntime(signal ?? this.#abortController.signal, temporaryRoot, validateLocation, auth, modelProvider);
765
829
  this.#runtimePromise = runtimePromise;
@@ -832,11 +896,9 @@ export class CodexSecurity {
832
896
  return await this.#dependencies.prepareRuntime(this.config, signal);
833
897
  }
834
898
  const processEnvironment = selectedScanEnvironment(this.#dependencies.environment, auth, modelProvider);
835
- const persistentCredentialHome = scanAuthentication(this.#dependencies.environment, auth, modelProvider)
836
- .method === "stored_credentials";
837
- const codexHome = persistentCredentialHome
838
- ? await prepareCodexSecurityCredentialHome(processEnvironment, validateLocation)
839
- : await createIsolatedHome(temporaryRoot, validateLocation);
899
+ const codexHome = validateLocation === undefined
900
+ ? await prepareCodexSecurityCredentialHome(processEnvironment)
901
+ : await realpath(codexSecurityCredentialHome(processEnvironment));
840
902
  let bootstrapWorkspace;
841
903
  try {
842
904
  throwIfAborted(signal);
@@ -846,7 +908,7 @@ export class CodexSecurity {
846
908
  const configuredAmbientHome = environmentValue(processEnvironment, "CODEX_HOME");
847
909
  const ambientHome = configuredAmbientHome ?? nodeAmbientHome;
848
910
  const mergedConfig = await mergedCodexConfig(this.config);
849
- const codexConfig = await preserveCodexSecurityPluginRegistration(codexHome, scanRuntimeCodexConfig(mergedConfig, codexSecurityStateDirectory(processEnvironment), persistentCredentialHome ? codexHome : undefined));
911
+ const codexConfig = await preserveCodexSecurityPluginRegistration(codexHome, scanRuntimeCodexConfig(mergedConfig, codexSecurityStateDirectory(processEnvironment), codexHome));
850
912
  await writeCodexConfig(join(codexHome, "config.toml"), codexConfig);
851
913
  const configPath = join(bootstrapWorkspace, "config-preflight.toml");
852
914
  await writeCodexConfig(configPath, scanPreflightCodexConfig(mergedConfig));
@@ -861,7 +923,7 @@ export class CodexSecurity {
861
923
  : await initialCredentialsAvailable(processEnvironment, ambientHome, codexHome);
862
924
  return {
863
925
  codexHome,
864
- persistentCredentialHome,
926
+ persistentCredentialHome: true,
865
927
  bootstrapWorkspace,
866
928
  configPath,
867
929
  plugin,
@@ -875,12 +937,13 @@ export class CodexSecurity {
875
937
  };
876
938
  }
877
939
  catch (error) {
878
- const cleanupResults = await Promise.allSettled([bootstrapWorkspace, persistentCredentialHome ? undefined : codexHome]
879
- .filter((path) => path !== undefined)
880
- .map((path) => cleanupSdkDirectory(path)));
881
- const cleanupFailures = cleanupResults.flatMap((result) => result.status === "rejected" ? [result.reason] : []);
882
- if (cleanupFailures.length > 0) {
883
- throw new AggregateError([error, ...cleanupFailures], "Codex Security runtime preparation failed and its isolated runtime could not be cleaned up.", { cause: error });
940
+ if (bootstrapWorkspace !== undefined) {
941
+ try {
942
+ await cleanupSdkDirectory(bootstrapWorkspace);
943
+ }
944
+ catch (cleanupError) {
945
+ throw new AggregateError([error, cleanupError], "Codex Security runtime preparation failed and its isolated runtime could not be cleaned up.", { cause: error });
946
+ }
884
947
  }
885
948
  throw error;
886
949
  }
@@ -890,6 +953,25 @@ export class CodexSecurity {
890
953
  throw new CodexSecurityError("CodexSecurity is closed.");
891
954
  }
892
955
  }
956
+ export async function listRepositoryFindings(workbench, targetId, status = "open") {
957
+ const findings = [];
958
+ let offset;
959
+ do {
960
+ const page = await workbench([
961
+ "list-global-findings",
962
+ "--target-id",
963
+ targetId,
964
+ ...(status === "open" ? ["--status", "open"] : []),
965
+ ...(offset === undefined ? [] : ["--offset", String(offset)]),
966
+ ]);
967
+ if (!Array.isArray(page["findings"]))
968
+ return undefined;
969
+ findings.push(...page["findings"]);
970
+ offset =
971
+ typeof page["nextOffset"] === "number" ? page["nextOffset"] : undefined;
972
+ } while (offset !== undefined);
973
+ return findings;
974
+ }
893
975
  function deepScanOptions(options) {
894
976
  const selected = {};
895
977
  for (const [name, , minimum] of DEEP_SCAN_SETTINGS) {
@@ -944,6 +1026,12 @@ async function prepareDeepScanConfig(codexHome, environment, options, signal) {
944
1026
  deep_scan: { ...existing, ...overrides },
945
1027
  }), { mode: 0o600, signal });
946
1028
  }
1029
+ export function createSecurity(config = {}) {
1030
+ return createSecurityInternal(config, { surface: "sdk" });
1031
+ }
1032
+ export function createSecurityInternal(config = {}, runtimeOptions) {
1033
+ return new CodexSecurity(config, DEFAULT_DEPENDENCIES, runtimeOptions);
1034
+ }
947
1035
  export async function initialCredentialsAvailable(environment, ambientHome, isolatedHome, importer = importAmbientAuth) {
948
1036
  if (environmentApiKey(environment) !== null)
949
1037
  return false;
@@ -956,7 +1044,7 @@ export async function initialCredentialsAvailable(environment, ambientHome, isol
956
1044
  }
957
1045
  // Reports a cleanup failure without letting it decide the result of the scan. Only the
958
1046
  // message is forwarded, and it reaches the onWarning observer alone: unlike the fail-scan
959
- // path it is never written to the workbench, so it adds no persisted, unredacted text.
1047
+ // path it is never written to the workbench, so it adds no persisted warning text.
960
1048
  function warnCleanupFailed(options, reason) {
961
1049
  // This runs where a throw would replace the scan result, so every step is inside the
962
1050
  // guard: reading the reason, coercing it, and reading the observers off the options can
@@ -1019,7 +1107,7 @@ export async function runScanEvents(options) {
1019
1107
  const startedThreadId = event["thread_id"];
1020
1108
  if (typeof startedThreadId === "string") {
1021
1109
  threadId = startedThreadId;
1022
- options.onThreadStarted?.(startedThreadId);
1110
+ await options.onThreadStarted?.(startedThreadId);
1023
1111
  }
1024
1112
  if (!scanStarted) {
1025
1113
  scanStarted = true;
@@ -1237,7 +1325,7 @@ function targetInstruction(target) {
1237
1325
  }
1238
1326
  return `Scan target: staged and unstaged working-tree changes against ${target.base}.`;
1239
1327
  }
1240
- function scanRecipe(repository, activeProjectPath, target, mode, repositoryRevision, pluginVersion, effectiveConfig, failOnSeverity, knowledgeBasePaths, maxCostUsd, deepScan) {
1328
+ function scanRecipe(repository, target, mode, repositoryRevision, pluginVersion, effectiveConfig, failOnSeverity, knowledgeBasePaths, maxCostUsd, deepScan) {
1241
1329
  return {
1242
1330
  repository,
1243
1331
  target: {
@@ -1251,7 +1339,7 @@ function scanRecipe(repository, activeProjectPath, target, mode, repositoryRevis
1251
1339
  mode,
1252
1340
  ...(repositoryRevision === null ? {} : { repositoryRevision }),
1253
1341
  pluginVersion,
1254
- config: scanPreflightCodexConfig(effectiveConfig, activeProjectPath),
1342
+ config: scanPreflightCodexConfig(effectiveConfig),
1255
1343
  ...(failOnSeverity === undefined ? {} : { failOnSeverity }),
1256
1344
  ...(knowledgeBasePaths === undefined ? {} : { knowledgeBasePaths }),
1257
1345
  ...(maxCostUsd === undefined ? {} : { maxCostUsd }),
@@ -1440,8 +1528,8 @@ function reconnectDetails(message) {
1440
1528
  //
1441
1529
  // Only `error.message` is reused, because that is the single shape the previous
1442
1530
  // code already surfaced. No other shape is forwarded or stringified: this message
1443
- // reaches `fail-scan --message` and is stored in `scans.failure_message` without
1444
- // redaction, so widening what is copied out of the payload would add a new
1531
+ // reaches `fail-scan --message` and is stored unchanged in `scans.failure_message`,
1532
+ // so widening what is copied out of the payload would add a new
1445
1533
  // credential-disclosure path to persistent scan history.
1446
1534
  function turnFailureMessage(error) {
1447
1535
  if (isRecord(error) && typeof error["message"] === "string") {
@@ -1497,17 +1585,12 @@ export function scanRuntimeCodexConfig(config, stateDirectory, protectedCredenti
1497
1585
  },
1498
1586
  };
1499
1587
  }
1500
- export function scanPreflightCodexConfig(config, activeProjectPath) {
1501
- const safeString = (value, maxLength) => typeof value === "string" &&
1588
+ export function scanPreflightCodexConfig(config) {
1589
+ const safeString = (value) => typeof value === "string" &&
1502
1590
  value.length > 0 &&
1503
- value.length <= maxLength &&
1504
- !/[\u0000-\u001f\u007f]/u.test(value) &&
1505
- !/(?:^|[^a-z0-9])(?:api[_-]?key|access[_-]?key(?:[_-]?id)?|key|secret|token|env|mcp|set|password|passwd|credential|authorization|bearer)(?:[^a-z0-9]|$)/iu.test(value);
1506
- const safeProfileName = (value) => safeString(value, 128) && /^[A-Za-z0-9_-]+$/u.test(value);
1507
- const safeInteger = (value) => typeof value === "number" &&
1508
- Number.isSafeInteger(value) &&
1509
- value >= 0 &&
1510
- value <= 1_000_000;
1591
+ !/[\u0000-\u001f\u007f]/u.test(value);
1592
+ const safeProfileName = (value) => safeString(value) && /^[A-Za-z0-9_-]+$/u.test(value);
1593
+ const safeInteger = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
1511
1594
  const capabilityFeatures = (value) => {
1512
1595
  if (!isRecord(value))
1513
1596
  return {};
@@ -1544,7 +1627,7 @@ export function scanPreflightCodexConfig(config, activeProjectPath) {
1544
1627
  "service_tier",
1545
1628
  ]) {
1546
1629
  const value = source[key];
1547
- if (safeString(value, 512))
1630
+ if (safeString(value))
1548
1631
  result[key] = value;
1549
1632
  }
1550
1633
  const features = capabilityFeatures(source["features"]);
@@ -1569,16 +1652,6 @@ export function scanPreflightCodexConfig(config, activeProjectPath) {
1569
1652
  }
1570
1653
  return result;
1571
1654
  };
1572
- const prioritizedEntries = (value, priority) => {
1573
- const entries = Object.entries(value);
1574
- if (priority === undefined || !Object.hasOwn(value, priority)) {
1575
- return entries;
1576
- }
1577
- return [
1578
- [priority, value[priority]],
1579
- ...entries.filter(([key]) => key !== priority),
1580
- ];
1581
- };
1582
1655
  const result = executionConfig(config);
1583
1656
  const selectedProfile = safeProfileName(config["profile"])
1584
1657
  ? config["profile"]
@@ -1589,17 +1662,13 @@ export function scanPreflightCodexConfig(config, activeProjectPath) {
1589
1662
  const profiles = config["profiles"];
1590
1663
  if (isRecord(profiles)) {
1591
1664
  const sanitized = {};
1592
- let accepted = 0;
1593
- for (const [name, profile] of prioritizedEntries(profiles, selectedProfile)) {
1665
+ for (const [name, profile] of Object.entries(profiles)) {
1594
1666
  if (!safeProfileName(name) || !isRecord(profile))
1595
1667
  continue;
1596
1668
  const projected = executionConfig(profile);
1597
1669
  if (Object.keys(projected).length === 0)
1598
1670
  continue;
1599
1671
  sanitized[name] = projected;
1600
- accepted += 1;
1601
- if (accepted === 256)
1602
- break;
1603
1672
  }
1604
1673
  if (Object.keys(sanitized).length > 0)
1605
1674
  result["profiles"] = sanitized;
@@ -1618,7 +1687,7 @@ export function scanPreflightCodexConfig(config, activeProjectPath) {
1618
1687
  const sanitized = {};
1619
1688
  for (const key of ["region", "profile"]) {
1620
1689
  const value = aws[key];
1621
- if (safeString(value, 512))
1690
+ if (safeString(value))
1622
1691
  sanitized[key] = value;
1623
1692
  }
1624
1693
  if (Object.keys(sanitized).length > 0) {
@@ -1630,45 +1699,23 @@ export function scanPreflightCodexConfig(config, activeProjectPath) {
1630
1699
  }
1631
1700
  const rootMarkers = config["project_root_markers"];
1632
1701
  if (Array.isArray(rootMarkers)) {
1633
- result["project_root_markers"] = rootMarkers
1634
- .filter((value) => safeString(value, 256))
1635
- .slice(0, 64);
1702
+ result["project_root_markers"] = rootMarkers.filter(safeString);
1636
1703
  }
1637
1704
  const projects = config["projects"];
1638
1705
  if (isRecord(projects)) {
1639
1706
  const sanitized = {};
1640
- let accepted = 0;
1641
- const activeProjectRoot = activeProjectPath === undefined
1642
- ? undefined
1643
- : Object.keys(projects)
1644
- .filter((path) => {
1645
- if (!safeString(path, 4096) || !isAbsolute(path))
1646
- return false;
1647
- const remaining = relative(path, activeProjectPath);
1648
- return (remaining === "" ||
1649
- (remaining !== ".." &&
1650
- !remaining.startsWith(`..${sep}`) &&
1651
- !isAbsolute(remaining)));
1652
- })
1653
- .sort((left, right) => right.length - left.length)[0];
1654
- for (const [path, project] of prioritizedEntries(projects, activeProjectRoot ?? activeProjectPath)) {
1655
- if (!safeString(path, 4096) || !isAbsolute(path) || !isRecord(project)) {
1707
+ for (const [path, project] of Object.entries(projects)) {
1708
+ if (!safeString(path) || !isAbsolute(path) || !isRecord(project)) {
1656
1709
  continue;
1657
1710
  }
1658
1711
  const trust = project["trust_level"];
1659
1712
  if (trust !== "trusted" && trust !== "untrusted")
1660
1713
  continue;
1661
1714
  sanitized[path] = { trust_level: trust };
1662
- accepted += 1;
1663
- if (accepted === 256)
1664
- break;
1665
1715
  }
1666
1716
  if (Object.keys(sanitized).length > 0)
1667
1717
  result["projects"] = sanitized;
1668
1718
  }
1669
- if (Buffer.byteLength(JSON.stringify(result), "utf8") > 256 * 1024) {
1670
- throw new CodexSecurityError("The sanitized Codex Security preflight config exceeds the size limit.");
1671
- }
1672
1719
  return result;
1673
1720
  }
1674
1721
  function requireOutputOutsideRepository(repository, outputDirectory, pathKind = "output") {