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: remove remediation patch size limit (#329)
mldangelo-oai959f125ffefd
diff --git a/sdk/typescript/_bundled_plugin/scripts/workbench_constants.py b/sdk/typescript/_bundled_plugin/scripts/workbench_constants.pyindex 13fd5a3cd..2019115f8 100644--- a/sdk/typescript/_bundled_plugin/scripts/workbench_constants.py+++ b/sdk/typescript/_bundled_plugin/scripts/workbench_constants.py@@ -39,7 +39,6 @@ SQLITE_RETRY_ATTEMPTS = 5CLAIM_LEASE_SECONDS = 120DELIVERED_ACTION_LEASE_SECONDS = 900PATCH_PREVIEW_BYTES = 16_000-PATCH_ARTIFACT_MAX_BYTES = 2 * 1024 * 1024FINDINGS_RESULT_LIMIT = 20FINDINGS_PAGE_MAX = 20FINDING_DETAILS_PREVIEW_BYTES = 16_000diff --git a/sdk/typescript/_bundled_plugin/scripts/workbench_db.py b/sdk/typescript/_bundled_plugin/scripts/workbench_db.pyindex d04684be3..b7c7395f6 100644--- a/sdk/typescript/_bundled_plugin/scripts/workbench_db.py+++ b/sdk/typescript/_bundled_plugin/scripts/workbench_db.py@@ -74,7 +74,6 @@ from workbench_constants import (FINDING_TITLE_BYTES,FINDINGS_PAGE_MAX,FINDINGS_RESULT_LIMIT,- PATCH_ARTIFACT_MAX_BYTES,PATCH_PREVIEW_BYTES,SQLITE_RETRY_ATTEMPTS,)@@ -2442,18 +2441,12 @@ def require_scan_relative_file(scan: sqlite3.Row, value: str) -> str:def require_matching_patch_digest(scan: sqlite3.Row, patch_path: str, patch_digest: str) -> None:digest = hashlib.sha256()with open_scan_local_file(Path(scan["scan_dir"]), patch_path) as patch:- require_bounded_patch_artifact(patch)while chunk := patch.read(1024 * 1024):digest.update(chunk)if f"sha256:{digest.hexdigest()}" != patch_digest:raise SystemExit("Patch digest does not match the scan-local patch file.")-def require_bounded_patch_artifact(patch: Any) -> None:- if os.fstat(patch.fileno()).st_size > PATCH_ARTIFACT_MAX_BYTES:- raise SystemExit("Patch artifact must be no larger than 2 MiB.")--def open_scan_local_file(scan_dir: Path, relative_path: str) -> Any:parsed = PurePosixPath(relative_path)if parsed.is_absolute() or not parsed.parts or ".." in parsed.parts:@@ -3150,7 +3143,6 @@ def patch_artifact_preview(at_line_start = Truetry:with open_scan_local_file(scan_dir, relative_path) as patch:- require_bounded_patch_artifact(patch)while chunk := patch.readline(1024 * 1024):digest.update(chunk)if len(preview) <= PATCH_PREVIEW_BYTES:diff --git a/sdk/typescript/tests-ts/workbench-patch.test.ts b/sdk/typescript/tests-ts/workbench-patch.test.tsnew file mode 100644index 000000000..b6e04ae95--- /dev/null+++ b/sdk/typescript/tests-ts/workbench-patch.test.ts@@ -0,0 +1,84 @@+import { createHash } from "node:crypto";+import { mkdtemp, realpath, rm, writeFile } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { afterEach, describe, expect, test } from "bun:test";+import { PLUGIN_ROOT } from "./plugin-root.js";++const temporaryDirectories: string[] = [];++afterEach(async () => {+ await Promise.all(+ temporaryDirectories+ .splice(0)+ .map((path) => rm(path, { recursive: true, force: true })),+ );+});++const patchProbe = [+ "import json, sys",+ "from pathlib import Path",+ "sys.path.insert(0, sys.argv[1])",+ "import workbench_db as workbench",+ "scan_dir, digest = Path(sys.argv[2]), sys.argv[3]",+ "scan = {'scan_dir': str(scan_dir)}",+ "workbench.require_matching_patch_digest(scan, 'remediation.patch', digest)",+ "preview, stats = workbench.patch_artifact_preview(scan_dir, 'remediation.patch', digest)",+ "try:",+ " workbench.require_matching_patch_digest(scan, 'remediation.patch', 'sha256:' + '0' * 64)",+ "except SystemExit as error:",+ " mismatch = str(error)",+ "else:",+ " mismatch = None",+ "print(json.dumps({'preview': preview, 'stats': stats, 'mismatch': mismatch}))",+].join("\n");++describe("workbench remediation patches", () => {+ test("streams patches larger than 2 MiB without weakening digest checks", async () => {+ const directory = await realpath(+ await mkdtemp(join(tmpdir(), "codex-security-large-patch-")),+ );+ temporaryDirectories.push(directory);+ const patch = Buffer.concat([+ Buffer.from("diff --git a/src.ts b/src.ts\n+"),+ Buffer.alloc(2 * 1024 * 1024, 0x78),+ Buffer.from("\n"),+ ]);+ await writeFile(join(directory, "remediation.patch"), patch);+ const digest = `sha256:${createHash("sha256").update(patch).digest("hex")}`;+ const python = Bun.which("python3") ?? Bun.which("python");+ expect(python).not.toBeNull();+ if (python === null) throw new Error("A Python interpreter is required.");++ const result = Bun.spawnSync(+ [+ python,+ "-I",+ "-B",+ "-c",+ patchProbe,+ join(PLUGIN_ROOT, "scripts"),+ directory,+ digest,+ ],+ { stdout: "pipe", stderr: "pipe" },+ );+ expect(new TextDecoder().decode(result.stderr)).toBe("");+ expect(result.exitCode).toBe(0);+ const output = JSON.parse(new TextDecoder().decode(result.stdout)) as {+ preview: string;+ stats: Record<string, number | boolean>;+ mismatch: string;+ };+ expect(output.preview).toStartWith("diff --git a/src.ts b/src.ts\n+");+ expect(output.preview).toEndWith("... patch preview truncated ...");+ expect(output.stats).toMatchObject({+ additions: 1,+ fileCount: 1,+ previewTruncated: true,+ });+ expect(output.mismatch).toBe(+ "Patch digest does not match the scan-local patch file.",+ );+ });+});