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(imports): bound remote error bodies and retain HTTP failure context (#2320)
Michael D'Angelo4b9952c03705
diff --git a/docs/scanner-imports/semgrep.md b/docs/scanner-imports/semgrep.mdindex faee32692..1b545f9b9 100644--- a/docs/scanner-imports/semgrep.md+++ b/docs/scanner-imports/semgrep.md@@ -257,7 +257,8 @@ membership has changed. Retry the collection if it reports a change.On HTTP 429, the collector retries the same GET once if `Retry-After` specifiesa wait of 10 seconds or less, in whole seconds or as an HTTP date. Zero or a-past date triggers an immediate retry. Cancellation stops the wait.+past date triggers an immediate retry. Cancellation stops the wait. Failed requests report+the HTTP status and up to 64 KiB of the vendor response.A finding that identifies its repository only by name can use the project name,the Git URL path, or the final path component of either. For any other name,diff --git a/docs/scanner-imports/snyk.md b/docs/scanner-imports/snyk.mdindex 88a4d4277..5d0ca574b 100644--- a/docs/scanner-imports/snyk.md+++ b/docs/scanner-imports/snyk.md@@ -47,7 +47,8 @@ location from `evidence.source_data.issue` in v1 or from the JSON text inOn HTTP 429, the collector retries the same GET once if `Retry-After` specifiesa wait of 10 seconds or less, in whole seconds or as an HTTP date. Zero or a-past date triggers an immediate retry. Cancellation stops the wait.+past date triggers an immediate retry. Cancellation stops the wait. Failed requests report+the HTTP status and up to 64 KiB of the vendor response.Use the same source key on later imports. The issue key within the APIorigin, organization, project and product identifies the finding. Replaying thediff --git a/sdk/typescript/src/hosted-scanner-http.ts b/sdk/typescript/src/hosted-scanner-http.tsindex 416dcdec1..235ff741b 100644--- a/sdk/typescript/src/hosted-scanner-http.ts+++ b/sdk/typescript/src/hosted-scanner-http.ts@@ -1,6 +1,6 @@import { waitForRetry } from "./deduplication/retry.js";import { canonicalNumber } from "./external-import-contract.js";-import { CodexSecurityError } from "./errors.js";+import { CodexSecurityError, errorMessage } from "./errors.js";import { normalizeHostedGitRepositoryUrl } from "./hosted-git-repository.js";import { importEnvironmentSetting } from "./external-imports-feature.js";import { isRecord } from "./record.js";@@ -143,6 +143,51 @@ function retryAfterMs(response: Response): number | undefined {return delay <= 10_000 ? delay : undefined;}+// A proxy's failure page can be arbitrarily large, including when chunked or compressed.+// Bound only the remote diagnostic; successful vendor findings use the normal parser.+const HTTP_ERROR_DETAIL_BYTES = 64 * 1024;++async function scannerHttpError(+ url: URL,+ response: Response,+ signal?: AbortSignal,+) {+ const retry = response.headers.get("retry-after");+ const status = `GET ${url.origin}${url.pathname} failed (HTTP ${response.status})${retry ? `; Retry-After: ${retry}` : ""}`;+ const reader = response.body?.getReader();+ if (!reader) return new CodexSecurityError(`${status}.`);+ const decoder = new TextDecoder();+ let remaining = HTTP_ERROR_DETAIL_BYTES;+ let detail = "";+ try {+ for (;;) {+ const { done, value } = await reader.read();+ if (done) {+ detail += decoder.decode();+ break;+ }+ const prefix = value.subarray(0, remaining);+ detail += decoder.decode(prefix, { stream: true });+ remaining -= prefix.byteLength;+ if (prefix.byteLength < value.byteLength) {+ void reader.cancel().catch(() => undefined);+ detail += `\nShowing the first ${HTTP_ERROR_DETAIL_BYTES} bytes of the error response.`;+ break;+ }+ }+ } catch (cause) {+ signal?.throwIfAborted();+ const received = detail ? `: ${detail}` : "";+ return new CodexSecurityError(+ `${status}${received}\nReading the error response failed: ${errorMessage(cause)}`,+ { cause },+ );+ } finally {+ reader.releaseLock();+ }+ return new CodexSecurityError(`${status}${detail ? `: ${detail}` : "."}`);+}+export function hostedScannerHttp(dependencies: HostedScannerHttpOptions,callerSignal?: AbortSignal,@@ -176,13 +221,7 @@ export function hostedScannerHttp(signal?.throwIfAborted();response = await get();}- if (!response.ok) {- const retry = response.headers.get("retry-after");- const detail = await response.text();- throw new CodexSecurityError(- `GET ${url.origin}${url.pathname} failed (HTTP ${response.status})${retry ? `; Retry-After: ${retry}` : ""}${detail ? `: ${detail}` : "."}`,- );- }+ if (!response.ok) throw await scannerHttpError(url, response, signal);return parseHostedScannerJson(new TextDecoder("utf-8", { fatal: true }).decode(await response.arrayBuffer(),diff --git a/sdk/typescript/tests-ts/hosted-scanner-http.test.ts b/sdk/typescript/tests-ts/hosted-scanner-http.test.tsindex d021a6045..dc3cc8b2a 100644--- a/sdk/typescript/tests-ts/hosted-scanner-http.test.ts+++ b/sdk/typescript/tests-ts/hosted-scanner-http.test.ts@@ -133,3 +133,54 @@ test("does not retry a fetch rejection", async () => {await expect(get({ fetch })).rejects.toBe(error);expect(fetch).toHaveBeenCalledTimes(1);});++test("bounds a streamed scanner error while retaining its status and diagnostic prefix", async () => {+ let pulls = 0;+ let canceled = false;+ const chunk = new TextEncoder().encode("x".repeat(8192));+ const response = new Response(+ new ReadableStream<Uint8Array>({+ pull(controller) {+ controller.enqueue(chunk);+ if (++pulls === 40) controller.close();+ },+ cancel() {+ canceled = true;+ },+ }),+ { status: 503 },+ );+ const error = await get({ fetch: async () => response }).catch(+ (reason: unknown) => reason,+ );+ expect(error).toBeInstanceOf(Error);+ expect((error as Error).message).toContain(+ `failed (HTTP 503): ${"x".repeat(64 * 1024)}\n`,+ );+ expect((error as Error).message).toContain("first 65536 bytes");+ expect(canceled).toBe(true);+ expect(pulls).toBeLessThan(40);+});++test("an interrupted error body retains the HTTP status, received text and transport cause", async () => {+ const failure = new Error("scanner disconnected");+ let sent = false;+ const response = new Response(+ new ReadableStream<Uint8Array>({+ pull(controller) {+ if (sent) return controller.error(failure);+ sent = true;+ controller.enqueue(new TextEncoder().encode("upstream maintenance"));+ },+ }),+ { status: 502 },+ );+ const error = await get({ fetch: async () => response }).catch(+ (reason: unknown) => reason,+ );+ expect((error as Error).message).toContain(+ "failed (HTTP 502): upstream maintenance",+ );+ expect((error as Error).message).toContain(failure.message);+ expect((error as Error).cause).toBe(failure);+});