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: canonicalize Windows history and publication paths (#497)
faizan-oai20bfe3ef79c3
diff --git a/sdk/typescript/_bundled_plugin/scripts/workbench_scan_history.py b/sdk/typescript/_bundled_plugin/scripts/workbench_scan_history.pyindex 323fdbe78..abf18b626 100644--- a/sdk/typescript/_bundled_plugin/scripts/workbench_scan_history.py+++ b/sdk/typescript/_bundled_plugin/scripts/workbench_scan_history.py@@ -18,6 +18,10 @@ from workbench_scan_usage import stored_scan_cost_fieldsfrom workbench_target import git_output+def _windows_path_key(value: str) -> str:+ return os.path.normcase(os.path.realpath(value))++def _same_repository(before: sqlite3.Row,after: sqlite3.Row,@@ -78,6 +82,8 @@ def _repository_origin(target: Path) -> tuple[str, str] | None:def list_scans(connection: sqlite3.Connection, args: argparse.Namespace | None = None) -> dict[str, Any]:+ if os.name == "nt":+ connection.create_function("codex_security_path_key", 1, _windows_path_key)clauses: list[str] = []values: list[Any] = []if args is not None and args.repository:@@ -110,7 +116,15 @@ def list_scans(if args is not None and args.scan_root:scan_root = str(Path(args.scan_root).expanduser().resolve())prefix = scan_root.rstrip(os.sep) + os.sep- clauses.append("(scans.scan_dir = ? OR substr(scans.scan_dir, 1, ?) = ?)")+ if os.name == "nt":+ scan_root = _windows_path_key(scan_root)+ prefix = scan_root.rstrip(os.sep) + os.sep+ clauses.append(+ "(codex_security_path_key(scans.scan_dir) = ? "+ "OR substr(codex_security_path_key(scans.scan_dir), 1, ?) = ?)"+ )+ else:+ clauses.append("(scans.scan_dir = ? OR substr(scans.scan_dir, 1, ?) = ?)")values.extend((scan_root, len(prefix), prefix))if args is not None and args.target_id:clauses.append("scans.target_id = ?")diff --git a/sdk/typescript/src/contract.ts b/sdk/typescript/src/contract.tsindex c8c4b8548..655b2ba64 100644--- a/sdk/typescript/src/contract.ts+++ b/sdk/typescript/src/contract.ts@@ -64,15 +64,24 @@ export interface LoadedContract {coverage: CoverageDocument;}+type LoadContractOptions = {+ pluginRoot: string;+ expectation?: ScanExpectation;+ workbenchValidated?: boolean;+ signal?: AbortSignal;+};+export async function loadContract(scanDirectory: string,- options: {- pluginRoot: string;- expectation?: ScanExpectation;- workbenchValidated?: boolean;- signal?: AbortSignal;- },+ options: LoadContractOptions,): Promise<LoadedContract> {+ return (await loadContractWithScanDirectory(scanDirectory, options)).contract;+}++export async function loadContractWithScanDirectory(+ scanDirectory: string,+ options: LoadContractOptions,+): Promise<{ contract: LoadedContract; scanDirectory: string }> {const scanRoot = await requireScanRoot(scanDirectory, options.signal);const scanDir = scanRoot.path;const documentDigests = new Map<string, string>();@@ -184,7 +193,17 @@ export async function loadContract();}await verifyScanRoot(scanRoot, options.signal);- return { manifest, findings, coverage };+ return {+ contract: { manifest, findings, coverage },+ scanDirectory: scanRoot.path,+ };+}++export async function requireCanonicalScanDirectory(+ scanDirectory: string,+ signal?: AbortSignal,+): Promise<string> {+ return (await requireScanRoot(scanDirectory, signal)).path;}type JsonRecord = Record<string, unknown>;diff --git a/sdk/typescript/src/publication.ts b/sdk/typescript/src/publication.tsindex f4255a0c2..78f758818 100644--- a/sdk/typescript/src/publication.ts+++ b/sdk/typescript/src/publication.ts@@ -1,5 +1,7 @@-import { resolve } from "node:path";-import { loadContract, type LoadedContract } from "./contract.js";+import {+ loadContractWithScanDirectory,+ type LoadedContract,+} from "./contract.js";import type {Finding,FindingCodeEvidence,@@ -50,16 +52,17 @@ export async function prepareScanPublication(scanDirectory: string,options: PrepareScanPublicationOptions,): Promise<PreparedScanPublication> {- const contract = await loadContract(scanDirectory, {- pluginRoot: await bundledPluginRoot(),- });+ const { contract, scanDirectory: canonicalScanDirectory } =+ await loadContractWithScanDirectory(scanDirectory, {+ pluginRoot: await bundledPluginRoot(),+ });const uploadedAt = options.uploadedAt ?? new Date().toISOString();const scanId = contract.manifest.scan.id;return {scanId,uploadId: scanId,- scanDirectory: resolve(scanDirectory),+ scanDirectory: canonicalScanDirectory,destination: {type: options.destination,teamId: options.teamId,diff --git a/sdk/typescript/tests-ts/publication.test.ts b/sdk/typescript/tests-ts/publication.test.tsindex 15d61526c..c676dff35 100644--- a/sdk/typescript/tests-ts/publication.test.ts+++ b/sdk/typescript/tests-ts/publication.test.ts@@ -1,5 +1,15 @@import { createHash } from "node:crypto";-import { chmod, cp, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";+import {+ chmod,+ cp,+ mkdir,+ mkdtemp,+ readFile,+ realpath,+ rm,+ symlink,+ writeFile,+} from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import { afterEach, describe, expect, test } from "bun:test";@@ -68,7 +78,7 @@ describe("scan publication preparation", () => {expect(publication).toMatchObject({scanId: "scan_example_001",uploadId: "scan_example_001",- scanDirectory,+ scanDirectory: await realpath(scanDirectory),destination: {type: "linear",teamId: "team_example",@@ -109,6 +119,31 @@ describe("scan publication preparation", () => {expect(issue.description).not.toContain("/blob/deadbeef/");});+ test("uses the canonical scan directory beneath an aliased parent", async () => {+ const root = await mkdtemp(+ join(tmpdir(), "codex-security-publication-alias-"),+ );+ temporaryDirectories.push(root);+ const parent = join(root, "actual-parent");+ const alias = join(root, "aliased-parent");+ const scanDirectory = join(parent, "scan");+ await mkdir(parent);+ await cp(EXAMPLE, scanDirectory, { recursive: true });+ if (process.platform !== "win32") await chmod(scanDirectory, 0o700);+ await symlink(+ parent,+ alias,+ process.platform === "win32" ? "junction" : "dir",+ );++ const publication = await prepareScanPublication(+ join(alias, "scan"),+ DESTINATION,+ );++ expect(publication.scanDirectory).toBe(await realpath(scanDirectory));+ });+test.each([["repository", "standard"],["scoped_path", "scoped_path"],diff --git a/sdk/typescript/tests-ts/workbench-scan-root-alias.test.ts b/sdk/typescript/tests-ts/workbench-scan-root-alias.test.tsnew file mode 100644index 000000000..a610737eb--- /dev/null+++ b/sdk/typescript/tests-ts/workbench-scan-root-alias.test.ts@@ -0,0 +1,57 @@+import { spawnSync } from "node:child_process";+import { mkdirSync, mkdtempSync, realpathSync, rmSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { expect, test } from "bun:test";+import { PLUGIN_ROOT } from "./plugin-root.js";++test.skipIf(process.platform !== "win32")(+ "matches scan-root filters across Windows path aliases",+ () => {+ const python =+ process.env["PYTHON"] ??+ Bun.which("python3") ??+ Bun.which("python") ??+ Bun.which("py");+ expect(python).not.toBeNull();+ if (python === null) throw new Error("A Python interpreter is required.");++ const root = realpathSync(+ mkdtempSync(join(tmpdir(), "codex-security-scan-root-case-")),+ );+ const scanRoot = join(root, "Scan History");+ mkdirSync(scanRoot);+ try {+ const probe = [+ "import argparse, json, sqlite3, sys",+ "sys.path.insert(0, sys.argv[1])",+ "import workbench_scan_history as history",+ "connection = sqlite3.connect(':memory:')",+ "connection.row_factory = sqlite3.Row",+ "connection.executescript('''",+ "CREATE TABLE scans (id TEXT, target_path TEXT, target_id TEXT, status TEXT, started_at TEXT, completed_at TEXT, continuation_thread_id TEXT, cost_json TEXT, handoff_status TEXT, mode TEXT, model TEXT, parent_scan_id TEXT, phase TEXT, recipe_json TEXT, reasoning_effort TEXT, scan_dir TEXT, scope TEXT, target_revision TEXT, target_summary TEXT, updated_at TEXT, canceled_at TEXT, completion_warnings_json TEXT);",+ "CREATE TABLE scan_progress (scan_id TEXT, reportable_findings_count INTEGER, scope_file_count INTEGER, review_items_completed INTEGER, review_items_total INTEGER, updated_at TEXT);",+ "CREATE TABLE finding_occurrences (scan_id TEXT);",+ "''')",+ "connection.execute('INSERT INTO scans VALUES (?, ?, NULL, ?, ?, NULL, NULL, NULL, NULL, ?, NULL, NULL, ?, NULL, NULL, ?, ?, NULL, NULL, ?, NULL, ?)', ('scan', 'repository', 'complete', '1', 'standard_repository', 'complete', sys.argv[2] + '/scan', '.', '1', '[]'))",+ "connection.execute('INSERT INTO scans VALUES (?, ?, NULL, ?, ?, NULL, NULL, NULL, NULL, ?, NULL, NULL, ?, NULL, NULL, ?, ?, NULL, NULL, ?, NULL, ?)', ('sibling', 'repository', 'complete', '1', 'standard_repository', 'complete', sys.argv[2] + ' Other/scan', '.', '1', '[]'))",+ "connection.execute('INSERT INTO scan_progress VALUES (?, 0, 1, 1, 1, ?)', ('scan', '1'))",+ "args = argparse.Namespace(repository=None, scan_root=sys.argv[2].upper(), target_id=None, mode=None, status=None, query=None, limit=None, offset=0)",+ "print(json.dumps(history.list_scans(connection, args)))",+ ].join("\n");+ const result = spawnSync(+ python,+ ["-I", "-B", "-c", probe, join(PLUGIN_ROOT, "scripts"), scanRoot],+ { encoding: "utf8", timeout: 10_000 },+ );++ expect(result.status, result.stderr).toBe(0);+ expect(result.stderr).toBe("");+ expect(JSON.parse(result.stdout)).toMatchObject({+ scans: [{ scanId: "scan" }],+ });+ } finally {+ rmSync(root, { recursive: true, force: true });+ }+ },+);