Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(sdk): honor cancellation during PDF knowledge-base extraction (#1529)

Peter73abb734f72b
diff --git a/sdk/typescript/src/knowledge-base.ts b/sdk/typescript/src/knowledge-base.ts
index 2df9c138b..307b7cac6 100644
--- a/sdk/typescript/src/knowledge-base.ts
+++ b/sdk/typescript/src/knowledge-base.ts
@@ -97,7 +97,7 @@ export async function readKnowledgeBaseSnapshot(
const extension = extname(document).toLowerCase();
const text =
extension === ".pdf"
- ? await extractPdf(document, bytes)
+ ? await extractPdf(document, bytes, signal)
: extension === ".docx"
? extractDocx(document, bytes)
: decodeText(document, bytes);
@@ -251,7 +251,11 @@ function decodeText(path: string, bytes: Uint8Array): string {
}
}
-async function extractPdf(path: string, bytes: Uint8Array): Promise<string> {
+async function extractPdf(
+ path: string,
+ bytes: Uint8Array,
+ signal?: AbortSignal,
+): Promise<string> {
try {
const { getDocument, VerbosityLevel } =
await import("pdfjs-dist/legacy/build/pdf.mjs");
@@ -264,7 +268,9 @@ async function extractPdf(path: string, bytes: Uint8Array): Promise<string> {
const document = await loadingTask.promise;
const pages: string[] = [];
for (let number = 1; number <= document.numPages; number++) {
+ signal?.throwIfAborted();
const content = await (await document.getPage(number)).getTextContent();
+ signal?.throwIfAborted();
pages.push(
content.items
.map((item) => ("str" in item ? item.str : ""))
@@ -276,6 +282,7 @@ async function extractPdf(path: string, bytes: Uint8Array): Promise<string> {
await loadingTask.destroy();
}
} catch (error) {
+ if (signal?.aborted) throw error;
throw new Error(`Cannot extract text from knowledge base PDF: ${path}`, {
cause: error,
});
diff --git a/sdk/typescript/tests-ts/knowledge-base.test.ts b/sdk/typescript/tests-ts/knowledge-base.test.ts
index 71eff27be..9786f6419 100644
--- a/sdk/typescript/tests-ts/knowledge-base.test.ts
+++ b/sdk/typescript/tests-ts/knowledge-base.test.ts
@@ -12,7 +12,7 @@ import {
import * as filesystem from "node:fs/promises";
import * as os from "node:os";
import { join } from "node:path";
-import { afterEach, describe, expect, spyOn, test } from "bun:test";
+import { afterEach, describe, expect, mock, spyOn, test } from "bun:test";
import { strToU8, zipSync } from "fflate";
import { ConfigurationError } from "../src/errors.js";
import {
@@ -521,6 +521,44 @@ describe("scan knowledge bases", () => {
},
);
+ test("cancels PDF extraction when the scan is aborted", async () => {
+ const root = await temporaryDirectory();
+ const source = join(root, "large.pdf");
+ await writeFile(source, pdf("Cancellable content"));
+ const pdfjs = await import("pdfjs-dist/legacy/build/pdf.mjs");
+ const controller = new AbortController();
+ const reason = new Error("Synthetic cancellation.");
+ let pagesRead = 0;
+ const destroy = mock(async () => {});
+ const parser = spyOn(pdfjs, "getDocument").mockImplementation(
+ () =>
+ ({
+ promise: Promise.resolve({
+ numPages: 3,
+ getPage: async () => {
+ pagesRead += 1;
+ return {
+ getTextContent: async () => {
+ controller.abort(reason);
+ return { items: [{ str: "page" }] };
+ },
+ };
+ },
+ }),
+ destroy,
+ }) as never,
+ );
+
+ try {
+ const prepared = prepareKnowledgeBase([source], controller.signal);
+ await expect(prepared).rejects.toBe(reason);
+ expect(pagesRead).toBe(1);
+ expect(destroy).toHaveBeenCalledTimes(1);
+ } finally {
+ parser.mockRestore();
+ }
+ });
+
test("preserves the local origin and cause when snapshot staging fails", async () => {
const root = await temporaryDirectory();
const parent = join(root, "not-a-directory");