@openai/codex-security 0.1.10 → 0.1.12
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 +72 -41
- package/_bundled_plugin/.codex-plugin/plugin.json +1 -1
- package/_bundled_plugin/.mcp.json +5 -1
- package/_bundled_plugin/examples/completed-scan/coverage.json +1 -1
- package/_bundled_plugin/examples/completed-scan/scan-manifest.json +2 -2
- 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/preflight/capability-profiles.toml +1 -34
- package/_bundled_plugin/references/config-preflight.md +11 -13
- package/_bundled_plugin/references/core-scan.md +94 -0
- package/_bundled_plugin/references/final-report.md +6 -6
- package/_bundled_plugin/references/scan-artifacts.md +15 -14
- package/_bundled_plugin/references/scan-prologue.md +27 -0
- package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +1 -106
- package/_bundled_plugin/scripts/config_preflight.py +40 -3
- package/_bundled_plugin/scripts/deep_scan_config.py +29 -6
- package/_bundled_plugin/scripts/deep_scan_workbench.py +132 -34
- package/_bundled_plugin/scripts/finalize_scan_contract.py +44 -224
- package/_bundled_plugin/scripts/finding_preview.py +122 -6
- package/_bundled_plugin/scripts/generate_in_scope_files.py +77 -31
- package/_bundled_plugin/scripts/generate_rank_input.py +58 -7
- package/_bundled_plugin/scripts/normalize_candidates.py +5 -5
- package/_bundled_plugin/scripts/rank_preview.py +4 -0
- package/_bundled_plugin/scripts/report_projection.py +40 -20
- package/_bundled_plugin/scripts/workbench_cli.py +5 -0
- package/_bundled_plugin/scripts/workbench_db.py +304 -32
- package/_bundled_plugin/scripts/workbench_progress.py +0 -5
- package/_bundled_plugin/scripts/workbench_scan_usage.py +43 -8
- package/_bundled_plugin/scripts/workbench_schema.py +16 -20
- package/_bundled_plugin/scripts/workbench_source_excerpt.py +1 -5
- package/_bundled_plugin/scripts/workbench_target.py +82 -0
- package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +8 -11
- package/_bundled_plugin/skills/deep-security-scan/SKILL.md +24 -108
- package/_bundled_plugin/skills/security-scan/SKILL.md +5 -88
- package/_bundled_plugin/skills/threat-model/SKILL.md +4 -2
- package/_bundled_plugin/skills/track-findings/SKILL.md +1 -1
- package/_bundled_plugin/skills/triage-finding/SKILL.md +5 -6
- package/_bundled_plugin/skills/triage-finding/references/github-rest-intake.md +6 -7
- package/_bundled_plugin/skills/validation/SKILL.md +9 -12
- package/_bundled_plugin/skills/vulnerability-writeup/SKILL.md +3 -2
- package/_bundled_plugin/skills/vulnerability-writeup/references/report-format.md +1 -1
- package/dist/api.d.ts +2 -0
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +393 -145
- 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/bulk-scan-discovery.js +14 -2
- package/dist/bulk-scan-discovery.js.map +1 -1
- package/dist/cli.d.ts +6 -3
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +202 -46
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +1 -0
- package/dist/config.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 +33 -2
- package/dist/cost.js.map +1 -1
- package/dist/knowledge-base.d.ts.map +1 -1
- package/dist/knowledge-base.js +12 -4
- package/dist/knowledge-base.js.map +1 -1
- package/dist/multiscan.d.ts +1 -0
- package/dist/multiscan.d.ts.map +1 -1
- package/dist/multiscan.js +77 -17
- 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 +140 -326
- package/dist/runtime.js.map +1 -1
- package/dist/scan-comparison.js +1 -1
- package/dist/scan-comparison.js.map +1 -1
- package/dist/scan-dashboard.d.ts +5 -1
- package/dist/scan-dashboard.d.ts.map +1 -1
- package/dist/scan-dashboard.js +65 -16
- package/dist/scan-dashboard.js.map +1 -1
- package/dist/scan-logs.d.ts +2 -0
- package/dist/scan-logs.d.ts.map +1 -1
- package/dist/scan-logs.js +97 -37
- package/dist/scan-logs.js.map +1 -1
- package/dist/targets.d.ts +1 -0
- package/dist/targets.d.ts.map +1 -1
- package/dist/targets.js +35 -4
- package/dist/targets.js.map +1 -1
- package/dist/trusted-executable.js +1 -1
- package/dist/trusted-executable.js.map +1 -1
- package/dist/version.d.ts +3 -3
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +6 -4
- package/dist/version.js.map +1 -1
- package/package.json +4 -4
- package/_bundled_plugin/references/shared-hard-rules.md +0 -15
- package/_bundled_plugin/scripts/validate_report_format.py +0 -138
- package/_bundled_plugin/skills/security-scan/references/repository-wide-scan.md +0 -33
package/dist/runtime.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { execFile as execFileCallback, spawn } from "node:child_process";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
|
-
import { constants, existsSync } from "node:fs";
|
|
4
|
-
import { chmod, copyFile, link, lstat, mkdir, mkdtemp, open, opendir, readFile, readdir, realpath, rename, rm, rmdir, stat, writeFile, } from "node:fs/promises";
|
|
3
|
+
import { constants, existsSync, readdirSync } from "node:fs";
|
|
4
|
+
import { chmod, cp, copyFile, link, lstat, mkdir, mkdtemp, open, opendir, readFile, readdir, realpath, rename, rm, rmdir, stat, writeFile, } from "node:fs/promises";
|
|
5
5
|
import { homedir, tmpdir } from "node:os";
|
|
6
6
|
import { createRequire } from "node:module";
|
|
7
|
-
import { basename, dirname, extname,
|
|
7
|
+
import { basename, dirname, extname, join, relative, resolve } from "node:path";
|
|
8
8
|
import { createInterface } from "node:readline";
|
|
9
9
|
import { fileURLToPath } from "node:url";
|
|
10
10
|
import { promisify } from "node:util";
|
|
@@ -27,19 +27,19 @@ const CREDENTIAL_LOGOUT_MARKER = ".codex-security-logged-out";
|
|
|
27
27
|
const CREDENTIAL_LOCK_POLL_MILLISECONDS = 25;
|
|
28
28
|
const INCOMPLETE_CREDENTIAL_LOCK_MILLISECONDS = 30_000;
|
|
29
29
|
const MAX_WINDOWS_CREDENTIAL_ACL_STDERR = 64 * 1024;
|
|
30
|
+
function environmentValue(environment, requested) {
|
|
31
|
+
const exact = environment[requested]?.trim();
|
|
32
|
+
if (exact)
|
|
33
|
+
return exact;
|
|
34
|
+
return Object.entries(environment)
|
|
35
|
+
.find(([name, value]) => name.toUpperCase() === requested && value?.trim())?.[1]
|
|
36
|
+
?.trim();
|
|
37
|
+
}
|
|
30
38
|
export function codexSecurityStateDirectory(environment = process.env) {
|
|
31
|
-
const
|
|
32
|
-
const exact = environment[requested]?.trim();
|
|
33
|
-
if (exact)
|
|
34
|
-
return exact;
|
|
35
|
-
return Object.entries(environment)
|
|
36
|
-
.find(([name, value]) => name.toUpperCase() === requested && value?.trim())?.[1]
|
|
37
|
-
?.trim();
|
|
38
|
-
};
|
|
39
|
-
const configured = environmentValue("CODEX_SECURITY_STATE_DIR");
|
|
39
|
+
const configured = environmentValue(environment, "CODEX_SECURITY_STATE_DIR");
|
|
40
40
|
if (configured !== undefined)
|
|
41
41
|
return resolve(expandHome(configured));
|
|
42
|
-
const codexHome = environmentValue("CODEX_HOME") ?? join(homedir(), ".codex");
|
|
42
|
+
const codexHome = environmentValue(environment, "CODEX_HOME") ?? join(homedir(), ".codex");
|
|
43
43
|
return resolve(expandHome(codexHome), "state", "plugins", "codex-security");
|
|
44
44
|
}
|
|
45
45
|
export function codexSecurityCredentialHome(environment = process.env) {
|
|
@@ -933,9 +933,16 @@ export async function preserveCodexSecurityPluginRegistration(codexHome, config)
|
|
|
933
933
|
};
|
|
934
934
|
}
|
|
935
935
|
export async function preparePersistentScanRoot(stateDirectory, repositoryName) {
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
936
|
+
await mkdir(stateDirectory, { recursive: true, mode: 0o700 });
|
|
937
|
+
let root = await realpath(stateDirectory);
|
|
938
|
+
for (const directory of ["scans", safePrefix(repositoryName)]) {
|
|
939
|
+
root = join(root, directory);
|
|
940
|
+
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
941
|
+
if (!(await lstat(root)).isDirectory()) {
|
|
942
|
+
throw new OutputDirectoryError(`Persistent scan output must use real directories: ${root}`);
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
return root;
|
|
939
946
|
}
|
|
940
947
|
export async function runWorkbench(options, args) {
|
|
941
948
|
let stdout;
|
|
@@ -1436,14 +1443,7 @@ export async function createMarketplace(codexHome, pluginRoot, signal) {
|
|
|
1436
1443
|
const root = await realpath(pluginRoot);
|
|
1437
1444
|
const marketplace = join(codexHome, "sdk-marketplace");
|
|
1438
1445
|
const pluginDestination = join(marketplace, "plugins", PLUGIN_NAME);
|
|
1439
|
-
|
|
1440
|
-
if (root === (await bundledPluginRoot()) &&
|
|
1441
|
-
(await isRegularFile(projectionContract))) {
|
|
1442
|
-
await copyExternalPayload(root, pluginDestination);
|
|
1443
|
-
}
|
|
1444
|
-
else {
|
|
1445
|
-
await copyPluginTree(root, pluginDestination, signal);
|
|
1446
|
-
}
|
|
1446
|
+
await copyPluginTree(root, pluginDestination, signal);
|
|
1447
1447
|
throwIfSignalAborted(signal);
|
|
1448
1448
|
const manifest = {
|
|
1449
1449
|
name: MARKETPLACE_NAME,
|
|
@@ -1468,27 +1468,14 @@ export async function createMarketplace(codexHome, pluginRoot, signal) {
|
|
|
1468
1468
|
throwIfSignalAborted(signal);
|
|
1469
1469
|
return marketplace;
|
|
1470
1470
|
}
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1471
|
+
export function resolveCodexCommand(environment = process.env) {
|
|
1472
|
+
const configured = environmentValue(environment, "CODEX_CLI_PATH");
|
|
1473
|
+
if (configured &&
|
|
1474
|
+
(process.platform !== "win32" || /\.(?:exe|com)$/iu.test(configured))) {
|
|
1475
|
+
return { command: resolve(configured) };
|
|
1475
1476
|
}
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
return { marketplace: false, plugin: false };
|
|
1479
|
-
}
|
|
1480
|
-
throw new PluginBootstrapError("Unable to inspect the existing Codex Security plugin registration.", { cause: error });
|
|
1481
|
-
}
|
|
1482
|
-
const marketplaces = isRecord(config) ? config["marketplaces"] : undefined;
|
|
1483
|
-
const plugins = isRecord(config) ? config["plugins"] : undefined;
|
|
1484
|
-
return {
|
|
1485
|
-
marketplace: isRecord(marketplaces) && isRecord(marketplaces[MARKETPLACE_NAME]),
|
|
1486
|
-
plugin: isRecord(plugins) &&
|
|
1487
|
-
isRecord(plugins[`${PLUGIN_NAME}@${MARKETPLACE_NAME}`]),
|
|
1488
|
-
};
|
|
1489
|
-
}
|
|
1490
|
-
export function resolveCodexCommand() {
|
|
1491
|
-
const { packageName, targetTriple } = codexPlatformPackage();
|
|
1477
|
+
const platform = process.platform === "android" ? "linux" : process.platform;
|
|
1478
|
+
const packageName = `@openai/codex-${platform}-${process.arch}`;
|
|
1492
1479
|
let packageJson;
|
|
1493
1480
|
try {
|
|
1494
1481
|
const require = createRequire(import.meta.url);
|
|
@@ -1498,110 +1485,74 @@ export function resolveCodexCommand() {
|
|
|
1498
1485
|
catch (error) {
|
|
1499
1486
|
throw new PluginBootstrapError(`The bundled Codex executable could not be resolved from ${packageName}. Reinstall @openai/codex with optional dependencies enabled, or set CODEX_CLI_PATH to an installed Codex executable.`, { cause: error });
|
|
1500
1487
|
}
|
|
1501
|
-
const
|
|
1502
|
-
|
|
1503
|
-
|
|
1488
|
+
const vendor = join(dirname(packageJson), "vendor");
|
|
1489
|
+
const target = readdirSync(vendor, { withFileTypes: true }).find((entry) => entry.isDirectory());
|
|
1490
|
+
const command = join(vendor, target?.name ?? "", "bin", process.platform === "win32" ? "codex.exe" : "codex");
|
|
1491
|
+
if (target === undefined || !existsSync(command)) {
|
|
1492
|
+
throw new PluginBootstrapError(`The ${packageName} package does not contain the Codex executable. Reinstall @openai/codex with optional dependencies enabled, or set CODEX_CLI_PATH to an installed Codex executable.`);
|
|
1504
1493
|
}
|
|
1505
|
-
return { command
|
|
1506
|
-
}
|
|
1507
|
-
export function codexPlatformPackage(platform = process.platform, architecture = process.arch) {
|
|
1508
|
-
const key = `${platform}:${architecture}`;
|
|
1509
|
-
const target = {
|
|
1510
|
-
"android:arm64": [
|
|
1511
|
-
"@openai/codex-linux-arm64",
|
|
1512
|
-
"aarch64-unknown-linux-musl",
|
|
1513
|
-
],
|
|
1514
|
-
"android:x64": ["@openai/codex-linux-x64", "x86_64-unknown-linux-musl"],
|
|
1515
|
-
"darwin:arm64": ["@openai/codex-darwin-arm64", "aarch64-apple-darwin"],
|
|
1516
|
-
"darwin:x64": ["@openai/codex-darwin-x64", "x86_64-apple-darwin"],
|
|
1517
|
-
"linux:arm64": ["@openai/codex-linux-arm64", "aarch64-unknown-linux-musl"],
|
|
1518
|
-
"linux:x64": ["@openai/codex-linux-x64", "x86_64-unknown-linux-musl"],
|
|
1519
|
-
"win32:arm64": ["@openai/codex-win32-arm64", "aarch64-pc-windows-msvc"],
|
|
1520
|
-
"win32:x64": ["@openai/codex-win32-x64", "x86_64-pc-windows-msvc"],
|
|
1521
|
-
}[key];
|
|
1522
|
-
if (target === undefined) {
|
|
1523
|
-
throw new PluginBootstrapError(`Codex does not support this platform: ${platform} (${architecture}).`);
|
|
1524
|
-
}
|
|
1525
|
-
return { packageName: target[0], targetTriple: target[1] };
|
|
1494
|
+
return { command };
|
|
1526
1495
|
}
|
|
1527
1496
|
export async function bootstrapPlugin(codexHome, pluginRoot, options = {}) {
|
|
1528
1497
|
const root = await realpath(pluginRoot);
|
|
1529
1498
|
const { name, version } = await pluginMetadata(root);
|
|
1530
|
-
const
|
|
1531
|
-
let upgradeExistingPlugin = false;
|
|
1532
|
-
let repairIncompletePlugin = false;
|
|
1533
|
-
let installedRoot = null;
|
|
1534
|
-
try {
|
|
1535
|
-
await verifyPluginRegistration(codexHome, existingMarketplace);
|
|
1536
|
-
installedRoot = await findInstalledPlugin(codexHome);
|
|
1537
|
-
}
|
|
1538
|
-
catch (error) {
|
|
1539
|
-
throwIfSignalAborted(options.signal);
|
|
1540
|
-
if (!(error instanceof PluginBootstrapError) &&
|
|
1541
|
-
nodeErrorCode(error) !== "ENOENT") {
|
|
1542
|
-
throw error;
|
|
1543
|
-
}
|
|
1544
|
-
const marketplace = await lstat(existingMarketplace).catch((failure) => {
|
|
1545
|
-
if (nodeErrorCode(failure) === "ENOENT")
|
|
1546
|
-
return null;
|
|
1547
|
-
throw failure;
|
|
1548
|
-
});
|
|
1549
|
-
if (marketplace !== null &&
|
|
1550
|
-
(!marketplace.isDirectory() || marketplace.isSymbolicLink())) {
|
|
1551
|
-
throw new PluginBootstrapError(`Codex Security marketplace is not a safe directory: ${existingMarketplace}`);
|
|
1552
|
-
}
|
|
1553
|
-
const registration = await codexSecurityPluginRegistration(codexHome);
|
|
1554
|
-
repairIncompletePlugin =
|
|
1555
|
-
marketplace !== null || registration.marketplace || registration.plugin;
|
|
1556
|
-
}
|
|
1557
|
-
if (installedRoot !== null) {
|
|
1558
|
-
const installed = await pluginMetadata(installedRoot);
|
|
1559
|
-
if (installed.name === name && installed.version === version) {
|
|
1560
|
-
return {
|
|
1561
|
-
pluginRoot: root,
|
|
1562
|
-
marketplaceRoot: existingMarketplace,
|
|
1563
|
-
installedRoot,
|
|
1564
|
-
marketplaceName: MARKETPLACE_NAME,
|
|
1565
|
-
name,
|
|
1566
|
-
version,
|
|
1567
|
-
};
|
|
1568
|
-
}
|
|
1569
|
-
upgradeExistingPlugin = true;
|
|
1570
|
-
}
|
|
1499
|
+
const marketplace = join(codexHome, "sdk-marketplace");
|
|
1571
1500
|
throwIfSignalAborted(options.signal);
|
|
1572
|
-
const command = options.codexCommand ?? resolveCodexCommand();
|
|
1501
|
+
const command = options.codexCommand ?? resolveCodexCommand(options.environment);
|
|
1573
1502
|
const environment = {
|
|
1574
1503
|
...(options.environment ?? process.env),
|
|
1575
1504
|
CODEX_HOME: codexHome,
|
|
1576
1505
|
};
|
|
1577
|
-
const run = options.runCodex ??
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
await
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1506
|
+
const run = options.runCodex ?? runPluginCommand;
|
|
1507
|
+
const existing = await lstat(marketplace).catch((error) => {
|
|
1508
|
+
if (nodeErrorCode(error) === "ENOENT")
|
|
1509
|
+
return null;
|
|
1510
|
+
throw error;
|
|
1511
|
+
});
|
|
1512
|
+
if (existing !== null && !existing.isDirectory()) {
|
|
1513
|
+
throw new PluginBootstrapError(`Codex Security plugin marketplace path must be a directory: ${marketplace}`);
|
|
1514
|
+
}
|
|
1515
|
+
const staged = existing === null
|
|
1516
|
+
? null
|
|
1517
|
+
: await pluginMetadata(join(marketplace, "plugins", PLUGIN_NAME)).catch(() => null);
|
|
1518
|
+
if (staged?.version !== version) {
|
|
1519
|
+
if (existing !== null) {
|
|
1520
|
+
await rm(marketplace, { recursive: true, force: true });
|
|
1521
|
+
}
|
|
1522
|
+
await createMarketplace(codexHome, root, options.signal);
|
|
1523
|
+
}
|
|
1524
|
+
const config = await readFile(join(codexHome, "config.toml"), "utf8").catch((error) => {
|
|
1525
|
+
if (nodeErrorCode(error) === "ENOENT")
|
|
1526
|
+
return "";
|
|
1527
|
+
throw error;
|
|
1528
|
+
});
|
|
1529
|
+
const marketplaces = parse(config)["marketplaces"];
|
|
1530
|
+
const registration = isRecord(marketplaces)
|
|
1531
|
+
? marketplaces[MARKETPLACE_NAME]
|
|
1532
|
+
: undefined;
|
|
1533
|
+
if (!isRecord(registration) ||
|
|
1534
|
+
registration["source_type"] !== "local" ||
|
|
1535
|
+
typeof registration["source"] !== "string" ||
|
|
1536
|
+
!(await sameFile(registration["source"], marketplace))) {
|
|
1537
|
+
await run(command, ["plugin", "marketplace", "add", marketplace], environment, options.signal);
|
|
1538
|
+
}
|
|
1539
|
+
const output = await run(command, ["plugin", "add", "--json", `${PLUGIN_NAME}@${MARKETPLACE_NAME}`], environment, options.signal);
|
|
1540
|
+
let installed;
|
|
1541
|
+
try {
|
|
1542
|
+
installed = JSON.parse(output);
|
|
1543
|
+
}
|
|
1544
|
+
catch (error) {
|
|
1545
|
+
throw new PluginBootstrapError("Codex plugin install did not return a valid JSON result.", { cause: error });
|
|
1546
|
+
}
|
|
1547
|
+
if (!isRecord(installed) ||
|
|
1548
|
+
typeof installed["installedPath"] !== "string" ||
|
|
1549
|
+
installed["version"] !== version) {
|
|
1550
|
+
throw new PluginBootstrapError("Codex plugin install did not return the selected plugin path and version.");
|
|
1600
1551
|
}
|
|
1601
1552
|
return {
|
|
1602
1553
|
pluginRoot: root,
|
|
1603
1554
|
marketplaceRoot: marketplace,
|
|
1604
|
-
installedRoot:
|
|
1555
|
+
installedRoot: installed["installedPath"],
|
|
1605
1556
|
marketplaceName: MARKETPLACE_NAME,
|
|
1606
1557
|
name,
|
|
1607
1558
|
version,
|
|
@@ -1637,7 +1588,7 @@ export async function resolvePluginPython(options = {}) {
|
|
|
1637
1588
|
if (options.configuredPath !== undefined) {
|
|
1638
1589
|
return await requirePython(options.configuredPath, "configured plugin Python", environment, protectedRoot, options.signal);
|
|
1639
1590
|
}
|
|
1640
|
-
const inherited = environment
|
|
1591
|
+
const inherited = environmentValue(environment, "PYTHON");
|
|
1641
1592
|
if (inherited) {
|
|
1642
1593
|
return await requirePython(inherited, "PYTHON", environment, protectedRoot, options.signal);
|
|
1643
1594
|
}
|
|
@@ -1677,19 +1628,53 @@ export function pluginExecutionEnvironment(python, environment = process.env) {
|
|
|
1677
1628
|
return {
|
|
1678
1629
|
...environment,
|
|
1679
1630
|
PYTHON: python,
|
|
1680
|
-
CODEX_CLI_PATH:
|
|
1631
|
+
CODEX_CLI_PATH: resolveCodexCommand(environment).command,
|
|
1681
1632
|
};
|
|
1682
1633
|
}
|
|
1683
1634
|
export async function cleanupSdkDirectory(path) {
|
|
1684
1635
|
await rm(path, { recursive: true, force: true });
|
|
1685
1636
|
}
|
|
1686
|
-
async function
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1637
|
+
export async function runCodexCommand(command, args, environment, input, signal) {
|
|
1638
|
+
const child = spawn(command.command, [...args], {
|
|
1639
|
+
env: environment,
|
|
1640
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
1641
|
+
windowsHide: true,
|
|
1642
|
+
signal,
|
|
1643
|
+
});
|
|
1644
|
+
let stdout = "";
|
|
1645
|
+
let stderr = "";
|
|
1646
|
+
let processError;
|
|
1647
|
+
child.stdout.setEncoding("utf8");
|
|
1648
|
+
child.stderr.setEncoding("utf8");
|
|
1649
|
+
child.stdout.on("data", (chunk) => {
|
|
1650
|
+
stdout += chunk;
|
|
1651
|
+
});
|
|
1652
|
+
child.stderr.on("data", (chunk) => {
|
|
1653
|
+
stderr += chunk;
|
|
1654
|
+
});
|
|
1655
|
+
const completion = new Promise((resolve, reject) => {
|
|
1656
|
+
child.once("error", (error) => {
|
|
1657
|
+
processError = error;
|
|
1658
|
+
});
|
|
1659
|
+
child.stdin.on("error", (error) => {
|
|
1660
|
+
if (!["EPIPE", "ECONNRESET", "EOF", "ERR_STREAM_DESTROYED"].includes(error.code ?? ""))
|
|
1661
|
+
processError = error;
|
|
1692
1662
|
});
|
|
1663
|
+
child.once("close", (exitCode) => processError === undefined
|
|
1664
|
+
? resolve({ success: exitCode === 0, exitCode, stdout, stderr })
|
|
1665
|
+
: reject(processError));
|
|
1666
|
+
});
|
|
1667
|
+
child.stdin.end(input);
|
|
1668
|
+
return await completion;
|
|
1669
|
+
}
|
|
1670
|
+
async function runPluginCommand(command, args, environment, signal) {
|
|
1671
|
+
try {
|
|
1672
|
+
const { success, exitCode, stdout, stderr } = await runCodexCommand(command, args, environment, undefined, signal);
|
|
1673
|
+
if (!success) {
|
|
1674
|
+
throw new Error(stderr.trim() ||
|
|
1675
|
+
stdout.trim() ||
|
|
1676
|
+
`Codex exited with status ${exitCode}.`);
|
|
1677
|
+
}
|
|
1693
1678
|
return stdout;
|
|
1694
1679
|
}
|
|
1695
1680
|
catch (error) {
|
|
@@ -1699,21 +1684,6 @@ async function runCodex(command, args, environment, signal) {
|
|
|
1699
1684
|
});
|
|
1700
1685
|
}
|
|
1701
1686
|
}
|
|
1702
|
-
async function findInstalledPlugin(codexHome) {
|
|
1703
|
-
const root = join(codexHome, "plugins", "cache", MARKETPLACE_NAME, PLUGIN_NAME);
|
|
1704
|
-
const candidates = [];
|
|
1705
|
-
for (const entry of await readdir(root, { withFileTypes: true }).catch(() => [])) {
|
|
1706
|
-
if (entry.isDirectory()) {
|
|
1707
|
-
const candidate = join(root, entry.name);
|
|
1708
|
-
if (await hasPluginManifest(candidate))
|
|
1709
|
-
candidates.push(candidate);
|
|
1710
|
-
}
|
|
1711
|
-
}
|
|
1712
|
-
if (candidates.length !== 1) {
|
|
1713
|
-
throw new PluginBootstrapError("Codex plugin install did not produce one installed Codex Security plugin.");
|
|
1714
|
-
}
|
|
1715
|
-
return await realpath(candidates[0]);
|
|
1716
|
-
}
|
|
1717
1687
|
async function discoverPluginRoot(root) {
|
|
1718
1688
|
if (await hasPluginManifest(root))
|
|
1719
1689
|
return await validatePluginRoot(root);
|
|
@@ -1729,154 +1699,29 @@ async function validatePluginRoot(root) {
|
|
|
1729
1699
|
await pluginMetadata(root);
|
|
1730
1700
|
return await realpath(root);
|
|
1731
1701
|
}
|
|
1732
|
-
async function verifyPluginRegistration(codexHome, marketplace) {
|
|
1733
|
-
const configPath = join(codexHome, "config.toml");
|
|
1734
|
-
let config;
|
|
1735
|
-
try {
|
|
1736
|
-
config = parse(await readFile(configPath, "utf8"));
|
|
1737
|
-
}
|
|
1738
|
-
catch (error) {
|
|
1739
|
-
throw new PluginBootstrapError("Codex plugin bootstrap produced an unreadable config.toml.", {
|
|
1740
|
-
cause: error,
|
|
1741
|
-
});
|
|
1742
|
-
}
|
|
1743
|
-
const marketplaces = isRecord(config) ? config["marketplaces"] : undefined;
|
|
1744
|
-
const plugins = isRecord(config) ? config["plugins"] : undefined;
|
|
1745
|
-
const marketplaceConfig = isRecord(marketplaces)
|
|
1746
|
-
? marketplaces[MARKETPLACE_NAME]
|
|
1747
|
-
: undefined;
|
|
1748
|
-
const pluginConfig = isRecord(plugins)
|
|
1749
|
-
? plugins[`${PLUGIN_NAME}@${MARKETPLACE_NAME}`]
|
|
1750
|
-
: undefined;
|
|
1751
|
-
if (!isRecord(marketplaceConfig) || !isRecord(pluginConfig)) {
|
|
1752
|
-
throw new PluginBootstrapError("Codex plugin bootstrap did not preserve plugin registration.");
|
|
1753
|
-
}
|
|
1754
|
-
const registeredSource = String(marketplaceConfig["source"] ?? "");
|
|
1755
|
-
if (!(await sameFile(registeredSource, marketplace))) {
|
|
1756
|
-
throw new PluginBootstrapError("Codex plugin marketplace registration has the wrong source.");
|
|
1757
|
-
}
|
|
1758
|
-
if (pluginConfig["enabled"] !== true) {
|
|
1759
|
-
throw new PluginBootstrapError("Codex Security plugin is not enabled after bootstrap.");
|
|
1760
|
-
}
|
|
1761
|
-
}
|
|
1762
1702
|
async function copyPluginTree(source, destination, signal) {
|
|
1763
|
-
const pending = [
|
|
1764
|
-
{ source, destination },
|
|
1765
|
-
];
|
|
1766
|
-
const directories = new Map();
|
|
1767
1703
|
await mkdir(dirname(destination), { recursive: true, mode: 0o700 });
|
|
1768
1704
|
try {
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
for await (const entry of pluginDirectoryEntries(current.source, signal)) {
|
|
1779
|
-
const childSource = join(current.source, entry);
|
|
1780
|
-
pending.push({
|
|
1781
|
-
source: childSource,
|
|
1782
|
-
destination: join(current.destination, entry),
|
|
1783
|
-
});
|
|
1784
|
-
}
|
|
1785
|
-
const afterRead = await lstat(current.source);
|
|
1786
|
-
if (!samePluginFile(metadata, afterRead)) {
|
|
1787
|
-
throw new PluginBootstrapError(`Plugin directory changed while it was being copied: ${current.source}`);
|
|
1788
|
-
}
|
|
1789
|
-
directories.set(current.source, afterRead);
|
|
1790
|
-
await mkdir(current.destination, { mode: 0o700 });
|
|
1791
|
-
continue;
|
|
1792
|
-
}
|
|
1793
|
-
if (!metadata.isFile()) {
|
|
1794
|
-
throw new PluginBootstrapError(`Plugin contains a non-regular file: ${current.source}`);
|
|
1795
|
-
}
|
|
1796
|
-
const input = await open(current.source, constants.O_RDONLY |
|
|
1797
|
-
(process.platform === "win32"
|
|
1798
|
-
? 0
|
|
1799
|
-
: constants.O_NOFOLLOW | constants.O_NONBLOCK));
|
|
1800
|
-
let output;
|
|
1801
|
-
try {
|
|
1802
|
-
if (!samePluginFile(metadata, await input.stat())) {
|
|
1803
|
-
throw new PluginBootstrapError(`Plugin source changed before it could be copied: ${current.source}`);
|
|
1804
|
-
}
|
|
1805
|
-
await requirePluginAncestors(source, current.source, directories, signal);
|
|
1806
|
-
const bytes = await readExactly(input, metadata.size, 0, signal);
|
|
1807
|
-
if (!samePluginFile(metadata, await input.stat())) {
|
|
1808
|
-
throw new PluginBootstrapError(`Plugin source changed while it was being copied: ${current.source}`);
|
|
1705
|
+
await cp(source, destination, {
|
|
1706
|
+
recursive: true,
|
|
1707
|
+
force: false,
|
|
1708
|
+
filter: async (path) => {
|
|
1709
|
+
throwIfSignalAborted(signal);
|
|
1710
|
+
const metadata = await lstat(path);
|
|
1711
|
+
if ((!metadata.isDirectory() && !metadata.isFile()) ||
|
|
1712
|
+
(await realpath(path)) !== path) {
|
|
1713
|
+
throw new PluginBootstrapError(`Plugin contains an unsafe source path: ${path}`);
|
|
1809
1714
|
}
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
(process.platform === "win32" ? 0 : constants.O_NOFOLLOW), 0o600);
|
|
1815
|
-
await output.writeFile(bytes);
|
|
1816
|
-
await output.chmod(metadata.mode & 0o777);
|
|
1817
|
-
}
|
|
1818
|
-
finally {
|
|
1819
|
-
await output?.close();
|
|
1820
|
-
await input.close();
|
|
1821
|
-
}
|
|
1822
|
-
}
|
|
1715
|
+
throwIfSignalAborted(signal);
|
|
1716
|
+
return true;
|
|
1717
|
+
},
|
|
1718
|
+
});
|
|
1823
1719
|
}
|
|
1824
1720
|
catch (error) {
|
|
1825
1721
|
await rm(destination, { recursive: true, force: true });
|
|
1826
1722
|
throw error;
|
|
1827
1723
|
}
|
|
1828
1724
|
}
|
|
1829
|
-
async function* pluginDirectoryEntries(path, signal) {
|
|
1830
|
-
throwIfSignalAborted(signal);
|
|
1831
|
-
const directory = await opendir(path);
|
|
1832
|
-
try {
|
|
1833
|
-
for (;;) {
|
|
1834
|
-
throwIfSignalAborted(signal);
|
|
1835
|
-
const entry = await directory.read();
|
|
1836
|
-
throwIfSignalAborted(signal);
|
|
1837
|
-
if (entry === null)
|
|
1838
|
-
return;
|
|
1839
|
-
yield entry.name;
|
|
1840
|
-
}
|
|
1841
|
-
}
|
|
1842
|
-
finally {
|
|
1843
|
-
await directory.close();
|
|
1844
|
-
}
|
|
1845
|
-
}
|
|
1846
|
-
async function requirePluginAncestors(root, path, directories, signal) {
|
|
1847
|
-
const relativePath = relative(root, path);
|
|
1848
|
-
if (relativePath === ".." ||
|
|
1849
|
-
relativePath.startsWith(`..${sep}`) ||
|
|
1850
|
-
isAbsolute(relativePath)) {
|
|
1851
|
-
throw new PluginBootstrapError(`Plugin source path escapes its root: ${path}`);
|
|
1852
|
-
}
|
|
1853
|
-
if (relativePath === "")
|
|
1854
|
-
return;
|
|
1855
|
-
let ancestor = root;
|
|
1856
|
-
const parents = ["", ...relativePath.split(sep).slice(0, -1)];
|
|
1857
|
-
for (const component of parents) {
|
|
1858
|
-
throwIfSignalAborted(signal);
|
|
1859
|
-
if (component !== "")
|
|
1860
|
-
ancestor = join(ancestor, component);
|
|
1861
|
-
const expected = directories.get(ancestor);
|
|
1862
|
-
const actual = await lstat(ancestor);
|
|
1863
|
-
if (expected === undefined ||
|
|
1864
|
-
actual.isSymbolicLink() ||
|
|
1865
|
-
!actual.isDirectory() ||
|
|
1866
|
-
!samePluginFile(expected, actual)) {
|
|
1867
|
-
throw new PluginBootstrapError(`Plugin source directory changed while it was being copied: ${ancestor}`);
|
|
1868
|
-
}
|
|
1869
|
-
}
|
|
1870
|
-
}
|
|
1871
|
-
function samePluginFile(first, second) {
|
|
1872
|
-
return (first.dev === second.dev &&
|
|
1873
|
-
first.ino === second.ino &&
|
|
1874
|
-
first.size === second.size &&
|
|
1875
|
-
first.mtimeMs === second.mtimeMs &&
|
|
1876
|
-
first.mode === second.mode &&
|
|
1877
|
-
first.isFile() === second.isFile() &&
|
|
1878
|
-
first.isDirectory() === second.isDirectory());
|
|
1879
|
-
}
|
|
1880
1725
|
async function readExactly(handle, length, position, signal) {
|
|
1881
1726
|
const buffer = Buffer.alloc(length);
|
|
1882
1727
|
let offset = 0;
|
|
@@ -1889,37 +1734,6 @@ async function readExactly(handle, length, position, signal) {
|
|
|
1889
1734
|
}
|
|
1890
1735
|
return buffer;
|
|
1891
1736
|
}
|
|
1892
|
-
async function copyExternalPayload(source, destination) {
|
|
1893
|
-
const contractPath = join(source, ".internal", "external-promotion", "external-projection-contract.json");
|
|
1894
|
-
let contract;
|
|
1895
|
-
try {
|
|
1896
|
-
contract = JSON.parse(await readFile(contractPath, "utf8"));
|
|
1897
|
-
}
|
|
1898
|
-
catch (error) {
|
|
1899
|
-
throw new PluginBootstrapError(`Invalid plugin projection contract: ${contractPath}`, {
|
|
1900
|
-
cause: error,
|
|
1901
|
-
});
|
|
1902
|
-
}
|
|
1903
|
-
const shippedExact = isRecord(contract)
|
|
1904
|
-
? contract["shippedExact"]
|
|
1905
|
-
: undefined;
|
|
1906
|
-
if (!Array.isArray(shippedExact) ||
|
|
1907
|
-
!shippedExact.every((value) => typeof value === "string")) {
|
|
1908
|
-
throw new PluginBootstrapError("Plugin projection contract must contain shippedExact paths.");
|
|
1909
|
-
}
|
|
1910
|
-
const paths = [".codex-plugin/plugin.json", ...shippedExact].filter((value) => !value.startsWith("sdk/"));
|
|
1911
|
-
for (const path of paths) {
|
|
1912
|
-
const normalized = safeArchivePath(path);
|
|
1913
|
-
const sourcePath = join(source, ...normalized.split("/"));
|
|
1914
|
-
const destinationPath = join(destination, ...normalized.split("/"));
|
|
1915
|
-
const metadata = await lstat(sourcePath).catch(() => null);
|
|
1916
|
-
if (metadata === null || !metadata.isFile() || metadata.isSymbolicLink()) {
|
|
1917
|
-
throw new PluginBootstrapError(`Bundled plugin file is missing or unsafe: ${sourcePath}`);
|
|
1918
|
-
}
|
|
1919
|
-
await mkdir(dirname(destinationPath), { recursive: true, mode: 0o700 });
|
|
1920
|
-
await copyFile(sourcePath, destinationPath, constants.COPYFILE_EXCL);
|
|
1921
|
-
}
|
|
1922
|
-
}
|
|
1923
1737
|
function safeArchivePath(value) {
|
|
1924
1738
|
const parts = value.split("/");
|
|
1925
1739
|
const normalized = parts
|