@openai/codex-security 0.1.4 → 0.1.6

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 (106) hide show
  1. package/README.md +127 -13
  2. package/_bundled_plugin/.codex-plugin/plugin.json +1 -1
  3. package/_bundled_plugin/.mcp.json +8 -1
  4. package/_bundled_plugin/examples/completed-scan/findings.json +6 -0
  5. package/_bundled_plugin/examples/completed-scan/scan-manifest.json +1 -1
  6. package/_bundled_plugin/mcp/mcp-app.html.br +0 -0
  7. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  8. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  9. package/_bundled_plugin/preflight/capability-profiles.toml +0 -57
  10. package/_bundled_plugin/references/config-preflight.md +1 -1
  11. package/_bundled_plugin/references/final-report.md +15 -7
  12. package/_bundled_plugin/references/finding-detail-fields.md +10 -2
  13. package/_bundled_plugin/references/scan-artifacts.md +11 -9
  14. package/_bundled_plugin/references/scan-contract.md +3 -1
  15. package/_bundled_plugin/schemas/definitions/artifact-common.schema.json +42 -0
  16. package/_bundled_plugin/schemas/definitions/discovery-candidate.schema.json +100 -0
  17. package/_bundled_plugin/schemas/tools/candidate-attack-paths.schema.json +181 -0
  18. package/_bundled_plugin/schemas/tools/candidate-validations.schema.json +166 -0
  19. package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +120 -0
  20. package/_bundled_plugin/schemas/tools/discovery-candidates.schema.json +83 -0
  21. package/_bundled_plugin/schemas/tools/review-items.schema.json +80 -0
  22. package/_bundled_plugin/schemas/tools/scan-draft.schema.json +572 -0
  23. package/_bundled_plugin/schemas/tools/worker-threat-model.schema.json +18 -0
  24. package/_bundled_plugin/scripts/deep_scan_config.py +11 -3
  25. package/_bundled_plugin/scripts/deep_scan_workbench.py +153 -133
  26. package/_bundled_plugin/scripts/finalize_scan_contract.py +39 -0
  27. package/_bundled_plugin/scripts/generate_in_scope_files.py +134 -0
  28. package/_bundled_plugin/scripts/workbench_cli.py +33 -1
  29. package/_bundled_plugin/scripts/workbench_db.py +181 -118
  30. package/_bundled_plugin/scripts/workbench_feedback.py +2 -0
  31. package/_bundled_plugin/scripts/workbench_native_indexes.py +5 -49
  32. package/_bundled_plugin/scripts/workbench_progress.py +86 -3
  33. package/_bundled_plugin/scripts/workbench_remediation.py +19 -1
  34. package/_bundled_plugin/scripts/workbench_scan_history.py +7 -1
  35. package/_bundled_plugin/scripts/workbench_scan_start.py +11 -5
  36. package/_bundled_plugin/scripts/workbench_scan_usage.py +557 -0
  37. package/_bundled_plugin/scripts/workbench_schema.py +501 -0
  38. package/_bundled_plugin/scripts/workbench_target.py +14 -1
  39. package/_bundled_plugin/scripts/workbench_validation.py +117 -13
  40. package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +4 -4
  41. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +34 -28
  42. package/_bundled_plugin/skills/finding-discovery/SKILL.md +1 -1
  43. package/_bundled_plugin/skills/security-diff-scan/SKILL.md +11 -8
  44. package/_bundled_plugin/skills/security-scan/SKILL.md +16 -9
  45. package/_bundled_plugin/skills/security-scan/references/repository-wide-scan.md +26 -14
  46. package/_bundled_plugin/skills/security-scan/references/scan-artifacts-and-ledger.md +1 -1
  47. package/_bundled_plugin/skills/threat-model/SKILL.md +2 -2
  48. package/_bundled_plugin/skills/validation/SKILL.md +5 -5
  49. package/_bundled_plugin/skills/vulnerability-writeup/SKILL.md +145 -227
  50. package/_bundled_plugin/skills/vulnerability-writeup/references/report-format.md +53 -68
  51. package/dist/api.d.ts +30 -8
  52. package/dist/api.d.ts.map +1 -1
  53. package/dist/api.js +438 -66
  54. package/dist/api.js.map +1 -1
  55. package/dist/auth.d.ts.map +1 -1
  56. package/dist/auth.js +262 -44
  57. package/dist/auth.js.map +1 -1
  58. package/dist/cli.d.ts +4 -3
  59. package/dist/cli.d.ts.map +1 -1
  60. package/dist/cli.js +713 -148
  61. package/dist/cli.js.map +1 -1
  62. package/dist/config.d.ts +28 -0
  63. package/dist/config.d.ts.map +1 -1
  64. package/dist/config.js +39 -2
  65. package/dist/config.js.map +1 -1
  66. package/dist/contract.d.ts.map +1 -1
  67. package/dist/contract.js +14 -0
  68. package/dist/contract.js.map +1 -1
  69. package/dist/cost.d.ts +7 -0
  70. package/dist/cost.d.ts.map +1 -1
  71. package/dist/cost.js +397 -26
  72. package/dist/cost.js.map +1 -1
  73. package/dist/errors.d.ts +2 -0
  74. package/dist/errors.d.ts.map +1 -1
  75. package/dist/errors.js +48 -0
  76. package/dist/errors.js.map +1 -1
  77. package/dist/index.d.ts +3 -2
  78. package/dist/index.d.ts.map +1 -1
  79. package/dist/index.js.map +1 -1
  80. package/dist/multiscan.d.ts +1 -0
  81. package/dist/multiscan.d.ts.map +1 -1
  82. package/dist/multiscan.js +5 -7
  83. package/dist/multiscan.js.map +1 -1
  84. package/dist/runtime.d.ts +40 -1
  85. package/dist/runtime.d.ts.map +1 -1
  86. package/dist/runtime.js +742 -46
  87. package/dist/runtime.js.map +1 -1
  88. package/dist/scan-activity.d.ts +13 -0
  89. package/dist/scan-activity.d.ts.map +1 -0
  90. package/dist/scan-activity.js +319 -0
  91. package/dist/scan-activity.js.map +1 -0
  92. package/dist/scan-dashboard.d.ts +47 -0
  93. package/dist/scan-dashboard.d.ts.map +1 -0
  94. package/dist/scan-dashboard.js +444 -0
  95. package/dist/scan-dashboard.js.map +1 -0
  96. package/dist/scan-history-renderer.d.ts.map +1 -1
  97. package/dist/scan-history-renderer.js +4 -1
  98. package/dist/scan-history-renderer.js.map +1 -1
  99. package/dist/version.d.ts +1 -1
  100. package/dist/version.js +1 -1
  101. package/dist/version.js.map +1 -1
  102. package/dist/worker-progress.d.ts +7 -0
  103. package/dist/worker-progress.d.ts.map +1 -1
  104. package/dist/worker-progress.js +68 -0
  105. package/dist/worker-progress.js.map +1 -1
  106. package/package.json +3 -3
package/dist/api.js CHANGED
@@ -1,17 +1,19 @@
1
1
  /// <reference lib="esnext.disposable" preserve="true" />
2
- import { chmod, lstat, mkdir, realpath, rm, writeFile } from "node:fs/promises";
2
+ import { chmod, lstat, mkdir, readFile, realpath, rm, writeFile, } from "node:fs/promises";
3
3
  import { randomUUID } from "node:crypto";
4
4
  import { homedir, tmpdir } from "node:os";
5
5
  import { basename, dirname, isAbsolute, join, relative, sep } from "node:path";
6
6
  import { Codex } from "@openai/codex-sdk";
7
+ import { parse as parseToml, stringify as stringifyToml, } from "smol-toml";
7
8
  import { accountStatus, CodexLoginHandle, loginApiKey as persistApiKey, logout as codexLogout, } from "./auth.js";
8
- import { mergedCodexConfig, scanModelConfiguration, writeCodexConfig, } from "./config.js";
9
+ import { EXTERNAL_CODEX_PROVIDERS, isExternalModelProvider, mergedCodexConfig, scanModelConfiguration, writeCodexConfig, } from "./config.js";
9
10
  import { estimateScanCost, ScanCostTracker } from "./cost.js";
10
11
  import { loadContract, requireScanFile, } from "./contract.js";
11
- import { AuthenticationRequiredError, CodexSecurityError, IncompleteScanError, OutputDirectoryError, OutputInsideProtectedRootError, ScanCostLimitExceededError, ScanInterruptedError, } from "./errors.js";
12
+ import { AuthenticationRequiredError, CodexSecurityError, IncompleteScanError, OutputDirectoryError, OutputInsideProtectedRootError, redactedErrorMessage, ScanCostLimitExceededError, ScanInterruptedError, } from "./errors.js";
12
13
  import { prepareKnowledgeBase, } from "./knowledge-base.js";
13
14
  import { ScanResult } from "./result.js";
14
- import { workerStatusFromEvent, } from "./worker-progress.js";
15
+ import { scanActivitiesFromEvent } from "./scan-activity.js";
16
+ import { scanProgressUpdatesFromEvent, workerStatusFromEvent, } from "./worker-progress.js";
15
17
  import { CODEX_EXECUTABLE_VERSION, CODEX_SDK_VERSION } from "./version.js";
16
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";
17
19
  import { enclosingGitWorktreeRoot, normalizeRepository, normalizeTarget, repositoryRevision, resolveRepositoryPath, validatedGitEnvironment, validateMode, } from "./targets.js";
@@ -20,6 +22,14 @@ const DEFAULT_DEPENDENCIES = {
20
22
  environment: process.env,
21
23
  };
22
24
  const SCAN_PERMISSION_PROFILE = "codex_security_scan";
25
+ const PERSONAL_TRUSTED_ACCESS_URL = "https://chatgpt.com/cyber";
26
+ const ORGANIZATIONAL_TRUSTED_ACCESS_URL = "https://openai.com/form/enterprise-trusted-access-for-cyber/";
27
+ const DEEP_SCAN_SETTINGS = [
28
+ ["workers", "workers", 1],
29
+ ["subagents", "subagents", 0],
30
+ ["stopAfterNoNew", "stop_after_no_new", 1],
31
+ ["maxDiscoveryRuns", "max_discovery_runs", 1],
32
+ ];
23
33
  export class CodexSecurity {
24
34
  config;
25
35
  metadata = {
@@ -59,13 +69,17 @@ export class CodexSecurity {
59
69
  repository: inputs.repository,
60
70
  target: inputs.target,
61
71
  mode: inputs.mode,
72
+ ...deepScanOptions(options),
62
73
  ...(options.knowledgeBasePaths?.length
63
74
  ? { knowledgeBasePaths: options.knowledgeBasePaths }
64
75
  : {}),
65
76
  outputDir: inputs.outputDir,
66
77
  ...(archiveDir === null ? {} : { archiveDir }),
67
- authentication: scanAuthentication(this.#dependencies.environment, options.auth),
78
+ authentication: scanAuthentication(this.#dependencies.environment, options.auth, configuration["model_provider"]),
68
79
  ...model,
80
+ ...(typeof configuration["model_provider"] === "string"
81
+ ? { modelProvider: configuration["model_provider"] }
82
+ : {}),
69
83
  ...(options.maxCostUsd === undefined
70
84
  ? {}
71
85
  : { maxCostUsd: options.maxCostUsd }),
@@ -85,7 +99,9 @@ export class CodexSecurity {
85
99
  let knowledgeBase = null;
86
100
  let costTracker = null;
87
101
  let releaseCredentialHome = null;
102
+ let scanFailure = false;
88
103
  let completionCost = null;
104
+ let preparedTargetWarnings = [];
89
105
  let activeScan = null;
90
106
  const workbench = this.#dependencies.runWorkbench ?? runWorkbench;
91
107
  try {
@@ -112,22 +128,40 @@ export class CodexSecurity {
112
128
  knowledgeBase = await prepareKnowledgeBase(options.knowledgeBasePaths, signal);
113
129
  }
114
130
  checkOpen();
115
- const authentication = scanAuthentication(this.#dependencies.environment, options.auth);
116
- const scanEnvironment = selectedScanEnvironment(this.#dependencies.environment, options.auth);
131
+ const requestedConfig = await mergedCodexConfig(this.config);
132
+ const modelProvider = requestedConfig["model_provider"];
133
+ const externalProvider = isExternalModelProvider(modelProvider)
134
+ ? EXTERNAL_CODEX_PROVIDERS[modelProvider]
135
+ : null;
136
+ let authentication = scanAuthentication(this.#dependencies.environment, options.auth, modelProvider);
137
+ const apiKey = authentication.method === "api_key"
138
+ ? environmentApiKey(this.#dependencies.environment, modelProvider)
139
+ : null;
140
+ if (externalProvider !== null && apiKey === null) {
141
+ throw new AuthenticationRequiredError(`Set ${externalProvider.env_key} to run a scan through ${externalProvider.name}.`);
142
+ }
143
+ const scanEnvironment = selectedScanEnvironment(this.#dependencies.environment, options.auth, modelProvider);
117
144
  if (authentication.method === "stored_credentials" &&
118
145
  this.#dependencies.prepareRuntime === undefined) {
119
146
  const credentialHome = await prepareCodexSecurityCredentialHome(scanEnvironment, (path) => requireOutputOutsideRepository(protectedRoot, path, "runtime"));
120
147
  releaseCredentialHome = await acquireCodexSecurityCredentialHomeLock(credentialHome, signal);
121
148
  }
122
149
  const previousRuntime = this.#runtime;
123
- const runtime = await this.#ensureRuntime(signal, temporaryRoot, (path) => requireOutputOutsideRepository(protectedRoot, path, "runtime"), options.auth);
150
+ const runtime = await this.#ensureRuntime(signal, temporaryRoot, (path) => requireOutputOutsideRepository(protectedRoot, path, "runtime"), options.auth, modelProvider);
124
151
  if (runtime === previousRuntime &&
125
152
  runtime.persistentCredentialHome === true &&
126
153
  this.#dependencies.prepareRuntime === undefined) {
127
154
  await this.#refreshPersistentRuntime(runtime, scanEnvironment, signal);
128
155
  }
156
+ const effectiveConfig = runtime.effectiveConfig ?? requestedConfig;
157
+ if (runtime.configPath !== undefined) {
158
+ await writeCodexConfig(runtime.configPath, scanPreflightCodexConfig(effectiveConfig, repo));
159
+ }
129
160
  const runtimeHome = await realpath(runtime.codexHome);
130
161
  requireOutputOutsideRepository(protectedRoot, runtimeHome, "runtime");
162
+ if (mode === "deep") {
163
+ await prepareDeepScanConfig(runtimeHome, this.#dependencies.environment, options, signal);
164
+ }
131
165
  if (options.expectedPluginVersion !== undefined &&
132
166
  runtime.plugin.version !== options.expectedPluginVersion) {
133
167
  throw new CodexSecurityError(`The original scan used plugin version ${options.expectedPluginVersion}, but the installed version is ${runtime.plugin.version}.`);
@@ -142,10 +176,7 @@ export class CodexSecurity {
142
176
  ? "stored_credentials"
143
177
  : null;
144
178
  }
145
- const apiKey = authentication.method === "api_key"
146
- ? environmentApiKey(this.#dependencies.environment)
147
- : null;
148
- if (apiKey !== null) {
179
+ if (externalProvider === null && apiKey !== null) {
149
180
  this.#runtimeCredentialSource = "api_key";
150
181
  }
151
182
  if (!runtime.credentialsAvailable &&
@@ -161,6 +192,7 @@ export class CodexSecurity {
161
192
  "'codex-security login --device-auth' on a remote or headless machine, or set " +
162
193
  "OPENAI_API_KEY or CODEX_API_KEY for CI.");
163
194
  }
195
+ authentication = await runtimeScanAuthentication(this.#dependencies.environment, runtime.codexHome, options.auth, modelProvider);
164
196
  notifyObserver("onAuthentication", options.onAuthentication, options.onObserverError, authentication);
165
197
  const python = await (this.#dependencies.resolvePluginPython ?? resolvePluginPython)({
166
198
  configuredPath: this.config.pythonPath,
@@ -202,13 +234,28 @@ export class CodexSecurity {
202
234
  mode,
203
235
  pluginVersion: runtime.plugin.version,
204
236
  };
205
- const effectiveConfig = runtime.effectiveConfig ?? (await mergedCodexConfig(this.config));
206
237
  const { model } = scanModelConfiguration(effectiveConfig);
207
238
  validateScanCostLimit(options.maxCostUsd, model);
239
+ let scopeFileCount = null;
240
+ let reviewedFileCount = 0;
241
+ const reportProgress = (progress) => {
242
+ if (scopeFileCount === null ||
243
+ progress.filesTotal > scopeFileCount ||
244
+ progress.filesCompleted < reviewedFileCount) {
245
+ return;
246
+ }
247
+ reviewedFileCount = progress.filesCompleted;
248
+ notifyObserver("onProgress", options.onProgress, options.onObserverError, { ...progress, filesTotal: scopeFileCount });
249
+ };
208
250
  const tracker = new ScanCostTracker({
209
251
  codexHome: runtime.codexHome,
210
252
  model,
253
+ repository: repo,
211
254
  maxCostUsd: options.maxCostUsd,
255
+ onActivity: (activity) => {
256
+ notifyObserver("onActivity", options.onActivity, options.onObserverError, activity);
257
+ },
258
+ onProgress: reportProgress,
212
259
  onCost: (cost) => {
213
260
  notifyObserver("onCost", options.onCost, options.onObserverError, cost);
214
261
  if (options.maxCostUsd !== undefined &&
@@ -219,12 +266,12 @@ export class CodexSecurity {
219
266
  onError: (error) => costAbortController.abort(error),
220
267
  });
221
268
  costTracker = tracker;
222
- const recipe = scanRecipe(repo, normalized, mode, expectation.repositoryRevision, runtime.plugin.version, effectiveConfig, options.failureSeverity, knowledgeBase?.sources, options.maxCostUsd);
269
+ const recipe = scanRecipe(repo, protectedRoot, normalized, mode, expectation.repositoryRevision, runtime.plugin.version, effectiveConfig, options.failureSeverity, knowledgeBase?.sources, options.maxCostUsd, deepScanOptions(options));
223
270
  const workbenchOptions = {
224
271
  python,
225
272
  pluginRoot: runtime.plugin.pluginRoot,
226
273
  environment: {
227
- ...selectedScanEnvironment(runtime.environment, options.auth),
274
+ ...selectedScanEnvironment(runtime.environment, options.auth, modelProvider),
228
275
  CODEX_SECURITY_STATE_DIR: stateDirectory,
229
276
  },
230
277
  signal,
@@ -285,6 +332,21 @@ export class CodexSecurity {
285
332
  throw new CodexSecurityError("The Codex Security workbench returned an invalid scan registration.");
286
333
  }
287
334
  const targetRevision = registeredRevision === "unversioned" ? null : registeredRevision;
335
+ const registeredFileCount = registration["scopeFileCount"];
336
+ scopeFileCount =
337
+ typeof registeredFileCount === "number" &&
338
+ Number.isSafeInteger(registeredFileCount) &&
339
+ registeredFileCount >= 0
340
+ ? registeredFileCount
341
+ : null;
342
+ if (scopeFileCount !== null) {
343
+ tracker.setExpectedFilesTotal(scopeFileCount);
344
+ notifyObserver("onProgress", options.onProgress, options.onObserverError, {
345
+ phase: "preflight",
346
+ filesCompleted: 0,
347
+ filesTotal: scopeFileCount,
348
+ });
349
+ }
288
350
  activeScan = { id: scanId, options: workbenchOptions };
289
351
  checkOpen();
290
352
  const feedback = await workbench({
@@ -346,12 +408,15 @@ export class CodexSecurity {
346
408
  : { CODEX_SECURITY_TARGET_PATHS_FILE: targetPathsFile }),
347
409
  };
348
410
  const environment = {
349
- ...pluginExecutionEnvironment(python, withoutCodexHome(selectedScanEnvironment(runtime.environment, options.auth))),
411
+ ...pluginExecutionEnvironment(python, withoutCodexHome(selectedScanEnvironment(runtime.environment, options.auth, modelProvider))),
412
+ ...(externalProvider === null
413
+ ? {}
414
+ : { [externalProvider.env_key]: apiKey }),
350
415
  CODEX_HOME: runtime.codexHome,
351
416
  ...runtimePaths,
352
417
  };
353
418
  const codex = this.#dependencies.createCodex({
354
- ...(apiKey === null ? {} : { apiKey }),
419
+ ...(externalProvider !== null || apiKey === null ? {} : { apiKey }),
355
420
  env: definedEnvironment(selectedScanEnvironment(environment, "chatgpt")),
356
421
  config: {
357
422
  default_permissions: SCAN_PERMISSION_PROFILE,
@@ -390,6 +455,7 @@ export class CodexSecurity {
390
455
  scanDir,
391
456
  pluginRoot: runtime.plugin.installedRoot,
392
457
  expectation,
458
+ authentication,
393
459
  workbenchValidated: true,
394
460
  model,
395
461
  onThreadStarted: (threadId) => tracker.start(threadId),
@@ -400,16 +466,32 @@ export class CodexSecurity {
400
466
  notifyObserver("onWarning", options.onWarning, options.onObserverError, "Scan completed, but its cost limit could not be verified because model pricing or token usage is unavailable.");
401
467
  }
402
468
  completionCost = snapshot.cost;
403
- await workbench(workbenchOptions, [
469
+ const preparation = await workbench(workbenchOptions, [
404
470
  "prepare-scan-completion",
405
471
  "--scan-id",
406
472
  scanId,
407
473
  ]);
474
+ preparedTargetWarnings = Array.isArray(preparation["targetWarnings"])
475
+ ? preparation["targetWarnings"].filter((warning) => typeof warning === "string")
476
+ : [];
408
477
  return snapshot.usage;
409
478
  },
410
479
  onScanStarted: options.onScanStarted,
480
+ onTrustedAccessStatus: options.onTrustedAccessStatus,
411
481
  onReconnect: options.onReconnect,
482
+ onActivity: options.onActivity,
483
+ onProgress: (progress) => {
484
+ if (progress.phase === "discovery" &&
485
+ progress.filesCompleted === 0 &&
486
+ reviewedFileCount === 0 &&
487
+ progress.filesTotal !== scopeFileCount) {
488
+ scopeFileCount = progress.filesTotal;
489
+ tracker.setExpectedFilesTotal(scopeFileCount);
490
+ }
491
+ reportProgress(progress);
492
+ },
412
493
  onWorkerStatus: options.onWorkerStatus,
494
+ onWarning: options.onWarning,
413
495
  onObserverError: options.onObserverError,
414
496
  });
415
497
  checkOpen();
@@ -424,15 +506,26 @@ export class CodexSecurity {
424
506
  activeScan = null;
425
507
  const completedScan = completion["scan"];
426
508
  if (isRecord(completedScan) && Array.isArray(completedScan["warnings"])) {
509
+ const targetWarnings = new Set([
510
+ ...preparedTargetWarnings,
511
+ ...(Array.isArray(completion["targetWarnings"])
512
+ ? completion["targetWarnings"].filter((warning) => typeof warning === "string")
513
+ : []),
514
+ ]);
427
515
  for (const warning of completedScan["warnings"]) {
428
516
  if (typeof warning === "string") {
429
- notifyObserver("onWarning", options.onWarning, options.onObserverError, warning);
517
+ notifyObserver("onWarning", options.onWarning, options.onObserverError, warning, targetWarnings.has(warning)
518
+ ? { kind: "target_changed" }
519
+ : undefined);
430
520
  }
431
521
  }
432
522
  }
433
523
  return result;
434
524
  }
435
525
  catch (error) {
526
+ // Recorded first: everything below can throw a different error for this same failed
527
+ // scan, and cleanup must treat all of those as a failure it is not allowed to mask.
528
+ scanFailure = true;
436
529
  const snapshot = await costTracker?.stop().catch(() => null);
437
530
  const failure = signal.reason instanceof ScanCostLimitExceededError
438
531
  ? signal.reason
@@ -443,10 +536,10 @@ export class CodexSecurity {
443
536
  "fail-scan",
444
537
  "--scan-id",
445
538
  activeScan.id,
539
+ // Redact before truncating: the stored message is read back by
540
+ // `scans show` and travels inside the results directory.
446
541
  "--message",
447
- (failure instanceof Error
448
- ? failure.message
449
- : String(failure)).slice(0, 2400),
542
+ redactedErrorMessage(failure).slice(0, 2400),
450
543
  ...(snapshot?.cost
451
544
  ? ["--cost-json", JSON.stringify(snapshot.cost)]
452
545
  : []),
@@ -462,14 +555,41 @@ export class CodexSecurity {
462
555
  throw failure;
463
556
  }
464
557
  finally {
558
+ // Removing the temporary scan inputs is best effort. A throw here would replace the
559
+ // outcome the try and catch blocks already produced, so these failures are reported
560
+ // as warnings: a scan that failed has to say why it failed, not why its temporary
561
+ // files outlived it. The whole step is guarded so that a cleanup which rejects, or
562
+ // throws synchronously, still cannot skip the credential lock release below.
465
563
  try {
466
- await Promise.all([
564
+ for (const cleanup of await Promise.allSettled([
467
565
  knowledgeBase?.cleanup(),
468
566
  removeTargetPathsFile(targetPathsFile),
469
- ]);
567
+ ])) {
568
+ if (cleanup.status === "rejected") {
569
+ warnCleanupFailed(options, cleanup.reason);
570
+ }
571
+ }
572
+ }
573
+ catch (error) {
574
+ warnCleanupFailed(options, error);
470
575
  }
471
576
  finally {
472
- await releaseCredentialHome?.();
577
+ // Releasing the credential home lock is not best effort, so it keeps its own
578
+ // finally and runs even if reporting the failures above went wrong. The release
579
+ // only marks itself done once the lock directory is gone, so a failure leaves an
580
+ // owner.json naming this still-running process; recoverStaleCredentialHomeLock
581
+ // then refuses to reclaim it because that pid is alive, and later scans in this
582
+ // process wait on a lock nothing frees. Reporting success while leaving the client
583
+ // in that state is worse than failing, so the failure is only downgraded to a
584
+ // warning when the scan already failed and that error is the one worth keeping.
585
+ try {
586
+ await releaseCredentialHome?.();
587
+ }
588
+ catch (error) {
589
+ if (!scanFailure)
590
+ throw error;
591
+ warnCleanupFailed(options, error);
592
+ }
473
593
  }
474
594
  }
475
595
  }
@@ -598,11 +718,11 @@ export class CodexSecurity {
598
718
  }
599
719
  }
600
720
  }
601
- async #ensureRuntime(signal, temporaryRoot, validateLocation, auth = "auto") {
721
+ async #ensureRuntime(signal, temporaryRoot, validateLocation, auth = "auto", modelProvider) {
602
722
  this.#requireOpen();
603
723
  if (this.#runtime !== null) {
604
- const usePersistentCredentials = scanAuthentication(this.#dependencies.environment, auth).method ===
605
- "stored_credentials";
724
+ const usePersistentCredentials = scanAuthentication(this.#dependencies.environment, auth, modelProvider)
725
+ .method === "stored_credentials";
606
726
  if (this.#dependencies.prepareRuntime !== undefined ||
607
727
  this.#runtime.persistentCredentialHome === undefined ||
608
728
  this.#runtime.persistentCredentialHome === usePersistentCredentials) {
@@ -615,7 +735,7 @@ export class CodexSecurity {
615
735
  this.#requireOpen();
616
736
  }
617
737
  if (this.#runtimePromise === null) {
618
- const runtimePromise = this.#prepareRuntime(signal ?? this.#abortController.signal, temporaryRoot, validateLocation, auth);
738
+ const runtimePromise = this.#prepareRuntime(signal ?? this.#abortController.signal, temporaryRoot, validateLocation, auth, modelProvider);
619
739
  this.#runtimePromise = runtimePromise;
620
740
  void runtimePromise.catch(() => {
621
741
  if (this.#runtimePromise === runtimePromise) {
@@ -654,6 +774,7 @@ export class CodexSecurity {
654
774
  runtime.effectiveConfig = mergedConfig;
655
775
  }
656
776
  async #validateLocalInputs(repository, options, signal) {
777
+ deepScanOptions(options);
657
778
  if (options.maxCostUsd !== undefined &&
658
779
  (!Number.isFinite(options.maxCostUsd) || options.maxCostUsd <= 0)) {
659
780
  throw new CodexSecurityError("The scan cost limit must be a positive USD amount.");
@@ -680,13 +801,13 @@ export class CodexSecurity {
680
801
  protectedRoot,
681
802
  };
682
803
  }
683
- async #prepareRuntime(signal, temporaryRoot, validateLocation, auth = "auto") {
804
+ async #prepareRuntime(signal, temporaryRoot, validateLocation, auth = "auto", modelProvider) {
684
805
  if (this.#dependencies.prepareRuntime !== undefined) {
685
806
  return await this.#dependencies.prepareRuntime(this.config, signal);
686
807
  }
687
- const processEnvironment = selectedScanEnvironment(this.#dependencies.environment, auth);
688
- const persistentCredentialHome = scanAuthentication(this.#dependencies.environment, auth).method ===
689
- "stored_credentials";
808
+ const processEnvironment = selectedScanEnvironment(this.#dependencies.environment, auth, modelProvider);
809
+ const persistentCredentialHome = scanAuthentication(this.#dependencies.environment, auth, modelProvider)
810
+ .method === "stored_credentials";
690
811
  const codexHome = persistentCredentialHome
691
812
  ? await prepareCodexSecurityCredentialHome(processEnvironment, validateLocation)
692
813
  : await createIsolatedHome(temporaryRoot, validateLocation);
@@ -708,7 +829,9 @@ export class CodexSecurity {
708
829
  environment: withoutCodexHome(processEnvironment),
709
830
  signal,
710
831
  });
711
- const credentialsAvailable = await initialCredentialsAvailable(processEnvironment, ambientHome, codexHome);
832
+ const credentialsAvailable = isExternalModelProvider(modelProvider)
833
+ ? false
834
+ : await initialCredentialsAvailable(processEnvironment, ambientHome, codexHome);
712
835
  return {
713
836
  codexHome,
714
837
  persistentCredentialHome,
@@ -740,6 +863,60 @@ export class CodexSecurity {
740
863
  throw new CodexSecurityError("CodexSecurity is closed.");
741
864
  }
742
865
  }
866
+ function deepScanOptions(options) {
867
+ const selected = {};
868
+ for (const [name, , minimum] of DEEP_SCAN_SETTINGS) {
869
+ const value = options[name];
870
+ if (value === undefined)
871
+ continue;
872
+ if ((options.mode ?? "standard") !== "deep") {
873
+ throw new CodexSecurityError("Deep scan settings require deep mode.");
874
+ }
875
+ if (!Number.isSafeInteger(value) || value < minimum) {
876
+ throw new CodexSecurityError(`Deep scan ${name} must be ${minimum === 0 ? "a non-negative" : "a positive"} integer.`);
877
+ }
878
+ selected[name] = value;
879
+ }
880
+ return selected;
881
+ }
882
+ async function prepareDeepScanConfig(codexHome, environment, options, signal) {
883
+ const ambientHome = environmentValue(environment, "CODEX_HOME") ?? join(homedir(), ".codex");
884
+ const source = join(ambientHome, "codex-security", "config.toml");
885
+ let configured = {};
886
+ try {
887
+ configured = parseToml(await readFile(source, { encoding: "utf8", signal }));
888
+ }
889
+ catch (error) {
890
+ if (!isRecord(error) || error["code"] !== "ENOENT") {
891
+ throw new CodexSecurityError(`Cannot read Codex Security configuration at ${source}.`, { cause: error });
892
+ }
893
+ }
894
+ const existing = configured["deep_scan"];
895
+ if (existing !== undefined && !isRecord(existing)) {
896
+ throw new CodexSecurityError(`Codex Security configuration [deep_scan] at ${source} must be a TOML table.`);
897
+ }
898
+ const overrides = {};
899
+ for (const [name, key] of DEEP_SCAN_SETTINGS) {
900
+ const value = options[name];
901
+ if (value !== undefined)
902
+ overrides[key] = value;
903
+ }
904
+ const destination = join(codexHome, "codex-security", "config.toml");
905
+ const hasOverrides = Object.keys(overrides).length > 0;
906
+ if (existing === undefined && !hasOverrides) {
907
+ if (destination !== source) {
908
+ await rm(destination, { force: true });
909
+ }
910
+ return;
911
+ }
912
+ if (destination === source && !hasOverrides)
913
+ return;
914
+ await mkdir(dirname(destination), { recursive: true, mode: 0o700 });
915
+ await writeFile(destination, stringifyToml({
916
+ ...configured,
917
+ deep_scan: { ...existing, ...overrides },
918
+ }), { mode: 0o600, signal });
919
+ }
743
920
  export async function initialCredentialsAvailable(environment, ambientHome, isolatedHome, importer = importAmbientAuth) {
744
921
  if (environmentApiKey(environment) !== null)
745
922
  return false;
@@ -750,6 +927,20 @@ export async function initialCredentialsAvailable(environment, ambientHome, isol
750
927
  return true;
751
928
  return await importer(ambientHome, isolatedHome);
752
929
  }
930
+ // Reports a cleanup failure without letting it decide the result of the scan. Only the
931
+ // message is forwarded, and it reaches the onWarning observer alone: unlike the fail-scan
932
+ // path it is never written to the workbench, so it adds no persisted, unredacted text.
933
+ function warnCleanupFailed(options, reason) {
934
+ // This runs where a throw would replace the scan result, so every step is inside the
935
+ // guard: reading the reason, coercing it, and reading the observers off the options can
936
+ // each throw for a sufficiently hostile value, and none of them may become the outcome
937
+ // of the scan. Losing a warning is the correct trade against losing the result.
938
+ try {
939
+ const message = String(reason instanceof Error ? reason.message : reason);
940
+ notifyObserver("onWarning", options.onWarning, options.onObserverError, `Could not clean up after the Codex Security scan: ${message}`);
941
+ }
942
+ catch { }
943
+ }
753
944
  async function removeTargetPathsFile(path) {
754
945
  if (path === null)
755
946
  return;
@@ -770,8 +961,29 @@ export async function runScanEvents(options) {
770
961
  let finalResponse = "";
771
962
  let usage = null;
772
963
  let lastStreamError = null;
964
+ let tacStatusReported = false;
773
965
  try {
774
966
  for await (const event of options.events) {
967
+ if (!tacStatusReported) {
968
+ const tacStatus = trustedAccessStatusFromEvent(event);
969
+ if (tacStatus !== null) {
970
+ tacStatusReported = true;
971
+ notifyObserver("onTrustedAccessStatus", options.onTrustedAccessStatus, options.onObserverError, tacStatus);
972
+ if (tacStatus !== "granted") {
973
+ notifyObserver("onWarning", options.onWarning, options.onObserverError, trustedAccessWarning(tacStatus, options.authentication));
974
+ }
975
+ }
976
+ }
977
+ for (const activity of scanActivitiesFromEvent(event, options.expectation.repository)) {
978
+ notifyObserver("onActivity", options.onActivity, options.onObserverError, activity);
979
+ }
980
+ for (const progress of scanProgressUpdatesFromEvent(event)) {
981
+ if (options.expectedFilesTotal !== undefined &&
982
+ progress.filesTotal !== options.expectedFilesTotal) {
983
+ continue;
984
+ }
985
+ notifyObserver("onProgress", options.onProgress, options.onObserverError, progress);
986
+ }
775
987
  const workerStatus = workerStatusFromEvent(event);
776
988
  if (workerStatus !== null) {
777
989
  notifyObserver("onWorkerStatus", options.onWorkerStatus, options.onObserverError, workerStatus);
@@ -797,10 +1009,8 @@ export async function runScanEvents(options) {
797
1009
  status = "completed";
798
1010
  usage = event["usage"];
799
1011
  }
800
- else if (event.type === "turn.failed" &&
801
- isRecord(event["error"]) &&
802
- typeof event["error"]["message"] === "string") {
803
- throw new CodexSecurityError(event["error"]["message"]);
1012
+ else if (event.type === "turn.failed") {
1013
+ throw new CodexSecurityError(turnFailureMessage(event["error"]));
804
1014
  }
805
1015
  else if (event.type === "error" &&
806
1016
  typeof event["message"] === "string") {
@@ -851,6 +1061,68 @@ export async function runScanEvents(options) {
851
1061
  throw error;
852
1062
  }
853
1063
  }
1064
+ function trustedAccessStatusFromEvent(event) {
1065
+ if (event.type !== "item.completed" || !isRecord(event["item"])) {
1066
+ return null;
1067
+ }
1068
+ const item = event["item"];
1069
+ if (item["type"] !== "mcp_tool_call" ||
1070
+ item["server"] !== "codex_apps" ||
1071
+ item["tool"] !== "get_tac_status") {
1072
+ return null;
1073
+ }
1074
+ if (item["status"] !== "completed" || !isRecord(item["result"])) {
1075
+ return "unknown";
1076
+ }
1077
+ const result = item["result"]["structured_content"];
1078
+ if (!isRecord(result) ||
1079
+ result["schemaVersion"] !== 1 ||
1080
+ !Array.isArray(result["grants"]) ||
1081
+ typeof result["checkedAt"] !== "string" ||
1082
+ Number.isNaN(Date.parse(result["checkedAt"])) ||
1083
+ result["stale"] !== false) {
1084
+ return "unknown";
1085
+ }
1086
+ const status = result["status"];
1087
+ if (status !== "granted" &&
1088
+ status !== "not_granted" &&
1089
+ status !== "unknown") {
1090
+ return "unknown";
1091
+ }
1092
+ if (result["grants"].some((grant) => !isTrustedAccessGrant(grant)) ||
1093
+ (status === "granted") !== result["grants"].length > 0) {
1094
+ return "unknown";
1095
+ }
1096
+ return status;
1097
+ }
1098
+ function isTrustedAccessGrant(grant) {
1099
+ if (!isRecord(grant))
1100
+ return false;
1101
+ const level = grant["level"];
1102
+ const source = grant["source"];
1103
+ return ((source === "user" && (level === "tac1" || level === "tac2")) ||
1104
+ (source === "current_account" &&
1105
+ (level === "tac1" || level === "tac3" || level === "government")));
1106
+ }
1107
+ function trustedAccessWarning(status, authentication) {
1108
+ const apiOrganization = (authentication?.method === "api_key" &&
1109
+ (authentication.source === "OPENAI_API_KEY" ||
1110
+ authentication.source === "CODEX_API_KEY")) ||
1111
+ (authentication?.method === "stored_credentials" &&
1112
+ authentication.credentialType === "api_key");
1113
+ const applicationUrl = apiOrganization
1114
+ ? ORGANIZATIONAL_TRUSTED_ACCESS_URL
1115
+ : PERSONAL_TRUSTED_ACCESS_URL;
1116
+ if (status === "not_granted") {
1117
+ const account = apiOrganization ? "your API organization" : "your account";
1118
+ return `Some cybersecurity requests or findings may be refused because ${account} does not have Trusted Access for Cyber. Apply at ${applicationUrl}.`;
1119
+ }
1120
+ const access = apiOrganization
1121
+ ? "Trusted Access for Cyber for your API organization"
1122
+ : "your Trusted Access for Cyber status";
1123
+ const action = apiOrganization ? "your organization's access" : "your access";
1124
+ return `Some cybersecurity requests or findings may be refused because ${access} could not be verified. Check ${action} or apply at ${applicationUrl}.`;
1125
+ }
854
1126
  async function scanPrompt(pluginRoot, target, mode, hasConfigPath = false, hasKnowledgeBase = false) {
855
1127
  const skillName = skillNameFor(target, mode);
856
1128
  const skillPath = join(pluginRoot, "skills", skillName, "SKILL.md");
@@ -861,6 +1133,11 @@ async function scanPrompt(pluginRoot, target, mode, hasConfigPath = false, hasKn
861
1133
  return [
862
1134
  `Use the installed $codex-security:${skillName} skill at "$CODEX_SECURITY_PLUGIN_ROOT/skills/${skillName}/SKILL.md".`,
863
1135
  "Run this Codex Security scan non-interactively.",
1136
+ ...(mode === "deep"
1137
+ ? [
1138
+ 'The SDK has already registered this scan. Call start_codex_security_deep_scan with { scanId: "$CODEX_SECURITY_SCAN_ID" }; never pass targetPath or create another scan.',
1139
+ ]
1140
+ : []),
864
1141
  ...(skillName === "deep-security-scan"
865
1142
  ? []
866
1143
  : [
@@ -877,6 +1154,8 @@ async function scanPrompt(pluginRoot, target, mode, hasConfigPath = false, hasKn
877
1154
  'When "$CODEX_SECURITY_TARGET_REVISION" is set, use its exact value as scan.target.revision.',
878
1155
  'When "$CODEX_SECURITY_TARGET_SNAPSHOT_DIGEST" is set, use its exact value as scan.target.snapshotDigest. For git_revision, omit scan.target.snapshotDigest.',
879
1156
  'Use exactly "codex-security-plugin" as scan.producer.name.',
1157
+ 'After the file inventory, after each fully reviewed file batch, and when entering each later phase, emit one standalone CODEX_SECURITY_SCAN_PROGRESS {"phase":"discovery","filesCompleted":3,"filesTotal":8} line in a completed command output or agent message. Use the actual phase and file counts. Never count unread or partially reviewed files.',
1158
+ 'Every delegated review assignment must say: After each completed batch, emit CODEX_SECURITY_SCAN_PROGRESS {"phase":"discovery","filesCompleted":3,"filesTotal":8} on its own line using your worker-local reviewed and assigned file counts.',
880
1159
  ...(hasConfigPath
881
1160
  ? [
882
1161
  'For normal config-preflight helper calls, append --config "$CODEX_SECURITY_CONFIG_PATH" so preflight reads the sanitized active runtime config. Preserve the documented runtime and --effective-config arguments for session-only values.',
@@ -913,7 +1192,7 @@ function targetInstruction(target) {
913
1192
  }
914
1193
  return `Scan target: staged and unstaged working-tree changes against ${target.base}.`;
915
1194
  }
916
- function scanRecipe(repository, target, mode, repositoryRevision, pluginVersion, effectiveConfig, failOnSeverity, knowledgeBasePaths, maxCostUsd) {
1195
+ function scanRecipe(repository, activeProjectPath, target, mode, repositoryRevision, pluginVersion, effectiveConfig, failOnSeverity, knowledgeBasePaths, maxCostUsd, deepScan) {
917
1196
  return {
918
1197
  repository,
919
1198
  target: {
@@ -927,10 +1206,13 @@ function scanRecipe(repository, target, mode, repositoryRevision, pluginVersion,
927
1206
  mode,
928
1207
  ...(repositoryRevision === null ? {} : { repositoryRevision }),
929
1208
  pluginVersion,
930
- config: scanPreflightCodexConfig(effectiveConfig),
1209
+ config: scanPreflightCodexConfig(effectiveConfig, activeProjectPath),
931
1210
  ...(failOnSeverity === undefined ? {} : { failOnSeverity }),
932
1211
  ...(knowledgeBasePaths === undefined ? {} : { knowledgeBasePaths }),
933
1212
  ...(maxCostUsd === undefined ? {} : { maxCostUsd }),
1213
+ ...(deepScan === undefined || Object.keys(deepScan).length === 0
1214
+ ? {}
1215
+ : { deepScan: { ...deepScan } }),
934
1216
  };
935
1217
  }
936
1218
  function validateScanCostLimit(maxCostUsd, model) {
@@ -985,12 +1267,14 @@ async function collectResult(turnResult, threadId, scanDir, pluginRoot, expectat
985
1267
  sarifPath,
986
1268
  });
987
1269
  }
988
- export function scanAuthentication(environment, auth = "auto") {
989
- if (auth === "chatgpt") {
1270
+ export function scanAuthentication(environment, auth = "auto", modelProvider) {
1271
+ if (auth === "chatgpt" && !isExternalModelProvider(modelProvider)) {
990
1272
  return { method: "stored_credentials", verified: false };
991
1273
  }
992
- const key = environmentApiKeyEntry(environment);
993
- if (auth === "api-key" && key === null) {
1274
+ const key = environmentApiKeyEntry(environment, modelProvider);
1275
+ if (auth === "api-key" &&
1276
+ key === null &&
1277
+ !isExternalModelProvider(modelProvider)) {
994
1278
  throw new AuthenticationRequiredError("API-key authentication requires OPENAI_API_KEY or CODEX_API_KEY. " +
995
1279
  "Set a valid API key or use '--auth chatgpt'.");
996
1280
  }
@@ -998,11 +1282,44 @@ export function scanAuthentication(environment, auth = "auto") {
998
1282
  ? { method: "stored_credentials", verified: false }
999
1283
  : { method: "api_key", source: key.source, verified: false };
1000
1284
  }
1001
- function selectedScanEnvironment(environment, auth = "auto") {
1002
- if (auth !== "chatgpt")
1285
+ async function runtimeScanAuthentication(environment, codexHome, auth = "auto", modelProvider) {
1286
+ const authentication = scanAuthentication(environment, auth, modelProvider);
1287
+ if (authentication.method !== "stored_credentials")
1288
+ return authentication;
1289
+ try {
1290
+ const stored = JSON.parse(await readFile(join(codexHome, "auth.json"), "utf8"));
1291
+ if (!isRecord(stored))
1292
+ return authentication;
1293
+ const mode = stored["auth_mode"];
1294
+ if (mode === "apikey" || mode === "api_key") {
1295
+ return { ...authentication, credentialType: "api_key" };
1296
+ }
1297
+ if (mode === "chatgpt") {
1298
+ return { ...authentication, credentialType: "chatgpt" };
1299
+ }
1300
+ }
1301
+ catch {
1302
+ return authentication;
1303
+ }
1304
+ return authentication;
1305
+ }
1306
+ function selectedScanEnvironment(environment, auth = "auto", modelProvider) {
1307
+ const selectedProviderKey = isExternalModelProvider(modelProvider)
1308
+ ? EXTERNAL_CODEX_PROVIDERS[modelProvider].env_key
1309
+ : null;
1310
+ if (auth !== "chatgpt" && selectedProviderKey === null)
1003
1311
  return environment;
1004
- return Object.fromEntries(Object.entries(environment).filter(([name]) => name.toUpperCase() !== "OPENAI_API_KEY" &&
1005
- name.toUpperCase() !== "CODEX_API_KEY"));
1312
+ return Object.fromEntries(Object.entries(environment).filter(([name]) => {
1313
+ const key = name.toUpperCase();
1314
+ if (key === "OPENAI_API_KEY" || key === "CODEX_API_KEY")
1315
+ return false;
1316
+ if (selectedProviderKey === null)
1317
+ return true;
1318
+ if (key === "OPENROUTER_API_KEY" || key === "FIREWORKS_API_KEY") {
1319
+ return key === selectedProviderKey;
1320
+ }
1321
+ return true;
1322
+ }));
1006
1323
  }
1007
1324
  function notifyObserver(observerName, observer, onObserverError, ...args) {
1008
1325
  void Promise.resolve()
@@ -1010,18 +1327,17 @@ function notifyObserver(observerName, observer, onObserverError, ...args) {
1010
1327
  .catch((error) => onObserverError?.(observerName, error))
1011
1328
  .catch(() => { });
1012
1329
  }
1013
- function environmentApiKey(environment) {
1014
- return environmentApiKeyEntry(environment)?.value ?? null;
1330
+ function environmentApiKey(environment, modelProvider) {
1331
+ return environmentApiKeyEntry(environment, modelProvider)?.value ?? null;
1015
1332
  }
1016
- function environmentApiKeyEntry(environment) {
1017
- for (const requested of ["OPENAI_API_KEY", "CODEX_API_KEY"]) {
1018
- const canonical = environment[requested]?.trim();
1019
- if (canonical)
1020
- return { source: requested, value: canonical };
1021
- for (const [name, value] of Object.entries(environment)) {
1022
- if (name.toUpperCase() === requested && value?.trim())
1023
- return { source: requested, value: value.trim() };
1024
- }
1333
+ function environmentApiKeyEntry(environment, modelProvider) {
1334
+ const keys = isExternalModelProvider(modelProvider)
1335
+ ? [EXTERNAL_CODEX_PROVIDERS[modelProvider].env_key]
1336
+ : ["OPENAI_API_KEY", "CODEX_API_KEY"];
1337
+ for (const requested of keys) {
1338
+ const value = environmentValue(environment, requested)?.trim();
1339
+ if (value)
1340
+ return { source: requested, value };
1025
1341
  }
1026
1342
  return null;
1027
1343
  }
@@ -1058,6 +1374,21 @@ function reconnectDetails(message) {
1058
1374
  : {}),
1059
1375
  };
1060
1376
  }
1377
+ // A failed turn must fail the scan whatever its error payload looks like.
1378
+ //
1379
+ // Only `error.message` is reused, because that is the single shape the previous
1380
+ // code already surfaced. No other shape is forwarded or stringified: this message
1381
+ // reaches `fail-scan --message` and is stored in `scans.failure_message` without
1382
+ // redaction, so widening what is copied out of the payload would add a new
1383
+ // credential-disclosure path to persistent scan history.
1384
+ function turnFailureMessage(error) {
1385
+ if (isRecord(error) && typeof error["message"] === "string") {
1386
+ const message = error["message"].trim();
1387
+ if (message.length > 0)
1388
+ return error["message"];
1389
+ }
1390
+ return "The Codex Security scan turn failed without a readable error message.";
1391
+ }
1061
1392
  export function classifyConnectionFailure(error) {
1062
1393
  const message = error instanceof Error ? error.message : String(error);
1063
1394
  if (/\b(?:sqlite3?|database|workbench)\b/iu.test(message)) {
@@ -1104,7 +1435,7 @@ export function scanRuntimeCodexConfig(config, stateDirectory, protectedCredenti
1104
1435
  },
1105
1436
  };
1106
1437
  }
1107
- export function scanPreflightCodexConfig(config) {
1438
+ export function scanPreflightCodexConfig(config, activeProjectPath) {
1108
1439
  const safeString = (value, maxLength) => typeof value === "string" &&
1109
1440
  value.length > 0 &&
1110
1441
  value.length <= maxLength &&
@@ -1176,19 +1507,43 @@ export function scanPreflightCodexConfig(config) {
1176
1507
  }
1177
1508
  return result;
1178
1509
  };
1510
+ const prioritizedEntries = (value, priority) => {
1511
+ const entries = Object.entries(value);
1512
+ if (priority === undefined || !Object.hasOwn(value, priority)) {
1513
+ return entries;
1514
+ }
1515
+ return [
1516
+ [priority, value[priority]],
1517
+ ...entries.filter(([key]) => key !== priority),
1518
+ ];
1519
+ };
1179
1520
  const result = executionConfig(config);
1180
- if (safeProfileName(config["profile"])) {
1181
- result["profile"] = config["profile"];
1521
+ const modelProvider = result["model_provider"];
1522
+ if (isExternalModelProvider(modelProvider)) {
1523
+ result["model_providers"] = {
1524
+ [modelProvider]: { ...EXTERNAL_CODEX_PROVIDERS[modelProvider] },
1525
+ };
1526
+ }
1527
+ const selectedProfile = safeProfileName(config["profile"])
1528
+ ? config["profile"]
1529
+ : undefined;
1530
+ if (selectedProfile !== undefined) {
1531
+ result["profile"] = selectedProfile;
1182
1532
  }
1183
1533
  const profiles = config["profiles"];
1184
1534
  if (isRecord(profiles)) {
1185
1535
  const sanitized = {};
1186
- for (const [name, profile] of Object.entries(profiles).slice(0, 256)) {
1536
+ let accepted = 0;
1537
+ for (const [name, profile] of prioritizedEntries(profiles, selectedProfile)) {
1187
1538
  if (!safeProfileName(name) || !isRecord(profile))
1188
1539
  continue;
1189
1540
  const projected = executionConfig(profile);
1190
- if (Object.keys(projected).length > 0)
1191
- sanitized[name] = projected;
1541
+ if (Object.keys(projected).length === 0)
1542
+ continue;
1543
+ sanitized[name] = projected;
1544
+ accepted += 1;
1545
+ if (accepted === 256)
1546
+ break;
1192
1547
  }
1193
1548
  if (Object.keys(sanitized).length > 0)
1194
1549
  result["profiles"] = sanitized;
@@ -1202,7 +1557,21 @@ export function scanPreflightCodexConfig(config) {
1202
1557
  const projects = config["projects"];
1203
1558
  if (isRecord(projects)) {
1204
1559
  const sanitized = {};
1205
- for (const [path, project] of Object.entries(projects).slice(0, 256)) {
1560
+ let accepted = 0;
1561
+ const activeProjectRoot = activeProjectPath === undefined
1562
+ ? undefined
1563
+ : Object.keys(projects)
1564
+ .filter((path) => {
1565
+ if (!safeString(path, 4096) || !isAbsolute(path))
1566
+ return false;
1567
+ const remaining = relative(path, activeProjectPath);
1568
+ return (remaining === "" ||
1569
+ (remaining !== ".." &&
1570
+ !remaining.startsWith(`..${sep}`) &&
1571
+ !isAbsolute(remaining)));
1572
+ })
1573
+ .sort((left, right) => right.length - left.length)[0];
1574
+ for (const [path, project] of prioritizedEntries(projects, activeProjectRoot ?? activeProjectPath)) {
1206
1575
  if (!safeString(path, 4096) || !isAbsolute(path) || !isRecord(project)) {
1207
1576
  continue;
1208
1577
  }
@@ -1210,6 +1579,9 @@ export function scanPreflightCodexConfig(config) {
1210
1579
  if (trust !== "trusted" && trust !== "untrusted")
1211
1580
  continue;
1212
1581
  sanitized[path] = { trust_level: trust };
1582
+ accepted += 1;
1583
+ if (accepted === 256)
1584
+ break;
1213
1585
  }
1214
1586
  if (Object.keys(sanitized).length > 0)
1215
1587
  result["projects"] = sanitized;