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
refactor(import): share scanner report file decoding (#2113)
Michael D'Angelo3d0fcf1b9772
diff --git a/sdk/typescript/src/scanner-import.ts b/sdk/typescript/src/scanner-import.tsindex 2172dd6ab..6a8dce6b0 100644--- a/sdk/typescript/src/scanner-import.ts+++ b/sdk/typescript/src/scanner-import.ts@@ -49,12 +49,16 @@ export function stableId(prefix: string, fields: unknown[]): string {return `${prefix}:${hash("sha256", JSON.stringify(fields))}`;}-/** Detect gzip from the report bytes and preserve identities with strict UTF-8. */+const strictUtf8 = new TextDecoder("utf-8", { fatal: true });+const gunzipAsync = promisify(gunzip);++async function readScannerBytes(path: string): Promise<Buffer> {+ const bytes = await readFile(path);+ return bytes[0] === 0x1f && bytes[1] === 0x8b ? gunzipAsync(bytes) : bytes;+}+export async function readScannerText(path: string): Promise<string> {- let bytes = await readFile(path);- if (bytes[0] === 0x1f && bytes[1] === 0x8b)- bytes = await promisify(gunzip)(bytes);- return new TextDecoder("utf-8", { fatal: true }).decode(bytes);+ return strictUtf8.decode(await readScannerBytes(path));}export function parseJsonOrJsonl(contents: string): unknown {@@ -69,12 +73,10 @@ export function parseJsonOrJsonl(contents: string): unknown {}export async function readScannerJson(path: string): Promise<unknown> {- let bytes = await readFile(path);- if (bytes[0] === 0x1f && bytes[1] === 0x8b)- bytes = await promisify(gunzip)(bytes);+ const bytes = await readScannerBytes(path);let decoded: string;try {- decoded = new TextDecoder("utf-8", { fatal: true }).decode(bytes);+ decoded = strictUtf8.decode(bytes);} catch (error) {throw new CodexSecurityError(`Scanner report is not valid UTF-8. ${errorMessage(error)}`,diff --git a/sdk/typescript/tests-ts/scanner-import.test.ts b/sdk/typescript/tests-ts/scanner-import.test.tsindex d5ea4b2a4..5fc83c379 100644--- a/sdk/typescript/tests-ts/scanner-import.test.ts+++ b/sdk/typescript/tests-ts/scanner-import.test.ts@@ -1,9 +1,83 @@-import { expect, test } from "bun:test";+import { afterEach, expect, test } from "bun:test";+import { readFile, writeFile } from "node:fs/promises";+import { join } from "node:path";+import { gunzipSync, gzipSync } from "node:zlib";import {hasSarifExternalPropertyReferences,+ readScannerJson,+ readScannerText,relativePath,sarifRunContextForResult,} from "../src/scanner-import.js";+import { createTemporaryDirectories } from "./support/temporary-directories.js";++const reports = createTemporaryDirectories();+afterEach(reports.cleanup);++async function failure(operation: () => unknown): Promise<Error> {+ try {+ await operation();+ } catch (error) {+ if (error instanceof Error) return error;+ throw error;+ }+ throw new Error("Expected the operation to fail");+}++test("scanner readers detect gzip by content and preserve BOM handling", async () => {+ const path = join(await reports.create("scanner-report-"), "report");+ await writeFile(path, gzipSync('\uFEFF\uFEFF"λ"'));+ expect(await readScannerText(path)).toBe('\uFEFF"λ"');+ expect(await readScannerJson(path)).toBe("λ");+});++test("scanner readers propagate filesystem and gzip errors", async () => {+ const path = join(await reports.create("scanner-report-"), "missing");+ const missing = await failure(() => readFile(path));+ for (const read of [readScannerText, readScannerJson]) {+ await expect(read(path)).rejects.toMatchObject({+ code: "ENOENT",+ message: missing.message,+ });+ }++ const truncated = Buffer.from([0x1f, 0x8b]);+ await writeFile(path, truncated);+ const gzip = await failure(() => gunzipSync(truncated));+ for (const read of [readScannerText, readScannerJson]) {+ await expect(read(path)).rejects.toMatchObject({+ code: "Z_BUF_ERROR",+ message: gzip.message,+ });+ }+});++test("scanner readers preserve UTF-8 and JSON parse diagnostics", async () => {+ const path = join(await reports.create("scanner-report-"), "report");+ const invalid = Uint8Array.of(0xff);+ await writeFile(path, gzipSync(invalid));+ const utf8 = await failure(() =>+ new TextDecoder("utf-8", { fatal: true }).decode(invalid),+ );+ await expect(readScannerText(path)).rejects.toMatchObject({+ name: "TypeError",+ message: utf8.message,+ });+ await expect(readScannerJson(path)).rejects.toMatchObject({+ name: "CodexSecurityError",+ message: `Scanner report is not valid UTF-8. ${utf8.message}`,+ cause: { name: "TypeError", message: utf8.message },+ });++ await writeFile(path, "{");+ expect(await readScannerText(path)).toBe("{");+ const syntax = await failure(() => JSON.parse("{"));+ await expect(readScannerJson(path)).rejects.toMatchObject({+ name: "CodexSecurityError",+ message: `Scanner JSON could not be parsed. ${syntax.message}`,+ cause: { name: "SyntaxError", message: syntax.message },+ });+});test("scanner repository paths retain colons outside Windows drive prefixes", () => {expect(relativePath("schema:prod.json")).toBe("schema:prod.json");