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(plugin): create private Windows scan output directories (#2390)
Michael D'Angelo5fa9b676a9d1
diff --git a/plugins/codex-security/scripts/windows_scan_local_files.py b/plugins/codex-security/scripts/windows_scan_local_files.pyindex a23cf8db0..864e0a8fa 100644--- a/plugins/codex-security/scripts/windows_scan_local_files.py+++ b/plugins/codex-security/scripts/windows_scan_local_files.py@@ -78,6 +78,7 @@ _ERROR_FILE_EXISTS = 80_ERROR_INSUFFICIENT_BUFFER = 122_ERROR_ALREADY_EXISTS = 183_ERROR_DIRECTORY = 267+_OWNER_SECURITY_INFORMATION = 0x00000001_DACL_SECURITY_INFORMATION = 0x00000004_MISSING_ERRORS = {_ERROR_FILE_NOT_FOUND, _ERROR_PATH_NOT_FOUND}_COLLISION_ERRORS = {_ERROR_FILE_EXISTS, _ERROR_ALREADY_EXISTS}@@ -496,19 +497,22 @@ def _create_directory(path: Path) -> None:_raise_last_error("CreateDirectoryW", path)-def create_directory_with_same_dacl(path: Path, source: Path) -> None:- """Create an archive replacement with the original directory's DACL."""- _require_windows()+def _directory_security_descriptor(source: Path, information: int) -> ctypes.Array:source_path = _extended_path(source)size = wintypes.DWORD()- _GetFileSecurityW(source_path, _DACL_SECURITY_INFORMATION, None, 0, ctypes.byref(size))+ _GetFileSecurityW(source_path, information, None, 0, ctypes.byref(size))if ctypes.get_last_error() != _ERROR_INSUFFICIENT_BUFFER:_raise_last_error("GetFileSecurityW", source)descriptor = ctypes.create_string_buffer(size.value)- if not _GetFileSecurityW(- source_path, _DACL_SECURITY_INFORMATION, descriptor, size.value, ctypes.byref(size)- ):+ if not _GetFileSecurityW(source_path, information, descriptor, size.value, ctypes.byref(size)):_raise_last_error("GetFileSecurityW", source)+ return descriptor+++def create_directory_with_same_dacl(path: Path, source: Path) -> None:+ """Create an archive replacement with the original directory's DACL."""+ _require_windows()+ descriptor = _directory_security_descriptor(source, _DACL_SECURITY_INFORMATION)attributes = _SecurityAttributes(ctypes.sizeof(_SecurityAttributes), ctypes.cast(descriptor, wintypes.LPVOID), False)@@ -572,6 +576,37 @@ def _locked_parent(handle.close()+def _generated_directory_policy(descriptor: object) -> tuple[str, bool, frozenset[str]]:+ # Let Windows normalize SID aliases, access masks and inheritance flags in+ # both descriptors. ACE order is immaterial for our three allow grants.+ sddl = wintypes.LPWSTR()+ if not _advapi32.ConvertSecurityDescriptorToStringSecurityDescriptorW(+ descriptor,+ 1,+ _OWNER_SECURITY_INFORMATION | _DACL_SECURITY_INFORMATION,+ ctypes.byref(sddl),+ None,+ ):+ _raise_last_error("ConvertSecurityDescriptorToStringSecurityDescriptorW")+ try:+ owner, _, dacl = sddl.value.partition("D:")+ flags, _, entries = dacl.partition("(")+ return owner, "P" in flags, frozenset(entries.removesuffix(")").split(")("))+ finally:+ _kernel32.LocalFree(sddl)+++def verify_private_scan_root(scan_dir: Path, expected_descriptor: object) -> Path:+ """Verify the new directory's owner and protected grants while the path is pinned."""+ with _locked_parent(scan_dir, ".security", create=False) as (root_path, _):+ actual = _directory_security_descriptor(+ root_path, _OWNER_SECURITY_INFORMATION | _DACL_SECURITY_INFORMATION+ )+ if _generated_directory_policy(actual) != _generated_directory_policy(expected_descriptor):+ raise _invalid_path(scan_dir, "new scan directory lacks its private owner or DACL")+ return root_path++def scan_root_identity(scan_dir: Path) -> tuple[Path, tuple[int, int]]:"""Return a canonical scan root and its identity while holding it fixed."""diff --git a/plugins/codex-security/scripts/workbench/storage.py b/plugins/codex-security/scripts/workbench/storage.pyindex 119e5b163..4d9150fd0 100644--- a/plugins/codex-security/scripts/workbench/storage.py+++ b/plugins/codex-security/scripts/workbench/storage.py@@ -4,6 +4,7 @@ from __future__ import annotationsimport osimport sqlite3+import tempfilefrom pathlib import Pathfrom typing import Any@@ -65,12 +66,105 @@ def saved_deep_workers(connection: sqlite3.Connection, scan: sqlite3.Row) -> lis]+def _create_private_windows_directory(path: Path, *, exist_ok: bool) -> Path:+ """Create one Windows directory with a protected inheritable DACL."""+ import ctypes+ import ctypes.wintypes++ from windows_scan_local_files import verify_private_scan_root++ class SecurityAttributes(ctypes.Structure):+ _fields_ = [+ ("length", ctypes.wintypes.DWORD),+ ("descriptor", ctypes.wintypes.LPVOID),+ ("inherit", ctypes.wintypes.BOOL),+ ]++ # Older supported Python releases ignore mkdir's 0700 mode on Windows.+ # Apply the same owner/SYSTEM/Administrators policy at creation so a+ # writable parent never becomes an inheritance window.+ kernel = ctypes.WinDLL("kernel32", use_last_error=True)+ volume = ctypes.create_unicode_buffer(32768)+ flags = ctypes.wintypes.DWORD()+ if not kernel.GetVolumePathNameW(str(path.parent.resolve()), volume, len(volume)):+ raise ctypes.WinError(ctypes.get_last_error())+ if not kernel.GetVolumeInformationW(volume, None, 0, None, None, ctypes.byref(flags), None, 0):+ raise ctypes.WinError(ctypes.get_last_error())+ if not flags.value & 0x8: # FILE_PERSISTENT_ACLS+ raise ValueError("Select a directory on a Windows volume that supports ACLs.")++ security = ctypes.WinDLL("advapi32", use_last_error=True)+ # GetCurrentProcessToken() is a Windows pseudo-handle and does not need closing.+ token = ctypes.wintypes.HANDLE(-4)+ token_user = 1 # TOKEN_INFORMATION_CLASS.TokenUser+ size = ctypes.wintypes.DWORD()+ security.GetTokenInformation(token, token_user, None, 0, ctypes.byref(size))+ if ctypes.get_last_error() != 122: # ERROR_INSUFFICIENT_BUFFER+ raise ctypes.WinError(ctypes.get_last_error())+ user = ctypes.create_string_buffer(size.value)+ if not security.GetTokenInformation(token, token_user, user, size, ctypes.byref(size)):+ raise ctypes.WinError(ctypes.get_last_error())+ # TOKEN_USER starts with SID_AND_ATTRIBUTES.Sid.+ sid = ctypes.wintypes.LPWSTR()+ if not security.ConvertSidToStringSidW(+ ctypes.wintypes.LPVOID.from_buffer(user), ctypes.byref(sid)+ ):+ raise ctypes.WinError(ctypes.get_last_error())+ try:+ owner = "" if exist_ok else f"O:{sid.value}"+ dacl = f"{owner}D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;{sid.value})"+ finally:+ kernel.LocalFree(sid)++ descriptor = ctypes.wintypes.LPVOID()+ if not security.ConvertStringSecurityDescriptorToSecurityDescriptorW(+ dacl, 1, ctypes.byref(descriptor), None+ ):+ raise ctypes.WinError(ctypes.get_last_error())+ try:+ attributes = SecurityAttributes(ctypes.sizeof(SecurityAttributes), descriptor, False)+ if kernel.CreateDirectoryW(str(path), ctypes.byref(attributes)):+ if exist_ok:+ return path+ return verify_private_scan_root(path, descriptor)+ error = ctypes.get_last_error()+ if exist_ok and error == 183 and path.is_dir(): # ERROR_ALREADY_EXISTS+ return path+ raise ctypes.WinError(error)+ finally:+ kernel.LocalFree(descriptor)++def create_private_directory(path: Path) -> None:"""Create missing directories privately without changing existing permissions."""+ if os.name == "nt" and path.is_dir():+ returntry:- path.mkdir(mode=0o700, exist_ok=True)+ if os.name == "nt":+ _create_private_windows_directory(path, exist_ok=True)+ else:+ path.mkdir(mode=0o700, exist_ok=True)except FileNotFoundError:if path.parent == path:raisecreate_private_directory(path.parent)- path.mkdir(mode=0o700, exist_ok=True)+ if os.name == "nt":+ _create_private_windows_directory(path, exist_ok=True)+ else:+ path.mkdir(mode=0o700, exist_ok=True)+++def create_private_temporary_directory(parent: Path, *, prefix: str) -> Path:+ """Create a unique private child without inheriting a permissive Windows DACL."""+ if os.name != "nt":+ return Path(tempfile.mkdtemp(prefix=prefix, dir=parent)).resolve()++ import secrets++ for _ in range(100):+ path = parent / f"{prefix}{secrets.token_hex(4)}"+ try:+ return _create_private_windows_directory(path, exist_ok=False)+ except FileExistsError:+ continue+ raise FileExistsError(f"Could not create a unique directory beneath {parent}.")diff --git a/plugins/codex-security/scripts/workbench_scan_start.py b/plugins/codex-security/scripts/workbench_scan_start.pyindex 8a93b69cf..89ca041b6 100644--- a/plugins/codex-security/scripts/workbench_scan_start.py+++ b/plugins/codex-security/scripts/workbench_scan_start.py@@ -19,7 +19,7 @@ from typing import Anysys.path.insert(0, str(Path(__file__).resolve().parent))from filesystem_identity import serialize_filesystem_identityfrom finalize_scan_contract import write_scan_local_bytes-from workbench.storage import state_dir+from workbench.storage import create_private_temporary_directory, state_dirfrom workbench_feedback import get_scan_feedbackfrom workbench_target import (directory_content_digest,@@ -300,12 +300,10 @@ def insert_running_scan(# Missing version attribution must not prevent scan registration.plugin_version = Noneif scan_dir is None:- scan_dir = Path(- tempfile.mkdtemp(- prefix=f"{safe_segment(revision)}_{compact_timestamp()}_",- dir=target_root,- )- ).resolve()+ scan_dir = create_private_temporary_directory(+ target_root,+ prefix=f"{safe_segment(revision)}_{compact_timestamp()}_",+ )connection.execute("""INSERT INTO scans (diff --git a/plugins/codex-security/tests/test_windows_workbench_output_permissions.py b/plugins/codex-security/tests/test_windows_workbench_output_permissions.pynew file mode 100644index 000000000..5aa0659f8--- /dev/null+++ b/plugins/codex-security/tests/test_windows_workbench_output_permissions.py@@ -0,0 +1,65 @@+"""Exercise native Windows direct-plugin output creation races."""++from __future__ import annotations++import os+import subprocess+import sys+from pathlib import Path++import pytest++SCRIPTS = Path(__file__).resolve().parents[1] / "scripts"+sys.path.insert(0, str(SCRIPTS))++import windows_scan_local_files # noqa: E402+from workbench import storage # noqa: E402++pytestmark = [+ pytest.mark.native_windows,+ pytest.mark.skipif(os.name != "nt", reason="requires native Windows ACLs and junctions"),+]++[email protected]("junction", [False, True], ids=["ordinary-directory", "junction"])+def test_private_temporary_directory_rejects_post_creation_substitution(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, junction: bool+) -> None:+ parent = tmp_path / "shared"+ parent.mkdir()+ subprocess.run(+ ["icacls.exe", str(parent), "/grant", "*S-1-1-0:(OI)(CI)R"],+ check=True,+ capture_output=True,+ )+ external = tmp_path / "external"+ external.mkdir()+ substituted: list[Path] = []+ verify = windows_scan_local_files.verify_private_scan_root++ def replace_before_verification(path: Path, descriptor: object) -> Path:+ path.rmdir()+ if junction:+ subprocess.run(+ ["cmd", "/c", "mklink", "/J", str(path), str(external)],+ check=True,+ capture_output=True,+ text=True,+ )+ else:+ path.mkdir() # An ordinary replacement inherits its parent's DACL.+ substituted.append(path)+ return verify(path, descriptor)++ monkeypatch.setattr(+ windows_scan_local_files, "verify_private_scan_root", replace_before_verification+ )+ try:+ reason = "canonical and non-reparse" if junction else "private owner or DACL"+ with pytest.raises(OSError, match=reason):+ storage.create_private_temporary_directory(parent, prefix="scan_")+ finally:+ for path in substituted:+ path.rmdir()++ assert list(external.iterdir()) == []diff --git a/sdk/typescript/tests-ts/windows-scan-output.test.ts b/sdk/typescript/tests-ts/windows-scan-output.test.tsindex 036ce6e5f..fa42aa94c 100644--- a/sdk/typescript/tests-ts/windows-scan-output.test.ts+++ b/sdk/typescript/tests-ts/windows-scan-output.test.ts@@ -8,7 +8,7 @@ import {symlink,writeFile,} from "node:fs/promises";-import { basename, join } from "node:path";+import { basename, dirname, join } from "node:path";import { promisify } from "node:util";import { afterEach, expect, test } from "bun:test";import { loadContract } from "../src/contract.js";@@ -108,6 +108,47 @@ async function createMultiscanCampaign(output: string): Promise<void> {expect(result).toMatchObject({ completed: 1, failed: 0 });}+async function createDirectPluginOutput(scanRoot: string): Promise<{+ scanDir: string;+ targetRoot: string;+ targetRootAcl: string;+ stateDir: string;+}> {+ const { source } = await repositoryFixture("direct-plugin-source");+ const stateDir = join(await temporaryDirectory(), "state");+ const targetRoot = join(scanRoot, basename(source.path));+ await mkdir(targetRoot);+ const targetRootAcl = await descriptor(targetRoot);+ const result = await runtime.runWorkbench(+ {+ python: await runtime.resolvePluginPython(),+ pluginRoot: PLUGIN_ROOT,+ environment: {+ PATH: process.env["PATH"],+ SystemRoot: process.env["SystemRoot"],+ CODEX_SECURITY_STATE_DIR: stateDir,+ },+ },+ [+ "start-headless-standard-scan",+ "--thread-id",+ "windows-acl-test-thread",+ "--target-path",+ source.path,+ "--scope",+ ".",+ "--scan-root",+ scanRoot,+ ],+ );+ const scan = result["scan"] as { scanDir?: unknown } | undefined;+ const scanDir = scan?.scanDir;+ expect(typeof scanDir).toBe("string");+ const resolvedScanDir = await realpath(scanDir as string);+ expect(dirname(resolvedScanDir)).toBe(await realpath(targetRoot));+ return { scanDir: resolvedScanDir, targetRoot, targetRootAcl, stateDir };+}+async function archiveSdkOutput(output: string, root: string): Promise<string> {let archive: string | undefined;await runtime.prepareOutputDir(@@ -177,6 +218,13 @@ windowsTest();const campaign = join(root, "campaign");await createMultiscanCampaign(campaign);+ const direct = await createDirectPluginOutput(root);+ const home = await runtime.prepareCodexSecurityCredentialHome({+ CODEX_SECURITY_STATE_DIR: direct.stateDir,+ });+ expect(home).toBe(await realpath(join(direct.stateDir, "codex-home")));+ expect(direct.targetRootAcl).toContain(";;;WD)");+ expect(await descriptor(direct.targetRoot)).toBe(direct.targetRootAcl);for (const output of [join(root, "new parent"),explicit,@@ -184,6 +232,7 @@ windowsTest(campaign,join(campaign, "artifacts"),join(campaign, "checkouts"),+ direct.scanDir,]) {const acl = await descriptor(output);expect(acl).toContain("D:P");@@ -397,7 +446,9 @@ test.skipIf(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");+ const direct = await createDirectPluginOutput(writableParent);+ expect(await descriptor(direct.targetRoot)).toBe(direct.targetRootAcl);+ const shared = join(direct.targetRoot, "shared.txt");await writeFile(shared, "synthetic shared data");const explicit = await runtime.prepareOutputDir(join(root, "explicit"),@@ -410,14 +461,18 @@ test.skipIf();const campaign = join(writableParent, "campaign");await createMultiscanCampaign(campaign);- for (const output of [explicit, generated, campaign]) {+ for (const output of [explicit, generated, campaign, direct.scanDir]) {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 privateProbes = [explicit, generated, archive, campaign].map((path) =>- join(path, "probe.txt"),- );+ const privateProbes = [+ explicit,+ generated,+ archive,+ campaign,+ direct.scanDir,+ ].map((path) => join(path, "probe.txt"));const script = await readFile(join(import.meta.dir, "support", "windows-other-user-access.ps1"),"utf8",