@openai/codex-security 0.1.10 → 0.1.11
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.
- package/README.md +27 -13
- package/_bundled_plugin/.codex-plugin/plugin.json +1 -1
- package/_bundled_plugin/.mcp.json +3 -1
- package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
- package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
- package/_bundled_plugin/scripts/deep_scan_config.py +21 -1
- package/_bundled_plugin/scripts/deep_scan_workbench.py +44 -14
- package/_bundled_plugin/scripts/finalize_scan_contract.py +15 -207
- package/_bundled_plugin/scripts/generate_in_scope_files.py +40 -16
- package/_bundled_plugin/scripts/generate_rank_input.py +38 -5
- package/_bundled_plugin/scripts/rank_preview.py +4 -0
- package/_bundled_plugin/scripts/report_projection.py +39 -1
- package/_bundled_plugin/scripts/workbench_cli.py +5 -0
- package/_bundled_plugin/scripts/workbench_db.py +283 -4
- package/_bundled_plugin/scripts/workbench_schema.py +15 -0
- package/_bundled_plugin/scripts/workbench_source_excerpt.py +1 -5
- package/_bundled_plugin/scripts/workbench_target.py +82 -0
- package/_bundled_plugin/skills/deep-security-scan/SKILL.md +5 -3
- package/dist/api.d.ts +1 -0
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +217 -92
- package/dist/api.js.map +1 -1
- package/dist/auth.d.ts +2 -3
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +59 -270
- package/dist/auth.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +14 -4
- package/dist/cli.js.map +1 -1
- package/dist/contract.js +7 -5
- package/dist/contract.js.map +1 -1
- package/dist/cost.d.ts +1 -0
- package/dist/cost.d.ts.map +1 -1
- package/dist/cost.js +23 -1
- package/dist/cost.js.map +1 -1
- package/dist/knowledge-base.d.ts.map +1 -1
- package/dist/knowledge-base.js +2 -1
- package/dist/knowledge-base.js.map +1 -1
- package/dist/multiscan.d.ts.map +1 -1
- package/dist/multiscan.js +1 -0
- package/dist/multiscan.js.map +1 -1
- package/dist/runtime.d.ts +9 -6
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +127 -284
- package/dist/runtime.js.map +1 -1
- package/dist/scan-comparison.js +1 -1
- package/dist/scan-comparison.js.map +1 -1
- package/dist/targets.d.ts +1 -0
- package/dist/targets.d.ts.map +1 -1
- package/dist/targets.js +22 -0
- package/dist/targets.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +2 -2
package/dist/api.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
-
import { chmod, lstat, mkdir, readFile, realpath, rm, writeFile, } from "node:fs/promises";
|
|
2
|
+
import { chmod, lstat, mkdir, readFile, realpath, rename, 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";
|
|
@@ -17,7 +17,7 @@ import { matchCompletedScan, } from "./scan-comparison.js";
|
|
|
17
17
|
import { scanProgressUpdatesFromEvent, workerStatusFromEvent, } from "./worker-progress.js";
|
|
18
18
|
import { CODEX_EXECUTABLE_VERSION, CODEX_SDK_VERSION } from "./version.js";
|
|
19
19
|
import { acquireCodexSecurityCredentialHomeLock, bootstrapPlugin, cleanupSdkDirectory, codexSecurityCredentialAllowsAmbientImport, codexSecurityCredentialHome, codexSecurityHasStoredFileCredentials, codexSecurityStateDirectory, createIsolatedHome, importAmbientAuth, prepareCodexSecurityCredentialHome, preserveCodexSecurityPluginRegistration, pluginExecutionEnvironment, planOutputArchive, prepareOutputDir, preparePersistentScanRoot, requireModelSafeOutputDir, resolveCodexCommand, resolvePluginPath, resolvePluginPython, runWorkbench, setCodexSecurityCredentialLogout, validateOutputDir, } from "./runtime.js";
|
|
20
|
-
import { enclosingGitWorktreeRoot, normalizeRepository, normalizeTarget, repositoryRevision, resolveRepositoryPath, validatedGitEnvironment, validateMode, } from "./targets.js";
|
|
20
|
+
import { enclosingGitWorktreeRoot, normalizeRepository, normalizeTarget, repositoryRevision, resolveRepositoryPath, validatedGitEnvironment, validateCommittedDiffCheckout, validateMode, } from "./targets.js";
|
|
21
21
|
const DEFAULT_DEPENDENCIES = {
|
|
22
22
|
createCodex: (options) => new Codex(options),
|
|
23
23
|
environment: process.env,
|
|
@@ -30,6 +30,7 @@ const DEEP_SCAN_SETTINGS = [
|
|
|
30
30
|
["subagents", "subagents", 0],
|
|
31
31
|
["stopAfterNoNew", "stop_after_no_new", 1],
|
|
32
32
|
["maxDiscoveryRuns", "max_discovery_runs", 1],
|
|
33
|
+
["maxTimeHours", "max_time_hours", 0],
|
|
33
34
|
];
|
|
34
35
|
export class CodexSecurity {
|
|
35
36
|
config;
|
|
@@ -107,6 +108,7 @@ export class CodexSecurity {
|
|
|
107
108
|
let releaseCredentialHome = null;
|
|
108
109
|
let scanFailure = false;
|
|
109
110
|
let completionCost = null;
|
|
111
|
+
let budgetRecovery = null;
|
|
110
112
|
let preparedTargetWarnings = [];
|
|
111
113
|
let runPostScan = null;
|
|
112
114
|
let activeScan = null;
|
|
@@ -153,14 +155,15 @@ export class CodexSecurity {
|
|
|
153
155
|
releaseCredentialHome = await acquireCodexSecurityCredentialHomeLock(credentialHome, signal);
|
|
154
156
|
}
|
|
155
157
|
const previousRuntime = this.#runtime;
|
|
156
|
-
const runtime = await this.#ensureRuntime(signal, temporaryRoot, (path) => requireOutputOutsideRepository(protectedRoot, path, "runtime"), options.auth,
|
|
158
|
+
const runtime = await this.#ensureRuntime(signal, temporaryRoot, (path) => requireOutputOutsideRepository(protectedRoot, path, "runtime"), options.auth, requestedConfig);
|
|
157
159
|
if (runtime === previousRuntime &&
|
|
158
160
|
this.#dependencies.prepareRuntime === undefined) {
|
|
159
|
-
await this.#refreshPersistentRuntime(runtime, scanEnvironment, signal);
|
|
161
|
+
await this.#refreshPersistentRuntime(runtime, scanEnvironment, signal, requestedConfig);
|
|
160
162
|
}
|
|
161
163
|
const effectiveConfig = runtime.effectiveConfig ?? requestedConfig;
|
|
164
|
+
const preflightConfig = scanPreflightCodexConfig(effectiveConfig);
|
|
162
165
|
if (runtime.configPath !== undefined) {
|
|
163
|
-
await writeCodexConfig(runtime.configPath,
|
|
166
|
+
await writeCodexConfig(runtime.configPath, preflightConfig);
|
|
164
167
|
}
|
|
165
168
|
const runtimeHome = await realpath(runtime.codexHome);
|
|
166
169
|
requireOutputOutsideRepository(protectedRoot, runtimeHome, "runtime");
|
|
@@ -236,7 +239,14 @@ export class CodexSecurity {
|
|
|
236
239
|
!isAbsolute(pluginRelativeToHome))) {
|
|
237
240
|
throw new OutputDirectoryError(`Shell-visible plugin root must be outside CODEX_HOME: ${canonicalShellPluginRoot}`);
|
|
238
241
|
}
|
|
239
|
-
const
|
|
242
|
+
const skillName = skillNameFor(normalized, mode);
|
|
243
|
+
const skillPath = join(shellPluginRoot, "skills", skillName, "SKILL.md");
|
|
244
|
+
const skillMetadata = await lstat(skillPath).catch(() => null);
|
|
245
|
+
if (skillMetadata === null ||
|
|
246
|
+
!skillMetadata.isFile() ||
|
|
247
|
+
skillMetadata.isSymbolicLink()) {
|
|
248
|
+
throw new IncompleteScanError(`Installed plugin is missing scan skill: ${skillName}`);
|
|
249
|
+
}
|
|
240
250
|
checkOpen();
|
|
241
251
|
const expectation = {
|
|
242
252
|
repository: repo,
|
|
@@ -247,6 +257,14 @@ export class CodexSecurity {
|
|
|
247
257
|
};
|
|
248
258
|
const { model } = scanModelConfiguration(effectiveConfig);
|
|
249
259
|
validateScanCostLimit(options.maxCostUsd, model);
|
|
260
|
+
if (mode === "deep" && options.maxCostUsd !== undefined) {
|
|
261
|
+
budgetRecovery = {
|
|
262
|
+
expectation,
|
|
263
|
+
pluginRoot: runtime.plugin.installedRoot,
|
|
264
|
+
model,
|
|
265
|
+
threadId: null,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
250
268
|
let scopeFileCount = null;
|
|
251
269
|
let reviewedFileCount = 0;
|
|
252
270
|
const reportProgress = (progress) => {
|
|
@@ -269,6 +287,7 @@ export class CodexSecurity {
|
|
|
269
287
|
codexHome: runtime.codexHome,
|
|
270
288
|
model,
|
|
271
289
|
repository: repo,
|
|
290
|
+
scanDirectory: scanDir,
|
|
272
291
|
maxCostUsd: options.maxCostUsd,
|
|
273
292
|
onActivity: options.onActivity === undefined
|
|
274
293
|
? undefined
|
|
@@ -286,7 +305,7 @@ export class CodexSecurity {
|
|
|
286
305
|
onError: reportTrackingError,
|
|
287
306
|
});
|
|
288
307
|
costTracker = tracker;
|
|
289
|
-
const recipe = scanRecipe(repo, normalized, mode, expectation.repositoryRevision, runtime.plugin.version,
|
|
308
|
+
const recipe = scanRecipe(repo, normalized, mode, expectation.repositoryRevision, runtime.plugin.version, preflightConfig, options.failureSeverity, knowledgeBase?.sources, options.maxCostUsd, deepScanOptions(options));
|
|
290
309
|
const workbenchOptions = {
|
|
291
310
|
python,
|
|
292
311
|
pluginRoot: runtime.plugin.pluginRoot,
|
|
@@ -369,6 +388,8 @@ export class CodexSecurity {
|
|
|
369
388
|
}
|
|
370
389
|
activeScan = { id: scanId, options: workbenchOptions };
|
|
371
390
|
checkOpen();
|
|
391
|
+
const basePrompt = scanPrompt(normalized, mode, skillName, scanId, runtime.configPath !== undefined, knowledgeBase !== null, options.scanPrompt);
|
|
392
|
+
checkOpen();
|
|
372
393
|
const feedback = await workbench({
|
|
373
394
|
...workbenchOptions,
|
|
374
395
|
failureMessage: "Could not load Codex Security false-positive feedback",
|
|
@@ -437,9 +458,11 @@ export class CodexSecurity {
|
|
|
437
458
|
CODEX_HOME: runtime.codexHome,
|
|
438
459
|
...runtimePaths,
|
|
439
460
|
};
|
|
440
|
-
const sdkCodexConfig =
|
|
461
|
+
const sdkCodexConfig = { ...preflightConfig };
|
|
441
462
|
delete sdkCodexConfig["projects"];
|
|
463
|
+
const codexPathOverride = environmentValue(this.#dependencies.environment, "CODEX_CLI_PATH")?.trim();
|
|
442
464
|
const codex = this.#dependencies.createCodex({
|
|
465
|
+
...(codexPathOverride === undefined ? {} : { codexPathOverride }),
|
|
443
466
|
...(externalProvider !== null || apiKey === null ? {} : { apiKey }),
|
|
444
467
|
env: definedEnvironment(selectedScanEnvironment(environment, "chatgpt")),
|
|
445
468
|
config: {
|
|
@@ -491,6 +514,8 @@ export class CodexSecurity {
|
|
|
491
514
|
workbenchValidated: true,
|
|
492
515
|
model,
|
|
493
516
|
onThreadStarted: async (threadId) => {
|
|
517
|
+
if (budgetRecovery !== null)
|
|
518
|
+
budgetRecovery.threadId = threadId;
|
|
494
519
|
tracker.start(threadId);
|
|
495
520
|
try {
|
|
496
521
|
await workbench(workbenchOptions, [
|
|
@@ -574,19 +599,61 @@ export class CodexSecurity {
|
|
|
574
599
|
if (runPostScan !== null) {
|
|
575
600
|
const followUp = runPostScan;
|
|
576
601
|
runPostScan = null;
|
|
577
|
-
await
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
});
|
|
589
|
-
|
|
602
|
+
const completedArtifacts = await Promise.all([
|
|
603
|
+
...new Set([
|
|
604
|
+
"scan-manifest.json",
|
|
605
|
+
"findings.json",
|
|
606
|
+
"coverage.json",
|
|
607
|
+
"report.md",
|
|
608
|
+
...result.manifest.scan.artifacts.map((artifact) => artifact.path),
|
|
609
|
+
]),
|
|
610
|
+
].map(async (name) => ({
|
|
611
|
+
name,
|
|
612
|
+
contents: await readFile(await requireScanFile(scanDir, name, name, signal), { signal }),
|
|
613
|
+
})));
|
|
614
|
+
try {
|
|
615
|
+
await runScanEvents({
|
|
616
|
+
thread,
|
|
617
|
+
events: (await followUp()).events,
|
|
618
|
+
signal,
|
|
619
|
+
scanDir,
|
|
620
|
+
pluginRoot: runtime.plugin.installedRoot,
|
|
621
|
+
expectation,
|
|
622
|
+
model,
|
|
623
|
+
onReconnect: options.onReconnect,
|
|
624
|
+
onWorkerStatus: options.onWorkerStatus,
|
|
625
|
+
onObserverError: options.onObserverError,
|
|
626
|
+
});
|
|
627
|
+
checkOpen();
|
|
628
|
+
}
|
|
629
|
+
catch (error) {
|
|
630
|
+
if (signal.aborted || this.#closed)
|
|
631
|
+
throw error;
|
|
632
|
+
for (const artifact of completedArtifacts) {
|
|
633
|
+
const path = join(scanDir, artifact.name);
|
|
634
|
+
const current = await readFile(path, { signal }).catch((readError) => {
|
|
635
|
+
if (readError.code !== "ENOENT")
|
|
636
|
+
throw readError;
|
|
637
|
+
return null;
|
|
638
|
+
});
|
|
639
|
+
if (current?.equals(artifact.contents))
|
|
640
|
+
continue;
|
|
641
|
+
const temporary = join(dirname(path), `.${randomUUID()}.${basename(path)}.restore`);
|
|
642
|
+
try {
|
|
643
|
+
await writeFile(temporary, artifact.contents, {
|
|
644
|
+
flag: "wx",
|
|
645
|
+
mode: 0o600,
|
|
646
|
+
signal,
|
|
647
|
+
});
|
|
648
|
+
await rename(temporary, path);
|
|
649
|
+
}
|
|
650
|
+
finally {
|
|
651
|
+
await rm(temporary, { force: true });
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
await collectResult(result.turnResult, result.threadId, scanDir, runtime.plugin.installedRoot, expectation, signal, true);
|
|
655
|
+
notifyObserver("onWarning", options.onWarning, options.onObserverError, `Could not run post-scan instructions: ${errorMessage(error)}`);
|
|
656
|
+
}
|
|
590
657
|
}
|
|
591
658
|
try {
|
|
592
659
|
const runWorkbench = (args) => workbench(workbenchOptions, args);
|
|
@@ -621,6 +688,54 @@ export class CodexSecurity {
|
|
|
621
688
|
const failure = signal.reason instanceof ScanCostLimitExceededError
|
|
622
689
|
? signal.reason
|
|
623
690
|
: error;
|
|
691
|
+
if (failure instanceof ScanCostLimitExceededError &&
|
|
692
|
+
budgetRecovery !== null &&
|
|
693
|
+
budgetRecovery.threadId !== null &&
|
|
694
|
+
activeScan !== null &&
|
|
695
|
+
!this.#abortController.signal.aborted &&
|
|
696
|
+
options.signal?.aborted !== true) {
|
|
697
|
+
try {
|
|
698
|
+
const completion = await workbench({ ...activeScan.options, signal: undefined }, [
|
|
699
|
+
"complete-budget-exhausted-scan",
|
|
700
|
+
"--scan-id",
|
|
701
|
+
activeScan.id,
|
|
702
|
+
"--cost-json",
|
|
703
|
+
JSON.stringify(snapshot?.cost ?? failure.cost),
|
|
704
|
+
"--message",
|
|
705
|
+
failure.message.slice(0, 2400),
|
|
706
|
+
]);
|
|
707
|
+
activeScan = null;
|
|
708
|
+
runPostScan = null;
|
|
709
|
+
const result = await collectResult({
|
|
710
|
+
status: "completed",
|
|
711
|
+
model: budgetRecovery.model,
|
|
712
|
+
usage: snapshot?.usage ?? null,
|
|
713
|
+
}, budgetRecovery.threadId, scanDir, budgetRecovery.pluginRoot, budgetRecovery.expectation, AbortSignal.any([
|
|
714
|
+
this.#abortController.signal,
|
|
715
|
+
...(options.signal === undefined ? [] : [options.signal]),
|
|
716
|
+
]), true);
|
|
717
|
+
if (result.coverage.completeness !== "partial") {
|
|
718
|
+
throw new IncompleteScanError("Budget-exhausted scan recovery did not report partial coverage.");
|
|
719
|
+
}
|
|
720
|
+
const completedScan = completion["scan"];
|
|
721
|
+
const targetWarnings = new Set(Array.isArray(completion["targetWarnings"])
|
|
722
|
+
? completion["targetWarnings"].filter((warning) => typeof warning === "string")
|
|
723
|
+
: []);
|
|
724
|
+
const warnings = isRecord(completedScan) && Array.isArray(completedScan["warnings"])
|
|
725
|
+
? completedScan["warnings"].filter((warning) => typeof warning === "string")
|
|
726
|
+
: [];
|
|
727
|
+
for (const warning of warnings.length > 0
|
|
728
|
+
? warnings
|
|
729
|
+
: [failure.message]) {
|
|
730
|
+
notifyObserver("onWarning", options.onWarning, options.onObserverError, warning, targetWarnings.has(warning)
|
|
731
|
+
? { kind: "target_changed" }
|
|
732
|
+
: undefined);
|
|
733
|
+
}
|
|
734
|
+
scanFailure = false;
|
|
735
|
+
return result;
|
|
736
|
+
}
|
|
737
|
+
catch { }
|
|
738
|
+
}
|
|
624
739
|
if (activeScan !== null) {
|
|
625
740
|
try {
|
|
626
741
|
await workbench({ ...activeScan.options, signal: undefined }, [
|
|
@@ -696,43 +811,33 @@ export class CodexSecurity {
|
|
|
696
811
|
}
|
|
697
812
|
}
|
|
698
813
|
async loginApiKey(apiKey) {
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
}
|
|
709
|
-
runtime.credentialsAvailable = true;
|
|
710
|
-
this.#runtimeCredentialSource = "api_key";
|
|
814
|
+
await this.#trackOperation(async () => {
|
|
815
|
+
const authentication = await this.#authentication();
|
|
816
|
+
this.#requireOpen();
|
|
817
|
+
const result = await persistApiKey(this.#codexCommand(), authentication.environment, apiKey, this.#abortController.signal);
|
|
818
|
+
if (!result.success) {
|
|
819
|
+
throw new CodexSecurityError(`Codex API-key login failed: ${result.stderr.trim() || result.stdout.trim() || "unknown error"}`);
|
|
820
|
+
}
|
|
821
|
+
await this.#recordLogin(authentication.codexHome, "api_key");
|
|
822
|
+
this.#requireOpen();
|
|
823
|
+
});
|
|
711
824
|
}
|
|
712
825
|
async loginChatGPT() {
|
|
713
|
-
|
|
714
|
-
this.#requireOpen();
|
|
715
|
-
const handle = this.#trackLoginHandle(new CodexLoginHandle(this.#codexCommand(), ["login"], runtime.environment, () => {
|
|
716
|
-
runtime.credentialsAvailable = true;
|
|
717
|
-
this.#runtimeCredentialSource = "stored_credentials";
|
|
718
|
-
}));
|
|
719
|
-
await handle.waitForInstructions();
|
|
720
|
-
this.#requireOpen();
|
|
721
|
-
return handle;
|
|
826
|
+
return await this.#startLogin(false);
|
|
722
827
|
}
|
|
723
828
|
async loginChatGPTDeviceCode() {
|
|
724
|
-
|
|
829
|
+
return await this.#startLogin(true);
|
|
830
|
+
}
|
|
831
|
+
async #startLogin(deviceCode) {
|
|
832
|
+
const authentication = await this.#authentication();
|
|
725
833
|
this.#requireOpen();
|
|
726
|
-
const handle = this.#trackLoginHandle(new CodexLoginHandle(this.#codexCommand(), ["login", "--device-auth"],
|
|
727
|
-
|
|
728
|
-
this.#runtimeCredentialSource = "stored_credentials";
|
|
729
|
-
}));
|
|
730
|
-
await handle.waitForInstructions({ deviceCode: true });
|
|
834
|
+
const handle = this.#trackLoginHandle(new CodexLoginHandle(this.#codexCommand(), deviceCode ? ["login", "--device-auth"] : ["login"], authentication.environment, () => this.#recordLogin(authentication.codexHome, "stored_credentials")));
|
|
835
|
+
await handle.waitForInstructions({ deviceCode });
|
|
731
836
|
this.#requireOpen();
|
|
732
837
|
return handle;
|
|
733
838
|
}
|
|
734
839
|
async account() {
|
|
735
|
-
return await this.#
|
|
840
|
+
return await this.#trackOperation(async () => {
|
|
736
841
|
const apiKey = environmentApiKey(this.#dependencies.environment);
|
|
737
842
|
if (apiKey !== null) {
|
|
738
843
|
return {
|
|
@@ -740,19 +845,25 @@ export class CodexSecurity {
|
|
|
740
845
|
details: "Authenticated with an API key.",
|
|
741
846
|
};
|
|
742
847
|
}
|
|
743
|
-
|
|
848
|
+
const authentication = await this.#authentication();
|
|
849
|
+
this.#requireOpen();
|
|
850
|
+
return await accountStatus(this.#codexCommand(), authentication.environment, this.#abortController.signal);
|
|
744
851
|
});
|
|
745
852
|
}
|
|
746
853
|
async logout() {
|
|
747
|
-
|
|
748
|
-
await
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
854
|
+
await this.#trackOperation(async () => {
|
|
855
|
+
const authentication = await this.#authentication();
|
|
856
|
+
this.#requireOpen();
|
|
857
|
+
await codexLogout(this.#codexCommand(), authentication.environment, this.#abortController.signal);
|
|
858
|
+
if (this.#runtime === null ||
|
|
859
|
+
this.#runtime.persistentCredentialHome === true) {
|
|
860
|
+
await setCodexSecurityCredentialLogout(authentication.codexHome, true);
|
|
861
|
+
}
|
|
862
|
+
if (this.#runtime !== null)
|
|
863
|
+
this.#runtime.credentialsAvailable = false;
|
|
864
|
+
this.#runtimeCredentialSource = null;
|
|
865
|
+
this.#requireOpen();
|
|
866
|
+
});
|
|
756
867
|
}
|
|
757
868
|
async close() {
|
|
758
869
|
if (this.#closePromise !== null)
|
|
@@ -794,15 +905,27 @@ export class CodexSecurity {
|
|
|
794
905
|
async [Symbol.asyncDispose]() {
|
|
795
906
|
await this.close();
|
|
796
907
|
}
|
|
797
|
-
async #
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
908
|
+
async #authentication() {
|
|
909
|
+
this.#requireOpen();
|
|
910
|
+
const environment = selectedScanEnvironment(this.#runtime?.environment ?? this.#dependencies.environment, "chatgpt");
|
|
911
|
+
const codexHome = this.#runtime?.codexHome ??
|
|
912
|
+
(await prepareCodexSecurityCredentialHome(environment));
|
|
913
|
+
return {
|
|
914
|
+
codexHome,
|
|
915
|
+
environment: {
|
|
916
|
+
...withoutCodexHome(environment),
|
|
917
|
+
CODEX_HOME: codexHome,
|
|
918
|
+
},
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
async #recordLogin(codexHome, source) {
|
|
922
|
+
if (this.#runtime === null ||
|
|
923
|
+
this.#runtime.persistentCredentialHome === true) {
|
|
924
|
+
await setCodexSecurityCredentialLogout(codexHome, false);
|
|
925
|
+
}
|
|
926
|
+
if (this.#runtime !== null)
|
|
927
|
+
this.#runtime.credentialsAvailable = true;
|
|
928
|
+
this.#runtimeCredentialSource = source;
|
|
806
929
|
}
|
|
807
930
|
async #trackOperation(operation) {
|
|
808
931
|
this.#requireOpen();
|
|
@@ -820,12 +943,12 @@ export class CodexSecurity {
|
|
|
820
943
|
}
|
|
821
944
|
}
|
|
822
945
|
}
|
|
823
|
-
async #ensureRuntime(signal, temporaryRoot, validateLocation, auth = "auto",
|
|
946
|
+
async #ensureRuntime(signal, temporaryRoot, validateLocation, auth = "auto", requestedConfig) {
|
|
824
947
|
this.#requireOpen();
|
|
825
948
|
if (this.#runtime !== null)
|
|
826
949
|
return this.#runtime;
|
|
827
950
|
if (this.#runtimePromise === null) {
|
|
828
|
-
const runtimePromise = this.#prepareRuntime(signal ?? this.#abortController.signal, temporaryRoot, validateLocation, auth,
|
|
951
|
+
const runtimePromise = this.#prepareRuntime(signal ?? this.#abortController.signal, temporaryRoot, validateLocation, auth, requestedConfig);
|
|
829
952
|
this.#runtimePromise = runtimePromise;
|
|
830
953
|
void runtimePromise.catch(() => {
|
|
831
954
|
if (this.#runtimePromise === runtimePromise) {
|
|
@@ -847,17 +970,15 @@ export class CodexSecurity {
|
|
|
847
970
|
return handle;
|
|
848
971
|
}
|
|
849
972
|
#codexCommand() {
|
|
850
|
-
return (this.#dependencies.resolveCodexCommand ??
|
|
973
|
+
return (this.#dependencies.resolveCodexCommand?.() ??
|
|
974
|
+
resolveCodexCommand(this.#dependencies.environment));
|
|
851
975
|
}
|
|
852
|
-
async #refreshPersistentRuntime(runtime, environment, signal) {
|
|
976
|
+
async #refreshPersistentRuntime(runtime, environment, signal, mergedConfig) {
|
|
853
977
|
throwIfAborted(signal);
|
|
854
|
-
const mergedConfig = await mergedCodexConfig(this.config);
|
|
855
978
|
const config = await preserveCodexSecurityPluginRegistration(runtime.codexHome, scanRuntimeCodexConfig(mergedConfig, codexSecurityStateDirectory(environment), runtime.codexHome));
|
|
856
979
|
await writeCodexConfig(join(runtime.codexHome, "config.toml"), config);
|
|
857
|
-
if (runtime.configPath !== undefined) {
|
|
858
|
-
await writeCodexConfig(runtime.configPath, scanPreflightCodexConfig(mergedConfig));
|
|
859
|
-
}
|
|
860
980
|
runtime.plugin = await bootstrapPlugin(runtime.codexHome, runtime.plugin.pluginRoot, {
|
|
981
|
+
codexCommand: this.#codexCommand(),
|
|
861
982
|
environment: withoutCodexHome(environment),
|
|
862
983
|
signal,
|
|
863
984
|
});
|
|
@@ -878,6 +999,8 @@ export class CodexSecurity {
|
|
|
878
999
|
throwIfAborted(signal);
|
|
879
1000
|
const mode = options.mode ?? "standard";
|
|
880
1001
|
validateMode(normalized, mode);
|
|
1002
|
+
await validateCommittedDiffCheckout(repo, normalized, signal);
|
|
1003
|
+
throwIfAborted(signal);
|
|
881
1004
|
const protectedRoot = (await enclosingGitWorktreeRoot(repo, signal)) ?? repo;
|
|
882
1005
|
const requestedOutput = await validateOutputDir(options.outputDir, options.archiveExisting);
|
|
883
1006
|
if (requestedOutput !== null) {
|
|
@@ -891,10 +1014,13 @@ export class CodexSecurity {
|
|
|
891
1014
|
protectedRoot,
|
|
892
1015
|
};
|
|
893
1016
|
}
|
|
894
|
-
async #prepareRuntime(signal, temporaryRoot, validateLocation, auth = "auto",
|
|
1017
|
+
async #prepareRuntime(signal, temporaryRoot, validateLocation, auth = "auto", requestedConfig) {
|
|
895
1018
|
if (this.#dependencies.prepareRuntime !== undefined) {
|
|
896
1019
|
return await this.#dependencies.prepareRuntime(this.config, signal);
|
|
897
1020
|
}
|
|
1021
|
+
const modelProvider = requestedConfig === undefined
|
|
1022
|
+
? undefined
|
|
1023
|
+
: scanModelProvider(requestedConfig);
|
|
898
1024
|
const processEnvironment = selectedScanEnvironment(this.#dependencies.environment, auth, modelProvider);
|
|
899
1025
|
const codexHome = validateLocation === undefined
|
|
900
1026
|
? await prepareCodexSecurityCredentialHome(processEnvironment)
|
|
@@ -907,13 +1033,13 @@ export class CodexSecurity {
|
|
|
907
1033
|
const nodeAmbientHome = join(homedir(), ".codex");
|
|
908
1034
|
const configuredAmbientHome = environmentValue(processEnvironment, "CODEX_HOME");
|
|
909
1035
|
const ambientHome = configuredAmbientHome ?? nodeAmbientHome;
|
|
910
|
-
const mergedConfig = await mergedCodexConfig(this.config);
|
|
1036
|
+
const mergedConfig = requestedConfig ?? (await mergedCodexConfig(this.config));
|
|
911
1037
|
const codexConfig = await preserveCodexSecurityPluginRegistration(codexHome, scanRuntimeCodexConfig(mergedConfig, codexSecurityStateDirectory(processEnvironment), codexHome));
|
|
912
1038
|
await writeCodexConfig(join(codexHome, "config.toml"), codexConfig);
|
|
913
1039
|
const configPath = join(bootstrapWorkspace, "config-preflight.toml");
|
|
914
|
-
await writeCodexConfig(configPath, scanPreflightCodexConfig(mergedConfig));
|
|
915
1040
|
throwIfAborted(signal);
|
|
916
1041
|
const plugin = await bootstrapPlugin(codexHome, pluginRoot, {
|
|
1042
|
+
codexCommand: this.#codexCommand(),
|
|
917
1043
|
environment: withoutCodexHome(processEnvironment),
|
|
918
1044
|
signal,
|
|
919
1045
|
});
|
|
@@ -981,7 +1107,12 @@ function deepScanOptions(options) {
|
|
|
981
1107
|
if ((options.mode ?? "standard") !== "deep") {
|
|
982
1108
|
throw new CodexSecurityError("Deep scan settings require deep mode.");
|
|
983
1109
|
}
|
|
984
|
-
if (
|
|
1110
|
+
if (name === "maxTimeHours") {
|
|
1111
|
+
if (!Number.isFinite(value) || value <= 0 || value > 96) {
|
|
1112
|
+
throw new CodexSecurityError("Deep scan maxTimeHours must be a positive number no greater than 96.");
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
else if (!Number.isSafeInteger(value) || value < minimum) {
|
|
985
1116
|
throw new CodexSecurityError(`Deep scan ${name} must be ${minimum === 0 ? "a non-negative" : "a positive"} integer.`);
|
|
986
1117
|
}
|
|
987
1118
|
selected[name] = value;
|
|
@@ -1238,23 +1369,17 @@ function trustedAccessWarning(status, authentication) {
|
|
|
1238
1369
|
const action = apiOrganization ? "your organization's access" : "your access";
|
|
1239
1370
|
return `Some cybersecurity requests or findings may be refused because ${access} could not be verified. Check ${action} or apply at ${applicationUrl}.`;
|
|
1240
1371
|
}
|
|
1241
|
-
|
|
1242
|
-
const skillName = skillNameFor(target, mode);
|
|
1243
|
-
const skillPath = join(pluginRoot, "skills", skillName, "SKILL.md");
|
|
1244
|
-
const metadata = await lstat(skillPath).catch(() => null);
|
|
1245
|
-
if (metadata === null || !metadata.isFile() || metadata.isSymbolicLink()) {
|
|
1246
|
-
throw new IncompleteScanError(`Installed plugin is missing scan skill: ${skillName}`);
|
|
1247
|
-
}
|
|
1372
|
+
function scanPrompt(target, mode, skillName, scanId, hasConfigPath = false, hasKnowledgeBase = false, additionalPrompt) {
|
|
1248
1373
|
return [
|
|
1249
1374
|
`Use the installed $codex-security:${skillName} skill at "$CODEX_SECURITY_PLUGIN_ROOT/skills/${skillName}/SKILL.md".`,
|
|
1250
1375
|
"Run this Codex Security scan non-interactively.",
|
|
1251
1376
|
...(mode === "deep"
|
|
1252
1377
|
? [
|
|
1253
|
-
|
|
1378
|
+
`The SDK has already registered this scan. Call start_codex_security_deep_scan with ${JSON.stringify({ scanId })}; never pass targetPath or create another scan.`,
|
|
1254
1379
|
]
|
|
1255
1380
|
: skillName === "security-scan"
|
|
1256
1381
|
? [
|
|
1257
|
-
|
|
1382
|
+
`The SDK has already registered this scan. Use exactly ${JSON.stringify(scanId)} and "$CODEX_SECURITY_SCAN_DIR"; never call a scan-start or completion tool, and leave finalization to the SDK.`,
|
|
1258
1383
|
]
|
|
1259
1384
|
: []),
|
|
1260
1385
|
...(skillName === "security-scan"
|
|
@@ -1270,7 +1395,7 @@ async function scanPrompt(pluginRoot, target, mode, hasConfigPath = false, hasKn
|
|
|
1270
1395
|
'Use "$PYTHON" as <python_command> for every plugin helper; replace any literal python or python3 helper invocation with this exact interpreter.',
|
|
1271
1396
|
'Repository root: "$CODEX_SECURITY_REPOSITORY"',
|
|
1272
1397
|
'Use this exact scan directory for all scan output: "$CODEX_SECURITY_SCAN_DIR"',
|
|
1273
|
-
|
|
1398
|
+
`Use exactly ${JSON.stringify(scanId)} as the scan ID in the manifest, findings, and coverage.`,
|
|
1274
1399
|
'Use exactly "$CODEX_SECURITY_TARGET_ID" as scan.target.targetId; do not derive a different target ID.',
|
|
1275
1400
|
'Use exactly "$CODEX_SECURITY_TARGET_DISPLAY_NAME" as scan.target.displayName; do not infer a display name from the Git remote.',
|
|
1276
1401
|
'Use exactly "$CODEX_SECURITY_TARGET_KIND" as scan.target.kind; do not infer the target kind from the checkout.',
|
|
@@ -1325,7 +1450,7 @@ function targetInstruction(target) {
|
|
|
1325
1450
|
}
|
|
1326
1451
|
return `Scan target: staged and unstaged working-tree changes against ${target.base}.`;
|
|
1327
1452
|
}
|
|
1328
|
-
function scanRecipe(repository, target, mode, repositoryRevision, pluginVersion,
|
|
1453
|
+
function scanRecipe(repository, target, mode, repositoryRevision, pluginVersion, preflightConfig, failOnSeverity, knowledgeBasePaths, maxCostUsd, deepScan) {
|
|
1329
1454
|
return {
|
|
1330
1455
|
repository,
|
|
1331
1456
|
target: {
|
|
@@ -1339,7 +1464,7 @@ function scanRecipe(repository, target, mode, repositoryRevision, pluginVersion,
|
|
|
1339
1464
|
mode,
|
|
1340
1465
|
...(repositoryRevision === null ? {} : { repositoryRevision }),
|
|
1341
1466
|
pluginVersion,
|
|
1342
|
-
config:
|
|
1467
|
+
config: preflightConfig,
|
|
1343
1468
|
...(failOnSeverity === undefined ? {} : { failOnSeverity }),
|
|
1344
1469
|
...(knowledgeBasePaths === undefined ? {} : { knowledgeBasePaths }),
|
|
1345
1470
|
...(maxCostUsd === undefined ? {} : { maxCostUsd }),
|