@openai/codex-security 0.1.3 → 0.1.5
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 +224 -14
- package/_bundled_plugin/examples/completed-scan/findings.json +6 -0
- package/_bundled_plugin/examples/completed-scan/scan-manifest.json +1 -1
- package/_bundled_plugin/references/finding-detail-fields.md +10 -2
- package/_bundled_plugin/references/scan-contract.md +2 -0
- package/_bundled_plugin/scripts/finalize_scan_contract.py +18 -0
- package/_bundled_plugin/scripts/workbench_cli.py +4 -0
- package/_bundled_plugin/scripts/workbench_db.py +93 -23
- package/_bundled_plugin/scripts/workbench_scan_start.py +13 -0
- package/_bundled_plugin/scripts/workbench_target.py +14 -1
- package/dist/api.d.ts +3 -2
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +288 -70
- package/dist/api.js.map +1 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +262 -44
- package/dist/auth.js.map +1 -1
- package/dist/cli.d.ts +4 -3
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +364 -154
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +29 -1
- package/dist/config.js.map +1 -1
- package/dist/contract.d.ts +1 -0
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +33 -22
- package/dist/contract.js.map +1 -1
- package/dist/cost.d.ts.map +1 -1
- package/dist/cost.js +57 -14
- package/dist/cost.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -1
- package/dist/multiscan.d.ts.map +1 -1
- package/dist/multiscan.js +2 -7
- package/dist/multiscan.js.map +1 -1
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +655 -20
- package/dist/runtime.js.map +1 -1
- package/dist/scan-comparison.d.ts +2 -0
- package/dist/scan-comparison.d.ts.map +1 -1
- package/dist/scan-comparison.js +26 -2
- package/dist/scan-comparison.js.map +1 -1
- package/dist/trusted-executable.js +1 -1
- package/dist/trusted-executable.js.map +1 -1
- package/package.json +5 -4
package/dist/api.js
CHANGED
|
@@ -8,12 +8,12 @@ import { accountStatus, CodexLoginHandle, loginApiKey as persistApiKey, logout a
|
|
|
8
8
|
import { mergedCodexConfig, scanModelConfiguration, writeCodexConfig, } from "./config.js";
|
|
9
9
|
import { estimateScanCost, ScanCostTracker } from "./cost.js";
|
|
10
10
|
import { loadContract, requireScanFile, } from "./contract.js";
|
|
11
|
-
import { AuthenticationRequiredError, CodexSecurityError, IncompleteScanError, OutputDirectoryError, OutputInsideProtectedRootError, ScanCostLimitExceededError, ScanInterruptedError, } from "./errors.js";
|
|
11
|
+
import { AuthenticationRequiredError, CodexSecurityError, IncompleteScanError, OutputDirectoryError, OutputInsideProtectedRootError, redactedErrorMessage, ScanCostLimitExceededError, ScanInterruptedError, } from "./errors.js";
|
|
12
12
|
import { prepareKnowledgeBase, } from "./knowledge-base.js";
|
|
13
13
|
import { ScanResult } from "./result.js";
|
|
14
14
|
import { workerStatusFromEvent, } from "./worker-progress.js";
|
|
15
15
|
import { CODEX_EXECUTABLE_VERSION, CODEX_SDK_VERSION } from "./version.js";
|
|
16
|
-
import { bootstrapPlugin, cleanupSdkDirectory, codexSecurityStateDirectory, createIsolatedHome, importAmbientAuth, pluginExecutionEnvironment, planOutputArchive, prepareOutputDir, preparePersistentScanRoot, requireModelSafeOutputDir, resolveCodexCommand, resolvePluginPath, resolvePluginPython, runWorkbench, validateOutputDir, } from "./runtime.js";
|
|
16
|
+
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
17
|
import { enclosingGitWorktreeRoot, normalizeRepository, normalizeTarget, repositoryRevision, resolveRepositoryPath, validatedGitEnvironment, validateMode, } from "./targets.js";
|
|
18
18
|
const DEFAULT_DEPENDENCIES = {
|
|
19
19
|
createCodex: (options) => new Codex(options),
|
|
@@ -84,6 +84,9 @@ export class CodexSecurity {
|
|
|
84
84
|
let targetPathsFile = null;
|
|
85
85
|
let knowledgeBase = null;
|
|
86
86
|
let costTracker = null;
|
|
87
|
+
let releaseCredentialHome = null;
|
|
88
|
+
let scanFailure = false;
|
|
89
|
+
let completionCost = null;
|
|
87
90
|
let activeScan = null;
|
|
88
91
|
const workbench = this.#dependencies.runWorkbench ?? runWorkbench;
|
|
89
92
|
try {
|
|
@@ -112,7 +115,22 @@ export class CodexSecurity {
|
|
|
112
115
|
checkOpen();
|
|
113
116
|
const authentication = scanAuthentication(this.#dependencies.environment, options.auth);
|
|
114
117
|
const scanEnvironment = selectedScanEnvironment(this.#dependencies.environment, options.auth);
|
|
118
|
+
if (authentication.method === "stored_credentials" &&
|
|
119
|
+
this.#dependencies.prepareRuntime === undefined) {
|
|
120
|
+
const credentialHome = await prepareCodexSecurityCredentialHome(scanEnvironment, (path) => requireOutputOutsideRepository(protectedRoot, path, "runtime"));
|
|
121
|
+
releaseCredentialHome = await acquireCodexSecurityCredentialHomeLock(credentialHome, signal);
|
|
122
|
+
}
|
|
123
|
+
const previousRuntime = this.#runtime;
|
|
115
124
|
const runtime = await this.#ensureRuntime(signal, temporaryRoot, (path) => requireOutputOutsideRepository(protectedRoot, path, "runtime"), options.auth);
|
|
125
|
+
if (runtime === previousRuntime &&
|
|
126
|
+
runtime.persistentCredentialHome === true &&
|
|
127
|
+
this.#dependencies.prepareRuntime === undefined) {
|
|
128
|
+
await this.#refreshPersistentRuntime(runtime, scanEnvironment, signal);
|
|
129
|
+
}
|
|
130
|
+
const effectiveConfig = runtime.effectiveConfig ?? (await mergedCodexConfig(this.config));
|
|
131
|
+
if (runtime.configPath !== undefined) {
|
|
132
|
+
await writeCodexConfig(runtime.configPath, scanPreflightCodexConfig(effectiveConfig, repo));
|
|
133
|
+
}
|
|
116
134
|
const runtimeHome = await realpath(runtime.codexHome);
|
|
117
135
|
requireOutputOutsideRepository(protectedRoot, runtimeHome, "runtime");
|
|
118
136
|
if (options.expectedPluginVersion !== undefined &&
|
|
@@ -133,15 +151,17 @@ export class CodexSecurity {
|
|
|
133
151
|
? environmentApiKey(this.#dependencies.environment)
|
|
134
152
|
: null;
|
|
135
153
|
if (apiKey !== null) {
|
|
136
|
-
const codexCommand = this.#codexCommand();
|
|
137
|
-
const login = await persistApiKey(codexCommand, runtime.environment, apiKey, signal);
|
|
138
|
-
if (!login.success) {
|
|
139
|
-
throw new CodexSecurityError(`Codex API-key login failed: ${login.stderr.trim() || login.stdout.trim() || "unknown error"}`);
|
|
140
|
-
}
|
|
141
|
-
runtime.credentialsAvailable = true;
|
|
142
154
|
this.#runtimeCredentialSource = "api_key";
|
|
143
155
|
}
|
|
144
|
-
if (!runtime.credentialsAvailable
|
|
156
|
+
if (!runtime.credentialsAvailable &&
|
|
157
|
+
authentication.method === "stored_credentials") {
|
|
158
|
+
const status = await accountStatus(this.#codexCommand(), runtime.environment, signal);
|
|
159
|
+
runtime.credentialsAvailable = status.authenticated;
|
|
160
|
+
this.#runtimeCredentialSource = status.authenticated
|
|
161
|
+
? "stored_credentials"
|
|
162
|
+
: null;
|
|
163
|
+
}
|
|
164
|
+
if (!runtime.credentialsAvailable && apiKey === null) {
|
|
145
165
|
throw new AuthenticationRequiredError("No credentials were found. Run 'codex-security login', use " +
|
|
146
166
|
"'codex-security login --device-auth' on a remote or headless machine, or set " +
|
|
147
167
|
"OPENAI_API_KEY or CODEX_API_KEY for CI.");
|
|
@@ -187,7 +207,6 @@ export class CodexSecurity {
|
|
|
187
207
|
mode,
|
|
188
208
|
pluginVersion: runtime.plugin.version,
|
|
189
209
|
};
|
|
190
|
-
const effectiveConfig = runtime.effectiveConfig ?? (await mergedCodexConfig(this.config));
|
|
191
210
|
const { model } = scanModelConfiguration(effectiveConfig);
|
|
192
211
|
validateScanCostLimit(options.maxCostUsd, model);
|
|
193
212
|
const tracker = new ScanCostTracker({
|
|
@@ -204,7 +223,7 @@ export class CodexSecurity {
|
|
|
204
223
|
onError: (error) => costAbortController.abort(error),
|
|
205
224
|
});
|
|
206
225
|
costTracker = tracker;
|
|
207
|
-
const recipe = scanRecipe(repo, normalized, mode, expectation.repositoryRevision, runtime.plugin.version, effectiveConfig, options.failureSeverity, knowledgeBase?.sources, options.maxCostUsd);
|
|
226
|
+
const recipe = scanRecipe(repo, protectedRoot, normalized, mode, expectation.repositoryRevision, runtime.plugin.version, effectiveConfig, options.failureSeverity, knowledgeBase?.sources, options.maxCostUsd);
|
|
208
227
|
const workbenchOptions = {
|
|
209
228
|
python,
|
|
210
229
|
pluginRoot: runtime.plugin.pluginRoot,
|
|
@@ -233,11 +252,43 @@ export class CodexSecurity {
|
|
|
233
252
|
]);
|
|
234
253
|
const scanId = registration["scanId"];
|
|
235
254
|
const targetId = registration["targetId"];
|
|
255
|
+
const contract = registration["contract"];
|
|
256
|
+
const contractTarget = isRecord(contract)
|
|
257
|
+
? contract["target"]
|
|
258
|
+
: undefined;
|
|
259
|
+
const allowedKinds = isRecord(contractTarget)
|
|
260
|
+
? contractTarget["allowedKinds"]
|
|
261
|
+
: undefined;
|
|
262
|
+
const targetKind = Array.isArray(allowedKinds) && allowedKinds.length === 1
|
|
263
|
+
? allowedKinds[0]
|
|
264
|
+
: undefined;
|
|
265
|
+
const diffTarget = isRecord(contract)
|
|
266
|
+
? contract["diffTarget"]
|
|
267
|
+
: undefined;
|
|
268
|
+
const snapshotDigest = targetKind === "git_diff" && isRecord(diffTarget)
|
|
269
|
+
? diffTarget["contentDigest"]
|
|
270
|
+
: isRecord(contractTarget)
|
|
271
|
+
? contractTarget["requiredSnapshotDigest"]
|
|
272
|
+
: undefined;
|
|
273
|
+
const registeredRevision = registration["targetRevision"];
|
|
236
274
|
if (typeof scanId !== "string" ||
|
|
237
275
|
typeof targetId !== "string" ||
|
|
238
|
-
registration["scanDir"] !== scanDir
|
|
276
|
+
registration["scanDir"] !== scanDir ||
|
|
277
|
+
typeof targetKind !== "string" ||
|
|
278
|
+
![
|
|
279
|
+
"git_revision",
|
|
280
|
+
"git_worktree",
|
|
281
|
+
"git_diff",
|
|
282
|
+
"directory_snapshot",
|
|
283
|
+
].includes(targetKind) ||
|
|
284
|
+
(snapshotDigest !== undefined && typeof snapshotDigest !== "string") ||
|
|
285
|
+
((targetKind === "git_worktree" ||
|
|
286
|
+
targetKind === "directory_snapshot") &&
|
|
287
|
+
typeof snapshotDigest !== "string") ||
|
|
288
|
+
typeof registeredRevision !== "string") {
|
|
239
289
|
throw new CodexSecurityError("The Codex Security workbench returned an invalid scan registration.");
|
|
240
290
|
}
|
|
291
|
+
const targetRevision = registeredRevision === "unversioned" ? null : registeredRevision;
|
|
241
292
|
activeScan = { id: scanId, options: workbenchOptions };
|
|
242
293
|
checkOpen();
|
|
243
294
|
const feedback = await workbench({
|
|
@@ -281,6 +332,13 @@ export class CodexSecurity {
|
|
|
281
332
|
CODEX_SECURITY_SCAN_ID: scanId,
|
|
282
333
|
CODEX_SECURITY_TARGET_ID: targetId,
|
|
283
334
|
CODEX_SECURITY_TARGET_DISPLAY_NAME: basename(repo),
|
|
335
|
+
CODEX_SECURITY_TARGET_KIND: targetKind,
|
|
336
|
+
...(targetRevision === null
|
|
337
|
+
? {}
|
|
338
|
+
: { CODEX_SECURITY_TARGET_REVISION: targetRevision }),
|
|
339
|
+
...(typeof snapshotDigest === "string"
|
|
340
|
+
? { CODEX_SECURITY_TARGET_SNAPSHOT_DIGEST: snapshotDigest }
|
|
341
|
+
: {}),
|
|
284
342
|
...(knowledgeBase === null
|
|
285
343
|
? {}
|
|
286
344
|
: { CODEX_SECURITY_KNOWLEDGE_BASE: knowledgeBase.path }),
|
|
@@ -297,7 +355,8 @@ export class CodexSecurity {
|
|
|
297
355
|
...runtimePaths,
|
|
298
356
|
};
|
|
299
357
|
const codex = this.#dependencies.createCodex({
|
|
300
|
-
|
|
358
|
+
...(apiKey === null ? {} : { apiKey }),
|
|
359
|
+
env: definedEnvironment(selectedScanEnvironment(environment, "chatgpt")),
|
|
301
360
|
config: {
|
|
302
361
|
default_permissions: SCAN_PERMISSION_PROFILE,
|
|
303
362
|
allow_login_shell: false,
|
|
@@ -335,6 +394,7 @@ export class CodexSecurity {
|
|
|
335
394
|
scanDir,
|
|
336
395
|
pluginRoot: runtime.plugin.installedRoot,
|
|
337
396
|
expectation,
|
|
397
|
+
workbenchValidated: true,
|
|
338
398
|
model,
|
|
339
399
|
onThreadStarted: (threadId) => tracker.start(threadId),
|
|
340
400
|
onFinalize: async (usage) => {
|
|
@@ -343,23 +403,12 @@ export class CodexSecurity {
|
|
|
343
403
|
if (options.maxCostUsd !== undefined && snapshot.cost === null) {
|
|
344
404
|
notifyObserver("onWarning", options.onWarning, options.onObserverError, "Scan completed, but its cost limit could not be verified because model pricing or token usage is unavailable.");
|
|
345
405
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
"
|
|
406
|
+
completionCost = snapshot.cost;
|
|
407
|
+
await workbench(workbenchOptions, [
|
|
408
|
+
"prepare-scan-completion",
|
|
349
409
|
"--scan-id",
|
|
350
410
|
scanId,
|
|
351
|
-
...(cost === null ? [] : ["--cost-json", JSON.stringify(cost)]),
|
|
352
411
|
]);
|
|
353
|
-
activeScan = null;
|
|
354
|
-
const completedScan = completion["scan"];
|
|
355
|
-
if (isRecord(completedScan) &&
|
|
356
|
-
Array.isArray(completedScan["warnings"])) {
|
|
357
|
-
for (const warning of completedScan["warnings"]) {
|
|
358
|
-
if (typeof warning === "string") {
|
|
359
|
-
notifyObserver("onWarning", options.onWarning, options.onObserverError, warning);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
412
|
return snapshot.usage;
|
|
364
413
|
},
|
|
365
414
|
onScanStarted: options.onScanStarted,
|
|
@@ -368,9 +417,29 @@ export class CodexSecurity {
|
|
|
368
417
|
onObserverError: options.onObserverError,
|
|
369
418
|
});
|
|
370
419
|
checkOpen();
|
|
420
|
+
const completion = await workbench(workbenchOptions, [
|
|
421
|
+
"complete-scan",
|
|
422
|
+
"--scan-id",
|
|
423
|
+
scanId,
|
|
424
|
+
...(completionCost === null
|
|
425
|
+
? []
|
|
426
|
+
: ["--cost-json", JSON.stringify(completionCost)]),
|
|
427
|
+
]);
|
|
428
|
+
activeScan = null;
|
|
429
|
+
const completedScan = completion["scan"];
|
|
430
|
+
if (isRecord(completedScan) && Array.isArray(completedScan["warnings"])) {
|
|
431
|
+
for (const warning of completedScan["warnings"]) {
|
|
432
|
+
if (typeof warning === "string") {
|
|
433
|
+
notifyObserver("onWarning", options.onWarning, options.onObserverError, warning);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
371
437
|
return result;
|
|
372
438
|
}
|
|
373
439
|
catch (error) {
|
|
440
|
+
// Recorded first: everything below can throw a different error for this same failed
|
|
441
|
+
// scan, and cleanup must treat all of those as a failure it is not allowed to mask.
|
|
442
|
+
scanFailure = true;
|
|
374
443
|
const snapshot = await costTracker?.stop().catch(() => null);
|
|
375
444
|
const failure = signal.reason instanceof ScanCostLimitExceededError
|
|
376
445
|
? signal.reason
|
|
@@ -381,10 +450,10 @@ export class CodexSecurity {
|
|
|
381
450
|
"fail-scan",
|
|
382
451
|
"--scan-id",
|
|
383
452
|
activeScan.id,
|
|
453
|
+
// Redact before truncating: the stored message is read back by
|
|
454
|
+
// `scans show` and travels inside the results directory.
|
|
384
455
|
"--message",
|
|
385
|
-
(failure
|
|
386
|
-
? failure.message
|
|
387
|
-
: String(failure)).slice(0, 2400),
|
|
456
|
+
redactedErrorMessage(failure).slice(0, 2400),
|
|
388
457
|
...(snapshot?.cost
|
|
389
458
|
? ["--cost-json", JSON.stringify(snapshot.cost)]
|
|
390
459
|
: []),
|
|
@@ -400,25 +469,60 @@ export class CodexSecurity {
|
|
|
400
469
|
throw failure;
|
|
401
470
|
}
|
|
402
471
|
finally {
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
472
|
+
// Removing the temporary scan inputs is best effort. A throw here would replace the
|
|
473
|
+
// outcome the try and catch blocks already produced, so these failures are reported
|
|
474
|
+
// as warnings: a scan that failed has to say why it failed, not why its temporary
|
|
475
|
+
// files outlived it. The whole step is guarded so that a cleanup which rejects, or
|
|
476
|
+
// throws synchronously, still cannot skip the credential lock release below.
|
|
477
|
+
try {
|
|
478
|
+
for (const cleanup of await Promise.allSettled([
|
|
479
|
+
knowledgeBase?.cleanup(),
|
|
480
|
+
removeTargetPathsFile(targetPathsFile),
|
|
481
|
+
])) {
|
|
482
|
+
if (cleanup.status === "rejected") {
|
|
483
|
+
warnCleanupFailed(options, cleanup.reason);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
catch (error) {
|
|
488
|
+
warnCleanupFailed(options, error);
|
|
489
|
+
}
|
|
490
|
+
finally {
|
|
491
|
+
// Releasing the credential home lock is not best effort, so it keeps its own
|
|
492
|
+
// finally and runs even if reporting the failures above went wrong. The release
|
|
493
|
+
// only marks itself done once the lock directory is gone, so a failure leaves an
|
|
494
|
+
// owner.json naming this still-running process; recoverStaleCredentialHomeLock
|
|
495
|
+
// then refuses to reclaim it because that pid is alive, and later scans in this
|
|
496
|
+
// process wait on a lock nothing frees. Reporting success while leaving the client
|
|
497
|
+
// in that state is worse than failing, so the failure is only downgraded to a
|
|
498
|
+
// warning when the scan already failed and that error is the one worth keeping.
|
|
499
|
+
try {
|
|
500
|
+
await releaseCredentialHome?.();
|
|
501
|
+
}
|
|
502
|
+
catch (error) {
|
|
503
|
+
if (!scanFailure)
|
|
504
|
+
throw error;
|
|
505
|
+
warnCleanupFailed(options, error);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
407
508
|
}
|
|
408
509
|
}
|
|
409
510
|
async loginApiKey(apiKey) {
|
|
410
511
|
const { result, runtime } = await this.#runOperation(async (preparedRuntime, signal) => ({
|
|
411
512
|
runtime: preparedRuntime,
|
|
412
513
|
result: await persistApiKey(this.#codexCommand(), preparedRuntime.environment, apiKey, signal),
|
|
413
|
-
}));
|
|
514
|
+
}), "chatgpt");
|
|
414
515
|
if (!result.success) {
|
|
415
516
|
throw new CodexSecurityError(`Codex API-key login failed: ${result.stderr.trim() || result.stdout.trim() || "unknown error"}`);
|
|
416
517
|
}
|
|
518
|
+
if (runtime.persistentCredentialHome === true) {
|
|
519
|
+
await setCodexSecurityCredentialLogout(runtime.codexHome, false);
|
|
520
|
+
}
|
|
417
521
|
runtime.credentialsAvailable = true;
|
|
418
522
|
this.#runtimeCredentialSource = "api_key";
|
|
419
523
|
}
|
|
420
524
|
async loginChatGPT() {
|
|
421
|
-
const runtime = await this.#ensureRuntime();
|
|
525
|
+
const runtime = await this.#ensureRuntime(undefined, undefined, undefined, "chatgpt");
|
|
422
526
|
this.#requireOpen();
|
|
423
527
|
const handle = this.#trackLoginHandle(new CodexLoginHandle(this.#codexCommand(), ["login"], runtime.environment, () => {
|
|
424
528
|
runtime.credentialsAvailable = true;
|
|
@@ -429,7 +533,7 @@ export class CodexSecurity {
|
|
|
429
533
|
return handle;
|
|
430
534
|
}
|
|
431
535
|
async loginChatGPTDeviceCode() {
|
|
432
|
-
const runtime = await this.#ensureRuntime();
|
|
536
|
+
const runtime = await this.#ensureRuntime(undefined, undefined, undefined, "chatgpt");
|
|
433
537
|
this.#requireOpen();
|
|
434
538
|
const handle = this.#trackLoginHandle(new CodexLoginHandle(this.#codexCommand(), ["login", "--device-auth"], runtime.environment, () => {
|
|
435
539
|
runtime.credentialsAvailable = true;
|
|
@@ -455,7 +559,10 @@ export class CodexSecurity {
|
|
|
455
559
|
const runtime = await this.#runOperation(async (preparedRuntime, signal) => {
|
|
456
560
|
await codexLogout(this.#codexCommand(), preparedRuntime.environment, signal);
|
|
457
561
|
return preparedRuntime;
|
|
458
|
-
});
|
|
562
|
+
}, "chatgpt");
|
|
563
|
+
if (runtime.persistentCredentialHome === true) {
|
|
564
|
+
await setCodexSecurityCredentialLogout(runtime.codexHome, true);
|
|
565
|
+
}
|
|
459
566
|
runtime.credentialsAvailable = false;
|
|
460
567
|
this.#runtimeCredentialSource = null;
|
|
461
568
|
}
|
|
@@ -481,22 +588,28 @@ export class CodexSecurity {
|
|
|
481
588
|
this.#runtime = null;
|
|
482
589
|
this.#runtimePromise = null;
|
|
483
590
|
if (runtime !== null && runtime !== undefined) {
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
591
|
+
await this.#cleanupRuntime(runtime);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
async #cleanupRuntime(runtime) {
|
|
595
|
+
const cleanupResults = await Promise.allSettled([
|
|
596
|
+
runtime.persistentCredentialHome ? undefined : runtime.codexHome,
|
|
597
|
+
runtime.bootstrapWorkspace,
|
|
598
|
+
]
|
|
599
|
+
.filter((path) => path !== undefined)
|
|
600
|
+
.map((path) => cleanupSdkDirectory(path)));
|
|
601
|
+
for (const result of cleanupResults) {
|
|
602
|
+
if (result.status === "rejected")
|
|
603
|
+
throw result.reason;
|
|
491
604
|
}
|
|
492
605
|
}
|
|
493
606
|
async [Symbol.asyncDispose]() {
|
|
494
607
|
await this.close();
|
|
495
608
|
}
|
|
496
|
-
async #runOperation(operation) {
|
|
609
|
+
async #runOperation(operation, auth = "auto") {
|
|
497
610
|
return await this.#trackOperation(async () => {
|
|
498
611
|
const signal = this.#abortController.signal;
|
|
499
|
-
const runtime = await this.#ensureRuntime(signal);
|
|
612
|
+
const runtime = await this.#ensureRuntime(signal, undefined, undefined, auth);
|
|
500
613
|
this.#requireOpen();
|
|
501
614
|
const result = await operation(runtime, signal);
|
|
502
615
|
this.#requireOpen();
|
|
@@ -521,8 +634,20 @@ export class CodexSecurity {
|
|
|
521
634
|
}
|
|
522
635
|
async #ensureRuntime(signal, temporaryRoot, validateLocation, auth = "auto") {
|
|
523
636
|
this.#requireOpen();
|
|
524
|
-
if (this.#runtime !== null)
|
|
525
|
-
|
|
637
|
+
if (this.#runtime !== null) {
|
|
638
|
+
const usePersistentCredentials = scanAuthentication(this.#dependencies.environment, auth).method ===
|
|
639
|
+
"stored_credentials";
|
|
640
|
+
if (this.#dependencies.prepareRuntime !== undefined ||
|
|
641
|
+
this.#runtime.persistentCredentialHome === undefined ||
|
|
642
|
+
this.#runtime.persistentCredentialHome === usePersistentCredentials) {
|
|
643
|
+
return this.#runtime;
|
|
644
|
+
}
|
|
645
|
+
await this.#cleanupRuntime(this.#runtime);
|
|
646
|
+
this.#runtime = null;
|
|
647
|
+
this.#runtimePromise = null;
|
|
648
|
+
this.#runtimeCredentialSource = null;
|
|
649
|
+
this.#requireOpen();
|
|
650
|
+
}
|
|
526
651
|
if (this.#runtimePromise === null) {
|
|
527
652
|
const runtimePromise = this.#prepareRuntime(signal ?? this.#abortController.signal, temporaryRoot, validateLocation, auth);
|
|
528
653
|
this.#runtimePromise = runtimePromise;
|
|
@@ -548,6 +673,20 @@ export class CodexSecurity {
|
|
|
548
673
|
#codexCommand() {
|
|
549
674
|
return (this.#dependencies.resolveCodexCommand ?? resolveCodexCommand)();
|
|
550
675
|
}
|
|
676
|
+
async #refreshPersistentRuntime(runtime, environment, signal) {
|
|
677
|
+
throwIfAborted(signal);
|
|
678
|
+
const mergedConfig = await mergedCodexConfig(this.config);
|
|
679
|
+
const config = await preserveCodexSecurityPluginRegistration(runtime.codexHome, scanRuntimeCodexConfig(mergedConfig, codexSecurityStateDirectory(environment), runtime.codexHome));
|
|
680
|
+
await writeCodexConfig(join(runtime.codexHome, "config.toml"), config);
|
|
681
|
+
if (runtime.configPath !== undefined) {
|
|
682
|
+
await writeCodexConfig(runtime.configPath, scanPreflightCodexConfig(mergedConfig));
|
|
683
|
+
}
|
|
684
|
+
runtime.plugin = await bootstrapPlugin(runtime.codexHome, runtime.plugin.pluginRoot, {
|
|
685
|
+
environment: withoutCodexHome(environment),
|
|
686
|
+
signal,
|
|
687
|
+
});
|
|
688
|
+
runtime.effectiveConfig = mergedConfig;
|
|
689
|
+
}
|
|
551
690
|
async #validateLocalInputs(repository, options, signal) {
|
|
552
691
|
if (options.maxCostUsd !== undefined &&
|
|
553
692
|
(!Number.isFinite(options.maxCostUsd) || options.maxCostUsd <= 0)) {
|
|
@@ -579,18 +718,22 @@ export class CodexSecurity {
|
|
|
579
718
|
if (this.#dependencies.prepareRuntime !== undefined) {
|
|
580
719
|
return await this.#dependencies.prepareRuntime(this.config, signal);
|
|
581
720
|
}
|
|
582
|
-
const
|
|
721
|
+
const processEnvironment = selectedScanEnvironment(this.#dependencies.environment, auth);
|
|
722
|
+
const persistentCredentialHome = scanAuthentication(this.#dependencies.environment, auth).method ===
|
|
723
|
+
"stored_credentials";
|
|
724
|
+
const codexHome = persistentCredentialHome
|
|
725
|
+
? await prepareCodexSecurityCredentialHome(processEnvironment, validateLocation)
|
|
726
|
+
: await createIsolatedHome(temporaryRoot, validateLocation);
|
|
583
727
|
let bootstrapWorkspace;
|
|
584
728
|
try {
|
|
585
729
|
throwIfAborted(signal);
|
|
586
|
-
bootstrapWorkspace = await createIsolatedHome(
|
|
730
|
+
bootstrapWorkspace = await createIsolatedHome(temporaryRoot, validateLocation);
|
|
587
731
|
const pluginRoot = await resolvePluginPath(this.config.pluginPath, bootstrapWorkspace, signal);
|
|
588
|
-
const processEnvironment = selectedScanEnvironment(this.#dependencies.environment, auth);
|
|
589
732
|
const nodeAmbientHome = join(homedir(), ".codex");
|
|
590
733
|
const configuredAmbientHome = environmentValue(processEnvironment, "CODEX_HOME");
|
|
591
734
|
const ambientHome = configuredAmbientHome ?? nodeAmbientHome;
|
|
592
735
|
const mergedConfig = await mergedCodexConfig(this.config);
|
|
593
|
-
const codexConfig = scanRuntimeCodexConfig(mergedConfig, codexSecurityStateDirectory(processEnvironment));
|
|
736
|
+
const codexConfig = await preserveCodexSecurityPluginRegistration(codexHome, scanRuntimeCodexConfig(mergedConfig, codexSecurityStateDirectory(processEnvironment), persistentCredentialHome ? codexHome : undefined));
|
|
594
737
|
await writeCodexConfig(join(codexHome, "config.toml"), codexConfig);
|
|
595
738
|
const configPath = join(bootstrapWorkspace, "config-preflight.toml");
|
|
596
739
|
await writeCodexConfig(configPath, scanPreflightCodexConfig(mergedConfig));
|
|
@@ -602,6 +745,7 @@ export class CodexSecurity {
|
|
|
602
745
|
const credentialsAvailable = await initialCredentialsAvailable(processEnvironment, ambientHome, codexHome);
|
|
603
746
|
return {
|
|
604
747
|
codexHome,
|
|
748
|
+
persistentCredentialHome,
|
|
605
749
|
bootstrapWorkspace,
|
|
606
750
|
configPath,
|
|
607
751
|
plugin,
|
|
@@ -615,7 +759,7 @@ export class CodexSecurity {
|
|
|
615
759
|
};
|
|
616
760
|
}
|
|
617
761
|
catch (error) {
|
|
618
|
-
const cleanupResults = await Promise.allSettled([bootstrapWorkspace, codexHome]
|
|
762
|
+
const cleanupResults = await Promise.allSettled([bootstrapWorkspace, persistentCredentialHome ? undefined : codexHome]
|
|
619
763
|
.filter((path) => path !== undefined)
|
|
620
764
|
.map((path) => cleanupSdkDirectory(path)));
|
|
621
765
|
const cleanupFailures = cleanupResults.flatMap((result) => result.status === "rejected" ? [result.reason] : []);
|
|
@@ -633,8 +777,27 @@ export class CodexSecurity {
|
|
|
633
777
|
export async function initialCredentialsAvailable(environment, ambientHome, isolatedHome, importer = importAmbientAuth) {
|
|
634
778
|
if (environmentApiKey(environment) !== null)
|
|
635
779
|
return false;
|
|
780
|
+
if (!(await codexSecurityCredentialAllowsAmbientImport(isolatedHome))) {
|
|
781
|
+
return false;
|
|
782
|
+
}
|
|
783
|
+
if (await codexSecurityHasStoredFileCredentials(isolatedHome))
|
|
784
|
+
return true;
|
|
636
785
|
return await importer(ambientHome, isolatedHome);
|
|
637
786
|
}
|
|
787
|
+
// Reports a cleanup failure without letting it decide the result of the scan. Only the
|
|
788
|
+
// message is forwarded, and it reaches the onWarning observer alone: unlike the fail-scan
|
|
789
|
+
// path it is never written to the workbench, so it adds no persisted, unredacted text.
|
|
790
|
+
function warnCleanupFailed(options, reason) {
|
|
791
|
+
// This runs where a throw would replace the scan result, so every step is inside the
|
|
792
|
+
// guard: reading the reason, coercing it, and reading the observers off the options can
|
|
793
|
+
// each throw for a sufficiently hostile value, and none of them may become the outcome
|
|
794
|
+
// of the scan. Losing a warning is the correct trade against losing the result.
|
|
795
|
+
try {
|
|
796
|
+
const message = String(reason instanceof Error ? reason.message : reason);
|
|
797
|
+
notifyObserver("onWarning", options.onWarning, options.onObserverError, `Could not clean up after the Codex Security scan: ${message}`);
|
|
798
|
+
}
|
|
799
|
+
catch { }
|
|
800
|
+
}
|
|
638
801
|
async function removeTargetPathsFile(path) {
|
|
639
802
|
if (path === null)
|
|
640
803
|
return;
|
|
@@ -682,10 +845,8 @@ export async function runScanEvents(options) {
|
|
|
682
845
|
status = "completed";
|
|
683
846
|
usage = event["usage"];
|
|
684
847
|
}
|
|
685
|
-
else if (event.type === "turn.failed"
|
|
686
|
-
|
|
687
|
-
typeof event["error"]["message"] === "string") {
|
|
688
|
-
throw new CodexSecurityError(event["error"]["message"]);
|
|
848
|
+
else if (event.type === "turn.failed") {
|
|
849
|
+
throw new CodexSecurityError(turnFailureMessage(event["error"]));
|
|
689
850
|
}
|
|
690
851
|
else if (event.type === "error" &&
|
|
691
852
|
typeof event["message"] === "string") {
|
|
@@ -720,7 +881,7 @@ export async function runScanEvents(options) {
|
|
|
720
881
|
finalResponse,
|
|
721
882
|
usage,
|
|
722
883
|
...(options.model === undefined ? {} : { model: options.model }),
|
|
723
|
-
}, threadId, options.scanDir, options.pluginRoot, options.expectation, options.signal);
|
|
884
|
+
}, threadId, options.scanDir, options.pluginRoot, options.expectation, options.signal, options.workbenchValidated);
|
|
724
885
|
if (options.signal.aborted) {
|
|
725
886
|
throw new ScanInterruptedError(`Codex Security scan was interrupted; partial output remains at ${options.scanDir}.`, options.scanDir);
|
|
726
887
|
}
|
|
@@ -758,6 +919,9 @@ async function scanPrompt(pluginRoot, target, mode, hasConfigPath = false, hasKn
|
|
|
758
919
|
'Use exactly "$CODEX_SECURITY_SCAN_ID" as the scan ID in the manifest, findings, and coverage.',
|
|
759
920
|
'Use exactly "$CODEX_SECURITY_TARGET_ID" as scan.target.targetId; do not derive a different target ID.',
|
|
760
921
|
'Use exactly "$CODEX_SECURITY_TARGET_DISPLAY_NAME" as scan.target.displayName; do not infer a display name from the Git remote.',
|
|
922
|
+
'Use exactly "$CODEX_SECURITY_TARGET_KIND" as scan.target.kind; do not infer the target kind from the checkout.',
|
|
923
|
+
'When "$CODEX_SECURITY_TARGET_REVISION" is set, use its exact value as scan.target.revision.',
|
|
924
|
+
'When "$CODEX_SECURITY_TARGET_SNAPSHOT_DIGEST" is set, use its exact value as scan.target.snapshotDigest. For git_revision, omit scan.target.snapshotDigest.',
|
|
761
925
|
'Use exactly "codex-security-plugin" as scan.producer.name.',
|
|
762
926
|
...(hasConfigPath
|
|
763
927
|
? [
|
|
@@ -795,7 +959,7 @@ function targetInstruction(target) {
|
|
|
795
959
|
}
|
|
796
960
|
return `Scan target: staged and unstaged working-tree changes against ${target.base}.`;
|
|
797
961
|
}
|
|
798
|
-
function scanRecipe(repository, target, mode, repositoryRevision, pluginVersion, effectiveConfig, failOnSeverity, knowledgeBasePaths, maxCostUsd) {
|
|
962
|
+
function scanRecipe(repository, activeProjectPath, target, mode, repositoryRevision, pluginVersion, effectiveConfig, failOnSeverity, knowledgeBasePaths, maxCostUsd) {
|
|
799
963
|
return {
|
|
800
964
|
repository,
|
|
801
965
|
target: {
|
|
@@ -809,7 +973,7 @@ function scanRecipe(repository, target, mode, repositoryRevision, pluginVersion,
|
|
|
809
973
|
mode,
|
|
810
974
|
...(repositoryRevision === null ? {} : { repositoryRevision }),
|
|
811
975
|
pluginVersion,
|
|
812
|
-
config: scanPreflightCodexConfig(effectiveConfig),
|
|
976
|
+
config: scanPreflightCodexConfig(effectiveConfig, activeProjectPath),
|
|
813
977
|
...(failOnSeverity === undefined ? {} : { failOnSeverity }),
|
|
814
978
|
...(knowledgeBasePaths === undefined ? {} : { knowledgeBasePaths }),
|
|
815
979
|
...(maxCostUsd === undefined ? {} : { maxCostUsd }),
|
|
@@ -822,7 +986,7 @@ function validateScanCostLimit(maxCostUsd, model) {
|
|
|
822
986
|
throw new CodexSecurityError(`A scan cost limit is not available for the configured model: ${model}.`);
|
|
823
987
|
}
|
|
824
988
|
}
|
|
825
|
-
async function collectResult(turnResult, threadId, scanDir, pluginRoot, expectation, signal) {
|
|
989
|
+
async function collectResult(turnResult, threadId, scanDir, pluginRoot, expectation, signal, workbenchValidated = false) {
|
|
826
990
|
const required = [
|
|
827
991
|
"scan-manifest.json",
|
|
828
992
|
"findings.json",
|
|
@@ -846,6 +1010,7 @@ async function collectResult(turnResult, threadId, scanDir, pluginRoot, expectat
|
|
|
846
1010
|
const { manifest, findings, coverage } = await loadContract(scanDir, {
|
|
847
1011
|
pluginRoot,
|
|
848
1012
|
expectation,
|
|
1013
|
+
workbenchValidated,
|
|
849
1014
|
signal,
|
|
850
1015
|
});
|
|
851
1016
|
let sarifPath = null;
|
|
@@ -939,6 +1104,21 @@ function reconnectDetails(message) {
|
|
|
939
1104
|
: {}),
|
|
940
1105
|
};
|
|
941
1106
|
}
|
|
1107
|
+
// A failed turn must fail the scan whatever its error payload looks like.
|
|
1108
|
+
//
|
|
1109
|
+
// Only `error.message` is reused, because that is the single shape the previous
|
|
1110
|
+
// code already surfaced. No other shape is forwarded or stringified: this message
|
|
1111
|
+
// reaches `fail-scan --message` and is stored in `scans.failure_message` without
|
|
1112
|
+
// redaction, so widening what is copied out of the payload would add a new
|
|
1113
|
+
// credential-disclosure path to persistent scan history.
|
|
1114
|
+
function turnFailureMessage(error) {
|
|
1115
|
+
if (isRecord(error) && typeof error["message"] === "string") {
|
|
1116
|
+
const message = error["message"].trim();
|
|
1117
|
+
if (message.length > 0)
|
|
1118
|
+
return error["message"];
|
|
1119
|
+
}
|
|
1120
|
+
return "The Codex Security scan turn failed without a readable error message.";
|
|
1121
|
+
}
|
|
942
1122
|
export function classifyConnectionFailure(error) {
|
|
943
1123
|
const message = error instanceof Error ? error.message : String(error);
|
|
944
1124
|
if (/\b(?:sqlite3?|database|workbench)\b/iu.test(message)) {
|
|
@@ -960,7 +1140,7 @@ export function classifyConnectionFailure(error) {
|
|
|
960
1140
|
return "timeout";
|
|
961
1141
|
return "unknown";
|
|
962
1142
|
}
|
|
963
|
-
export function scanRuntimeCodexConfig(config, stateDirectory) {
|
|
1143
|
+
export function scanRuntimeCodexConfig(config, stateDirectory, protectedCredentialHome) {
|
|
964
1144
|
const hardened = structuredClone(config);
|
|
965
1145
|
delete hardened["sandbox_mode"];
|
|
966
1146
|
const configuredPermissions = isRecord(hardened["permissions"])
|
|
@@ -977,12 +1157,15 @@ export function scanRuntimeCodexConfig(config, stateDirectory) {
|
|
|
977
1157
|
":root": "read",
|
|
978
1158
|
":workspace_roots": "write",
|
|
979
1159
|
[stateDirectory]: "write",
|
|
1160
|
+
...(protectedCredentialHome === undefined
|
|
1161
|
+
? {}
|
|
1162
|
+
: { [protectedCredentialHome]: "read" }),
|
|
980
1163
|
},
|
|
981
1164
|
},
|
|
982
1165
|
},
|
|
983
1166
|
};
|
|
984
1167
|
}
|
|
985
|
-
export function scanPreflightCodexConfig(config) {
|
|
1168
|
+
export function scanPreflightCodexConfig(config, activeProjectPath) {
|
|
986
1169
|
const safeString = (value, maxLength) => typeof value === "string" &&
|
|
987
1170
|
value.length > 0 &&
|
|
988
1171
|
value.length <= maxLength &&
|
|
@@ -1054,19 +1237,37 @@ export function scanPreflightCodexConfig(config) {
|
|
|
1054
1237
|
}
|
|
1055
1238
|
return result;
|
|
1056
1239
|
};
|
|
1240
|
+
const prioritizedEntries = (value, priority) => {
|
|
1241
|
+
const entries = Object.entries(value);
|
|
1242
|
+
if (priority === undefined || !Object.hasOwn(value, priority)) {
|
|
1243
|
+
return entries;
|
|
1244
|
+
}
|
|
1245
|
+
return [
|
|
1246
|
+
[priority, value[priority]],
|
|
1247
|
+
...entries.filter(([key]) => key !== priority),
|
|
1248
|
+
];
|
|
1249
|
+
};
|
|
1057
1250
|
const result = executionConfig(config);
|
|
1058
|
-
|
|
1059
|
-
|
|
1251
|
+
const selectedProfile = safeProfileName(config["profile"])
|
|
1252
|
+
? config["profile"]
|
|
1253
|
+
: undefined;
|
|
1254
|
+
if (selectedProfile !== undefined) {
|
|
1255
|
+
result["profile"] = selectedProfile;
|
|
1060
1256
|
}
|
|
1061
1257
|
const profiles = config["profiles"];
|
|
1062
1258
|
if (isRecord(profiles)) {
|
|
1063
1259
|
const sanitized = {};
|
|
1064
|
-
|
|
1260
|
+
let accepted = 0;
|
|
1261
|
+
for (const [name, profile] of prioritizedEntries(profiles, selectedProfile)) {
|
|
1065
1262
|
if (!safeProfileName(name) || !isRecord(profile))
|
|
1066
1263
|
continue;
|
|
1067
1264
|
const projected = executionConfig(profile);
|
|
1068
|
-
if (Object.keys(projected).length
|
|
1069
|
-
|
|
1265
|
+
if (Object.keys(projected).length === 0)
|
|
1266
|
+
continue;
|
|
1267
|
+
sanitized[name] = projected;
|
|
1268
|
+
accepted += 1;
|
|
1269
|
+
if (accepted === 256)
|
|
1270
|
+
break;
|
|
1070
1271
|
}
|
|
1071
1272
|
if (Object.keys(sanitized).length > 0)
|
|
1072
1273
|
result["profiles"] = sanitized;
|
|
@@ -1080,7 +1281,21 @@ export function scanPreflightCodexConfig(config) {
|
|
|
1080
1281
|
const projects = config["projects"];
|
|
1081
1282
|
if (isRecord(projects)) {
|
|
1082
1283
|
const sanitized = {};
|
|
1083
|
-
|
|
1284
|
+
let accepted = 0;
|
|
1285
|
+
const activeProjectRoot = activeProjectPath === undefined
|
|
1286
|
+
? undefined
|
|
1287
|
+
: Object.keys(projects)
|
|
1288
|
+
.filter((path) => {
|
|
1289
|
+
if (!safeString(path, 4096) || !isAbsolute(path))
|
|
1290
|
+
return false;
|
|
1291
|
+
const remaining = relative(path, activeProjectPath);
|
|
1292
|
+
return (remaining === "" ||
|
|
1293
|
+
(remaining !== ".." &&
|
|
1294
|
+
!remaining.startsWith(`..${sep}`) &&
|
|
1295
|
+
!isAbsolute(remaining)));
|
|
1296
|
+
})
|
|
1297
|
+
.sort((left, right) => right.length - left.length)[0];
|
|
1298
|
+
for (const [path, project] of prioritizedEntries(projects, activeProjectRoot ?? activeProjectPath)) {
|
|
1084
1299
|
if (!safeString(path, 4096) || !isAbsolute(path) || !isRecord(project)) {
|
|
1085
1300
|
continue;
|
|
1086
1301
|
}
|
|
@@ -1088,6 +1303,9 @@ export function scanPreflightCodexConfig(config) {
|
|
|
1088
1303
|
if (trust !== "trusted" && trust !== "untrusted")
|
|
1089
1304
|
continue;
|
|
1090
1305
|
sanitized[path] = { trust_level: trust };
|
|
1306
|
+
accepted += 1;
|
|
1307
|
+
if (accepted === 256)
|
|
1308
|
+
break;
|
|
1091
1309
|
}
|
|
1092
1310
|
if (Object.keys(sanitized).length > 0)
|
|
1093
1311
|
result["projects"] = sanitized;
|