@openai/codex-security 0.1.24 → 0.1.25

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 (105) hide show
  1. package/README.md +177 -19
  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/scripts/rank_preview.py +11 -1
  6. package/_bundled_plugin/scripts/workbench_cli.py +8 -1
  7. package/_bundled_plugin/scripts/workbench_db.py +44 -2
  8. package/_bundled_plugin/scripts/workbench_scan_history.py +374 -66
  9. package/_bundled_plugin/scripts/workbench_schema.py +15 -0
  10. package/_bundled_plugin/scripts/workbench_target.py +86 -12
  11. package/dist/api.d.ts +15 -2
  12. package/dist/api.d.ts.map +1 -1
  13. package/dist/api.js +330 -28
  14. package/dist/api.js.map +1 -1
  15. package/dist/auth.d.ts.map +1 -1
  16. package/dist/auth.js +35 -3
  17. package/dist/auth.js.map +1 -1
  18. package/dist/cli.d.ts +2 -0
  19. package/dist/cli.d.ts.map +1 -1
  20. package/dist/cli.js +86 -33
  21. package/dist/cli.js.map +1 -1
  22. package/dist/component-plan.d.ts.map +1 -1
  23. package/dist/component-plan.js +4 -2
  24. package/dist/component-plan.js.map +1 -1
  25. package/dist/component-scan.d.ts.map +1 -1
  26. package/dist/component-scan.js +21 -6
  27. package/dist/component-scan.js.map +1 -1
  28. package/dist/config.d.ts.map +1 -1
  29. package/dist/config.js +46 -2
  30. package/dist/config.js.map +1 -1
  31. package/dist/cost.d.ts +1 -0
  32. package/dist/cost.d.ts.map +1 -1
  33. package/dist/cost.js +23 -11
  34. package/dist/cost.js.map +1 -1
  35. package/dist/deduplication/checkpointed-review.d.ts.map +1 -1
  36. package/dist/deduplication/checkpointed-review.js +2 -1
  37. package/dist/deduplication/checkpointed-review.js.map +1 -1
  38. package/dist/deduplication/codex-review.d.ts +3 -0
  39. package/dist/deduplication/codex-review.d.ts.map +1 -1
  40. package/dist/deduplication/codex-review.js +122 -36
  41. package/dist/deduplication/codex-review.js.map +1 -1
  42. package/dist/deduplication/deduplication-prompts.d.ts +4 -3
  43. package/dist/deduplication/deduplication-prompts.d.ts.map +1 -1
  44. package/dist/deduplication/deduplication-prompts.js +18 -9
  45. package/dist/deduplication/deduplication-prompts.js.map +1 -1
  46. package/dist/deduplication/deduplication-reviewer.d.ts +2 -7
  47. package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -1
  48. package/dist/deduplication/deduplication-reviewer.js +46 -24
  49. package/dist/deduplication/deduplication-reviewer.js.map +1 -1
  50. package/dist/deduplication/deduplication.d.ts.map +1 -1
  51. package/dist/deduplication/deduplication.js +186 -23
  52. package/dist/deduplication/deduplication.js.map +1 -1
  53. package/dist/deduplication/scan.d.ts +9 -1
  54. package/dist/deduplication/scan.d.ts.map +1 -1
  55. package/dist/deduplication/scan.js +24 -7
  56. package/dist/deduplication/scan.js.map +1 -1
  57. package/dist/errors.d.ts +13 -0
  58. package/dist/errors.d.ts.map +1 -1
  59. package/dist/errors.js +7 -0
  60. package/dist/errors.js.map +1 -1
  61. package/dist/index.d.ts +5 -5
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.js +2 -2
  64. package/dist/index.js.map +1 -1
  65. package/dist/mock-scan.d.ts +4 -0
  66. package/dist/mock-scan.d.ts.map +1 -0
  67. package/dist/mock-scan.js +210 -0
  68. package/dist/mock-scan.js.map +1 -0
  69. package/dist/publish.d.ts.map +1 -1
  70. package/dist/publish.js +2 -2
  71. package/dist/publish.js.map +1 -1
  72. package/dist/runtime.d.ts +1 -0
  73. package/dist/runtime.d.ts.map +1 -1
  74. package/dist/runtime.js +49 -14
  75. package/dist/runtime.js.map +1 -1
  76. package/dist/scan-comparison.d.ts +11 -2
  77. package/dist/scan-comparison.d.ts.map +1 -1
  78. package/dist/scan-comparison.js +237 -39
  79. package/dist/scan-comparison.js.map +1 -1
  80. package/dist/scan-dashboard.d.ts +3 -1
  81. package/dist/scan-dashboard.d.ts.map +1 -1
  82. package/dist/scan-dashboard.js +70 -3
  83. package/dist/scan-dashboard.js.map +1 -1
  84. package/dist/scan-history-renderer.d.ts.map +1 -1
  85. package/dist/scan-history-renderer.js +21 -0
  86. package/dist/scan-history-renderer.js.map +1 -1
  87. package/dist/server/api.d.ts +6 -0
  88. package/dist/server/api.d.ts.map +1 -0
  89. package/dist/server/api.js +4 -0
  90. package/dist/server/api.js.map +1 -0
  91. package/dist/server/dashboard/app.css +1 -1
  92. package/dist/server/embeddings.d.ts +2 -1
  93. package/dist/server/embeddings.d.ts.map +1 -1
  94. package/dist/server/embeddings.js +8 -3
  95. package/dist/server/embeddings.js.map +1 -1
  96. package/dist/server/serve.d.ts.map +1 -1
  97. package/dist/server/serve.js +1 -1
  98. package/dist/server/serve.js.map +1 -1
  99. package/dist/targets.d.ts.map +1 -1
  100. package/dist/targets.js +3 -5
  101. package/dist/targets.js.map +1 -1
  102. package/dist/version.d.ts +1 -1
  103. package/dist/version.js +1 -1
  104. package/dist/version.js.map +1 -1
  105. package/package.json +9 -4
package/dist/api.js CHANGED
@@ -1,15 +1,16 @@
1
1
  /// <reference lib="esnext.disposable" preserve="true" />
2
- import { chmod, lstat, mkdir, readFile, realpath, rm, writeFile, } from "node:fs/promises";
2
+ import { statSync } from "node:fs";
3
+ import { chmod, lstat, mkdir, mkdtemp, readFile, realpath, rm, writeFile, } from "node:fs/promises";
3
4
  import { randomUUID } from "node:crypto";
4
5
  import { homedir, tmpdir } from "node:os";
5
- import { basename, dirname, isAbsolute, join, relative, resolve, sep, } from "node:path";
6
+ import { basename, delimiter, dirname, isAbsolute, join, relative, resolve, sep, } from "node:path";
6
7
  import { Codex, } from "@openai/codex-sdk";
7
8
  import { parse as parseToml, stringify as stringifyToml, } from "smol-toml";
8
9
  import { z } from "incur";
9
- import { accountStatus, CodexLoginHandle, loginApiKey as persistApiKey, logout as codexLogout, } from "./auth.js";
10
+ import { accountStatus, configuredCodexHome, CodexLoginHandle, loginApiKey as persistApiKey, logout as codexLogout, } from "./auth.js";
10
11
  import { jsonForPrompt, pluginPythonCommand, shellEnvironmentReference, } from "./codex-prompt.js";
11
- import { EXTERNAL_CODEX_PROVIDERS, isExternalModelProvider, mergedCodexConfig, scanApprovalPolicy, scanModelConfiguration, scanModelProvider, writeCodexConfig, } from "./config.js";
12
- import { estimateScanCost, ScanCostTracker, sumTokenUsage, } from "./cost.js";
12
+ import { DEFAULT_CODEX_CONFIG, EXTERNAL_CODEX_PROVIDERS, isExternalModelProvider, hasCommandAuth, mergedCodexConfig, modelProviderConfigOverride, resolveCommandAuthConfig, scanApprovalPolicy, scanModelConfiguration, scanModelProvider, writeCodexConfig, } from "./config.js";
13
+ import { estimateScanCost, ScanCostTracker, } from "./cost.js";
13
14
  import { DeepScanProgressTracker, } from "./deep-progress.js";
14
15
  import { loadContract, readScanFile, requireScanFile, } from "./contract.js";
15
16
  import { runCustomValidation, writeCustomValidationStatus, } from "./custom-validation.js";
@@ -18,12 +19,13 @@ import { AuthenticationRequiredError, CodexSecurityError, ConfigurationError, In
18
19
  import { prepareKnowledgeBase, } from "./knowledge-base.js";
19
20
  import { FindingWorkflow, workflowDigest } from "./finding-workflow.js";
20
21
  import { ScanResult, } from "./result.js";
22
+ import { writeMockScanDraft } from "./mock-scan.js";
21
23
  import { scanActivitiesFromEvent } from "./scan-activity.js";
22
24
  import { matchCompletedScan, matchScanFindingsInternal, } from "./scan-comparison.js";
23
25
  import { scanProgressUpdatesFromEvent, workerStatusFromEvent, } from "./worker-progress.js";
24
26
  import { CODEX_SECURITY_THREAD_SOURCES } from "./thread-source.js";
25
27
  import { CODEX_EXECUTABLE_VERSION, CODEX_SDK_VERSION } from "./version.js";
26
- import { acquireCodexSecurityCredentialHomeLock, bootstrapPlugin, bundledPluginRoot, cleanupSdkDirectory, codexSecurityCredentialAllowsAmbientImport, codexSecurityCredentialHome, codexSecurityHasStoredFileCredentials, codexSecurityStateDirectory, createIsolatedHome, expandHome, importAmbientAuth, prepareCodexSecurityCredentialHome, preserveCodexSecurityPluginRegistration, pluginExecutionEnvironment, planOutputArchive, prepareScanArtifactRestorer, prepareOutputDir, preparePersistentOutputRoot, requireModelSafeOutputDir, requireOutputOutsideRepository, resolveCodexCommand, resolvePluginPath, resolvePluginPython, runWorkbench, setCodexSecurityCredentialLogout, validateOutputDir, } from "./runtime.js";
28
+ import { acquireCodexSecurityCredentialHomeLock, bootstrapPlugin, bundledPluginRoot, cleanupSdkDirectory, codexSecurityCredentialAllowsAmbientImport, codexSecurityCredentialHome, codexSecurityHasStoredFileCredentials, codexSecurityStateDirectory, createIsolatedHome, executablePathForSpawn, expandHome, importAmbientAuth, prepareCodexSecurityCredentialHome, preserveCodexSecurityPluginRegistration, pluginExecutionEnvironment, pluginMetadata, planOutputArchive, prepareScanArtifactRestorer, prepareOutputDir, preparePersistentOutputRoot, requireModelSafeOutputDir, requireOutputOutsideRepository, resolveCodexCommand, resolvePluginPath, resolvePluginPython, runWorkbench, setCodexSecurityCredentialLogout, validateOutputDir, } from "./runtime.js";
27
29
  import { enclosingGitWorktreeRoot, normalizeRepository, normalizeTarget, repositoryRevision, resolveRepositoryPath, validatedGitEnvironment, validateCommittedDiffCheckout, validateMode, } from "./targets.js";
28
30
  const DEEP_SCAN_CONFIG_PATH_ENVIRONMENT = "CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH";
29
31
  const VALIDATION_DISPOSITIONS = [
@@ -278,7 +280,7 @@ export class CodexSecurity {
278
280
  : {}),
279
281
  outputDir: inputs.outputDir,
280
282
  ...(archiveDir === null ? {} : { archiveDir }),
281
- authentication: scanAuthentication(this.#dependencies.environment, options.auth, modelProvider),
283
+ authentication: scanAuthentication(this.#dependencies.environment, options.auth, modelProvider, hasCommandAuth(configuration)),
282
284
  ...model,
283
285
  ...(typeof modelProvider === "string" ? { modelProvider } : {}),
284
286
  ...(options.maxCostUsd === undefined
@@ -288,12 +290,22 @@ export class CodexSecurity {
288
290
  }
289
291
  async #run(repository, options) {
290
292
  this.#requireOpen();
293
+ if (options.mock)
294
+ return await this.#runMock(repository, options);
291
295
  const costAbortController = new AbortController();
292
296
  const signal = AbortSignal.any([
293
297
  this.#abortController.signal,
294
298
  costAbortController.signal,
295
299
  ...(options.signal === undefined ? [] : [options.signal]),
296
300
  ]);
301
+ const budgetAbortController = new AbortController();
302
+ const budgetSignal = AbortSignal.any([
303
+ signal,
304
+ budgetAbortController.signal,
305
+ ]);
306
+ let maxCostUsd = options.maxCostUsd;
307
+ let latestCost = null;
308
+ let notifiedLimit;
297
309
  let scanDir = "";
298
310
  let archivedScanDir = null;
299
311
  let targetPathsFile = null;
@@ -430,11 +442,56 @@ export class CodexSecurity {
430
442
  onCost: options.onCost === undefined && options.maxCostUsd === undefined
431
443
  ? undefined
432
444
  : (cost) => {
433
- notifyObserver("onCost", options.onCost, options.onObserverError, cost);
434
- if (options.maxCostUsd !== undefined &&
435
- cost.estimatedUsd > options.maxCostUsd) {
436
- costAbortController.abort(new ScanCostLimitExceededError(options.maxCostUsd, cost, scanDir));
445
+ latestCost = cost;
446
+ notifyObserver("onCost", options.onCost, options.onObserverError, cost, maxCostUsd);
447
+ if (maxCostUsd !== undefined &&
448
+ cost.estimatedUsd > maxCostUsd) {
449
+ costAbortController.abort(new ScanCostLimitExceededError(maxCostUsd, cost, scanDir));
450
+ return;
437
451
  }
452
+ const request = options.onBudgetApproaching;
453
+ if (request === undefined ||
454
+ maxCostUsd === undefined ||
455
+ budgetSignal.aborted ||
456
+ notifiedLimit === maxCostUsd ||
457
+ cost.estimatedUsd < maxCostUsd * 0.8)
458
+ return;
459
+ const limit = maxCostUsd;
460
+ notifiedLimit = limit;
461
+ void Promise.resolve()
462
+ .then(async () => {
463
+ if (budgetSignal.aborted)
464
+ return;
465
+ const next = await request({
466
+ maxCostUsd: limit,
467
+ cost,
468
+ signal: budgetSignal,
469
+ });
470
+ if (next === undefined ||
471
+ budgetSignal.aborted ||
472
+ activeScan === null)
473
+ return;
474
+ if (!Number.isFinite(next) ||
475
+ next <= Math.max(limit, latestCost.estimatedUsd)) {
476
+ throw new CodexSecurityError("The new cost limit must exceed the current limit and estimated cost.");
477
+ }
478
+ await workbench({ ...activeScan.options, signal: budgetSignal }, [
479
+ "set-scan-cost-limit",
480
+ "--scan-id",
481
+ activeScan.id,
482
+ "--max-cost-usd",
483
+ String(next),
484
+ ]);
485
+ if (budgetSignal.aborted)
486
+ return;
487
+ maxCostUsd = next;
488
+ notifyObserver("onCost", options.onCost, options.onObserverError, latestCost, maxCostUsd);
489
+ })
490
+ .catch((error) => {
491
+ if (!budgetSignal.aborted) {
492
+ notifyObserver("onWarning", options.onWarning, options.onObserverError, `Could not increase scan cost limit: ${errorMessage(error)}`);
493
+ }
494
+ });
438
495
  },
439
496
  onError: reportTrackingError,
440
497
  });
@@ -672,6 +729,9 @@ export class CodexSecurity {
672
729
  },
673
730
  onFinalize: async (usage) => {
674
731
  if (options.validationPrompt !== undefined) {
732
+ tracker.recordUsage(usage);
733
+ await tracker.refresh().catch(reportTrackingError);
734
+ checkOpen();
675
735
  await runCustomValidation({
676
736
  repository: repo,
677
737
  target: normalized,
@@ -706,12 +766,16 @@ export class CodexSecurity {
706
766
  if (turn.status !== "completed")
707
767
  throw new IncompleteScanError(turn.lastStreamError ??
708
768
  "The custom validation turn did not complete.");
709
- usage = sumTokenUsage(usage, turn.usage);
769
+ budgetAbortController.abort();
770
+ tracker.recordUsage(turn.usage, turn.threadId);
771
+ await tracker.refresh().catch(reportTrackingError);
772
+ checkOpen();
710
773
  return turn.finalResponse;
711
774
  },
712
775
  });
713
776
  customValidationComplete = true;
714
777
  }
778
+ budgetAbortController.abort();
715
779
  const snapshot = await tracker.stop(usage).catch((error) => {
716
780
  if (options.maxCostUsd !== undefined)
717
781
  throw error;
@@ -867,6 +931,7 @@ export class CodexSecurity {
867
931
  matchFindings: this.#dependencies.matchFindings ??
868
932
  ((input, comparisonOptions) => matchScanFindingsInternal(input, comparisonOptions, {
869
933
  surface: this.#surface,
934
+ allowBatching: false,
870
935
  })),
871
936
  environment,
872
937
  model,
@@ -885,9 +950,14 @@ export class CodexSecurity {
885
950
  // scan, and cleanup must treat all of those as a failure it is not allowed to mask.
886
951
  scanFailure = true;
887
952
  const snapshot = await costTracker?.stop().catch(() => null);
888
- const failure = signal.reason instanceof ScanCostLimitExceededError
953
+ let failure = signal.reason instanceof ScanCostLimitExceededError
889
954
  ? signal.reason
890
955
  : error;
956
+ if (failure instanceof ScanCostLimitExceededError &&
957
+ snapshot?.cost &&
958
+ snapshot.cost.estimatedUsd > failure.cost.estimatedUsd) {
959
+ failure = new ScanCostLimitExceededError(failure.maxCostUsd, snapshot.cost, scanDir);
960
+ }
891
961
  if (failure instanceof ScanCostLimitExceededError &&
892
962
  budgetRecovery !== null &&
893
963
  budgetRecovery.threadId !== null &&
@@ -980,6 +1050,7 @@ export class CodexSecurity {
980
1050
  throw failure;
981
1051
  }
982
1052
  finally {
1053
+ budgetAbortController.abort();
983
1054
  deepProgressTracker?.stop();
984
1055
  // Removing the temporary scan inputs is best effort. A throw here would replace the
985
1056
  // outcome the try and catch blocks already produced, so these failures are reported
@@ -1050,6 +1121,9 @@ export class CodexSecurity {
1050
1121
  }
1051
1122
  const authentication = await this.#authentication();
1052
1123
  this.#requireOpen();
1124
+ const ambientHome = environmentValue(this.#dependencies.environment, "CODEX_HOME") ??
1125
+ join(homedir(), ".codex");
1126
+ await initialCredentialsAvailable(this.#dependencies.environment, ambientHome, authentication.codexHome);
1053
1127
  return await accountStatus(this.#codexCommand(), authentication.environment, this.#abortController.signal);
1054
1128
  });
1055
1129
  }
@@ -1148,8 +1222,11 @@ export class CodexSecurity {
1148
1222
  }
1149
1223
  #createSessionCodex(session, runtimePaths, auth = "auto") {
1150
1224
  const { runtime, python, modelProvider, externalProvider, apiKey, sessionConfig, } = session;
1225
+ const commandAuth = hasCommandAuth(sessionConfig);
1151
1226
  const environment = {
1152
- ...pluginExecutionEnvironment(python, withoutCodexHome(selectedScanEnvironment(runtime.environment, auth, modelProvider))),
1227
+ ...pluginExecutionEnvironment(python, withoutCodexHome(selectedScanEnvironment(commandAuth
1228
+ ? withoutOpenAiApiKeys(runtime.environment)
1229
+ : runtime.environment, auth, modelProvider))),
1153
1230
  ...(externalProvider === null
1154
1231
  ? {}
1155
1232
  : { [externalProvider.env_key]: apiKey }),
@@ -1168,17 +1245,29 @@ export class CodexSecurity {
1168
1245
  // cannot safely encode their path and selector keys as dotted overrides.
1169
1246
  delete sdkCodexConfig["projects"];
1170
1247
  delete sdkCodexConfig["permissions"];
1248
+ if (commandAuth)
1249
+ delete sdkCodexConfig["model_providers"];
1171
1250
  const configuredResponsesMetadata = isRecord(sdkCodexConfig["responses_api_metadata"])
1172
1251
  ? sdkCodexConfig["responses_api_metadata"]
1173
1252
  : {};
1174
- const codexPathOverride = environmentValue(this.#dependencies.environment, "CODEX_CLI_PATH") ===
1253
+ let codexPathOverride = environmentValue(this.#dependencies.environment, "CODEX_CLI_PATH") ===
1175
1254
  undefined
1176
1255
  ? undefined
1177
1256
  : this.#codexCommand().command;
1257
+ let sdkEnvironment = definedEnvironment(withoutOpenAiApiKeys(environment));
1258
+ if (process.platform === "win32" && codexPathOverride === undefined) {
1259
+ codexPathOverride = environment["CODEX_CLI_PATH"];
1260
+ sdkEnvironment = bundledCodexSdkEnvironment(codexPathOverride, sdkEnvironment);
1261
+ }
1178
1262
  const codex = this.#dependencies.createCodex({
1179
- ...(codexPathOverride === undefined ? {} : { codexPathOverride }),
1263
+ ...(codexPathOverride === undefined
1264
+ ? {}
1265
+ : { codexPathOverride: executablePathForSpawn(codexPathOverride) }),
1180
1266
  ...(externalProvider !== null || apiKey === null ? {} : { apiKey }),
1181
- env: definedEnvironment(selectedScanEnvironment(environment, "chatgpt")),
1267
+ ...(commandAuth
1268
+ ? { configOverrides: modelProviderConfigOverride(sessionConfig) }
1269
+ : {}),
1270
+ env: sdkEnvironment,
1182
1271
  config: {
1183
1272
  ...sdkCodexConfig,
1184
1273
  responses_api_metadata: {
@@ -1196,19 +1285,22 @@ export class CodexSecurity {
1196
1285
  throwIfAborted(signal);
1197
1286
  };
1198
1287
  try {
1199
- const requestedConfig = await mergedCodexConfig(this.config);
1288
+ const requestedConfig = resolveCommandAuthConfig(await mergedCodexConfig(this.config), configuredCodexHome(this.#dependencies.environment));
1289
+ const commandAuth = hasCommandAuth(requestedConfig);
1200
1290
  const modelProvider = scanModelProvider(requestedConfig);
1201
- const externalProvider = isExternalModelProvider(modelProvider)
1291
+ const externalProvider = !commandAuth && isExternalModelProvider(modelProvider)
1202
1292
  ? EXTERNAL_CODEX_PROVIDERS[modelProvider]
1203
1293
  : null;
1204
- let authentication = scanAuthentication(this.#dependencies.environment, options.auth, modelProvider);
1294
+ let authentication = scanAuthentication(this.#dependencies.environment, options.auth, modelProvider, commandAuth);
1205
1295
  const apiKey = authentication.method === "api_key"
1206
1296
  ? environmentApiKey(this.#dependencies.environment, modelProvider)
1207
1297
  : null;
1208
1298
  if (externalProvider !== null && apiKey === null) {
1209
1299
  throw new AuthenticationRequiredError(`Set ${externalProvider.env_key} to run a scan through ${externalProvider.name}.`);
1210
1300
  }
1211
- const scanEnvironment = selectedScanEnvironment(this.#dependencies.environment, options.auth, modelProvider);
1301
+ const scanEnvironment = selectedScanEnvironment(commandAuth
1302
+ ? withoutOpenAiApiKeys(this.#dependencies.environment)
1303
+ : this.#dependencies.environment, options.auth, modelProvider);
1212
1304
  if (this.#dependencies.prepareRuntime === undefined) {
1213
1305
  const credentialHome = await prepareCodexSecurityCredentialHome(scanEnvironment, (path) => requireOutputOutsideRepository(protectedRoot, path, "runtime"));
1214
1306
  releaseCredentialHome = await acquireCodexSecurityCredentialHomeLock(credentialHome, signal);
@@ -1259,12 +1351,14 @@ export class CodexSecurity {
1259
1351
  }
1260
1352
  if (!runtime.credentialsAvailable &&
1261
1353
  apiKey === null &&
1354
+ !commandAuth &&
1262
1355
  authentication.method !== "aws_credentials") {
1263
1356
  throw new AuthenticationRequiredError("No credentials were found. Run 'codex-security login', use " +
1264
1357
  "'codex-security login --device-auth' on a remote or headless machine, or set " +
1265
1358
  "OPENAI_API_KEY or CODEX_API_KEY for CI.");
1266
1359
  }
1267
- authentication = await runtimeScanAuthentication(this.#dependencies.environment, runtime.codexHome, options.auth, modelProvider);
1360
+ if (!commandAuth)
1361
+ authentication = await runtimeScanAuthentication(this.#dependencies.environment, runtime.codexHome, options.auth, modelProvider);
1268
1362
  if (options.safetyIdentifier !== undefined &&
1269
1363
  authentication.method !== "api_key" &&
1270
1364
  !(authentication.method === "stored_credentials" &&
@@ -1352,7 +1446,165 @@ export class CodexSecurity {
1352
1446
  : undefined;
1353
1447
  runtime.effectiveConfig = mergedConfig;
1354
1448
  }
1449
+ async #runMock(repository, options) {
1450
+ const signal = AbortSignal.any([
1451
+ this.#abortController.signal,
1452
+ ...(options.signal ? [options.signal] : []),
1453
+ ]);
1454
+ const local = await this.#validateLocalInputs(repository, options, signal);
1455
+ const temporaryRoot = await realpath(tmpdir());
1456
+ requireOutputOutsideRepository(local.protectedRoot, temporaryRoot, "temporary");
1457
+ const workspace = await mkdtemp(join(temporaryRoot, "codex-security-mock-"));
1458
+ const workbench = this.#dependencies.runWorkbench ?? runWorkbench;
1459
+ let activeScan;
1460
+ let scanDir = "";
1461
+ try {
1462
+ const pluginRoot = await resolvePluginPath(this.config.pluginPath, workspace, signal);
1463
+ const plugin = await pluginMetadata(pluginRoot);
1464
+ if (options.expectedPluginVersion !== undefined &&
1465
+ options.expectedPluginVersion !== plugin.version) {
1466
+ throw new CodexSecurityError("The selected plugin version does not match the expected plugin version.");
1467
+ }
1468
+ const python = await (this.#dependencies.resolvePluginPython ?? resolvePluginPython)({
1469
+ configuredPath: this.config.pythonPath,
1470
+ environment: this.#dependencies.environment,
1471
+ protectedRoot: local.protectedRoot,
1472
+ signal,
1473
+ });
1474
+ if (options.knowledgeBasePaths?.length) {
1475
+ const knowledgeBase = await prepareKnowledgeBase(options.knowledgeBasePaths, signal);
1476
+ await knowledgeBase.cleanup();
1477
+ }
1478
+ const outputRoot = local.outputDir === null
1479
+ ? await preparePersistentOutputRoot(local.stateDirectory, "scans", basename(local.repository))
1480
+ : undefined;
1481
+ let archivedScanDir;
1482
+ scanDir = await prepareOutputDir(local.outputDir ?? undefined, basename(local.repository), outputRoot, (path) => requireOutputOutsideRepository(local.protectedRoot, path), options.archiveExisting, (path) => {
1483
+ archivedScanDir = path;
1484
+ notifyObserver("onOutputArchived", options.onOutputArchived, options.onObserverError, path);
1485
+ });
1486
+ requireModelSafeOutputDir(scanDir);
1487
+ notifyObserver("onOutputDirReady", options.onOutputDirReady, options.onObserverError, scanDir);
1488
+ const revision = await repositoryRevision(local.repository, signal);
1489
+ const { model } = scanModelConfiguration({
1490
+ ...DEFAULT_CODEX_CONFIG,
1491
+ ...this.config.codexOverrides,
1492
+ });
1493
+ const workbenchOptions = {
1494
+ python,
1495
+ pluginRoot,
1496
+ environment: {
1497
+ ...this.#dependencies.environment,
1498
+ CODEX_SECURITY_STATE_DIR: local.stateDirectory,
1499
+ },
1500
+ signal,
1501
+ failureMessage: "Could not save the mock scan",
1502
+ };
1503
+ const registration = await workbench(workbenchOptions, [
1504
+ "register-cli-scan",
1505
+ "--repository",
1506
+ local.repository,
1507
+ "--scan-dir",
1508
+ scanDir,
1509
+ "--registration-json-stdin",
1510
+ ...(options.archiveExisting ? ["--archive-existing"] : []),
1511
+ ...(archivedScanDir === undefined
1512
+ ? []
1513
+ : ["--archived-scan-dir", archivedScanDir]),
1514
+ ...(options.parentScanId === undefined
1515
+ ? []
1516
+ : ["--parent-scan-id", options.parentScanId]),
1517
+ ], JSON.stringify({
1518
+ recipe: {
1519
+ ...scanRecipe(local.repository, local.target, local.mode, revision, plugin.version, { model }, options.failureSeverity, options.knowledgeBasePaths, options.maxCostUsd),
1520
+ mock: true,
1521
+ },
1522
+ userContext: options.scanPrompt,
1523
+ ...(options.workflowId === undefined
1524
+ ? {}
1525
+ : { workflowId: options.workflowId }),
1526
+ }));
1527
+ const scanId = registration["scanId"];
1528
+ const targetId = registration["targetId"];
1529
+ if (typeof scanId !== "string" ||
1530
+ typeof targetId !== "string" ||
1531
+ registration["scanDir"] !== scanDir) {
1532
+ throw new CodexSecurityError("The workbench returned an invalid mock scan registration.");
1533
+ }
1534
+ activeScan = { id: scanId, options: workbenchOptions };
1535
+ notifyObserver("onScanStarted", options.onScanStarted, options.onObserverError);
1536
+ await writeMockScanDraft(scanDir, scanId, local.target, registration, signal);
1537
+ const usage = {
1538
+ input_tokens: 0,
1539
+ cached_input_tokens: 0,
1540
+ output_tokens: 0,
1541
+ };
1542
+ const cost = estimateScanCost(model, usage);
1543
+ await workbench(workbenchOptions, [
1544
+ "prepare-scan-completion",
1545
+ "--scan-id",
1546
+ scanId,
1547
+ ]);
1548
+ const completion = await workbench(workbenchOptions, [
1549
+ "complete-scan",
1550
+ "--scan-id",
1551
+ scanId,
1552
+ ...(cost === null ? [] : ["--cost-json", JSON.stringify(cost)]),
1553
+ ]);
1554
+ activeScan = undefined;
1555
+ const completedScan = completion["scan"];
1556
+ if (isRecord(completedScan) && Array.isArray(completedScan["warnings"])) {
1557
+ for (const warning of completedScan["warnings"]) {
1558
+ if (typeof warning === "string")
1559
+ notifyObserver("onWarning", options.onWarning, options.onObserverError, warning, Array.isArray(completion["targetWarnings"]) &&
1560
+ completion["targetWarnings"].includes(warning)
1561
+ ? { kind: "target_changed" }
1562
+ : undefined);
1563
+ }
1564
+ }
1565
+ const result = await collectResult({
1566
+ status: "completed",
1567
+ model,
1568
+ usage,
1569
+ mock: true,
1570
+ finalResponse: "Synthetic mock scan; no security analysis was performed.",
1571
+ }, "", scanDir, pluginRoot, {
1572
+ repository: local.repository,
1573
+ repositoryRevision: revision,
1574
+ target: local.target,
1575
+ mode: local.mode,
1576
+ pluginVersion: plugin.version,
1577
+ }, signal, true);
1578
+ // Stable fixture identities are indexed by complete-scan without model matching.
1579
+ result.repositoryFindings = (await listRepositoryFindings((args) => workbench(workbenchOptions, args), targetId));
1580
+ return result;
1581
+ }
1582
+ catch (error) {
1583
+ if (activeScan !== undefined) {
1584
+ await workbench({ ...activeScan.options, signal: undefined }, [
1585
+ "fail-scan",
1586
+ "--scan-id",
1587
+ activeScan.id,
1588
+ "--message",
1589
+ safeErrorMessage(error).slice(0, 2400),
1590
+ ]).catch(() => undefined);
1591
+ }
1592
+ if (this.#closed)
1593
+ this.#requireOpen();
1594
+ throwIfAborted(signal, scanDir);
1595
+ throw error;
1596
+ }
1597
+ finally {
1598
+ await cleanupSdkDirectory(workspace);
1599
+ }
1600
+ }
1355
1601
  async #validateLocalInputs(repository, options, signal) {
1602
+ if (options.mock &&
1603
+ (options.mode === "deep" ||
1604
+ options.validationPrompt !== undefined ||
1605
+ options.postScanPrompt !== undefined)) {
1606
+ throw new CodexSecurityError("Mock scans support Standard mode without custom validation or post-scan prompts; those workflows require model calls.");
1607
+ }
1356
1608
  deepScanOptions(options);
1357
1609
  const identifier = options.safetyIdentifier;
1358
1610
  if (identifier !== undefined &&
@@ -1423,7 +1675,9 @@ export class CodexSecurity {
1423
1675
  const modelProvider = requestedConfig === undefined
1424
1676
  ? undefined
1425
1677
  : scanModelProvider(requestedConfig);
1426
- const processEnvironment = selectedScanEnvironment(this.#dependencies.environment, auth, modelProvider);
1678
+ const processEnvironment = selectedScanEnvironment(requestedConfig !== undefined && hasCommandAuth(requestedConfig)
1679
+ ? withoutOpenAiApiKeys(this.#dependencies.environment)
1680
+ : this.#dependencies.environment, auth, modelProvider);
1427
1681
  const codexHome = validateLocation === undefined
1428
1682
  ? await prepareCodexSecurityCredentialHome(processEnvironment)
1429
1683
  : await realpath(codexSecurityCredentialHome(processEnvironment));
@@ -1448,7 +1702,8 @@ export class CodexSecurity {
1448
1702
  const deepScanConfigPath = (await pluginSupportsIsolatedDeepScanConfig(plugin.pluginRoot))
1449
1703
  ? join(bootstrapWorkspace, "deep-scan-config.toml")
1450
1704
  : undefined;
1451
- const credentialsAvailable = isExternalModelProvider(modelProvider) ||
1705
+ const credentialsAvailable = hasCommandAuth(mergedConfig) ||
1706
+ isExternalModelProvider(modelProvider) ||
1452
1707
  modelProvider === "amazon-bedrock"
1453
1708
  ? false
1454
1709
  : await initialCredentialsAvailable(processEnvironment, ambientHome, codexHome);
@@ -1987,10 +2242,12 @@ async function collectResult(turnResult, threadId, scanDir, pluginRoot, expectat
1987
2242
  sarifPath,
1988
2243
  });
1989
2244
  }
1990
- export function scanAuthentication(environment, auth = "auto", modelProvider) {
2245
+ export function scanAuthentication(environment, auth = "auto", modelProvider, commandAuth = false) {
1991
2246
  if (!SCAN_AUTH_MODES.includes(auth)) {
1992
2247
  throw new TypeError("Scan authentication mode must be auto, chatgpt, or api-key.");
1993
2248
  }
2249
+ if (commandAuth)
2250
+ return { method: "command", verified: false };
1994
2251
  if (modelProvider === "amazon-bedrock") {
1995
2252
  const sources = [
1996
2253
  "AWS_BEARER_TOKEN_BEDROCK",
@@ -2021,6 +2278,19 @@ export function scanAuthentication(environment, auth = "auto", modelProvider) {
2021
2278
  ? { method: "stored_credentials", verified: false }
2022
2279
  : { method: "api_key", source: key.source, verified: false };
2023
2280
  }
2281
+ /** Shell-neutral guidance so PowerShell users are not told to run POSIX `unset`. */
2282
+ export function formatEnvironmentVariableRemovalGuidance(names) {
2283
+ if (names.length === 0) {
2284
+ return "remove OPENAI_API_KEY and CODEX_API_KEY from the environment";
2285
+ }
2286
+ if (names.length === 1) {
2287
+ return `remove ${names[0]} from the environment`;
2288
+ }
2289
+ if (names.length === 2) {
2290
+ return `remove ${names[0]} and ${names[1]} from the environment`;
2291
+ }
2292
+ return `remove ${names.slice(0, -1).join(", ")}, and ${names[names.length - 1]} from the environment`;
2293
+ }
2024
2294
  async function runtimeScanAuthentication(environment, codexHome, auth = "auto", modelProvider) {
2025
2295
  const authentication = scanAuthentication(environment, auth, modelProvider);
2026
2296
  if (authentication.method !== "stored_credentials")
@@ -2051,10 +2321,8 @@ export function selectedScanEnvironment(environment, auth = "auto", modelProvide
2051
2321
  if (auth !== "chatgpt" && selectedProviderKey === null && !bedrockProvider) {
2052
2322
  return environment;
2053
2323
  }
2054
- return Object.fromEntries(Object.entries(environment).filter(([name]) => {
2324
+ return Object.fromEntries(Object.entries(withoutOpenAiApiKeys(environment)).filter(([name]) => {
2055
2325
  const key = name.toUpperCase();
2056
- if (key === "OPENAI_API_KEY" || key === "CODEX_API_KEY")
2057
- return false;
2058
2326
  if (key === "OPENROUTER_API_KEY" || key === "FIREWORKS_API_KEY") {
2059
2327
  return (!bedrockProvider &&
2060
2328
  (selectedProviderKey === null || key === selectedProviderKey));
@@ -2062,6 +2330,9 @@ export function selectedScanEnvironment(environment, auth = "auto", modelProvide
2062
2330
  return true;
2063
2331
  }));
2064
2332
  }
2333
+ function withoutOpenAiApiKeys(environment) {
2334
+ return Object.fromEntries(Object.entries(environment).filter(([name]) => !["OPENAI_API_KEY", "CODEX_API_KEY"].includes(name.toUpperCase())));
2335
+ }
2065
2336
  function notifyObserver(observerName, observer, onObserverError, ...args) {
2066
2337
  void Promise.resolve()
2067
2338
  .then(() => observer?.(...args))
@@ -2199,6 +2470,12 @@ function sharedCredentialCodexConfig(config, credentialHome) {
2199
2470
  shared[key] = structuredClone(config[key]);
2200
2471
  }
2201
2472
  const modelProvider = scanModelProvider(config);
2473
+ if (hasCommandAuth(config)) {
2474
+ for (const key of ["profile", "profiles"]) {
2475
+ if (Object.hasOwn(config, key))
2476
+ shared[key] = structuredClone(config[key]);
2477
+ }
2478
+ }
2202
2479
  if (typeof modelProvider === "string" && modelProvider.length > 0) {
2203
2480
  shared["model_provider"] = modelProvider;
2204
2481
  const providers = config["model_providers"];
@@ -2373,6 +2650,31 @@ function throwIfAborted(signal, scanDir = "") {
2373
2650
  : "Codex Security scan was interrupted during preparation.";
2374
2651
  throw new ScanInterruptedError(message, scanDir, { cause: signal.reason });
2375
2652
  }
2653
+ function bundledCodexSdkEnvironment(command, environment) {
2654
+ // An SDK executable override disables its bundled-tool PATH setup.
2655
+ const toolsDirectory = join(dirname(dirname(command)), "codex-path");
2656
+ try {
2657
+ if (!statSync(toolsDirectory).isDirectory())
2658
+ return environment;
2659
+ }
2660
+ catch {
2661
+ return environment;
2662
+ }
2663
+ const result = { ...environment };
2664
+ const pathKeys = Object.keys(result).filter((key) => key.toLowerCase() === "path");
2665
+ const pathKey = pathKeys.includes("Path")
2666
+ ? "Path"
2667
+ : pathKeys.at(-1) ?? "PATH";
2668
+ for (const key of pathKeys) {
2669
+ if (key !== pathKey)
2670
+ delete result[key];
2671
+ }
2672
+ const entries = (result[pathKey] ?? "")
2673
+ .split(delimiter)
2674
+ .filter((entry) => entry.length > 0 && entry !== toolsDirectory);
2675
+ result[pathKey] = [toolsDirectory, ...entries].join(delimiter);
2676
+ return result;
2677
+ }
2376
2678
  function definedEnvironment(environment) {
2377
2679
  return Object.fromEntries(Object.entries(environment).filter((entry) => entry[1] !== undefined));
2378
2680
  }