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): explain unreadable plugin installation receipts (#2403)
Michael D'Angeloc5ee6a0f5891
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.tsindex adf31f6c2..d6fcc37f1 100644--- a/sdk/typescript/src/runtime.ts+++ b/sdk/typescript/src/runtime.ts@@ -3050,9 +3050,20 @@ export async function bootstrapPlugin(const previous: unknown = await readFile(installRecord, "utf8").then((value) => JSON.parse(value) as unknown).catch((error: unknown) => {- if (nodeErrorCode(error) === "ENOENT" || error instanceof SyntaxError) {+ const code = nodeErrorCode(error);+ if (code === "ENOENT" || error instanceof SyntaxError) {return null;}+ if (+ process.platform === "win32" &&+ error instanceof Error &&+ (code === "EACCES" || code === "EPERM")+ ) {+ // Keep the original system error, including its path, code and syscall.+ error.message +=+ " If the SDK created this plugin installation receipt but cannot reopen it, " ++ "check its read permissions and the permissions new files inherit from the sdk-marketplace directory.";+ }throw error;});diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.tsindex 4bbbd0b85..bdfa0e23e 100644--- a/sdk/typescript/tests-ts/runtime.test.ts+++ b/sdk/typescript/tests-ts/runtime.test.ts@@ -201,6 +201,30 @@ function windowsUserSid() {return sid;}+function setWindowsTestDacl(+ python: Parameters<typeof runNodePython>[0],+ path: string,+ descriptor: string,+) {+ const setup = runNodePython(python, [+ "-c",+ [+ "import ctypes, sys",+ "from ctypes import wintypes",+ "adv = ctypes.WinDLL('advapi32', use_last_error=True)",+ "kernel = ctypes.WinDLL('kernel32', use_last_error=True)",+ "sd = wintypes.LPVOID()",+ "if not adv.ConvertStringSecurityDescriptorToSecurityDescriptorW(sys.argv[2], 1, ctypes.byref(sd), None): raise ctypes.WinError(ctypes.get_last_error())",+ "try:",+ " if not adv.SetFileSecurityW(sys.argv[1], wintypes.DWORD(0x80000004), sd): raise ctypes.WinError(ctypes.get_last_error())",+ "finally: kernel.LocalFree(sd)",+ ].join("\n"),+ win32.toNamespacedPath(path),+ descriptor,+ ]);+ expect(setup.status).toBe(0);+}+function inspectWindowsTestAcl(statements: string[],environment: NodeJS.ProcessEnv,@@ -1881,6 +1905,107 @@ for await (const line of createInterface({ input: process.stdin })) {},);+ test.skipIf(process.platform !== "win32")(+ "explains access denied when the SDK cannot reopen its Windows plugin installation receipt",+ async () => {+ const user = windowsUserSid()!;+ if (user === "S-1-5-18") return; // SYSTEM is the independent file trustee.+ const python = await resolvePluginPython();+ const root = await temporaryDirectory();+ // This fixture needs a local volume with FILE_PERSISTENT_ACLS.+ const volume = runNodePython(python, [+ "-c",+ [+ "import ctypes, sys",+ "from ctypes import wintypes",+ "kernel = ctypes.WinDLL('kernel32', use_last_error=True)",+ "volume = ctypes.create_unicode_buffer(32768)",+ "if not kernel.GetVolumePathNameW(sys.argv[1], volume, len(volume)): raise ctypes.WinError(ctypes.get_last_error())",+ "kind = kernel.GetDriveTypeW(volume.value)",+ "if kind in (0, 1): raise RuntimeError('Could not determine the test volume type')",+ "flags = wintypes.DWORD()",+ "if kind in (2, 3, 6):",+ " if not kernel.GetVolumeInformationW(volume.value, None, 0, None, None, ctypes.byref(flags), None, 0): raise ctypes.WinError(ctypes.get_last_error())",+ "print('supported' if flags.value & 0x8 else 'unsupported')",+ ].join("\n"),+ win32.toNamespacedPath(root),+ ]);+ expect(volume.status, volume.stderr).toBe(0);+ const support = volume.stdout.trim();+ expect(["supported", "unsupported"]).toContain(support);+ if (support === "unsupported") return;+ const selected = await plugin(root);+ const home = join(root, "home");+ const marketplace = await createMarketplace(home, selected);+ const installed = join(root, "installed");+ await cp(selected, installed, { recursive: true });+ await writeFile(+ join(home, "config.toml"),+ '[marketplaces.codex-security-sdk]\nsource_type="local"\nsource=' ++ JSON.stringify(marketplace) ++ '\n[plugins."codex-security@codex-security-sdk"]\nenabled=true\n',+ );+ const deny = "(D;OICI;FR;;;S-1-5-21-0-0-0-424242)";+ const system = "(A;OICI;FA;;;SY)";+ const full = "D:P" + deny + "(A;OICI;FA;;;" + user + ")" + system;+ const receipt = join(marketplace, "installed-plugin.json");+ let installs = 0;+ const options = {+ codexCommand: { command: "unused" },+ environment: {},+ runCodex: async (_command: unknown, args: readonly string[]) => {+ expect(args.slice(0, 2)).toEqual(["plugin", "add"]);+ installs++;+ return JSON.stringify({ installedPath: installed, version: "1.2.3" });+ },+ };+ setWindowsTestDacl(+ python,+ marketplace,+ "D:P" + deny + "(A;;FA;;;" + user + ")" + system,+ );+ try {+ expect(+ (await bootstrapPlugin(home, selected, options)).installedRoot,+ ).toBe(installed);+ const original = await readFile(receipt, "utf8").then(+ () => undefined,+ (error: NodeJS.ErrnoException) => error,+ );+ expect(["EPERM", "EACCES"]).toContain(original?.code ?? "");+ const failure = await bootstrapPlugin(home, selected, options).then(+ () => {+ throw new Error("Expected Windows to reject reopening the receipt");+ },+ (error: NodeJS.ErrnoException) => error,+ );+ expect(failure.code).toBe(original!.code);+ expect(failure.path).toBe(receipt);+ expect(failure.syscall).toBe(original!.syscall);+ expect(failure.message).toContain(original!.message);+ expect(failure.message).toContain("permissions new files inherit");+ expect(installs).toBe(1);+ } finally {+ // Correct this fixture's grant on the directory and its existing receipt.+ setWindowsTestDacl(python, marketplace, full);+ if (existsSync(receipt))+ setWindowsTestDacl(+ python,+ receipt,+ "D:P" + deny + "(A;;FA;;;" + user + ")" + system,+ );+ }+ // A receipt newly created under the corrected parent also reopens normally.+ await rm(receipt);+ await bootstrapPlugin(home, selected, options);+ expect(+ (await bootstrapPlugin(home, selected, options)).installedRoot,+ ).toBe(installed);+ expect(installs).toBe(2);+ },+ 60_000,+ );+test("uses the installed plugin path returned by Codex", async () => {const root = await temporaryDirectory();const selected = await plugin(root);@@ -5247,30 +5372,16 @@ describe("runtime directories and plugin Python boundary", () => {const sddl = `D:P(D;OICI;FR;;;${unmappedSid})(A;OICI;FA;;;${userSid})(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)`;// Set the raw DACL without resolving the synthetic trustee or invoking PowerShell methods.const python = await resolvePluginPython();- const setDacl = (path: string, descriptor: string) => {- const setup = runNodePython(python, [- "-c",- [- "import ctypes, sys",- "from ctypes import wintypes",- "adv = ctypes.WinDLL('advapi32', use_last_error=True)",- "kernel = ctypes.WinDLL('kernel32', use_last_error=True)",- "sd = wintypes.LPVOID()",- "if not adv.ConvertStringSecurityDescriptorToSecurityDescriptorW(sys.argv[2], 1, ctypes.byref(sd), None): raise ctypes.WinError(ctypes.get_last_error())",- "try:",- " if not adv.SetFileSecurityW(sys.argv[1], wintypes.DWORD(0x80000004), sd): raise ctypes.WinError(ctypes.get_last_error())",- "finally: kernel.LocalFree(sd)",- ].join("\n"),- win32.toNamespacedPath(path),- descriptor,- ]);- expect(setup.status).toBe(0);- };- setDacl(home, sddl);++ setWindowsTestDacl(python, home, sddl);await mkdir(child);await writeFile(file, "synthetic credential fixture\n");// A subtree reset would replace this distinct protected file ACL with the home ACL.- setDacl(file, `D:P(D;;FR;;;${unmappedSid})(A;;FA;;;${userSid})`);+ setWindowsTestDacl(+ python,+ file,+ `D:P(D;;FR;;;${unmappedSid})(A;;FA;;;${userSid})`,+ );const readDescriptors = () => {const result = inspectWindowsTestAcl(