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): follow directory links in selected project config paths (#2514)

Michael D'Angelobbc3c7a79168
diff --git a/docs/project-configuration.md b/docs/project-configuration.md
index c56eb3ddb..a048aec2a 100644
--- a/docs/project-configuration.md
+++ b/docs/project-configuration.md
@@ -228,7 +228,9 @@ the file loader also returns
`projectConfig` path and source metadata. The optional directory argument defaults
to the current directory. It locates a selected file or anchors an in-memory
object's context, prompt, and output paths. Files anchor those paths to their own
-directory. Neither helper reads prompt contents or prepares a runtime.
+directory. On POSIX, `link/../scan.yaml` selects the file in the parent of
+the directory that `link` points to. Neither helper reads prompt contents or
+prepares a runtime.
Resolved context, prompt, and output paths have the SDK's `AbsolutePath` type;
scope paths remain relative to the selected repository.
diff --git a/sdk/typescript/src/project-config.ts b/sdk/typescript/src/project-config.ts
index 11689f2cb..3fdaea609 100644
--- a/sdk/typescript/src/project-config.ts
+++ b/sdk/typescript/src/project-config.ts
@@ -1,5 +1,7 @@
-import { readFile } from "node:fs/promises";
+import { isUtf8 } from "node:buffer";
+import { readFile, realpath } from "node:fs/promises";
import {
+ basename,
dirname,
extname,
isAbsolute,
@@ -23,7 +25,7 @@ import {
type ProjectConfigInput,
type ProjectScope,
} from "./project-config-schema.js";
-import { expandHome } from "./runtime.js";
+import { expandHomeForLookup } from "./runtime.js";
import {
DEFAULT_SCAN_AUTH,
DEFAULT_SCAN_MODE,
@@ -132,10 +134,24 @@ export async function readProjectConfig(
file: string,
directory = process.cwd(),
): Promise<ProjectConfigSource> {
- const path = resolve(directory, expandHome(file));
+ const selected = expandHomeForLookup(file);
+ const requested = isAbsolute(selected)
+ ? selected
+ : `${directory || "."}${sep}${selected}`;
+ const traversesParent = sep === "/" && requested.split("/").includes("..");
+ let path = traversesParent ? requested : resolve(directory, selected);
const extension = projectConfigExtension(path);
let text: string;
try {
+ // Resolve the parent through the filesystem while retaining the selected filename.
+ if (traversesParent) {
+ const parent = await realpath(dirname(path), { encoding: "buffer" });
+ if (!isUtf8(parent))
+ throw new Error(
+ "The resolved configuration directory is not a valid UTF-8 path.",
+ );
+ path = resolve(parent.toString(), basename(path));
+ }
text = await readFile(path, "utf8");
} catch (error) {
throw new ConfigurationError(
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index 19e0f20d5..3817a79b7 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -3024,7 +3024,7 @@ export function resolveCodexCommand(
const expanded =
configured === undefined
? undefined
- : expandExecutableHome(configured, environment);
+ : expandHomeForLookup(configured, environment);
if (
expanded &&
(process.platform !== "win32" || /\.(?:exe|com)$/iu.test(expanded))
@@ -3836,7 +3836,7 @@ async function usablePython(
): Promise<string | null> {
const command = await resolveTrustedExecutable(
isPythonPathCandidate(candidate)
- ? expandExecutableHome(candidate, environment)
+ ? expandHomeForLookup(candidate, environment)
: candidate,
environment,
protectedRoot,
@@ -3884,9 +3884,9 @@ export function sameFile(left: string, right: string): Promise<boolean> {
);
}
-function expandExecutableHome(
+export function expandHomeForLookup(
value: string,
- environment: ProcessEnvironment,
+ environment: ProcessEnvironment = process.env,
): string {
const path = value.startsWith("~\\") ? value.replaceAll("\\", "/") : value;
// Expand only the home prefix; joining the suffix would collapse symlink/.. paths.
diff --git a/sdk/typescript/tests-ts/project-config.test.ts b/sdk/typescript/tests-ts/project-config.test.ts
index 7660a7bf1..56ea23a32 100644
--- a/sdk/typescript/tests-ts/project-config.test.ts
+++ b/sdk/typescript/tests-ts/project-config.test.ts
@@ -1,6 +1,6 @@
import assert from "node:assert/strict";
-import { mkdir, readFile, writeFile } from "node:fs/promises";
-import { join } from "node:path";
+import { mkdir, readFile, symlink, writeFile } from "node:fs/promises";
+import { join, relative } from "node:path";
import { afterEach, describe, expect, test } from "bun:test";
import Ajv from "ajv";
import {
@@ -377,6 +377,70 @@ describe("project configuration input contract", () => {
});
describe("project configuration resolution", () => {
+ test.skipIf(process.platform === "win32")(
+ "follows a directory link before a parent component in the selected file",
+ async () => {
+ const root = await temporaryDirectory();
+ const selected = join(root, "selected");
+ const child = join(selected, "child");
+ await mkdir(child, { recursive: true });
+ await symlink(child, join(root, "link"));
+ for (const [directory, minutes] of [
+ [root, 2],
+ [selected, 7],
+ ] as const) {
+ await writeFile(
+ join(directory, "scan.json"),
+ JSON.stringify({
+ scan: { max_time_minutes: minutes },
+ output: { directory: "reports" },
+ }),
+ );
+ }
+ for (const [file, directory] of [
+ ["link/../scan.json", root],
+ [`${root}/link/../scan.json`, root],
+ ["scan.json", `${root}/link/..`],
+ [`${relative(process.cwd(), root)}/link/../scan.json`, ""],
+ ] as const) {
+ const project = await readProjectConfig(file, directory);
+ expect(project.path).toBe(join(selected, "scan.json"));
+ expect(resolveScanSettings(project, {}, root).options).toMatchObject({
+ maxTimeMinutes: 7,
+ outputDir: join(selected, "reports"),
+ });
+ }
+ await expect(
+ readProjectConfig("missing/../scan.json", root),
+ ).rejects.toMatchObject({
+ cause: { code: "ENOENT" },
+ });
+ const alias = join(root, "alias.json");
+ await symlink(join(selected, "scan.json"), alias);
+ expect((await readProjectConfig(alias)).directory).toBe(root);
+ },
+ );
+
+ test.skipIf(process.platform !== "linux")(
+ "does not select a replacement-character directory for a non-UTF-8 parent",
+ async () => {
+ const root = await temporaryDirectory();
+ const parent = Buffer.concat([
+ Buffer.from(`${root}/`),
+ Buffer.from([0xff]),
+ ]);
+ const child = Buffer.concat([parent, Buffer.from("/child")]);
+ await mkdir(child, { recursive: true });
+ await symlink(child, join(root, "link"));
+ await writeFile(Buffer.concat([parent, Buffer.from("/scan.json")]), "{}");
+ await mkdir(join(root, "\uFFFD"));
+ await writeFile(join(root, "\uFFFD", "scan.json"), "{}");
+ await expect(
+ readProjectConfig("link/../scan.json", root),
+ ).rejects.toThrow("valid UTF-8 path");
+ },
+ );
+
test("resolves paths according to their layer and replaces lists", async () => {
const root = await temporaryDirectory();
const project = {