OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(targets): check resolved path types before scan registration (#549)
Sylvester Kaczmarekbdd5000fee4f
diff --git a/sdk/typescript/src/targets.ts b/sdk/typescript/src/targets.tsindex d1cfe415e..8d5f464a9 100644--- a/sdk/typescript/src/targets.ts+++ b/sdk/typescript/src/targets.ts@@ -1,4 +1,5 @@import { isNonEmptyString } from "./value.js";+import { isUtf8 } from "node:buffer";import { execFile as execFileCallback } from "node:child_process";import { realpathSync, type Stats } from "node:fs";import { lstat, readFile, realpath, stat } from "node:fs/promises";@@ -128,10 +129,13 @@ export async function normalizeRepository(): Promise<string> {const candidate = resolveRepositoryPath(repository);requirePortableWindowsRepositoryPath(candidate);- let canonical: string;+ let resolved: Buffer;try {- canonical = await abortable(() => realpath(candidate), signal);- if (!(await abortable(() => stat(canonical), signal)).isDirectory()) {+ resolved = await abortable(+ () => realpath(candidate, { encoding: "buffer" }),+ signal,+ );+ if (!(await abortable(() => lstat(resolved), signal)).isDirectory()) {throw new Error("not a directory");}} catch (error) {@@ -143,14 +147,29 @@ export async function normalizeRepository(},);}+ requireUtf8Path("Repository path", candidate, resolved);+ const canonical = resolved.toString();requirePortableWindowsRepositoryPath(canonical);return canonical;}export function resolveRepositoryPath(repository: string): string {+ requireUtf8Path("Repository path", repository);return resolve(expandHome(repository));}+function requireUtf8Path(+ label: "Repository path" | "Path target",+ value: string,+ resolved?: Buffer,+): void {+ if (resolved === undefined ? !value.isWellFormed() : !isUtf8(resolved)) {+ throw new InvalidTargetError(+ `${label} cannot be represented as UTF-8: ${value}`,+ );+ }+}+function requirePortableWindowsRepositoryPath(path: string): void {if (process.platform !== "win32") return;const ambiguous = windowsUnsafePathComponent(path);@@ -572,26 +591,37 @@ export async function normalizeTarget("Path scan targets must not contain an empty path.",);}+ requireUtf8Path("Path target", value);const candidate = isAbsolute(expandHome(value))? resolve(expandHome(value)): resolve(root, expandHome(value));- let metadata: Stats;let canonical: string;try {- metadata = await abortable(() => stat(candidate), signal);- canonical = await abortable(() => realpath(candidate), signal);+ // Some runtimes cannot canonicalize special files to filesystem paths.+ requireScanPathType(+ value,+ await abortable(() => stat(candidate), signal),+ );+ const resolved = await abortable(+ () => realpath(candidate, { encoding: "buffer" }),+ signal,+ );+ canonical = resolved.toString();+ if (!isAbsolute(canonical)) {+ throw new InvalidTargetError(`Could not resolve path target: ${value}`);+ }+ requireScanPathType(+ value,+ await abortable(() => lstat(resolved), signal),+ );+ requireUtf8Path("Path target", value, resolved);} catch (error) {throwIfAborted(signal);+ if (error instanceof InvalidTargetError) throw error;throw new InvalidTargetError(`Path target does not exist: ${value}`, {cause: error,});}- // Match the bundled scan scope resolver's supported filesystem types.- if (!metadata.isFile() && !metadata.isDirectory()) {- throw new InvalidTargetError(- `Path target is not a regular file or directory: ${value}`,- );- }const relativePath = relative(root, canonical);if (relativePathIsOutside(relativePath)) {throw new InvalidTargetError(@@ -612,6 +642,14 @@ export async function normalizeTarget(return { kind: "paths", paths: [...paths] };}+function requireScanPathType(value: string, metadata: Stats): void {+ if (!metadata.isFile() && !metadata.isDirectory()) {+ throw new InvalidTargetError(+ `Path target is not a regular file or directory: ${value}`,+ );+ }+}+export async function validateCommittedDiffCheckout(repository: string,target: NormalizedTarget,diff --git a/sdk/typescript/tests-ts/external-findings-local.test.ts b/sdk/typescript/tests-ts/external-findings-local.test.tsindex 887f4ac7a..a701a2c23 100644--- a/sdk/typescript/tests-ts/external-findings-local.test.ts+++ b/sdk/typescript/tests-ts/external-findings-local.test.ts@@ -766,7 +766,8 @@ describe("local normalized scanner findings", () => {const original = { lstat: fs.lstat, realpath: fs.realpath, stat: fs.stat };const held = new Map();function lookup(method, path, args) {- const gate = held.get(method + ":" + path);+ // Repository normalization checks a byte path before the protected-root lookups.+ const gate = typeof path === "string" && held.get(method + ":" + path);if (gate) { gate.entered(); return gate.wait; }return original[method](path, ...args);}diff --git a/sdk/typescript/tests-ts/fixtures/socket-target.ts b/sdk/typescript/tests-ts/fixtures/socket-target.tsnew file mode 100644index 000000000..002e47243--- /dev/null+++ b/sdk/typescript/tests-ts/fixtures/socket-target.ts@@ -0,0 +1,56 @@+import assert from "node:assert/strict";+import { execFileSync } from "node:child_process";+import { readlink, rename, symlink, writeFile } from "node:fs/promises";+import { createServer } from "node:net";+import { basename, join } from "node:path";+import { normalizeTarget } from "../../src/targets.js";++const root = process.argv[2]!;+async function expectSpecialFile(path: string) {+ await assert.rejects(normalizeTarget(root, [path]), {+ name: "InvalidTargetError",+ message: `Path target is not a regular file or directory: ${path}`,+ });+}++const socket = join(root, "target.sock");+const alias = join(root, "alias");+const server = createServer();+try {+ await new Promise<void>((resolve, reject) => {+ server.once("error", reject);+ server.listen("target.sock", resolve);+ });+ await symlink(socket, alias);+ await expectSpecialFile(socket);+ await expectSpecialFile(alias);+} finally {+ server.close();+}++if (process.platform === "linux") {+ // A descriptor label or a decoded byte name can also name an ordinary file.+ const label = await readlink("/proc/self/fd/0");+ if (basename(label) === label) await writeFile(label, "sibling");+ await symlink("/proc/self/fd/0", "input-link");+ await expectSpecialFile("input-link");++ const fifo = Buffer.concat([Buffer.from("doc-"), Buffer.from([0xff])]);+ execFileSync("mkfifo", ["queue"]);+ await rename("queue", fifo);+ await symlink(fifo, "queue-link");+ const ordinary = "doc-\uFFFD";+ await writeFile(ordinary, "sibling");+ await expectSpecialFile("queue-link");+ await rename(fifo, "old-queue");+ await writeFile(fifo, "different file");+ await assert.rejects(normalizeTarget(root, ["queue-link"]), {+ name: "InvalidTargetError",+ message: "Path target cannot be represented as UTF-8: queue-link",+ });+ await symlink(ordinary, "ordinary-link");+ assert.deepEqual(+ (await normalizeTarget(root, [ordinary, "ordinary-link"])).paths,+ [ordinary],+ );+}diff --git a/sdk/typescript/tests-ts/targets.test.ts b/sdk/typescript/tests-ts/targets.test.tsindex efa32a061..439f1300e 100644--- a/sdk/typescript/tests-ts/targets.test.ts+++ b/sdk/typescript/tests-ts/targets.test.ts@@ -7,6 +7,7 @@ import {mkdir,readFile,realpath,+ rename,rm,symlink,utimes,@@ -32,6 +33,7 @@ import {gitProtectionRoots,} from "../src/targets.js";import { createApiTestFixtures } from "./support/temporary-directories.js";+import { runCommand } from "./support/shell.js";// @ts-expect-error DiffTarget is intentionally nominal; use its constructor helpers.const structurallyInvalidTarget: ScanTarget = {@@ -308,6 +310,58 @@ describe("scan target normalization", () => {},);+ test("keeps repository and target paths distinct from their UTF-8 replacements", async () => {+ const root = await temporaryDirectory();+ const repository = join(root, "repo-\uFFFD");+ const file = "sibling-\uFFFD.ts";+ await mkdir(repository);+ await writeFile(join(repository, file), "");+ await expect(+ normalizeTarget(join(root, "repo-") + "\uD800", [file]),+ ).rejects.toThrow("Repository path cannot be represented as UTF-8");+ await expect(+ normalizeTarget(repository, ["sibling-\uDC00.ts"]),+ ).rejects.toThrow("Path target cannot be represented as UTF-8");+ expect((await normalizeTarget(repository, [file])).paths).toEqual([file]);+ if (process.platform === "linux") {+ const raw = Buffer.concat([+ Buffer.from(join(root, "repo-")),+ Buffer.from([0xff]),+ ]);+ const alias = join(root, "selected");+ await mkdir(raw);+ try {+ await symlink(raw, alias);+ await expect(normalizeTarget(alias, [file])).rejects.toThrow(+ "Repository path cannot be represented as UTF-8",+ );+ } finally {+ await rm(raw, { recursive: true });+ }+ }+ });++ test.skipIf(process.platform === "win32").each(["node", "bun"])(+ "identifies special file targets and their links in %s",+ async (runtime) => {+ const root = await temporaryDirectory("cs-target-");+ const build = await Bun.build({+ entrypoints: [+ fileURLToPath(+ new URL("./fixtures/socket-target.ts", import.meta.url),+ ),+ ],+ outdir: root,+ naming: "socket-target.mjs",+ target: "node",+ });+ const result = await runCommand(runtime, [build.outputs[0]!.path, root], {+ cwd: root,+ });+ expect(result.status, result.stderr || result.error?.message).toBe(0);+ },+ );+test.skipIf(process.platform !== "win32")("rejects NTFS alternate streams before runtime initialization",async () => {@@ -335,19 +389,44 @@ describe("scan target normalization", () => {},);- test("reports a path that disappears during normalization as invalid", async () => {+ test.each(+ process.platform === "win32"+ ? ["removed"]+ : ["removed", "retargeted", "replaced after resolution"],+ )("rejects a path %s during canonical resolution", async (change) => {const repo = await repository();+ const target = join(repo, "src", "app.ts");+ if (change === "retargeted") {+ const pending = join(repo, "pending");+ execFileSync("mkfifo", [pending]);+ if (process.platform === "linux") {+ const raw = Buffer.concat([Buffer.from(pending), Buffer.from([0xff])]);+ await rename(pending, raw);+ await symlink(raw, pending);+ await writeFile(raw.toString(), "regular sibling");+ }+ }+ if (change === "replaced after resolution")+ await writeFile(join(repo, "..", "outside-file"), "outside");const script = `import { mockFs } from ${JSON.stringify(fileURLToPath(new URL("./support/module-mocks.ts", import.meta.url)))};- import { rmSync } from "node:fs";+ import { rmSync, symlinkSync } from "node:fs";import * as original from "node:fs/promises";import { join } from "node:path";- const [repo, targets] = process.argv.slice(1);- const target = join(repo, "src", "app.ts");+ const [repo, targets, target, change] = process.argv.slice(1);const actualRealpath = original.realpath;mockFs(() => ({realpath: async (path, ...args) => {- if (path === target) rmSync(target);+ if (path === target && change === "replaced after resolution") {+ const canonical = await actualRealpath(path, ...args);+ rmSync(target);+ symlinkSync(join(repo, "..", "outside-file"), target);+ return canonical;+ }+ if (path === target) {+ rmSync(target);+ if (change === "retargeted") symlinkSync(join(repo, "pending"), target);+ }return await actualRealpath(path, ...args);},}));@@ -371,11 +450,17 @@ describe("scan target normalization", () => {script,repo,fileURLToPath(new URL("../src/targets.ts", import.meta.url)),+ target,+ change,],{ encoding: "utf8" },);expect(result.status).toBe(0);- expect(result.stdout).toContain("REJECTED true Path target does not exist");+ expect(result.stdout).toContain(+ change === "removed"+ ? "REJECTED true Path target does not exist"+ : "REJECTED true Path target is not a regular file or directory",+ );});test("binds ref and working-tree targets to commit IDs", async () => {