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(windows): preserve unrelated credential deny rules on local volumes (#2388)

Michael D'Angeloea747d642607
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index 1c1a65a4d..adf31f6c2 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -494,6 +494,7 @@ const WINDOWS_PRINCIPAL_ALIASES: Readonly<Record<string, string>> = {
HA: "S-1-5-32-578",
AA: "S-1-5-32-579",
};
+const WINDOWS_ACCOUNT_SID = /^S-1-5-21-(?:\d+-){3}\d+$/u;
const WINDOWS_SID = /^S-1-(?:\d+-)*\d+$/u;
const WINDOWS_GUID =
/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iu;
@@ -537,6 +538,7 @@ export function inspectWindowsCredentialAcl(
currentUserSid: string,
options: {
resolvedAliases?: Readonly<Record<string, string>>;
+ currentGroupSids?: ReadonlySet<string>;
scope?: "directory" | "file" | "ancestor";
} = {},
): WindowsCredentialAcl {
@@ -639,7 +641,11 @@ export function inspectWindowsCredentialAcl(
grantsCurrentContainerAccess = true;
}
}
- } else {
+ } else if (
+ principal === currentUserSid ||
+ !WINDOWS_ACCOUNT_SID.test(principal) ||
+ (options.currentGroupSids?.has(principal) ?? true)
+ ) {
deniedPrincipals.add(principal);
}
remaining = rest;
@@ -834,6 +840,7 @@ export async function inspectWindowsCredentialAclSnapshot(
environment?: NodeJS.ProcessEnv;
resolvedAliases?: Readonly<Record<string, string>>;
resolveDescriptorAliases?: (descriptor: string) => Promise<void>;
+ currentGroupSids?: ReadonlySet<string>;
},
): Promise<{
home: WindowsCredentialAcl;
@@ -888,6 +895,7 @@ export async function inspectWindowsCredentialAclSnapshot(
descriptor,
currentUserSid,
{
+ currentGroupSids: options.currentGroupSids,
resolvedAliases: options.resolvedAliases,
scope: "ancestor",
},
@@ -918,6 +926,7 @@ export async function inspectWindowsCredentialAclSnapshot(
if (index === ancestors.length) {
try {
home = inspectWindowsCredentialAcl(descriptor, currentUserSid, {
+ currentGroupSids: options.currentGroupSids,
resolvedAliases: options.resolvedAliases,
});
} catch (error) {
@@ -933,6 +942,7 @@ export async function inspectWindowsCredentialAclSnapshot(
descriptor,
currentUserSid,
{
+ currentGroupSids: options.currentGroupSids,
resolvedAliases: options.resolvedAliases,
scope: "file",
},
@@ -964,6 +974,33 @@ export async function inspectWindowsCredentialAclSnapshot(
throw new Error("Windows credential descendants could not be verified");
}
+/** A file server may add its own groups to the client's token. */
+export async function isLocalWindowsCredentialVolume(
+ path: string,
+ readDriveType: (drive: string) => Promise<string>,
+): Promise<boolean> {
+ const drive = /^\\\\[?.]\\([A-Z]:)\\/iu.exec(
+ win32.toNamespacedPath(path),
+ )?.[1];
+ if (!drive) return false;
+ // Win32_LogicalDisk reports removable, local disk, and RAM disk as 2, 3, 6.
+ return ["2", "3", "6"].includes((await readDriveType(drive)).trim());
+}
+
+/** Keep every token group, including groups used only for deny ACEs. */
+export function parseWindowsTokenGroupSids(output: string): Set<string> {
+ const groups = new Set<string>();
+ for (const row of output.trim().split(/\r?\n/u)) {
+ const sid =
+ /^"(?:[^"]|"")*","(?:[^"]|"")*","(S-1-(?:\d+-)*\d+)","(?:[^"]|"")*"$/u.exec(
+ row,
+ )?.[1];
+ if (!sid) throw new Error("Unable to identify Windows token group SIDs");
+ groups.add(sid);
+ }
+ return groups;
+}
+
async function secureWindowsCredentialHome(path: string): Promise<void> {
const ancestorAclScript = windowsCredentialAncestorAclScript(path);
const treePath = win32.toNamespacedPath(path);
@@ -1093,6 +1130,30 @@ async function secureWindowsCredentialHome(path: string): Promise<void> {
);
resolvedAliases[principal] = requireNumericSid(resolved.stdout.trim());
};
+ const loadLocalGroupSids = async (): Promise<Set<string> | null> => {
+ const local = await isLocalWindowsCredentialVolume(
+ treePath,
+ async (drive) => {
+ const volume = await execFile(
+ powershell,
+ [
+ ...powershellArgs,
+ `$ErrorActionPreference = 'Stop'; (CimCmdlets\\Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID = '${drive}'").DriveType`,
+ ],
+ processOptions,
+ );
+ return volume.stdout;
+ },
+ );
+ if (!local) return null;
+ const groups = await execFile(
+ join(systemDirectory, "whoami.exe"),
+ ["/groups", "/fo", "csv", "/nh"],
+ processOptions,
+ );
+ return parseWindowsTokenGroupSids(groups.stdout);
+ };
+ let currentGroupSids: Set<string> | null | undefined;
const resolveDescriptorAliases = async (
descriptor: string,
): Promise<void> => {
@@ -1102,8 +1163,20 @@ async function secureWindowsCredentialHome(path: string): Promise<void> {
let remaining = header[3]!;
while (remaining.startsWith("(")) {
const { rule, rest } = windowsSecurityDescriptorRule(remaining);
- const principal = rule.split(";")[5];
- if (principal !== undefined) await resolvePrincipal(principal);
+ const fields = rule.split(";");
+ const principal = fields[5];
+ if (principal !== undefined) {
+ await resolvePrincipal(principal);
+ const trustee = resolvedAliases[principal] ?? principal;
+ if (
+ currentGroupSids === undefined &&
+ ["D", "OD", "XD"].includes(fields[0]!) &&
+ trustee !== sid &&
+ WINDOWS_ACCOUNT_SID.test(trustee)
+ ) {
+ currentGroupSids = await loadLocalGroupSids();
+ }
+ }
remaining = rest;
}
};
@@ -1113,6 +1186,9 @@ async function secureWindowsCredentialHome(path: string): Promise<void> {
command: powershell,
args: [...powershellArgs, script],
environment: processOptions.env,
+ get currentGroupSids() {
+ return currentGroupSids ?? undefined;
+ },
resolvedAliases,
resolveDescriptorAliases,
});
diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.ts
index 35b359279..4bbbd0b85 100644
--- a/sdk/typescript/tests-ts/runtime.test.ts
+++ b/sdk/typescript/tests-ts/runtime.test.ts
@@ -81,6 +81,8 @@ import {
executablePathForSpawn,
inspectWindowsCredentialAcl,
inspectWindowsCredentialAclSnapshot,
+ isLocalWindowsCredentialVolume,
+ parseWindowsTokenGroupSids,
isPythonPathCandidate,
planOutputArchive,
prepareCodexSecurityCredentialHome,
@@ -4564,6 +4566,61 @@ describe("runtime directories and plugin Python boundary", () => {
});
});
+ test("uses local group membership only on local Windows volumes", async () => {
+ const readType = mock(async () => "4");
+ for (const path of [
+ String.raw`\\server\share\home`,
+ String.raw`\\?\UNC\server\share\home`,
+ ]) {
+ expect(await isLocalWindowsCredentialVolume(path, readType)).toBe(false);
+ }
+ expect(readType).not.toHaveBeenCalled();
+ for (const path of [String.raw`X:\home`, String.raw`\\?\X:\home`]) {
+ expect(await isLocalWindowsCredentialVolume(path, readType)).toBe(false);
+ expect(readType).toHaveBeenLastCalledWith("X:");
+ expect(
+ await isLocalWindowsCredentialVolume(path, async () => "3\r\n"),
+ ).toBe(true);
+ expect(await isLocalWindowsCredentialVolume(path, async () => "")).toBe(
+ false,
+ );
+ }
+ });
+
+ test("preserves account denies only after excluding current token membership", () => {
+ const user = "S-1-5-21-111-222-333-1001";
+ const group = "S-1-5-21-111-222-333-512";
+ const other = "S-1-5-21-0-0-0-424242";
+ const currentGroupSids = parseWindowsTokenGroupSids(
+ `"Domain, Group","Group","${group}","Group used for deny only"\r\n`,
+ );
+ const descriptor = `O:${user}D:P(D;OICI;FR;;;${other})(A;OICI;FA;;;${user})`;
+ expect(
+ inspectWindowsCredentialAcl(descriptor, user).grantsCurrentUserAccess,
+ ).toBe(false);
+ expect(
+ inspectWindowsCredentialAcl(descriptor, user, { currentGroupSids }),
+ ).toMatchObject({
+ grantsCurrentUserAccess: true,
+ deniedPrincipals: [],
+ });
+ for (const principal of [user, "DA", "WD", "OW"]) {
+ expect(
+ inspectWindowsCredentialAcl(
+ descriptor.replace(other, principal),
+ user,
+ {
+ currentGroupSids,
+ resolvedAliases: { DA: group },
+ },
+ ).grantsCurrentUserAccess,
+ ).toBe(false);
+ }
+ for (const invalid of ["", `"a","b","${group}","c"\r\ninvalid`]) {
+ expect(() => parseWindowsTokenGroupSids(invalid)).toThrow();
+ }
+ });
+
test("requires effective, inheritable Windows credential access", () => {
const user = "S-1-5-21-111-222-333-1001";
for (const [flags, rights] of [
@@ -5161,6 +5218,119 @@ describe("runtime directories and plugin Python boundary", () => {
},
);
+ test.skipIf(process.platform !== "win32")(
+ "preserves an unmapped Windows deny SID on a private credential home and its children",
+ async () => {
+ const root = await temporaryDirectory();
+ const state = join(root, "state");
+ const home = join(state, "codex-home");
+ await mkdir(home, { recursive: true });
+ const child = join(home, "sessions");
+ const file = join(child, "fixture.txt");
+ const unmappedSid = "S-1-5-21-0-0-0-424242";
+ const environment = {
+ ...process.env,
+ CODEX_SECURITY_TEST_ACL_PATH: win32.toNamespacedPath(home),
+ CODEX_SECURITY_TEST_CHILD: win32.toNamespacedPath(child),
+ CODEX_SECURITY_TEST_FILE: win32.toNamespacedPath(file),
+ };
+ const identity = windowsTestCommand("whoami.exe", [
+ "/user",
+ "/fo",
+ "csv",
+ "/nh",
+ ]);
+ expect(identity.status).toBe(0);
+ const userSid = identity.stdout.match(/"(S-1-[0-9-]+)"\s*$/iu)?.[1];
+ if (!userSid)
+ throw new Error("Could not identify the Windows fixture user SID");
+ 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);
+ 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})`);
+
+ const readDescriptors = () => {
+ const result = inspectWindowsTestAcl(
+ [
+ "$ErrorActionPreference = 'Stop'",
+ '$env:PSModulePath = "$PSHOME\\Modules"',
+ "(Microsoft.PowerShell.Security\\Get-Acl -LiteralPath $env:CODEX_SECURITY_TEST_ACL_PATH).Sddl",
+ "(Microsoft.PowerShell.Security\\Get-Acl -LiteralPath $env:CODEX_SECURITY_TEST_CHILD).Sddl",
+ "(Microsoft.PowerShell.Security\\Get-Acl -LiteralPath $env:CODEX_SECURITY_TEST_FILE).Sddl",
+ ],
+ environment,
+ );
+ if (result.status !== 0)
+ throw new Error(
+ "Fixture ACL read failed: " +
+ (result.stderr.match(
+ /FullyQualifiedErrorId\s*:\s*([\w.,-]+)/u,
+ )?.[1] ?? "unknown"),
+ );
+ return result.stdout.trim().split(/\r?\n/u);
+ };
+ const before = readDescriptors();
+ expect(before.length).toBe(3);
+ expect(before[0]!.includes("D:P") && before[2]!.includes("D:P")).toBe(
+ true,
+ );
+ expect(before[2] !== before[1]).toBe(true);
+ expect(
+ before.every((descriptor) => descriptor.includes(`;;;${unmappedSid})`)),
+ ).toBe(true);
+ const expectedHome = await realpath(home);
+ for (let attempt = 0; attempt < 2; attempt++) {
+ const prepared = await prepareCodexSecurityCredentialHome({
+ CODEX_SECURITY_STATE_DIR: state,
+ }).catch((error: unknown) => {
+ const cause = error instanceof Error ? error.cause : undefined;
+ if (
+ cause instanceof Error &&
+ "cmd" in cause &&
+ typeof cause.cmd === "string" &&
+ /\bicacls\.exe\b/iu.test(cause.cmd) &&
+ cause.cmd.includes(" /remove:d ") &&
+ cause.cmd.includes(`*${unmappedSid}`)
+ ) {
+ throw new Error("icacls attempted to remove the synthetic DENY");
+ }
+ throw error;
+ });
+ expect(prepared === expectedHome).toBe(true);
+ const after = readDescriptors();
+ expect(
+ after.map((descriptor, index) => descriptor === before[index]),
+ ).toEqual([true, true, true]);
+ }
+ expect(await readFile(file, "utf8")).toBe(
+ "synthetic credential fixture\n",
+ );
+ },
+ );
+
test.skipIf(process.platform !== "win32")(
"removes unsafe inherited Windows credential-home permissions",
async () => {