@openai/codex-security 0.1.27 → 0.1.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/README.md +250 -55
  2. package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
  3. package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
  4. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  5. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  6. package/_bundled_plugin/references/artifact-storage.md +56 -0
  7. package/_bundled_plugin/references/config-preflight.md +7 -1
  8. package/_bundled_plugin/references/final-report.md +2 -0
  9. package/_bundled_plugin/references/scan-artifacts.md +6 -14
  10. package/_bundled_plugin/references/scan-prologue.md +2 -0
  11. package/_bundled_plugin/references/security-guidance.md +2 -0
  12. package/_bundled_plugin/scripts/deep_scan_config.py +10 -8
  13. package/_bundled_plugin/scripts/deep_scan_defaults.json +8 -0
  14. package/_bundled_plugin/scripts/reserved_artifact_paths.json +6 -0
  15. package/_bundled_plugin/scripts/windows_scan_local_files.py +1 -3
  16. package/_bundled_plugin/scripts/workbench/storage.py +18 -0
  17. package/_bundled_plugin/scripts/workbench_cli.py +13 -0
  18. package/_bundled_plugin/scripts/workbench_db.py +13 -13
  19. package/_bundled_plugin/scripts/workbench_saved_results.py +72 -1
  20. package/_bundled_plugin/scripts/workbench_scan_usage.py +23 -10
  21. package/_bundled_plugin/skills/assess-patch-risk/SKILL.md +2 -0
  22. package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +2 -0
  23. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +5 -1
  24. package/_bundled_plugin/skills/finding-discovery/SKILL.md +2 -0
  25. package/_bundled_plugin/skills/fix-finding/SKILL.md +3 -1
  26. package/_bundled_plugin/skills/fix-finding/agents/openai.yaml +1 -1
  27. package/_bundled_plugin/skills/propose-security-hardening/SKILL.md +2 -0
  28. package/_bundled_plugin/skills/security-scan/references/desktop-scan.md +5 -1
  29. package/_bundled_plugin/skills/threat-model/SKILL.md +2 -0
  30. package/_bundled_plugin/skills/validation/SKILL.md +2 -0
  31. package/_bundled_plugin/skills/vulnerability-writeup/SKILL.md +2 -0
  32. package/dist/api.d.ts +7 -23
  33. package/dist/api.d.ts.map +1 -1
  34. package/dist/api.js +109 -113
  35. package/dist/api.js.map +1 -1
  36. package/dist/auth.d.ts.map +1 -1
  37. package/dist/auth.js.map +1 -1
  38. package/dist/bulk-scan-discovery.d.ts +1 -1
  39. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  40. package/dist/bulk-scan-discovery.js +2 -2
  41. package/dist/bulk-scan-discovery.js.map +1 -1
  42. package/dist/classify-scan-severity.js.map +1 -1
  43. package/dist/classify-severity.d.ts.map +1 -1
  44. package/dist/classify-severity.js.map +1 -1
  45. package/dist/cli.d.ts +6 -2
  46. package/dist/cli.d.ts.map +1 -1
  47. package/dist/cli.js +646 -365
  48. package/dist/cli.js.map +1 -1
  49. package/dist/cloud-publish.js.map +1 -1
  50. package/dist/component-plan.js.map +1 -1
  51. package/dist/component-scan.d.ts +4 -2
  52. package/dist/component-scan.d.ts.map +1 -1
  53. package/dist/component-scan.js +7 -1
  54. package/dist/component-scan.js.map +1 -1
  55. package/dist/config-path.d.ts +6 -0
  56. package/dist/config-path.d.ts.map +1 -0
  57. package/dist/config-path.js +6 -0
  58. package/dist/config-path.js.map +1 -0
  59. package/dist/config.d.ts +2 -0
  60. package/dist/config.d.ts.map +1 -1
  61. package/dist/config.js +13 -3
  62. package/dist/config.js.map +1 -1
  63. package/dist/contract.js.map +1 -1
  64. package/dist/cost-model.d.ts +20 -6
  65. package/dist/cost-model.d.ts.map +1 -1
  66. package/dist/cost-model.js +73 -8
  67. package/dist/cost-model.js.map +1 -1
  68. package/dist/cost.d.ts.map +1 -1
  69. package/dist/custom-publish.js.map +1 -1
  70. package/dist/deduplication/checkpointed-review.d.ts.map +1 -1
  71. package/dist/deduplication/checkpointed-review.js.map +1 -1
  72. package/dist/deduplication/codex-review.d.ts.map +1 -1
  73. package/dist/deduplication/codex-review.js.map +1 -1
  74. package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -1
  75. package/dist/deduplication/deduplication-reviewer.js.map +1 -1
  76. package/dist/deduplication/deduplication.js.map +1 -1
  77. package/dist/deduplication/scan.d.ts.map +1 -1
  78. package/dist/deduplication/scan.js.map +1 -1
  79. package/dist/deep-config.d.ts +17 -0
  80. package/dist/deep-config.d.ts.map +1 -0
  81. package/dist/deep-config.js +145 -0
  82. package/dist/deep-config.js.map +1 -0
  83. package/dist/deep-progress.d.ts.map +1 -1
  84. package/dist/deep-scan-defaults.d.ts +9 -0
  85. package/dist/deep-scan-defaults.d.ts.map +1 -0
  86. package/dist/deep-scan-defaults.js +10 -0
  87. package/dist/deep-scan-defaults.js.map +1 -0
  88. package/dist/errors.d.ts.map +1 -1
  89. package/dist/errors.js +2 -2
  90. package/dist/errors.js.map +1 -1
  91. package/dist/feedback.d.ts.map +1 -1
  92. package/dist/feedback.js +21 -7
  93. package/dist/feedback.js.map +1 -1
  94. package/dist/finding-catalogue.js.map +1 -1
  95. package/dist/finding-workflow.d.ts.map +1 -1
  96. package/dist/finding-workflow.js.map +1 -1
  97. package/dist/findings-client.d.ts.map +1 -1
  98. package/dist/findings-client.js.map +1 -1
  99. package/dist/findings-import.d.ts +6 -6
  100. package/dist/github.d.ts +625 -7
  101. package/dist/github.d.ts.map +1 -1
  102. package/dist/github.js.map +1 -1
  103. package/dist/import-scan.js.map +1 -1
  104. package/dist/index.d.ts +5 -0
  105. package/dist/index.d.ts.map +1 -1
  106. package/dist/index.js +1 -0
  107. package/dist/index.js.map +1 -1
  108. package/dist/knowledge-base.d.ts.map +1 -1
  109. package/dist/knowledge-base.js +17 -6
  110. package/dist/knowledge-base.js.map +1 -1
  111. package/dist/models.d.ts.map +1 -1
  112. package/dist/models.js.map +1 -1
  113. package/dist/multiscan.d.ts +6 -6
  114. package/dist/multiscan.d.ts.map +1 -1
  115. package/dist/multiscan.js +69 -4
  116. package/dist/multiscan.js.map +1 -1
  117. package/dist/patch-tui.d.ts.map +1 -1
  118. package/dist/patch-tui.js.map +1 -1
  119. package/dist/project-config-schema.d.ts +191 -0
  120. package/dist/project-config-schema.d.ts.map +1 -0
  121. package/dist/project-config-schema.js +85 -0
  122. package/dist/project-config-schema.js.map +1 -0
  123. package/dist/project-config.d.ts +50 -0
  124. package/dist/project-config.d.ts.map +1 -0
  125. package/dist/project-config.js +220 -0
  126. package/dist/project-config.js.map +1 -0
  127. package/dist/prompt-files.d.ts +13 -0
  128. package/dist/prompt-files.d.ts.map +1 -0
  129. package/dist/prompt-files.js +74 -0
  130. package/dist/prompt-files.js.map +1 -0
  131. package/dist/publication-events.d.ts.map +1 -1
  132. package/dist/publication-events.js.map +1 -1
  133. package/dist/publish.d.ts.map +1 -1
  134. package/dist/publish.js +1 -1
  135. package/dist/publish.js.map +1 -1
  136. package/dist/result.d.ts +2 -1
  137. package/dist/result.d.ts.map +1 -1
  138. package/dist/result.js +5 -0
  139. package/dist/result.js.map +1 -1
  140. package/dist/runtime.js +4 -4
  141. package/dist/runtime.js.map +1 -1
  142. package/dist/scan-comparison.d.ts.map +1 -1
  143. package/dist/scan-comparison.js.map +1 -1
  144. package/dist/scan-dashboard.d.ts +1 -0
  145. package/dist/scan-dashboard.d.ts.map +1 -1
  146. package/dist/scan-dashboard.js +34 -22
  147. package/dist/scan-dashboard.js.map +1 -1
  148. package/dist/scan-history-renderer.d.ts.map +1 -1
  149. package/dist/scan-history-renderer.js.map +1 -1
  150. package/dist/scan-logs.d.ts +12 -5
  151. package/dist/scan-logs.d.ts.map +1 -1
  152. package/dist/scan-logs.js +49 -18
  153. package/dist/scan-logs.js.map +1 -1
  154. package/dist/scan-modes.d.ts +3 -0
  155. package/dist/scan-modes.d.ts.map +1 -0
  156. package/dist/scan-modes.js +2 -0
  157. package/dist/scan-modes.js.map +1 -0
  158. package/dist/scan-settings.d.ts +81 -0
  159. package/dist/scan-settings.d.ts.map +1 -0
  160. package/dist/scan-settings.js +123 -0
  161. package/dist/scan-settings.js.map +1 -0
  162. package/dist/security-policy-cli.js +2 -2
  163. package/dist/security-policy-cli.js.map +1 -1
  164. package/dist/security-policy.d.ts.map +1 -1
  165. package/dist/security-policy.js.map +1 -1
  166. package/dist/server/dashboard/THIRD_PARTY_NOTICES.txt +2 -2
  167. package/dist/server/dashboard/app.js +2 -2
  168. package/dist/server/dashboard.js.map +1 -1
  169. package/dist/server/embeddings.d.ts.map +1 -1
  170. package/dist/server/embeddings.js.map +1 -1
  171. package/dist/server/errors.d.ts.map +1 -1
  172. package/dist/server/errors.js.map +1 -1
  173. package/dist/server/findings-service.d.ts.map +1 -1
  174. package/dist/server/findings-service.js.map +1 -1
  175. package/dist/server/serve.js.map +1 -1
  176. package/dist/server/sqlite-store.d.ts.map +1 -1
  177. package/dist/server/sqlite-store.js.map +1 -1
  178. package/dist/severity-store.js.map +1 -1
  179. package/dist/targets.d.ts +2 -1
  180. package/dist/targets.d.ts.map +1 -1
  181. package/dist/targets.js.map +1 -1
  182. package/dist/thread-source.d.ts.map +1 -1
  183. package/dist/version.js.map +1 -1
  184. package/dist/worker-progress.d.ts.map +1 -1
  185. package/dist/worker-progress.js.map +1 -1
  186. package/package.json +31 -28
  187. package/schemas/project-config.schema.json +211 -0
package/dist/api.js CHANGED
@@ -5,7 +5,6 @@ import { randomUUID } from "node:crypto";
5
5
  import { homedir, tmpdir } from "node:os";
6
6
  import { basename, delimiter, dirname, isAbsolute, join, relative, resolve, sep, } from "node:path";
7
7
  import { Codex, } from "@openai/codex-sdk";
8
- import { parse as parseToml, stringify as stringifyToml, } from "smol-toml";
9
8
  import { z } from "incur";
10
9
  import { CODEX_AUTH_CONFIG_KEYS, NO_CREDENTIALS_MESSAGE, accountStatus, configuredCodexHome, CodexLoginHandle, loginApiKey as persistApiKey, logout as codexLogout, } from "./auth.js";
11
10
  import { jsonForPrompt, pluginPythonCommand, shellEnvironmentReference, } from "./codex-prompt.js";
@@ -13,6 +12,10 @@ import { DEFAULT_CODEX_CONFIG, EXTERNAL_CODEX_PROVIDERS, inlineToml, isExternalM
13
12
  import { estimateScanCost, ScanCostTracker, } from "./cost.js";
14
13
  import { DeepScanProgressTracker, } from "./deep-progress.js";
15
14
  import { findScanSession } from "./scan-logs.js";
15
+ import { deepScanOptions, resolveDeepScanConfig, writeDeepScanConfig, } from "./deep-config.js";
16
+ import { DEFAULT_SCAN_AUTH, DEFAULT_SCAN_MODE, SCAN_AUTH_MODES, ScanSettingsSchema, } from "./scan-settings.js";
17
+ import { resolveScanPrompts } from "./prompt-files.js";
18
+ export { SCAN_AUTH_MODES } from "./scan-settings.js";
16
19
  import { loadContract, readScanFile, requireScanFile, } from "./contract.js";
17
20
  import { runCustomValidation, writeCustomValidationStatus, } from "./custom-validation.js";
18
21
  import { customDiscoveryPrompt, customValidationConfig, } from "./custom-validation-prompt.js";
@@ -42,7 +45,6 @@ const validationResponseSchema = z
42
45
  report: z.string().trim().min(1),
43
46
  })
44
47
  .strict();
45
- export const SCAN_AUTH_MODES = ["auto", "chatgpt", "api-key"];
46
48
  const DEFAULT_DEPENDENCIES = {
47
49
  createCodex: (options) => new Codex(options),
48
50
  environment: process.env,
@@ -52,13 +54,6 @@ const POLICY_PERMISSION_PROFILE = "codex_security_policy";
52
54
  const SAFETY_IDENTIFIER_ENV = "CODEX_SAFETY_IDENTIFIER";
53
55
  const PERSONAL_TRUSTED_ACCESS_URL = "https://chatgpt.com/cyber";
54
56
  const ORGANIZATIONAL_TRUSTED_ACCESS_URL = "https://openai.com/form/enterprise-trusted-access-for-cyber/";
55
- const DEEP_SCAN_SETTINGS = [
56
- ["workers", "workers", 1],
57
- ["subagents", "subagents", 0],
58
- ["stopAfterNoNew", "stop_after_no_new", 1],
59
- ["maxDiscoveryRuns", "max_discovery_runs", 1],
60
- ["maxTimeHours", "max_time_hours", 0],
61
- ];
62
57
  export class CodexSecurity {
63
58
  config;
64
59
  metadata = {
@@ -93,7 +88,7 @@ export class CodexSecurity {
93
88
  this.#abortController.signal,
94
89
  ...(options.signal ? [options.signal] : []),
95
90
  ]);
96
- const local = await this.#validateLocalInputs(repository, { ...options, outputDir: undefined, archiveExisting: false }, signal);
91
+ const local = await this.#prepareLocalInputs(repository, { ...options, outputDir: undefined, archiveExisting: false }, signal);
97
92
  const workflow = new FindingWorkflow(workflowId, this.#dependencies.environment, this.#dependencies.runWorkbench, this.config.pythonPath);
98
93
  if (options.outputDir !== undefined)
99
94
  await workflow.protectArtifacts(options.outputDir);
@@ -103,8 +98,9 @@ export class CodexSecurity {
103
98
  config: this.config,
104
99
  options: {
105
100
  ...options,
101
+ ...local.prompts,
106
102
  target: options.target ?? "repository",
107
- mode: options.mode ?? "standard",
103
+ mode: options.mode ?? DEFAULT_SCAN_MODE,
108
104
  outputDir: options.outputDir === undefined
109
105
  ? undefined
110
106
  : resolve(expandHome(options.outputDir)),
@@ -148,7 +144,7 @@ export class CodexSecurity {
148
144
  }
149
145
  await workflow.begin("scan");
150
146
  try {
151
- const result = await this.#run(repository, options);
147
+ const result = await this.#run(repository, options, local);
152
148
  await workflow.protectArtifacts(result.scanDir);
153
149
  await workflow.bind({
154
150
  scanId: result.manifest.scan.id,
@@ -189,7 +185,7 @@ export class CodexSecurity {
189
185
  throw new CodexSecurityError("A finding must be nonempty text or a JSON object.");
190
186
  }
191
187
  const finding = jsonForPrompt(options.finding);
192
- const inputs = await this.#validateLocalInputs(options.repositoryPath, options, signal);
188
+ const inputs = await this.#prepareLocalInputs(options.repositoryPath, options, signal);
193
189
  const temporaryRoot = await realpath(tmpdir());
194
190
  requireOutputOutsideRepository(inputs.protectedRoot, temporaryRoot, "temporary");
195
191
  const session = await this.#prepareSession(inputs, options, signal, temporaryRoot);
@@ -256,7 +252,7 @@ export class CodexSecurity {
256
252
  }
257
253
  async preflight(repository, options = {}) {
258
254
  this.#requireOpen();
259
- const inputs = await this.#validateLocalInputs(repository, options, options.signal);
255
+ const inputs = await this.#prepareLocalInputs(repository, options, options.signal);
260
256
  return await this.#preflightInputs(inputs, options);
261
257
  }
262
258
  async #preflightInputs(inputs, options) {
@@ -277,7 +273,10 @@ export class CodexSecurity {
277
273
  repository: inputs.repository,
278
274
  target: inputs.target,
279
275
  mode: inputs.mode,
280
- ...deepScanOptions(options),
276
+ ...inputs.deepScanConfiguration?.settings,
277
+ ...(inputs.deepScanConfiguration === undefined
278
+ ? {}
279
+ : { deepScanSources: inputs.deepScanConfiguration.sources }),
281
280
  ...(options.knowledgeBasePaths?.length
282
281
  ? { knowledgeBasePaths: options.knowledgeBasePaths }
283
282
  : {}),
@@ -526,7 +525,7 @@ export class CodexSecurity {
526
525
  }
527
526
  }
528
527
  }
529
- async #run(repository, options) {
528
+ async #run(repository, options, preparedInputs) {
530
529
  this.#requireOpen();
531
530
  if (options.mock)
532
531
  return await this.#runMock(repository, options);
@@ -566,8 +565,16 @@ export class CodexSecurity {
566
565
  this.#requireOpen();
567
566
  throwIfAborted(signal, scanDir);
568
567
  };
569
- // Validate all local inputs before runtime initialization or plugin-Python discovery.
570
- const { repository: repo, target: normalized, mode, outputDir: requestedOutput, protectedRoot, stateDirectory, } = await this.#validateLocalInputs(repository, options, signal);
568
+ // Workflows reuse the prepared prompts and deep settings, but validate the
569
+ // output only when starting new work; a completed workflow may already own it.
570
+ const inputs = preparedInputs === undefined
571
+ ? await this.#prepareLocalInputs(repository, options, signal)
572
+ : {
573
+ ...preparedInputs,
574
+ outputDir: await prepareScanOutputDir(options, preparedInputs.protectedRoots),
575
+ };
576
+ const { repository: repo, target: normalized, mode, outputDir: requestedOutput, protectedRoot, stateDirectory, deepScanConfiguration, prompts, } = inputs;
577
+ options = { ...options, ...prompts };
571
578
  checkOpen();
572
579
  let temporaryRoot;
573
580
  if (requestedOutput === null ||
@@ -584,11 +591,12 @@ export class CodexSecurity {
584
591
  const { runtime, runtimeHome, effectiveConfig, preflightConfig, modelProvider, authentication, approvalPolicy, python, } = session;
585
592
  releaseCredentialHome = session.releaseCredentialHome;
586
593
  const deepScanConfigPath = mode === "deep"
587
- ? runtime.deepScanConfigPath ??
588
- join(runtimeHome, "codex-security", "config.toml")
594
+ ? (runtime.deepScanConfigPath ??
595
+ join(runtimeHome, "codex-security", "config.toml"))
589
596
  : undefined;
590
- if (deepScanConfigPath !== undefined) {
591
- await prepareDeepScanConfig(deepScanConfigPath, this.#dependencies.environment, options, signal);
597
+ if (deepScanConfigPath !== undefined &&
598
+ deepScanConfiguration !== undefined) {
599
+ await writeDeepScanConfig(deepScanConfigPath, deepScanConfiguration);
592
600
  }
593
601
  checkOpen();
594
602
  const scanOutputRoot = requestedOutput === null &&
@@ -737,7 +745,21 @@ export class CodexSecurity {
737
745
  onError: reportTrackingError,
738
746
  });
739
747
  costTracker = tracker;
740
- const recipe = scanRecipe(repo, normalized, mode, expectation.repositoryRevision, runtime.plugin.version, { ...preflightConfig, approval_policy: approvalPolicy }, options.failureSeverity, knowledgeBase?.sources, options.maxCostUsd, deepScanOptions(options));
748
+ const recipe = scanRecipe({
749
+ repository: repo,
750
+ target: normalized,
751
+ mode,
752
+ repositoryRevision: expectation.repositoryRevision,
753
+ pluginVersion: runtime.plugin.version,
754
+ config: { ...preflightConfig, approval_policy: approvalPolicy },
755
+ failOnSeverity: options.failureSeverity,
756
+ knowledgeBasePaths: knowledgeBase?.sources,
757
+ maxCostUsd: options.maxCostUsd,
758
+ deepScan: deepScanConfiguration?.settings,
759
+ auth: options.auth,
760
+ });
761
+ if (options.scanPrompt?.trim())
762
+ recipe["requiresScanPrompt"] = true;
741
763
  if (options.safetyIdentifier !== undefined)
742
764
  recipe["safetyIdentifier"] = options.safetyIdentifier;
743
765
  if (options.postScanPrompt !== undefined)
@@ -1769,7 +1791,7 @@ export class CodexSecurity {
1769
1791
  ...sources.gitMetadataPaths,
1770
1792
  ]),
1771
1793
  ];
1772
- const inputs = await this.#validateLocalInputs(target.repository, {
1794
+ const inputs = await this.#prepareLocalInputs(target.repository, {
1773
1795
  auth: options.auth,
1774
1796
  target: target.scope === "." ? "repository" : [dirname(target.targetPath)],
1775
1797
  outputDir: options.outputDir,
@@ -1788,7 +1810,8 @@ export class CodexSecurity {
1788
1810
  this.#abortController.signal,
1789
1811
  ...(options.signal ? [options.signal] : []),
1790
1812
  ]);
1791
- const local = await this.#validateLocalInputs(repository, options, signal);
1813
+ const local = await this.#prepareLocalInputs(repository, options, signal);
1814
+ options = { ...options, ...local.prompts };
1792
1815
  const temporaryRoot = await realpath(tmpdir());
1793
1816
  requireOutputOutsideRepository(local.protectedRoot, temporaryRoot, "temporary");
1794
1817
  const workspace = await mkdtemp(join(temporaryRoot, "codex-security-mock-"));
@@ -1853,7 +1876,18 @@ export class CodexSecurity {
1853
1876
  : ["--parent-scan-id", options.parentScanId]),
1854
1877
  ], JSON.stringify({
1855
1878
  recipe: {
1856
- ...scanRecipe(local.repository, local.target, local.mode, revision, plugin.version, { model }, options.failureSeverity, options.knowledgeBasePaths, options.maxCostUsd),
1879
+ ...scanRecipe({
1880
+ repository: local.repository,
1881
+ target: local.target,
1882
+ mode: local.mode,
1883
+ repositoryRevision: revision,
1884
+ pluginVersion: plugin.version,
1885
+ config: { model },
1886
+ failOnSeverity: options.failureSeverity,
1887
+ knowledgeBasePaths: options.knowledgeBasePaths,
1888
+ maxCostUsd: options.maxCostUsd,
1889
+ auth: options.auth,
1890
+ }),
1857
1891
  mock: true,
1858
1892
  },
1859
1893
  userContext: options.scanPrompt,
@@ -1935,7 +1969,7 @@ export class CodexSecurity {
1935
1969
  await cleanupSdkDirectory(workspace);
1936
1970
  }
1937
1971
  }
1938
- async #validateLocalInputs(repository, options, signal, protectedRoots) {
1972
+ async #prepareLocalInputs(repository, options, signal, protectedRoots) {
1939
1973
  if (options.resumeScanId !== undefined &&
1940
1974
  (options.mode !== "deep" ||
1941
1975
  !options.outputDir ||
@@ -1948,10 +1982,12 @@ export class CodexSecurity {
1948
1982
  if (options.mock &&
1949
1983
  (options.mode === "deep" ||
1950
1984
  options.validationPrompt !== undefined ||
1951
- options.postScanPrompt !== undefined)) {
1985
+ options.postScanPrompt !== undefined ||
1986
+ options.validationPromptFile !== undefined ||
1987
+ options.postScanPromptFile !== undefined)) {
1952
1988
  throw new CodexSecurityError("Mock scans support Standard mode without custom validation or post-scan prompts; those workflows require model calls.");
1953
1989
  }
1954
- deepScanOptions(options);
1990
+ const deep = deepScanOptions(options);
1955
1991
  const identifier = options.safetyIdentifier;
1956
1992
  if (identifier !== undefined &&
1957
1993
  (typeof identifier !== "string" ||
@@ -1961,7 +1997,7 @@ export class CodexSecurity {
1961
1997
  throw new ConfigurationError("safetyIdentifier must contain 1 to 64 characters, must not be blank, and must not contain NUL.");
1962
1998
  }
1963
1999
  if (options.maxCostUsd !== undefined &&
1964
- (!Number.isFinite(options.maxCostUsd) || options.maxCostUsd <= 0)) {
2000
+ !ScanSettingsSchema.shape.maxCostUsd.safeParse(options.maxCostUsd).success) {
1965
2001
  throw new CodexSecurityError("The scan cost limit must be a positive USD amount.");
1966
2002
  }
1967
2003
  const repositoryPath = resolveRepositoryPath(repository);
@@ -1971,11 +2007,12 @@ export class CodexSecurity {
1971
2007
  validatedGitEnvironment(this.#dependencies.environment);
1972
2008
  const normalized = await normalizeTarget(repo, requestedTarget, signal);
1973
2009
  throwIfAborted(signal);
1974
- const mode = options.mode ?? "standard";
2010
+ const mode = options.mode ?? DEFAULT_SCAN_MODE;
1975
2011
  validateMode(normalized, mode);
1976
- if (options.validationPrompt !== undefined) {
1977
- if (typeof options.validationPrompt !== "string" ||
1978
- !options.validationPrompt.trim()) {
2012
+ const prompts = await resolveScanPrompts(options, repo);
2013
+ if (prompts.validationPrompt !== undefined) {
2014
+ if (typeof prompts.validationPrompt !== "string" ||
2015
+ !prompts.validationPrompt.trim()) {
1979
2016
  throw new CodexSecurityError("The validation prompt must not be empty.");
1980
2017
  }
1981
2018
  if (mode === "deep")
@@ -1987,10 +2024,7 @@ export class CodexSecurity {
1987
2024
  (await enclosingGitWorktreeRoot(repo, signal)) ??
1988
2025
  repo;
1989
2026
  protectedRoots ??= [protectedRoot];
1990
- const requestedOutput = await validateOutputDir(options.outputDir, options.resumeScanId !== undefined || options.archiveExisting);
1991
- if (requestedOutput !== null) {
1992
- requireOutputOutsideRepositories(protectedRoots, requestedOutput);
1993
- }
2027
+ const requestedOutput = await prepareScanOutputDir(options, protectedRoots);
1994
2028
  const stateDirectory = codexSecurityStateDirectory(this.#dependencies.environment);
1995
2029
  let canonicalStateDirectory = stateDirectory;
1996
2030
  while (true) {
@@ -2016,6 +2050,12 @@ export class CodexSecurity {
2016
2050
  protectedRoot,
2017
2051
  protectedRoots,
2018
2052
  stateDirectory,
2053
+ prompts,
2054
+ ...(mode === "deep"
2055
+ ? {
2056
+ deepScanConfiguration: await resolveDeepScanConfig(deep, join(expandHome(environmentValue(this.#dependencies.environment, "CODEX_HOME") ?? join(homedir(), ".codex"), this.#dependencies.environment), "codex-security", "config.toml"), signal),
2057
+ }
2058
+ : {}),
2019
2059
  };
2020
2060
  }
2021
2061
  async #prepareRuntime(signal, temporaryRoot, validateLocation, auth = "auto", requestedConfig) {
@@ -2109,74 +2149,6 @@ export async function listRepositoryFindings(workbench, targetId, status = "open
2109
2149
  } while (offset !== undefined);
2110
2150
  return findings;
2111
2151
  }
2112
- function deepScanOptions(options) {
2113
- const selected = {};
2114
- for (const [name, , minimum] of DEEP_SCAN_SETTINGS) {
2115
- const value = options[name];
2116
- if (value === undefined)
2117
- continue;
2118
- if ((options.mode ?? "standard") !== "deep") {
2119
- throw new CodexSecurityError("Deep scan settings require deep mode.");
2120
- }
2121
- if (name === "maxTimeHours") {
2122
- if (!Number.isFinite(value) || value <= 0 || value > 96) {
2123
- throw new CodexSecurityError("Deep scan maxTimeHours must be a positive number no greater than 96.");
2124
- }
2125
- }
2126
- else if (!Number.isSafeInteger(value) || value < minimum) {
2127
- throw new CodexSecurityError(`Deep scan ${name} must be ${minimum === 0 ? "a non-negative" : "a positive"} integer.`);
2128
- }
2129
- selected[name] = value;
2130
- }
2131
- return selected;
2132
- }
2133
- async function prepareDeepScanConfig(destination, environment, options, signal) {
2134
- const ambientHome = expandHome(environmentValue(environment, "CODEX_HOME") ?? join(homedir(), ".codex"), environment);
2135
- const source = join(ambientHome, "codex-security", "config.toml");
2136
- let configured = {};
2137
- try {
2138
- configured = parseToml(await readFile(source, { encoding: "utf8", signal }));
2139
- }
2140
- catch (error) {
2141
- if (!isRecord(error) || error["code"] !== "ENOENT") {
2142
- throw new CodexSecurityError(`Cannot read Codex Security configuration at ${source}.`, { cause: error });
2143
- }
2144
- }
2145
- const existing = configured["deep_scan"];
2146
- if (existing !== undefined && !isRecord(existing)) {
2147
- throw new CodexSecurityError(`Codex Security configuration [deep_scan] at ${source} must be a TOML table.`);
2148
- }
2149
- const overrides = {};
2150
- for (const [name, key] of DEEP_SCAN_SETTINGS) {
2151
- const value = options[name];
2152
- if (value !== undefined)
2153
- overrides[key] = value;
2154
- }
2155
- const sharedConfig = await sameExistingPath(source, destination);
2156
- const hasOverrides = Object.keys(overrides).length > 0;
2157
- if (existing === undefined && !hasOverrides) {
2158
- if (!sharedConfig) {
2159
- await rm(destination, { force: true });
2160
- }
2161
- return;
2162
- }
2163
- if (sharedConfig && !hasOverrides)
2164
- return;
2165
- await mkdir(dirname(destination), { recursive: true, mode: 0o700 });
2166
- await writeFile(destination, stringifyToml({
2167
- ...configured,
2168
- deep_scan: { ...existing, ...overrides },
2169
- }), { mode: 0o600, signal });
2170
- }
2171
- async function sameExistingPath(left, right) {
2172
- if (left === right)
2173
- return true;
2174
- const [canonicalLeft, canonicalRight] = await Promise.all([
2175
- realpath(left).catch(() => null),
2176
- realpath(right).catch(() => null),
2177
- ]);
2178
- return canonicalLeft !== null && canonicalLeft === canonicalRight;
2179
- }
2180
2152
  export function createSecurity(config = {}) {
2181
2153
  return createSecurityInternal(config, { surface: "sdk" });
2182
2154
  }
@@ -2517,7 +2489,7 @@ function targetInstruction(target, python) {
2517
2489
  }
2518
2490
  return `Scan target: staged and unstaged working-tree changes against ${target.base}.`;
2519
2491
  }
2520
- function scanRecipe(repository, target, mode, repositoryRevision, pluginVersion, preflightConfig, failOnSeverity, knowledgeBasePaths, maxCostUsd, deepScan) {
2492
+ function scanRecipe({ repository, target, mode, repositoryRevision, pluginVersion, config, failOnSeverity, knowledgeBasePaths, maxCostUsd, deepScan, auth, }) {
2521
2493
  return {
2522
2494
  repository,
2523
2495
  target: {
@@ -2531,15 +2503,22 @@ function scanRecipe(repository, target, mode, repositoryRevision, pluginVersion,
2531
2503
  mode,
2532
2504
  ...(repositoryRevision === null ? {} : { repositoryRevision }),
2533
2505
  pluginVersion,
2534
- config: preflightConfig,
2506
+ config,
2507
+ ...(auth === undefined ? {} : { auth }),
2535
2508
  ...(failOnSeverity === undefined ? {} : { failOnSeverity }),
2536
2509
  ...(knowledgeBasePaths === undefined ? {} : { knowledgeBasePaths }),
2537
2510
  ...(maxCostUsd === undefined ? {} : { maxCostUsd }),
2538
- ...(deepScan === undefined || Object.keys(deepScan).length === 0
2511
+ ...(deepScan === undefined
2539
2512
  ? {}
2540
- : { deepScan: { ...deepScan } }),
2513
+ : { deepScan: { ...deepScan }, deepScanResolved: true }),
2541
2514
  };
2542
2515
  }
2516
+ async function prepareScanOutputDir(options, protectedRoots) {
2517
+ const output = await validateOutputDir(options.outputDir, options.resumeScanId !== undefined || options.archiveExisting);
2518
+ if (output !== null)
2519
+ requireOutputOutsideRepositories(protectedRoots, output);
2520
+ return output;
2521
+ }
2543
2522
  function validateScanCostLimit(maxCostUsd, model) {
2544
2523
  if (maxCostUsd === undefined)
2545
2524
  return;
@@ -2550,13 +2529,30 @@ function validateScanCostLimit(maxCostUsd, model) {
2550
2529
  function addScanCosts(previous, current) {
2551
2530
  if (previous === null)
2552
2531
  return { ...current };
2532
+ const { estimatedUsdRange: currentRange, ...currentCost } = current;
2533
+ const previousRange = previous.estimatedUsdRange;
2553
2534
  return {
2554
- model: current.model,
2535
+ ...currentCost,
2555
2536
  inputTokens: previous.inputTokens + current.inputTokens,
2556
2537
  cachedInputTokens: previous.cachedInputTokens + current.cachedInputTokens,
2557
2538
  cacheWriteInputTokens: previous.cacheWriteInputTokens + current.cacheWriteInputTokens,
2558
2539
  outputTokens: previous.outputTokens + current.outputTokens,
2559
2540
  estimatedUsd: previous.estimatedUsd + current.estimatedUsd,
2541
+ ...(previous.cacheWriteInputTokensReported === false ||
2542
+ current.cacheWriteInputTokensReported === false
2543
+ ? { cacheWriteInputTokensReported: false }
2544
+ : {}),
2545
+ ...(previousRange === undefined || currentRange === undefined
2546
+ ? {}
2547
+ : {
2548
+ estimatedUsdRange: {
2549
+ context: "unknown",
2550
+ min: previousRange.min + currentRange.min,
2551
+ max: previousRange.max === null || currentRange.max === null
2552
+ ? null
2553
+ : previousRange.max + currentRange.max,
2554
+ },
2555
+ }),
2560
2556
  };
2561
2557
  }
2562
2558
  async function collectResult(turnResult, threadId, scanDir, pluginRoot, expectation, signal, workbenchValidated = false) {
@@ -2604,7 +2600,7 @@ async function collectResult(turnResult, threadId, scanDir, pluginRoot, expectat
2604
2600
  sarifPath,
2605
2601
  });
2606
2602
  }
2607
- export function scanAuthentication(environment, auth = "auto", modelProvider, commandAuth = false) {
2603
+ export function scanAuthentication(environment, auth = DEFAULT_SCAN_AUTH, modelProvider, commandAuth = false) {
2608
2604
  if (!SCAN_AUTH_MODES.includes(auth)) {
2609
2605
  throw new TypeError("Scan authentication mode must be auto, chatgpt, or api-key.");
2610
2606
  }
@@ -3084,7 +3080,7 @@ function bundledCodexSdkEnvironment(command, environment) {
3084
3080
  const pathKeys = Object.keys(result).filter((key) => key.toLowerCase() === "path");
3085
3081
  const pathKey = pathKeys.includes("Path")
3086
3082
  ? "Path"
3087
- : pathKeys.at(-1) ?? "PATH";
3083
+ : (pathKeys.at(-1) ?? "PATH");
3088
3084
  for (const key of pathKeys) {
3089
3085
  if (key !== pathKey)
3090
3086
  delete result[key];