@openai/codex-security 0.1.2 → 0.1.4
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 +203 -9
- package/_bundled_plugin/references/scan-contract.md +2 -0
- package/_bundled_plugin/scripts/deep_scan_workbench.py +1 -1
- package/_bundled_plugin/scripts/finalize_scan_contract.py +289 -4
- package/_bundled_plugin/scripts/workbench_cli.py +4 -0
- package/_bundled_plugin/scripts/workbench_db.py +44 -24
- package/_bundled_plugin/scripts/workbench_scan_start.py +13 -0
- package/dist/api.d.ts +2 -1
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +173 -51
- package/dist/api.js.map +1 -1
- package/dist/bulk-scan-discovery.js +10 -11
- package/dist/bulk-scan-discovery.js.map +1 -1
- package/dist/cli.d.ts +3 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +116 -49
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +33 -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 +19 -22
- package/dist/contract.js.map +1 -1
- package/dist/runtime.d.ts +11 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +462 -8
- 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/runtime.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAEA,OAAO,EAAyB,KAAK,KAAK,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/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BAAsB,mBAAmB,CACvC,OAAO,GAAE,mBAAwB,GAChC,OAAO,CAAC,MAAM,CAAC,CAkEjB;AAED,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,MAAM,EACd,WAAW,GAAE,kBAAgC,GAC5C,kBAAkB,CAEpB;AAED,wBAAsB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAErE;AA+aD,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAMhE;AA8BD,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAMhD"}
|
package/dist/runtime.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { execFile as execFileCallback } from "node:child_process";
|
|
2
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
3
|
import { constants, existsSync } from "node:fs";
|
|
4
4
|
import { chmod, copyFile, lstat, mkdir, mkdtemp, open, readFile, readdir, realpath, rename, rm, rmdir, stat, writeFile, } from "node:fs/promises";
|
|
5
5
|
import { homedir, tmpdir } from "node:os";
|
|
@@ -8,6 +8,7 @@ import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep, }
|
|
|
8
8
|
import { fileURLToPath } from "node:url";
|
|
9
9
|
import { promisify } from "node:util";
|
|
10
10
|
import { crc32 } from "node:zlib";
|
|
11
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
11
12
|
import extractZip from "extract-zip";
|
|
12
13
|
import { parse } from "smol-toml";
|
|
13
14
|
import { CodexSecurityError, OutputDirectoryError, PluginBootstrapError, PluginPythonUnavailableError, } from "./errors.js";
|
|
@@ -24,6 +25,10 @@ const MAX_PLUGIN_COPY_ENTRIES = 4_096;
|
|
|
24
25
|
const MAX_PLUGIN_COPY_FILE_SIZE = 128 * 1024 * 1024;
|
|
25
26
|
const MAX_PLUGIN_COPY_SIZE = 512 * 1024 * 1024;
|
|
26
27
|
const MODEL_UNSAFE_PATH = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/u;
|
|
28
|
+
const CREDENTIAL_LOCK_NAME = ".codex-security-scan.lock";
|
|
29
|
+
const CREDENTIAL_LOGOUT_MARKER = ".codex-security-logged-out";
|
|
30
|
+
const CREDENTIAL_LOCK_POLL_MILLISECONDS = 25;
|
|
31
|
+
const INCOMPLETE_CREDENTIAL_LOCK_MILLISECONDS = 30_000;
|
|
27
32
|
export function codexSecurityStateDirectory(environment = process.env) {
|
|
28
33
|
const environmentValue = (requested) => {
|
|
29
34
|
const exact = environment[requested]?.trim();
|
|
@@ -39,6 +44,267 @@ export function codexSecurityStateDirectory(environment = process.env) {
|
|
|
39
44
|
const codexHome = environmentValue("CODEX_HOME") ?? join(homedir(), ".codex");
|
|
40
45
|
return resolve(expandHome(codexHome), "state", "plugins", "codex-security");
|
|
41
46
|
}
|
|
47
|
+
export function codexSecurityCredentialHome(environment = process.env) {
|
|
48
|
+
return join(codexSecurityStateDirectory(environment), "codex-home");
|
|
49
|
+
}
|
|
50
|
+
export async function prepareCodexSecurityCredentialHome(environment = process.env, validateLocation) {
|
|
51
|
+
const path = codexSecurityCredentialHome(environment);
|
|
52
|
+
try {
|
|
53
|
+
try {
|
|
54
|
+
await mkdir(path, { recursive: true, mode: 0o700 });
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
if (nodeErrorCode(error) === "EEXIST") {
|
|
58
|
+
const existing = await lstat(path).catch(() => null);
|
|
59
|
+
if (existing !== null &&
|
|
60
|
+
(!existing.isDirectory() || existing.isSymbolicLink())) {
|
|
61
|
+
throw new OutputDirectoryError(`Codex Security credential home is not a directory: ${path}`, { cause: error });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
if ((process.umask() & 0o700) !== 0)
|
|
67
|
+
await chmod(path, 0o700);
|
|
68
|
+
const metadata = await lstat(path);
|
|
69
|
+
if (!metadata.isDirectory() || metadata.isSymbolicLink()) {
|
|
70
|
+
throw new OutputDirectoryError(`Codex Security credential home is not a directory: ${path}`);
|
|
71
|
+
}
|
|
72
|
+
const canonical = await realpath(path);
|
|
73
|
+
requireModelSafeOutputDir(canonical);
|
|
74
|
+
validateLocation?.(canonical);
|
|
75
|
+
await requirePrivateCredentialHome(metadata, canonical);
|
|
76
|
+
return canonical;
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (error instanceof OutputDirectoryError)
|
|
80
|
+
throw error;
|
|
81
|
+
throw new OutputDirectoryError(`Unable to prepare the Codex Security credential home: ${path}`, { cause: error });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export async function requirePrivateCredentialHome(metadata, path, options = {}) {
|
|
85
|
+
if ((options.platform ?? process.platform) !== "win32") {
|
|
86
|
+
requirePrivateOutputDirectory(metadata, path);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
await (options.secureWindowsHome ?? secureWindowsCredentialHome)(path);
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
throw new OutputDirectoryError(`Unable to create a private Windows credential home: ${path}`, { cause: error });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async function secureWindowsCredentialHome(path) {
|
|
97
|
+
const systemRoot = process.env["SystemRoot"] ?? "C:\\Windows";
|
|
98
|
+
const powershell = join(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
99
|
+
const script = [
|
|
100
|
+
"$ErrorActionPreference = 'Stop'",
|
|
101
|
+
"$path = [Environment]::GetEnvironmentVariable('CODEX_SECURITY_CREDENTIAL_ACL_PATH', 'Process')",
|
|
102
|
+
"$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()",
|
|
103
|
+
"if ($null -eq $identity.User) { throw 'Unable to identify the current Windows user' }",
|
|
104
|
+
"$acl = New-Object System.Security.AccessControl.DirectorySecurity",
|
|
105
|
+
"$acl.SetAccessRuleProtection($true, $false)",
|
|
106
|
+
"$inheritance = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit",
|
|
107
|
+
"$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($identity.User, [System.Security.AccessControl.FileSystemRights]::FullControl, $inheritance, [System.Security.AccessControl.PropagationFlags]::None, [System.Security.AccessControl.AccessControlType]::Allow)",
|
|
108
|
+
"$acl.SetOwner($identity.User)",
|
|
109
|
+
"$acl.SetAccessRule($rule)",
|
|
110
|
+
"[System.IO.Directory]::SetAccessControl($path, $acl)",
|
|
111
|
+
"$verified = [System.IO.Directory]::GetAccessControl($path)",
|
|
112
|
+
"if (-not $verified.AreAccessRulesProtected) { throw 'Credential ACL still inherits access rules' }",
|
|
113
|
+
"$unexpected = @($verified.Access | Where-Object { $_.AccessControlType -eq [System.Security.AccessControl.AccessControlType]::Allow -and $_.IdentityReference.Translate([System.Security.Principal.SecurityIdentifier]).Value -ne $identity.User.Value })",
|
|
114
|
+
"if ($unexpected.Count -ne 0) { throw 'Credential ACL grants access to another identity' }",
|
|
115
|
+
].join("; ");
|
|
116
|
+
await execFile(powershell, ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", script], {
|
|
117
|
+
env: {
|
|
118
|
+
...process.env,
|
|
119
|
+
CODEX_SECURITY_CREDENTIAL_ACL_PATH: path,
|
|
120
|
+
},
|
|
121
|
+
encoding: "utf8",
|
|
122
|
+
windowsHide: true,
|
|
123
|
+
maxBuffer: 1024 * 1024,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
export async function acquireCodexSecurityCredentialHomeLock(codexHome, signal) {
|
|
127
|
+
const lock = join(codexHome, CREDENTIAL_LOCK_NAME);
|
|
128
|
+
const ownerPath = join(lock, "owner.json");
|
|
129
|
+
const token = randomUUID();
|
|
130
|
+
while (true) {
|
|
131
|
+
throwIfSignalAborted(signal);
|
|
132
|
+
try {
|
|
133
|
+
await mkdir(lock, { mode: 0o700 });
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
if (nodeErrorCode(error) !== "EEXIST")
|
|
137
|
+
throw error;
|
|
138
|
+
if (await recoverStaleCredentialHomeLock(lock))
|
|
139
|
+
continue;
|
|
140
|
+
await delay(CREDENTIAL_LOCK_POLL_MILLISECONDS, undefined, { signal });
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
await writeFile(ownerPath, `${JSON.stringify({ pid: process.pid, token })}\n`, { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
await rm(lock, { recursive: true, force: true }).catch(() => undefined);
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
let released = false;
|
|
151
|
+
return async () => {
|
|
152
|
+
if (released)
|
|
153
|
+
return;
|
|
154
|
+
const owner = JSON.parse(await readFile(ownerPath, "utf8"));
|
|
155
|
+
if (owner.token !== token) {
|
|
156
|
+
throw new PluginBootstrapError("The Codex Security credential-home lock is no longer owned by this scan.");
|
|
157
|
+
}
|
|
158
|
+
await rm(lock, { recursive: true, force: true });
|
|
159
|
+
released = true;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async function recoverStaleCredentialHomeLock(lock) {
|
|
164
|
+
const metadata = await lstat(lock).catch((error) => {
|
|
165
|
+
if (nodeErrorCode(error) === "ENOENT")
|
|
166
|
+
return null;
|
|
167
|
+
throw error;
|
|
168
|
+
});
|
|
169
|
+
if (metadata === null)
|
|
170
|
+
return true;
|
|
171
|
+
if (!metadata.isDirectory() || metadata.isSymbolicLink()) {
|
|
172
|
+
throw new OutputDirectoryError(`Codex Security credential-home lock is not a directory: ${lock}`);
|
|
173
|
+
}
|
|
174
|
+
let owner;
|
|
175
|
+
try {
|
|
176
|
+
owner = JSON.parse(await readFile(join(lock, "owner.json"), "utf8"));
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
if (nodeErrorCode(error) !== "ENOENT" && !(error instanceof SyntaxError)) {
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
if (Date.now() - metadata.mtimeMs <
|
|
183
|
+
INCOMPLETE_CREDENTIAL_LOCK_MILLISECONDS) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (isRecord(owner) && typeof owner["pid"] === "number") {
|
|
188
|
+
try {
|
|
189
|
+
process.kill(owner["pid"], 0);
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
if (nodeErrorCode(error) !== "ESRCH") {
|
|
194
|
+
if (nodeErrorCode(error) === "EPERM")
|
|
195
|
+
return false;
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
else if (Date.now() - metadata.mtimeMs <
|
|
201
|
+
INCOMPLETE_CREDENTIAL_LOCK_MILLISECONDS) {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
const quarantine = `${lock}.stale-${randomUUID()}`;
|
|
205
|
+
try {
|
|
206
|
+
await rename(lock, quarantine);
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
if (nodeErrorCode(error) === "ENOENT")
|
|
210
|
+
return true;
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
await rm(quarantine, { recursive: true, force: true });
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
export async function setCodexSecurityCredentialLogout(codexHome, loggedOut) {
|
|
217
|
+
const marker = join(codexHome, CREDENTIAL_LOGOUT_MARKER);
|
|
218
|
+
if (!loggedOut) {
|
|
219
|
+
await rm(marker, { force: true });
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const temporary = join(codexHome, `.codex-security-logout-${randomUUID()}.tmp`);
|
|
223
|
+
try {
|
|
224
|
+
const handle = await open(temporary, "wx", 0o600);
|
|
225
|
+
try {
|
|
226
|
+
await handle.chmod(0o600);
|
|
227
|
+
await handle.writeFile("logged out\n", "utf8");
|
|
228
|
+
await handle.sync();
|
|
229
|
+
}
|
|
230
|
+
finally {
|
|
231
|
+
await handle.close();
|
|
232
|
+
}
|
|
233
|
+
await rename(temporary, marker);
|
|
234
|
+
}
|
|
235
|
+
finally {
|
|
236
|
+
await rm(temporary, { force: true });
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
export async function codexSecurityCredentialAllowsAmbientImport(codexHome) {
|
|
240
|
+
try {
|
|
241
|
+
const marker = await lstat(join(codexHome, CREDENTIAL_LOGOUT_MARKER));
|
|
242
|
+
if (!marker.isFile() || marker.isSymbolicLink()) {
|
|
243
|
+
throw new OutputDirectoryError(`Codex Security logout marker is not a regular file: ${codexHome}`);
|
|
244
|
+
}
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
if (nodeErrorCode(error) === "ENOENT")
|
|
249
|
+
return true;
|
|
250
|
+
throw error;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
export async function codexSecurityHasStoredFileCredentials(codexHome) {
|
|
254
|
+
const path = join(codexHome, "auth.json");
|
|
255
|
+
let metadata;
|
|
256
|
+
try {
|
|
257
|
+
metadata = await lstat(path);
|
|
258
|
+
}
|
|
259
|
+
catch (error) {
|
|
260
|
+
if (nodeErrorCode(error) === "ENOENT")
|
|
261
|
+
return false;
|
|
262
|
+
throw error;
|
|
263
|
+
}
|
|
264
|
+
if (!metadata.isFile() || metadata.isSymbolicLink()) {
|
|
265
|
+
throw new OutputDirectoryError(`Codex Security stored authentication is not a regular file: ${path}`);
|
|
266
|
+
}
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
export async function preserveCodexSecurityPluginRegistration(codexHome, config) {
|
|
270
|
+
let existing;
|
|
271
|
+
try {
|
|
272
|
+
existing = parse(await readFile(join(codexHome, "config.toml"), "utf8"));
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
if (nodeErrorCode(error) === "ENOENT")
|
|
276
|
+
return config;
|
|
277
|
+
throw new PluginBootstrapError("Unable to read the existing Codex Security plugin registration.", { cause: error });
|
|
278
|
+
}
|
|
279
|
+
const marketplaces = isRecord(existing)
|
|
280
|
+
? existing["marketplaces"]
|
|
281
|
+
: undefined;
|
|
282
|
+
const plugins = isRecord(existing) ? existing["plugins"] : undefined;
|
|
283
|
+
const marketplace = isRecord(marketplaces)
|
|
284
|
+
? marketplaces[MARKETPLACE_NAME]
|
|
285
|
+
: undefined;
|
|
286
|
+
const plugin = isRecord(plugins)
|
|
287
|
+
? plugins[`${PLUGIN_NAME}@${MARKETPLACE_NAME}`]
|
|
288
|
+
: undefined;
|
|
289
|
+
const source = isRecord(marketplace) ? marketplace["source"] : undefined;
|
|
290
|
+
if (!isRecord(marketplace) ||
|
|
291
|
+
marketplace["source_type"] !== "local" ||
|
|
292
|
+
typeof source !== "string" ||
|
|
293
|
+
!(await sameFile(source, join(codexHome, "sdk-marketplace"))) ||
|
|
294
|
+
!isRecord(plugin) ||
|
|
295
|
+
plugin["enabled"] !== true) {
|
|
296
|
+
return config;
|
|
297
|
+
}
|
|
298
|
+
return {
|
|
299
|
+
...config,
|
|
300
|
+
marketplaces: {
|
|
301
|
+
[MARKETPLACE_NAME]: { source_type: "local", source },
|
|
302
|
+
},
|
|
303
|
+
plugins: {
|
|
304
|
+
[`${PLUGIN_NAME}@${MARKETPLACE_NAME}`]: { enabled: true },
|
|
305
|
+
},
|
|
306
|
+
};
|
|
307
|
+
}
|
|
42
308
|
export async function preparePersistentScanRoot(stateDirectory, repositoryName) {
|
|
43
309
|
const root = join(stateDirectory, "scans", safePrefix(repositoryName));
|
|
44
310
|
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
@@ -296,21 +562,34 @@ export async function importAmbientAuth(ambientHome, isolatedHome) {
|
|
|
296
562
|
return false;
|
|
297
563
|
}
|
|
298
564
|
await mkdir(isolatedHome, { recursive: true, mode: 0o700 });
|
|
565
|
+
if (await codexSecurityHasStoredFileCredentials(isolatedHome))
|
|
566
|
+
return true;
|
|
299
567
|
const destination = join(isolatedHome, "auth.json");
|
|
300
|
-
const temporary = join(isolatedHome, `.auth-${
|
|
568
|
+
const temporary = join(isolatedHome, `.auth-${randomUUID()}.tmp`);
|
|
301
569
|
try {
|
|
302
570
|
await copyFile(source, temporary, constants.COPYFILE_EXCL);
|
|
303
571
|
await chmod(temporary, 0o600);
|
|
304
|
-
|
|
572
|
+
try {
|
|
573
|
+
await copyFile(temporary, destination, constants.COPYFILE_EXCL);
|
|
574
|
+
}
|
|
575
|
+
catch (error) {
|
|
576
|
+
if (nodeErrorCode(error) === "EEXIST" &&
|
|
577
|
+
(await codexSecurityHasStoredFileCredentials(isolatedHome))) {
|
|
578
|
+
return true;
|
|
579
|
+
}
|
|
580
|
+
throw error;
|
|
581
|
+
}
|
|
305
582
|
await chmod(destination, 0o600);
|
|
306
583
|
return true;
|
|
307
584
|
}
|
|
308
585
|
catch (error) {
|
|
309
|
-
await rm(temporary, { force: true }).catch(() => undefined);
|
|
310
586
|
throw new PluginBootstrapError("Unable to copy ambient Codex authentication.", {
|
|
311
587
|
cause: error,
|
|
312
588
|
});
|
|
313
589
|
}
|
|
590
|
+
finally {
|
|
591
|
+
await rm(temporary, { force: true }).catch(() => undefined);
|
|
592
|
+
}
|
|
314
593
|
}
|
|
315
594
|
export async function extractPluginZip(archive, destination, signal) {
|
|
316
595
|
const archivePath = resolve(expandHome(archive));
|
|
@@ -495,6 +774,119 @@ export async function createMarketplace(codexHome, pluginRoot, signal) {
|
|
|
495
774
|
throwIfSignalAborted(signal);
|
|
496
775
|
return marketplace;
|
|
497
776
|
}
|
|
777
|
+
async function pluginProjectionFingerprint(root, signal) {
|
|
778
|
+
throwIfSignalAborted(signal);
|
|
779
|
+
const canonical = await realpath(root);
|
|
780
|
+
const contractPath = join(canonical, ".internal", "external-promotion", "external-projection-contract.json");
|
|
781
|
+
let paths;
|
|
782
|
+
if (canonical === (await bundledPluginRoot()) &&
|
|
783
|
+
(await isRegularFile(contractPath))) {
|
|
784
|
+
let contract;
|
|
785
|
+
try {
|
|
786
|
+
contract = JSON.parse(await readFile(contractPath, "utf8"));
|
|
787
|
+
}
|
|
788
|
+
catch (error) {
|
|
789
|
+
throw new PluginBootstrapError(`Invalid plugin projection contract: ${contractPath}`, { cause: error });
|
|
790
|
+
}
|
|
791
|
+
const shipped = isRecord(contract) ? contract["shippedExact"] : undefined;
|
|
792
|
+
if (!Array.isArray(shipped) ||
|
|
793
|
+
!shipped.every((path) => typeof path === "string")) {
|
|
794
|
+
throw new PluginBootstrapError("Plugin projection contract must contain shippedExact paths.");
|
|
795
|
+
}
|
|
796
|
+
paths = [
|
|
797
|
+
...new Set([".codex-plugin/plugin.json", ...shipped]
|
|
798
|
+
.filter((path) => !path.startsWith("sdk/"))
|
|
799
|
+
.map((path) => safeArchivePath(path))),
|
|
800
|
+
];
|
|
801
|
+
}
|
|
802
|
+
else {
|
|
803
|
+
paths = [];
|
|
804
|
+
const pending = [canonical];
|
|
805
|
+
let entries = 0;
|
|
806
|
+
while (pending.length > 0) {
|
|
807
|
+
throwIfSignalAborted(signal);
|
|
808
|
+
const path = pending.pop();
|
|
809
|
+
const metadata = await lstat(path);
|
|
810
|
+
if (++entries > MAX_PLUGIN_COPY_ENTRIES) {
|
|
811
|
+
throw new PluginBootstrapError(`Plugin source exceeds the copy entry limit: ${path}`);
|
|
812
|
+
}
|
|
813
|
+
if (metadata.isSymbolicLink()) {
|
|
814
|
+
throw new PluginBootstrapError(`Plugin contains an unsafe source path: ${path}`);
|
|
815
|
+
}
|
|
816
|
+
if (metadata.isDirectory()) {
|
|
817
|
+
for (const entry of await readdir(path)) {
|
|
818
|
+
pending.push(join(path, entry));
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
else if (metadata.isFile()) {
|
|
822
|
+
paths.push(relative(canonical, path).split(sep).join("/"));
|
|
823
|
+
}
|
|
824
|
+
else {
|
|
825
|
+
throw new PluginBootstrapError(`Plugin contains a non-regular file: ${path}`);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
paths.sort();
|
|
830
|
+
const fingerprint = createHash("sha256");
|
|
831
|
+
let totalSize = 0;
|
|
832
|
+
for (const relativePath of paths) {
|
|
833
|
+
throwIfSignalAborted(signal);
|
|
834
|
+
const path = join(canonical, ...relativePath.split("/"));
|
|
835
|
+
const metadata = await lstat(path);
|
|
836
|
+
if (!metadata.isFile() || metadata.isSymbolicLink()) {
|
|
837
|
+
throw new PluginBootstrapError(`Plugin projection contains an unsafe source path: ${path}`);
|
|
838
|
+
}
|
|
839
|
+
if (metadata.size > MAX_PLUGIN_COPY_FILE_SIZE) {
|
|
840
|
+
throw new PluginBootstrapError(`Plugin source exceeds the per-file safety limit: ${path}`);
|
|
841
|
+
}
|
|
842
|
+
totalSize += metadata.size;
|
|
843
|
+
if (totalSize > MAX_PLUGIN_COPY_SIZE) {
|
|
844
|
+
throw new PluginBootstrapError("Plugin source exceeds the copy safety limit.");
|
|
845
|
+
}
|
|
846
|
+
const handle = await open(path, constants.O_RDONLY |
|
|
847
|
+
(process.platform === "win32"
|
|
848
|
+
? 0
|
|
849
|
+
: constants.O_NOFOLLOW | constants.O_NONBLOCK));
|
|
850
|
+
try {
|
|
851
|
+
if (!samePluginFile(metadata, await handle.stat())) {
|
|
852
|
+
throw new PluginBootstrapError(`Plugin source changed before its integrity could be verified: ${path}`);
|
|
853
|
+
}
|
|
854
|
+
const contents = await readExactly(handle, metadata.size, 0, signal);
|
|
855
|
+
if (!samePluginFile(metadata, await handle.stat())) {
|
|
856
|
+
throw new PluginBootstrapError(`Plugin source changed while its integrity was being verified: ${path}`);
|
|
857
|
+
}
|
|
858
|
+
fingerprint.update(relativePath);
|
|
859
|
+
fingerprint.update("\0");
|
|
860
|
+
fingerprint.update(String(metadata.size));
|
|
861
|
+
fingerprint.update("\0");
|
|
862
|
+
fingerprint.update(contents);
|
|
863
|
+
fingerprint.update("\0");
|
|
864
|
+
}
|
|
865
|
+
finally {
|
|
866
|
+
await handle.close();
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
return fingerprint.digest("hex");
|
|
870
|
+
}
|
|
871
|
+
async function codexSecurityPluginRegistration(codexHome) {
|
|
872
|
+
let config;
|
|
873
|
+
try {
|
|
874
|
+
config = parse(await readFile(join(codexHome, "config.toml"), "utf8"));
|
|
875
|
+
}
|
|
876
|
+
catch (error) {
|
|
877
|
+
if (nodeErrorCode(error) === "ENOENT") {
|
|
878
|
+
return { marketplace: false, plugin: false };
|
|
879
|
+
}
|
|
880
|
+
throw new PluginBootstrapError("Unable to inspect the existing Codex Security plugin registration.", { cause: error });
|
|
881
|
+
}
|
|
882
|
+
const marketplaces = isRecord(config) ? config["marketplaces"] : undefined;
|
|
883
|
+
const plugins = isRecord(config) ? config["plugins"] : undefined;
|
|
884
|
+
return {
|
|
885
|
+
marketplace: isRecord(marketplaces) && isRecord(marketplaces[MARKETPLACE_NAME]),
|
|
886
|
+
plugin: isRecord(plugins) &&
|
|
887
|
+
isRecord(plugins[`${PLUGIN_NAME}@${MARKETPLACE_NAME}`]),
|
|
888
|
+
};
|
|
889
|
+
}
|
|
498
890
|
export function resolveCodexCommand() {
|
|
499
891
|
const { packageName, targetTriple } = codexPlatformPackage();
|
|
500
892
|
const require = createRequire(import.meta.url);
|
|
@@ -529,25 +921,87 @@ export function codexPlatformPackage(platform = process.platform, architecture =
|
|
|
529
921
|
export async function bootstrapPlugin(codexHome, pluginRoot, options = {}) {
|
|
530
922
|
const root = await realpath(pluginRoot);
|
|
531
923
|
const { name, version } = await pluginMetadata(root);
|
|
532
|
-
const
|
|
924
|
+
const existingMarketplace = join(codexHome, "sdk-marketplace");
|
|
925
|
+
let upgradeExistingPlugin = false;
|
|
926
|
+
let repairIncompletePlugin = false;
|
|
927
|
+
let installedRoot = null;
|
|
928
|
+
try {
|
|
929
|
+
await verifyPluginRegistration(codexHome, existingMarketplace);
|
|
930
|
+
installedRoot = await findInstalledPlugin(codexHome);
|
|
931
|
+
}
|
|
932
|
+
catch (error) {
|
|
933
|
+
throwIfSignalAborted(options.signal);
|
|
934
|
+
if (!(error instanceof PluginBootstrapError) &&
|
|
935
|
+
nodeErrorCode(error) !== "ENOENT") {
|
|
936
|
+
throw error;
|
|
937
|
+
}
|
|
938
|
+
const marketplace = await lstat(existingMarketplace).catch((failure) => {
|
|
939
|
+
if (nodeErrorCode(failure) === "ENOENT")
|
|
940
|
+
return null;
|
|
941
|
+
throw failure;
|
|
942
|
+
});
|
|
943
|
+
if (marketplace !== null &&
|
|
944
|
+
(!marketplace.isDirectory() || marketplace.isSymbolicLink())) {
|
|
945
|
+
throw new PluginBootstrapError(`Codex Security marketplace is not a safe directory: ${existingMarketplace}`);
|
|
946
|
+
}
|
|
947
|
+
const registration = await codexSecurityPluginRegistration(codexHome);
|
|
948
|
+
repairIncompletePlugin =
|
|
949
|
+
marketplace !== null || registration.marketplace || registration.plugin;
|
|
950
|
+
}
|
|
951
|
+
if (installedRoot !== null) {
|
|
952
|
+
const installed = await pluginMetadata(installedRoot);
|
|
953
|
+
if (installed.name === name && installed.version === version) {
|
|
954
|
+
const [selectedFingerprint, marketplaceFingerprint] = await Promise.all([
|
|
955
|
+
pluginProjectionFingerprint(root, options.signal),
|
|
956
|
+
pluginProjectionFingerprint(join(existingMarketplace, "plugins", PLUGIN_NAME), options.signal),
|
|
957
|
+
]);
|
|
958
|
+
if (selectedFingerprint === marketplaceFingerprint) {
|
|
959
|
+
return {
|
|
960
|
+
pluginRoot: root,
|
|
961
|
+
marketplaceRoot: existingMarketplace,
|
|
962
|
+
installedRoot,
|
|
963
|
+
marketplaceName: MARKETPLACE_NAME,
|
|
964
|
+
name,
|
|
965
|
+
version,
|
|
966
|
+
};
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
upgradeExistingPlugin = true;
|
|
970
|
+
}
|
|
971
|
+
throwIfSignalAborted(options.signal);
|
|
533
972
|
const command = options.codexCommand ?? resolveCodexCommand();
|
|
534
973
|
const environment = {
|
|
535
974
|
...(options.environment ?? process.env),
|
|
536
975
|
CODEX_HOME: codexHome,
|
|
537
976
|
};
|
|
538
977
|
const run = options.runCodex ?? runCodex;
|
|
978
|
+
if (upgradeExistingPlugin || repairIncompletePlugin) {
|
|
979
|
+
const registration = await codexSecurityPluginRegistration(codexHome);
|
|
980
|
+
if (registration.plugin) {
|
|
981
|
+
await run(command, ["plugin", "remove", `${PLUGIN_NAME}@${MARKETPLACE_NAME}`], environment, options.signal);
|
|
982
|
+
throwIfSignalAborted(options.signal);
|
|
983
|
+
}
|
|
984
|
+
if (registration.marketplace) {
|
|
985
|
+
await run(command, ["plugin", "marketplace", "remove", MARKETPLACE_NAME], environment, options.signal);
|
|
986
|
+
throwIfSignalAborted(options.signal);
|
|
987
|
+
}
|
|
988
|
+
throwIfSignalAborted(options.signal);
|
|
989
|
+
await rm(existingMarketplace, { recursive: true, force: true });
|
|
990
|
+
throwIfSignalAborted(options.signal);
|
|
991
|
+
}
|
|
992
|
+
const marketplace = await createMarketplace(codexHome, root, options.signal);
|
|
539
993
|
await run(command, ["plugin", "marketplace", "add", marketplace], environment, options.signal);
|
|
540
994
|
await run(command, ["plugin", "add", `${PLUGIN_NAME}@${MARKETPLACE_NAME}`], environment, options.signal);
|
|
541
995
|
await verifyPluginRegistration(codexHome, marketplace);
|
|
542
|
-
const
|
|
543
|
-
const installed = await pluginMetadata(
|
|
996
|
+
const verifiedInstalledRoot = await findInstalledPlugin(codexHome);
|
|
997
|
+
const installed = await pluginMetadata(verifiedInstalledRoot);
|
|
544
998
|
if (installed.name !== name || installed.version !== version) {
|
|
545
999
|
throw new PluginBootstrapError("Installed Codex Security plugin metadata does not match the selected plugin.");
|
|
546
1000
|
}
|
|
547
1001
|
return {
|
|
548
1002
|
pluginRoot: root,
|
|
549
1003
|
marketplaceRoot: marketplace,
|
|
550
|
-
installedRoot,
|
|
1004
|
+
installedRoot: verifiedInstalledRoot,
|
|
551
1005
|
marketplaceName: MARKETPLACE_NAME,
|
|
552
1006
|
name,
|
|
553
1007
|
version,
|