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(windows): report filesystem module failures during output validation (#2516)
Michael D'Angelo5012030c85b4
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.mdindex 41ee08c52..2c29bf59a 100644--- a/sdk/typescript/docs/cli.md+++ b/sdk/typescript/docs/cli.md@@ -1140,6 +1140,11 @@ and does not migrate existing credentials or reports.Sandbox startup errors include the native diagnostic followed by a command toreproduce it. Linux namespace and AppArmor guidance appears only on Linux.+If Windows Application Control blocks `windows.node` while validating a scan+output directory, look for the module's block event in Event Viewer under+Applications and Services Logs → Microsoft → Windows → CodeIntegrity → Operational.+[Microsoft's App Control event reference](https://learn.microsoft.com/en-us/windows/security/application-security/application-control/app-control-for-business/operations/event-id-explanations)+On Windows, credential-home checks require trusted ownership and permissionsthrough every ancestor, including the volume root. Codex Security can repairthe credential home and its descendants; it does not change ancestor ownershipdiff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.tsindex 3817a79b7..ec60f66e9 100644--- a/sdk/typescript/src/runtime.ts+++ b/sdk/typescript/src/runtime.ts@@ -2006,7 +2006,9 @@ export async function validateOutputDir(const writableLeaf =!archiveExisting ||(process.platform === "win32" && (await readdir(path)).length === 0);- const canonical = await checkedOutputPath(path, writableLeaf);+ const canonical = await checkedOutputPath(path, writableLeaf).catch(+ rethrowOutputPathValidationError,+ );requireModelSafeOutputDir(canonical);return canonical;}@@ -2020,7 +2022,9 @@ export async function validateOutputDir(relative(parent, path),);requireModelSafeOutputDir(canonical);- await requireSecureOutputAncestry(canonical);+ await requireSecureOutputAncestry(canonical).catch(+ rethrowOutputPathValidationError,+ );return canonical;}break;@@ -2043,6 +2047,25 @@ export async function validateOutputDir(}}+function rethrowOutputPathValidationError(error: unknown): never {+ if (+ process.platform !== "win32" ||+ nodeErrorCode(error) !== "ERR_DLOPEN_FAILED"+ )+ throw error;+ const blocked =+ error instanceof Error &&+ /^(?:LoadLibrary failed: )?An Application Control policy has blocked this file\.(?:\s|$)/u.test(+ error.message,+ );+ throw new OutputDirectoryError(+ blocked+ ? "Windows Application Control blocked the bundled Codex Security filesystem module (windows.node). Check the CodeIntegrity Operational log in Event Viewer."+ : "Could not load the bundled Codex Security filesystem module (windows.node) on Windows.",+ { cause: error },+ );+}+export async function prepareScanArtifactRestorer(options: WorkbenchCommandOptions,scanDirectory: string,diff --git a/sdk/typescript/tests-ts/cli-output-directory.test.ts b/sdk/typescript/tests-ts/cli-output-directory.test.tsindex 82522a051..a12009ced 100644--- a/sdk/typescript/tests-ts/cli-output-directory.test.ts+++ b/sdk/typescript/tests-ts/cli-output-directory.test.ts@@ -1,8 +1,14 @@import { chmod, mkdir, stat, symlink, writeFile } from "node:fs/promises";+import * as fsPromises from "node:fs/promises";+import * as moduleApi from "node:module";import { join } from "node:path";-import { afterEach, describe, expect, mock, test } from "bun:test";+import { afterEach, describe, expect, mock, spyOn, test } from "bun:test";import { main } from "../src/cli.js";-import { validateOutputDir } from "../src/runtime.js";+import {+ bundledPluginRoot,+ createWindowsOutputDirectory,+ validateOutputDir,+} from "../src/runtime.js";import { dependencies } from "./cli-fixtures.js";import { git } from "./git-fixture.js";import { createCliTest } from "./support/cli-run.js";@@ -40,7 +46,122 @@ async function scanFixture() {return { root, output, started, deps };}+async function mockWindowsOutputBinding(load: () => unknown) {+ const binding = join(+ await bundledPluginRoot(),+ "mcp",+ "native",+ `win32-${process.arch}`,+ "windows.node",+ );+ const createRequire = moduleApi.createRequire;+ return spyOn(moduleApi, "createRequire").mockImplementation((from) => {+ const require = createRequire(from);+ return Object.assign(+ (id: string) => (id === binding ? load() : require(id)),+ require,+ );+ });+}+describe("CLI output directory permissions", () => {+ test.skipIf(process.platform !== "win32").each([+ [+ "Node policy block",+ "An Application Control policy has blocked this file.\r\nC:\\synthetic-install\\windows.node",+ true,+ ],+ [+ "Bun policy block",+ "LoadLibrary failed: An Application Control policy has blocked this file.",+ true,+ ],+ [+ "another load failure",+ "Invalid image in C:\\An Application Control policy has blocked this file.\\windows.node",+ false,+ ],+ ] as const)(+ "identifies a Windows module load failure (%s)",+ async (_label, detail, blocked) => {+ const { output, started, deps } = await scanFixture();+ const cause = Object.assign(new Error(detail), {+ code: "ERR_DLOPEN_FAILED",+ });+ using _loader = await mockWindowsOutputBinding(() => {+ throw cause;+ });+ const { runCli, stdout } = createCliTest(main);+ expect(+ await runCli(["scan", ".", "--json", "--output-dir", output], deps),+ ).toBe(2);+ const message = blocked+ ? "Windows Application Control blocked the bundled Codex Security filesystem module (windows.node). Check the CodeIntegrity Operational log in Event Viewer."+ : "Could not load the bundled Codex Security filesystem module (windows.node) on Windows.";+ expect(JSON.parse(stdout.text())).toEqual({+ status: "failed",+ code: "SCAN_FAILED",+ message,+ });+ await mkdir(output);+ await expect(validateOutputDir(output)).rejects.toMatchObject({+ message,+ cause,+ });+ await expect(createWindowsOutputDirectory(output, false)).rejects.toBe(+ cause,+ );+ expect(started).not.toHaveBeenCalled();+ },+ );++ test.skipIf(process.platform !== "win32")(+ "preserves the Windows output ACL error after loading the module",+ async () => {+ const { output } = await scanFixture();+ using _loader = await mockWindowsOutputBinding(() => ({+ validateWindowsOutputPath: () => 5,+ }));+ await expect(validateOutputDir(output)).rejects.toThrow(+ "could not be trusted (Windows error 5)",+ );+ },+ );++ test.skipIf(process.platform !== "win32")(+ "preserves other output inspection failures",+ async () => {+ const { output } = await scanFixture();+ const cause = Object.assign(+ new Error("An Application Control policy has blocked this file."),+ { code: "EACCES" },+ );+ const load = mock(() => {+ throw cause;+ });+ using _loader = await mockWindowsOutputBinding(load);+ await expect(validateOutputDir(output)).rejects.toMatchObject({+ message: `Unable to inspect scan output directory: ${output}`,+ cause,+ });+ const unrelated = Object.assign(new Error("Other module"), {+ code: "ERR_DLOPEN_FAILED",+ });+ using _lstat = spyOn(fsPromises, "lstat").mockRejectedValueOnce(+ unrelated,+ );+ await expect(validateOutputDir(output)).rejects.toMatchObject({+ message: `Unable to inspect scan output directory: ${output}`,+ cause: unrelated,+ });+ await writeFile(output, "existing file");+ await expect(validateOutputDir(output)).rejects.toThrow(+ `Scan output is not a directory: ${output}`,+ );+ expect(load).toHaveBeenCalledTimes(1);+ },+ );+test.skipIf(process.platform === "win32").each([["standard", [], 0o755],["deep", [], 0o775],