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(sdk): stage knowledge in the selected temporary directory (#2559)

Michael D'Angelo0384bb30e587
diff --git a/sdk/typescript/src/knowledge-base.ts b/sdk/typescript/src/knowledge-base.ts
index 2afa5e2ed..a0ce4508e 100644
--- a/sdk/typescript/src/knowledge-base.ts
+++ b/sdk/typescript/src/knowledge-base.ts
@@ -154,7 +154,7 @@ export async function prepareKnowledgeBase(
: await readKnowledgeBaseSnapshot(input, signal);
const protectedRoots = await knowledgeBaseProtectedRoots(snapshot, signal);
const path = await mkdtemp(
- join(directory ?? tmpdir(), "codex-security-knowledge-"),
+ join(await realpath(directory ?? tmpdir()), "codex-security-knowledge-"),
);
try {
for (const [filename, text] of Object.entries(snapshot.documents)) {
diff --git a/sdk/typescript/tests-ts/knowledge-base.test.ts b/sdk/typescript/tests-ts/knowledge-base.test.ts
index 7497a3d84..371d0981b 100644
--- a/sdk/typescript/tests-ts/knowledge-base.test.ts
+++ b/sdk/typescript/tests-ts/knowledge-base.test.ts
@@ -11,7 +11,7 @@ import {
} from "node:fs/promises";
import * as filesystem from "node:fs/promises";
import * as os from "node:os";
-import { join } from "node:path";
+import { dirname, join, relative, sep } from "node:path";
import { afterEach, describe, expect, mock, spyOn, test } from "bun:test";
import { strToU8, zipSync } from "fflate";
import { ConfigurationError } from "../src/errors.js";
@@ -896,6 +896,37 @@ describe("scan knowledge bases", () => {
]);
});
+ test.each(["relative", "linked parent"])(
+ "stages knowledge in the worker-readable temporary directory: %s",
+ async (spelling) => {
+ const root = await temporaryDirectory();
+ const source = join(root, "policy.md");
+ await writeFile(source, "Synthetic policy.");
+ const staging = join(root, "staging");
+ await mkdir(staging);
+ let temporary = relative(process.cwd(), staging);
+ if (spelling === "linked parent") {
+ const child = join(staging, "child");
+ const link = join(root, "alias");
+ await mkdir(child);
+ await symlink(
+ child,
+ link,
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ temporary = link + sep + "..";
+ }
+ const expected = await filesystem.realpath(temporary);
+ using _temporary = spyOn(os, "tmpdir").mockReturnValue(temporary);
+ const prepared = await prepareKnowledgeBase([source]);
+ temporaryDirectories.track(prepared.path);
+ expect(dirname(prepared.path)).toBe(expected);
+ expect(await extractedDocuments(prepared.path)).toEqual([
+ "Synthetic policy.",
+ ]);
+ },
+ );
+
test("skips macOS metadata files copied next to documents", async () => {
const root = await temporaryDirectory();
await writeFile(join(root, "guide.md"), "Rotate admin keys monthly.");