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(sdk): privately create Windows multiscan campaign output (#2344)
cjc001338fd204927c1
diff --git a/sdk/typescript/src/multiscan.ts b/sdk/typescript/src/multiscan.tsindex 6f0f01b91..334bfbea9 100644--- a/sdk/typescript/src/multiscan.ts+++ b/sdk/typescript/src/multiscan.ts@@ -18,7 +18,15 @@ import {writeFile,} from "node:fs/promises";import { hostname } from "node:os";-import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";+import {+ basename,+ dirname,+ isAbsolute,+ join,+ relative,+ resolve,+ sep,+} from "node:path";import { promisify } from "node:util";import Papa from "papaparse";import type { CodexSecurity, ScanOptions } from "./api.js";@@ -38,6 +46,8 @@ import {} from "./knowledge-base.js";import { resolveScanPrompts } from "./prompt-files.js";import {+ canonicalConfigPath,+ createWindowsOutputDirectory,requireSecureOutputAncestry,resolvePluginPath,validateOutputDir,@@ -749,29 +759,28 @@ async function removeCheckout(path: string): Promise<string | undefined> {}async function ensureOutputDirectory(path: string): Promise<string> {- const metadata = await lstat(path, { bigint: true }).catch(+ let metadata = await lstat(path, { bigint: true }).catch(undefinedIfMissingFile,);- if (metadata?.isSymbolicLink()) {+ if (metadata === undefined) {+ if (process.platform === "win32") {+ // Resolve parent aliases while leaving the leaf for lstat to verify.+ path = join(await canonicalConfigPath(dirname(path)), basename(path));+ await createWindowsOutputDirectory(path, true);+ } else {+ await mkdir(path, { recursive: true, mode: 0o700 });+ }+ metadata = await lstat(path, { bigint: true });+ }+ if (metadata.isSymbolicLink()) {throw new Error("Multiscan output directories must not be symbolic links.");}- if (metadata !== undefined && !metadata.isDirectory()) {+ if (!metadata.isDirectory()) {throw new Error("Multiscan output paths must be directories.");}- let prepared = path;- if (metadata === undefined) {- prepared =- process.platform === "win32"- ? await canonicalWindowsCreationPath(path)- : path;- await mkdir(prepared, { recursive: true, mode: 0o700 });- }- const canonical = await realpath(prepared);+ const canonical = await realpath(path);const directory = await lstat(canonical, { bigint: true });- if (- metadata !== undefined &&- (directory.dev !== metadata.dev || directory.ino !== metadata.ino)- ) {+ if (directory.dev !== metadata.dev || directory.ino !== metadata.ino) {throw new Error("Multiscan output directories changed during preparation.");}if (process.platform === "win32") return canonical;@@ -789,20 +798,6 @@ async function ensureOutputDirectory(path: string): Promise<string> {return canonical;}-async function canonicalWindowsCreationPath(path: string): Promise<string> {- let ancestor = dirname(path);- for (;;) {- try {- return resolve(await realpath(ancestor), relative(ancestor, path));- } catch (error) {- if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;- const parent = dirname(ancestor);- if (parent === ancestor) throw error;- ancestor = parent;- }- }-}-async function appendReceipt(path: string, receipt: string): Promise<void> {const file = await open(path, "a", 0o600);try {diff --git a/sdk/typescript/tests-ts/multiscan-checkout.test.ts b/sdk/typescript/tests-ts/multiscan-checkout.test.tsindex 1a8bf8d3c..88f43ff72 100644--- a/sdk/typescript/tests-ts/multiscan-checkout.test.ts+++ b/sdk/typescript/tests-ts/multiscan-checkout.test.ts@@ -1174,6 +1174,40 @@ describe("multiscan", () => {}});+ test("rejects a campaign link created concurrently with output preparation", async () => {+ const { paths } = await repositoryFixture("concurrent-output");+ const external = join(paths.root, "external");+ await mkdir(external, { mode: 0o700 });+ const originalLstat = filesystem.lstat;+ const inspectOutput = spyOn(filesystem, "lstat").mockImplementation(+ async (path, options) => {+ try {+ return (await originalLstat(path, options as never)) as never;+ } catch (error) {+ if (+ path === paths.output &&+ (error as NodeJS.ErrnoException).code === "ENOENT"+ ) {+ await symlink(+ external,+ paths.output,+ process.platform === "win32" ? "junction" : "dir",+ );+ }+ throw error;+ }+ },+ );+ try {+ await expect(+ runMultiscan(options(paths, client(completeRun))),+ ).rejects.toThrow("symbolic links");+ expect(await readdir(external)).toEqual([]);+ } finally {+ inspectOutput.mockRestore();+ }+ });+test("rejects linked task artifact directories without touching external files", async () => {const { paths } = await repositoryFixture("victim");const external = join(paths.root, "external");diff --git a/sdk/typescript/tests-ts/windows-scan-output.test.ts b/sdk/typescript/tests-ts/windows-scan-output.test.tsindex b678bfd81..12ae15be8 100644--- a/sdk/typescript/tests-ts/windows-scan-output.test.ts+++ b/sdk/typescript/tests-ts/windows-scan-output.test.ts@@ -12,45 +12,62 @@ import { basename, join } from "node:path";import { promisify } from "node:util";import { afterEach, expect, test } from "bun:test";import { loadContract } from "../src/contract.js";+import { runMultiscan } from "../src/multiscan.js";import * as runtime from "../src/runtime.js";import { PLUGIN_ROOT } from "./plugin-root.js";-import { createApiTestFixtures } from "./support/temporary-directories.js";+import {+ client,+ completeRun,+ createMultiscanFixtures,+ options,+} from "./support/multiscan.js";-const { temporaryDirectory, cleanup } = createApiTestFixtures(- "codex-security-output-acl-",-);+const { temporaryDirectory, repositoryFixture, cleanup } =+ createMultiscanFixtures();afterEach(cleanup);const exec = promisify(execFile);const windowsTest = test.skipIf(process.platform !== "win32");const example = join(PLUGIN_ROOT, "examples", "completed-scan");+const system = join(process.env["SystemRoot"] ?? "C:\\Windows", "System32");+const powershell = join(system, "WindowsPowerShell", "v1.0");+async function icacls(path: string, ...args: string[]): Promise<void> {- await exec(- join(process.env["SystemRoot"] ?? "C:\\Windows", "System32", "icacls.exe"),- [path, ...args],- );+ await exec(join(system, "icacls.exe"), [path, ...args]);}-async function inspectAcl(path: string, expression: string): Promise<string> {- const system = join(process.env["SystemRoot"] ?? "C:\\Windows", "System32");- const result = await exec(- join(system, "WindowsPowerShell", "v1.0", "powershell.exe"),+async function sharedRoot(): Promise<string> {+ const root = await temporaryDirectory();+ await icacls(root, "/grant", "*S-1-1-0:(OI)(CI)R");+ return root;+}++async function runPowerShell(script: string, environment: NodeJS.ProcessEnv) {+ const { stdout } = await exec(+ join(powershell, "powershell.exe"),["-NoLogo","-NoProfile","-NonInteractive",- "-Command",- `$ErrorActionPreference = 'Stop'; $acl = Microsoft.PowerShell.Security\\Get-Acl -LiteralPath $env:CODEX_SECURITY_TEST_ACL_PATH; ${expression}`,+ "-EncodedCommand",+ Buffer.from(script, "utf16le").toString("base64"),],{env: {...process.env,- CODEX_SECURITY_TEST_ACL_PATH: path,- PSModulePath: join(system, "WindowsPowerShell", "v1.0", "Modules"),+ ...environment,+ PSModulePath: join(powershell, "Modules"),},},);- return result.stdout.trim();+ return stdout.trim();+}++function inspectAcl(path: string, expression: string): Promise<string> {+ return runPowerShell(+ `$ErrorActionPreference = 'Stop'; $acl = Microsoft.PowerShell.Security\\Get-Acl -LiteralPath $env:CODEX_SECURITY_TEST_ACL_PATH; ${expression}`,+ { CODEX_SECURITY_TEST_ACL_PATH: path },+ );}async function descriptor(path: string): Promise<string> {@@ -64,6 +81,30 @@ async function accessRules(path: string): Promise<string> {);}+async function createMultiscanCampaign(output: string): Promise<void> {+ const { paths } = await repositoryFixture("campaign-source");+ const result = await runMultiscan(+ options({ ...paths, output }, client(completeRun), { maxAttempts: 1 }),+ );+ expect(result).toMatchObject({ completed: 1, failed: 0 });+}++async function archiveSdkOutput(output: string, root: string): Promise<string> {+ let archive: string | undefined;+ await runtime.prepareOutputDir(+ output,+ "fixture",+ root,+ undefined,+ true,+ (path) => {+ archive = path;+ },+ );+ expect(archive).toBeDefined();+ return archive!;+}+async function archiveWorkbenchOutput(output: string,root: string,@@ -103,8 +144,7 @@ async function archiveWorkbenchOutput(windowsTest("new Windows scan roots start private beneath a shared parent",async () => {- const root = await temporaryDirectory();- await icacls(root, "/grant", "*S-1-1-0:(OI)(CI)R");+ const root = await sharedRoot();const before = await descriptor(root);expect(before).toContain(";;;WD)");const explicit = join(root, "new parent", "scan");@@ -116,7 +156,16 @@ windowsTest("fixture",root,);- for (const output of [join(root, "new parent"), explicit, generated]) {+ const campaign = join(root, "campaign");+ await createMultiscanCampaign(campaign);+ for (const output of [+ join(root, "new parent"),+ explicit,+ generated,+ campaign,+ join(campaign, "artifacts"),+ join(campaign, "checkouts"),+ ]) {const acl = await descriptor(output);expect(acl).toContain("D:P");expect(acl).not.toContain(";;;WD)");@@ -128,8 +177,7 @@ windowsTest(windowsTest.each([204, 233])("generated Windows output supports a %i-character repository name",async (length) => {- const root = await temporaryDirectory();- await icacls(root, "/grant", "*S-1-1-0:(OI)(CI)R");+ const root = await sharedRoot();const output = await runtime.prepareOutputDir(undefined,"r".repeat(length),@@ -149,8 +197,7 @@ windowsTest.each([204, 233])(windowsTest("fresh SDK output keeps its private identity through workbench registration",async () => {- const root = await temporaryDirectory();- await icacls(root, "/grant", "*S-1-1-0:(OI)(CI)R");+ const root = await sharedRoot();const repository = join(root, "repository");await mkdir(repository);await writeFile(join(repository, "source.txt"), "Synthetic repository");@@ -203,8 +250,7 @@ windowsTest.each()("generated Windows output remains archivable through %s for a %i-character repository name",async (archiver, length) => {- const root = await temporaryDirectory();- await icacls(root, "/grant", "*S-1-1-0:(OI)(CI)R");+ const root = await sharedRoot();const output = await runtime.prepareOutputDir(undefined,"r".repeat(length),@@ -215,28 +261,16 @@ windowsTest.each("Synthetic retained scan data",);const original = await descriptor(output);- let archive: string | undefined;- if (archiver === "SDK") {- await runtime.prepareOutputDir(- output,- "fixture",- root,- undefined,- true,- (path) => {- archive = path;- },- );- } else {- archive = await archiveWorkbenchOutput(output, root);- }+ const archive =+ archiver === "SDK"+ ? await archiveSdkOutput(output, root)+ : await archiveWorkbenchOutput(output, root);expect(await descriptor(output)).toBe(original);- expect(archive).toBeDefined();- expect(basename(archive!).length).toBeLessThanOrEqual(255);- expect(await readFile(join(archive!, "retained.txt"), "utf8")).toBe(+ expect(basename(archive).length).toBeLessThanOrEqual(255);+ expect(await readFile(join(archive, "retained.txt"), "utf8")).toBe("Synthetic retained scan data",);- expect(await descriptor(archive!)).toBe(original);+ expect(await descriptor(archive)).toBe(original);},);@@ -280,28 +314,29 @@ windowsTest("existing Windows scan ACLs are preserved", async () => {});windowsTest(- "archiving Windows output preserves old ACLs and privately creates the replacement",+ "existing Windows multiscan campaign ACLs are preserved",async () => {const root = await temporaryDirectory();- await icacls(root, "/grant", "*S-1-1-0:(OI)(CI)R");+ const output = join(root, "campaign");+ await mkdir(output);+ await icacls(output, "/grant", "*S-1-5-32-545:(OI)(CI)M");+ const permissions = await accessRules(output);+ await createMultiscanCampaign(output);+ expect(await accessRules(output)).toBe(permissions);+ },+);++windowsTest(+ "archiving Windows output preserves old ACLs and privately creates the replacement",+ async () => {+ const root = await sharedRoot();const output = join(root, "scan");await mkdir(output);await writeFile(join(output, "retained.txt"), "Synthetic retained data");const original = await descriptor(output);- let archive: string | undefined;- await runtime.prepareOutputDir(- output,- "fixture",- root,- undefined,- true,- (path) => {- archive = path;- },- );- expect(archive).toBeDefined();- expect(await descriptor(archive!)).toBe(original);- expect(await readFile(join(archive!, "retained.txt"), "utf8")).toBe(+ const archive = await archiveSdkOutput(output, root);+ expect(await descriptor(archive)).toBe(original);+ expect(await readFile(join(archive, "retained.txt"), "utf8")).toBe("Synthetic retained data",);expect(await descriptor(output)).not.toContain(";;;WD)");@@ -339,11 +374,12 @@ test.skipIf()("another local user cannot read or change new and archived scan output",async () => {- const root = await temporaryDirectory();- await icacls(root, "/grant", "*S-1-1-0:(OI)(CI)R");- const shared = join(root, "shared.txt");+ const root = await sharedRoot();+ const writableParent = join(root, "writable parent");+ await mkdir(writableParent);+ await icacls(writableParent, "/grant", "*S-1-1-0:(OI)(CI)M");+ const shared = join(writableParent, "shared.txt");await writeFile(shared, "synthetic shared data");- await icacls(shared, "/grant", "*S-1-1-0:M");const explicit = await runtime.prepareOutputDir(join(root, "explicit"),"fixture",@@ -353,44 +389,31 @@ test.skipIf("fixture",root,);- for (const output of [explicit, generated]) {+ const campaign = join(writableParent, "campaign");+ await createMultiscanCampaign(campaign);+ for (const output of [explicit, generated, campaign]) {await writeFile(join(output, "probe.txt"), "synthetic private data");}const archive = await archiveWorkbenchOutput(generated, root);await writeFile(join(generated, "probe.txt"), "synthetic private data");- const paths = [- shared,- ...[explicit, generated, archive].map((path) => join(path, "probe.txt")),- ];+ const privateProbes = [explicit, generated, archive, campaign].map((path) =>+ join(path, "probe.txt"),+ );const script = await readFile(join(import.meta.dir, "support", "windows-other-user-access.ps1"),"utf8",);- const system = join(process.env["SystemRoot"] ?? "C:\\Windows", "System32");- const result = await exec(- join(system, "WindowsPowerShell", "v1.0", "powershell.exe"),- [- "-NoLogo",- "-NoProfile",- "-NonInteractive",- "-EncodedCommand",- Buffer.from(script, "utf16le").toString("base64"),- ],- {- env: {- ...process.env,- CODEX_SECURITY_TEST_ACCESS_PATHS: JSON.stringify(paths),- PSModulePath: join(system, "WindowsPowerShell", "v1.0", "Modules"),- },- },- );- expect(JSON.parse(result.stdout)).toEqual([+ const result = await runPowerShell(script, {+ CODEX_SECURITY_TEST_ACCESS_PATHS: JSON.stringify([+ shared,+ ...privateProbes,+ ]),+ });+ expect(JSON.parse(result)).toEqual([{ read: true, write: true },- { read: false, write: false },- { read: false, write: false },- { read: false, write: false },+ ...privateProbes.map(() => ({ read: false, write: false })),]);- for (const path of paths.slice(1)) {+ for (const path of privateProbes) {expect(await readFile(path, "utf8")).toBe("synthetic private data");}},