Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(plugin): require owned temporary artifact storage (#1278)

mldangelo-oai1ddd9b844a05
diff --git a/plugins/codex-security/mcp-app/src/artifact-storage.ts b/plugins/codex-security/mcp-app/src/artifact-storage.ts
index a476e8f65..f72e85c56 100644
--- a/plugins/codex-security/mcp-app/src/artifact-storage.ts
+++ b/plugins/codex-security/mcp-app/src/artifact-storage.ts
@@ -107,7 +107,17 @@ async function storageContext(
.catch((error: NodeJS.ErrnoException) => {
if (error.code !== "EEXIST") throw error;
});
- return { ...context, root };
+ const canonicalRoot = await requireArtifactRoot(
+ root,
+ "Temporary artifact storage",
+ );
+ const uid = process.geteuid?.();
+ if (uid !== undefined && (await fs.lstat(root)).uid !== uid) {
+ throw new Error(
+ "Temporary artifact storage must be owned by the current user.",
+ );
+ }
+ return { ...context, root: canonicalRoot };
}
function components(path: string): string[] {
diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.ts b/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.ts
index d19b85682..d487c4b58 100644
--- a/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.ts
+++ b/plugins/codex-security/mcp-app/tests/test_artifact_storage_regressions.ts
@@ -113,6 +113,219 @@ async function connect(overrides: Record<string, string | undefined> = {}) {
}
try {
+ await test(
+ "temporary artifact operations reject a collection owned by another user",
+ { skip: typeof process.geteuid !== "function" },
+ async (t) => {
+ const context = await standaloneArtifactContext(
+ repository,
+ workbench,
+ true,
+ path.join(fixture, "temporary-owner"),
+ "persistent",
+ );
+ const saved = await saveCodexSecurityArtifact(
+ context,
+ { storage: "temporary", path: "proof.txt", content: "owned evidence" },
+ workbench,
+ );
+ temporaryDirectories.push(saved.directory);
+ const lstat = fs.lstat;
+ t.mock.method(
+ fs,
+ "lstat",
+ async (...args: Parameters<typeof fs.lstat>) => {
+ const metadata = await lstat(...args);
+ return String(args[0]) === saved.directory
+ ? Object.assign(Object.create(metadata), {
+ uid: process.geteuid!() + 1,
+ })
+ : metadata;
+ },
+ );
+ let calls = 0;
+ const run = (...args: Parameters<typeof workbench>) => {
+ calls += 1;
+ return workbench(...args);
+ };
+ try {
+ const operations = [
+ () =>
+ saveCodexSecurityArtifact(context, { storage: "temporary" }, run),
+ () =>
+ saveCodexSecurityArtifact(
+ context,
+ {
+ storage: "temporary",
+ path: "proof.txt",
+ content: "replacement",
+ },
+ run,
+ ),
+ () =>
+ readCodexSecurityArtifact(
+ context,
+ { storage: "temporary", path: "proof.txt", encoding: "utf8" },
+ run,
+ ),
+ () =>
+ saveCodexSecurityArtifact(
+ context,
+ {
+ storage: "persistent",
+ path: "artifacts/imported.txt",
+ sourcePath: saved.path,
+ },
+ run,
+ ),
+ ];
+ for (const operation of operations) {
+ await assert.rejects(operation, /owned by the current user/);
+ assert.equal(calls, 0);
+ }
+ } finally {
+ t.mock.restoreAll();
+ }
+ assert.equal(await fs.readFile(saved.path, "utf8"), "owned evidence");
+ await assert.rejects(
+ fs.stat(path.join(context.root, "artifacts/imported.txt")),
+ {
+ code: "ENOENT",
+ },
+ );
+ assert.equal(
+ (
+ await saveCodexSecurityArtifact(
+ context,
+ { storage: "temporary" },
+ workbench,
+ )
+ ).directory,
+ saved.directory,
+ );
+ assert.equal(
+ (
+ await readCodexSecurityArtifact(
+ context,
+ { storage: "temporary", path: "proof.txt", encoding: "utf8" },
+ workbench,
+ )
+ ).content,
+ "owned evidence",
+ );
+ const imported = await saveCodexSecurityArtifact(
+ context,
+ {
+ storage: "persistent",
+ path: "artifacts/imported.txt",
+ sourcePath: saved.path,
+ },
+ workbench,
+ );
+ assert.equal(await fs.readFile(imported.path, "utf8"), "owned evidence");
+ },
+ );
+
+ await test(
+ "temporary collections use the effective filesystem identity",
+ {
+ skip:
+ typeof process.getuid !== "function" ||
+ typeof process.geteuid !== "function",
+ },
+ async (t) => {
+ const context = await standaloneArtifactContext(
+ repository,
+ workbench,
+ true,
+ path.join(fixture, "effective-owner"),
+ "persistent",
+ );
+ const effectiveUid = process.geteuid!();
+ t.mock.method(
+ process as typeof process & { getuid: () => number },
+ "getuid",
+ () => effectiveUid + 1,
+ );
+ try {
+ const saved = await saveCodexSecurityArtifact(
+ context,
+ {
+ storage: "temporary",
+ path: "proof.txt",
+ content: "owned evidence",
+ },
+ workbench,
+ );
+ temporaryDirectories.push(saved.directory);
+ assert.equal((await fs.lstat(saved.directory)).uid, effectiveUid);
+ const read = await readCodexSecurityArtifact(
+ context,
+ { storage: "temporary", path: "proof.txt", encoding: "utf8" },
+ workbench,
+ );
+ assert.equal(read.content, "owned evidence");
+ } finally {
+ t.mock.restoreAll();
+ }
+ },
+ );
+
+ await test(
+ "temporary collections check ownership of the selected path",
+ { skip: typeof process.geteuid !== "function" },
+ async (t) => {
+ const context = await standaloneArtifactContext(
+ repository,
+ workbench,
+ true,
+ path.join(fixture, "selected-owner"),
+ "persistent",
+ );
+ const saved = await saveCodexSecurityArtifact(
+ context,
+ { storage: "temporary" },
+ workbench,
+ );
+ temporaryDirectories.push(saved.directory);
+ const resolved = path.join(fixture, "resolved-owner");
+ await fs.mkdir(resolved, { mode: 0o700 });
+ const realpath = fs.realpath;
+ const lstat = fs.lstat;
+ t.mock.method(
+ fs,
+ "realpath",
+ async (...args: Parameters<typeof fs.realpath>) =>
+ String(args[0]) === saved.directory ? resolved : realpath(...args),
+ );
+ t.mock.method(
+ fs,
+ "lstat",
+ async (...args: Parameters<typeof fs.lstat>) => {
+ const metadata = await lstat(...args);
+ return String(args[0]) === saved.directory
+ ? Object.assign(Object.create(metadata), {
+ uid: process.geteuid!() + 1,
+ })
+ : metadata;
+ },
+ );
+ try {
+ await assert.rejects(
+ saveCodexSecurityArtifact(
+ context,
+ { storage: "temporary" },
+ workbench,
+ ),
+ /owned by the current user/,
+ );
+ } finally {
+ t.mock.restoreAll();
+ }
+ assert.deepEqual(await fs.readdir(resolved), []);
+ },
+ );
+
await test("binary imports and readback preserve files larger than the workbench JSON buffer", async () => {
const call = await connect();
const location = { targetPath: repository };