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(cli): resolve temporary paths before patching a saved checkout (#2558)

Michael D'Angelobae3008e79bb
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 7c48a4d11..22c35c36f 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -9097,7 +9097,9 @@ async function assessPatchRisk(
request.files === undefined
? []
: ["--", ...request.files.map((file) => file)];
- const root = await mkdtemp(join(tmpdir(), "codex-security-patch-risk-"));
+ const root = await mkdtemp(
+ join(await realpath(tmpdir()), "codex-security-patch-risk-"),
+ );
const patchPath = join(root, "patch.diff");
try {
const head =
@@ -9170,7 +9172,9 @@ async function withPatchIndex<T>(
git: (args: string[], input?: string) => Promise<string>,
) => Promise<T>,
): Promise<T> {
- const root = await mkdtemp(join(tmpdir(), "codex-security-patch-tree-"));
+ const root = await mkdtemp(
+ join(await realpath(tmpdir()), "codex-security-patch-tree-"),
+ );
try {
return await operation((args, input) =>
dependencies.runRepositoryCommand("git", args, repository, {
diff --git a/sdk/typescript/src/finding-prompt.ts b/sdk/typescript/src/finding-prompt.ts
index 7a32d82d2..fea0c24c3 100644
--- a/sdk/typescript/src/finding-prompt.ts
+++ b/sdk/typescript/src/finding-prompt.ts
@@ -1,4 +1,4 @@
-import { mkdtemp, rm, writeFile } from "node:fs/promises";
+import { mkdtemp, realpath, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
@@ -17,7 +17,9 @@ export async function prepareFindingPrompt(
if (characterCount(prompt) <= MAX_CODEX_INPUT_CHARACTERS)
return { prompt, async [Symbol.asyncDispose]() {} };
- const directory = await mkdtemp(join(tmpdir(), "codex-security-finding-"));
+ const directory = await mkdtemp(
+ join(await realpath(tmpdir()), "codex-security-finding-"),
+ );
const cleanup = async () => {
// Cleanup must not replace a completed result or the original failure.
await rm(directory, { recursive: true, force: true }).catch(() => {});
diff --git a/sdk/typescript/tests-ts/cli-patch.test.ts b/sdk/typescript/tests-ts/cli-patch.test.ts
index cf31b03e7..d88922fc6 100644
--- a/sdk/typescript/tests-ts/cli-patch.test.ts
+++ b/sdk/typescript/tests-ts/cli-patch.test.ts
@@ -3,12 +3,12 @@ import { gitText } from "./support/shell.js";
import { emptyPage } from "./support/linear-pagination.js";
import { resolving } from "./support/promises.js";
import { parse as parseToml } from "smol-toml";
-import { afterEach, describe, expect, test, mock } from "bun:test";
+import { afterEach, describe, expect, test, mock, spyOn } from "bun:test";
import { execFileSync } from "node:child_process";
import { hash } from "node:crypto";
import { mkdir, readFile, rm, symlink, writeFile } from "node:fs/promises";
-import { tmpdir } from "node:os";
-import { dirname, join, relative, resolve } from "node:path";
+import * as os from "node:os";
+import { dirname, isAbsolute, join, relative, resolve } from "node:path";
import { Writable } from "node:stream";
import { stripVTControlCharacters } from "node:util";
import type { Finding, JsonObject, SeverityLevel } from "../src/index.js";
@@ -982,6 +982,9 @@ describe("scan and patch workflow", () => {
test("assesses only changes made during a literal patch run", async () => {
const directory = await temporaryDirectory("codex-security-patch-risk-");
+ using _temporary = spyOn(os, "tmpdir").mockReturnValue(
+ relative(process.cwd(), directory),
+ );
const repository = join(directory, "repository");
await mkdir(repository, { recursive: true });
const git = repositoryGit(repository);
@@ -1046,6 +1049,7 @@ describe("scan and patch workflow", () => {
.split("\n")
.find((line) => line.startsWith('{"path":'))!,
) as { path: string; sha256: string };
+ expect(isAbsolute(artifact.path)).toBe(true);
const patch = await readFile(artifact.path, "utf8");
expect(patch).toContain("+patch change");
expect(patch).not.toContain("+user change");
@@ -1059,7 +1063,11 @@ describe("scan and patch workflow", () => {
output?.stdout.write("Patch complete.");
return 0;
},
- onRepositoryCommand: runGitRepositoryCommand,
+ onRepositoryCommand: (command, args, cwd, options) => {
+ const index = options?.environment?.["GIT_INDEX_FILE"];
+ if (index !== undefined) expect(isAbsolute(index)).toBe(true);
+ return runGitRepositoryCommand(command, args, cwd, options);
+ },
},
);
@@ -1431,7 +1439,7 @@ describe("scan and patch workflow", () => {
test.each(["synthetic.provider", "openai"])(
"preserves %s command-provider authentication when patching after a scan",
async (provider) => {
- const home = join(tmpdir(), "synthetic-auth-home");
+ const home = join(os.tmpdir(), "synthetic-auth-home");
let providerOverride: string | undefined;
const outcome = await runWorkflow(
[
diff --git a/sdk/typescript/tests-ts/finding-prompt.test.ts b/sdk/typescript/tests-ts/finding-prompt.test.ts
index 9fd51c973..d8ba63937 100644
--- a/sdk/typescript/tests-ts/finding-prompt.test.ts
+++ b/sdk/typescript/tests-ts/finding-prompt.test.ts
@@ -1,6 +1,7 @@
import { readFile, stat } from "node:fs/promises";
-import { dirname } from "node:path";
-import { expect, test } from "bun:test";
+import * as os from "node:os";
+import { dirname, isAbsolute, relative } from "node:path";
+import { expect, spyOn, test } from "bun:test";
import { MAX_CODEX_INPUT_CHARACTERS } from "../src/codex-prompt.js";
import { prepareFindingPrompt } from "../src/finding-prompt.js";
@@ -20,6 +21,8 @@ test.each([false, true])(
evidence: '"\\\n🦞'.repeat(200_000),
tail: "Final evidence",
});
+ const temporaryRoot = relative(process.cwd(), os.tmpdir()) || ".";
+ using _temporary = spyOn(os, "tmpdir").mockReturnValue(temporaryRoot);
let path = "";
const run = async () => {
await using prepared = await prepareFindingPrompt(
@@ -28,6 +31,7 @@ test.each([false, true])(
);
expect(prepared.prompt.length).toBeLessThan(MAX_CODEX_INPUT_CHARACTERS);
path = JSON.parse(prepared.prompt.match(/^".*finding\.json"$/m)![0]);
+ expect(isAbsolute(path)).toBe(true);
expect(await readFile(path, "utf8")).toBe(finding);
if (process.platform !== "win32") {
expect((await stat(path)).mode & 0o777).toBe(0o600);