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(bulk): propagate wizard cancellation to prompts (#540)

Sylvester Kaczmarek719bedcbd351
diff --git a/sdk/typescript/src/bulk-scan-discovery.ts b/sdk/typescript/src/bulk-scan-discovery.ts
index 3558bccf6..58bdb1472 100644
--- a/sdk/typescript/src/bulk-scan-discovery.ts
+++ b/sdk/typescript/src/bulk-scan-discovery.ts
@@ -147,7 +147,11 @@ export async function runBulkScanWizard(
}
prompt.write(`\nFound ${discovered.length} repositories.\n`);
- const repositories = await selectGitHubRepositories(discovered, prompt);
+ const repositories = await selectGitHubRepositories(
+ discovered,
+ prompt,
+ signal,
+ );
const outputDir = resolve(
dependencies.currentDirectory(),
@@ -155,6 +159,7 @@ export async function runBulkScanWizard(
await prompt.input(
"Where should scan results be saved?",
defaultOutputDir,
+ signal,
),
),
);
@@ -164,7 +169,7 @@ export async function runBulkScanWizard(
`\nReady to scan ${repositories.length} repositories?\n` +
`Results: ${outputDir}\nRepository list: ${inputPath}\n`,
);
- if (!(await prompt.confirm("Start scanning?"))) {
+ if (!(await prompt.confirm("Start scanning?", false, signal))) {
prompt.write("\nScan canceled.\n");
return null;
}
@@ -212,6 +217,8 @@ async function selectGitHubOwner(
return await prompt.select(
"Which account or organization should we scan?",
owners.map((owner) => ({ label: owner, value: owner })),
+ undefined,
+ signal,
);
}
prompt.write(`\nFinding repositories in ${personal}.\n`);
@@ -221,6 +228,7 @@ async function selectGitHubOwner(
async function selectGitHubRepositories(
repositories: GitHubRepository[],
prompt: BulkScanPrompt,
+ signal?: AbortSignal,
): Promise<GitHubRepository[]> {
const selected = new Set<string>();
while (selected.size < repositories.length) {
@@ -237,6 +245,8 @@ async function selectGitHubRepositories(
.filter(({ fullName }) => !selected.has(fullName))
.map(({ fullName }) => ({ label: fullName, value: fullName })),
],
+ undefined,
+ signal,
);
if (!choice) break;
selected.add(choice);
diff --git a/sdk/typescript/tests-ts/bulk-scan-discovery.test.ts b/sdk/typescript/tests-ts/bulk-scan-discovery.test.ts
index cf4e1b26a..10472a2cf 100644
--- a/sdk/typescript/tests-ts/bulk-scan-discovery.test.ts
+++ b/sdk/typescript/tests-ts/bulk-scan-discovery.test.ts
@@ -37,6 +37,8 @@ async function temporaryDirectory(): Promise<string> {
class FakePrompt implements BulkScanPrompt {
public readonly messages: string[] = [];
public readonly questions: string[] = [];
+ public readonly signals: (AbortSignal | undefined)[] = [];
+ public beforeAnswer?: (signal?: AbortSignal) => Promise<void>;
public interactive = true;
public confirms: boolean[] = [];
public inputs: string[] = [];
@@ -51,21 +53,37 @@ class FakePrompt implements BulkScanPrompt {
this.messages.push(value);
}
- public async confirm(question: string, fallback = false): Promise<boolean> {
+ public async confirm(
+ question: string,
+ fallback = false,
+ signal?: AbortSignal,
+ ): Promise<boolean> {
this.questions.push(question);
+ this.signals.push(signal);
+ await this.beforeAnswer?.(signal);
return this.confirms.shift() ?? fallback;
}
- public async input(question: string, fallback = ""): Promise<string> {
+ public async input(
+ question: string,
+ fallback = "",
+ signal?: AbortSignal,
+ ): Promise<string> {
this.questions.push(question);
+ this.signals.push(signal);
+ await this.beforeAnswer?.(signal);
return this.inputs.shift() ?? fallback;
}
public async select<Value extends string>(
question: string,
options: readonly { label: string; value: Value }[],
+ _presentation?: unknown,
+ signal?: AbortSignal,
): Promise<Value> {
this.questions.push(question);
+ this.signals.push(signal);
+ await this.beforeAnswer?.(signal);
this.searchOptions.push(options.map(({ label }) => label));
const value = this.choices.shift();
return (options.find((option) => option.value === value) ?? options[0]!)
@@ -461,4 +479,52 @@ describe("bulk scan repository discovery", () => {
expect(prompt.questions).toEqual([]);
expect(requests).toEqual([]);
});
+
+ test("passes cancellation to every setup prompt", async () => {
+ const root = await temporaryDirectory();
+ const { dependencies, prompt } = discoveryDependencies(root, {
+ organizations: ["acme"],
+ });
+ const signal = new AbortController().signal;
+
+ await runBulkScanWizard(dependencies, signal);
+
+ expect(prompt.signals).toEqual([signal, signal, signal, signal]);
+ });
+
+ test.each([
+ [1, "account selection"],
+ [2, "repository selection"],
+ [3, "repeated repository selection"],
+ [4, "output directory"],
+ [5, "start confirmation"],
+ ] as const)("stops at prompt %i (%s) when canceled", async (stage) => {
+ const root = await temporaryDirectory();
+ const { dependencies, prompt } = discoveryDependencies(root, {
+ organizations: ["acme"],
+ });
+ prompt.choices.push("acme", "acme/payments-api", "");
+ const controller = new AbortController();
+ const started = Promise.withResolvers<void>();
+ const canceled = new Error("Setup canceled");
+ prompt.beforeAnswer = async (signal) => {
+ if (prompt.signals.length !== stage) return;
+ await new Promise<void>((_resolve, reject) => {
+ signal?.addEventListener("abort", () => reject(signal.reason), {
+ once: true,
+ });
+ started.resolve();
+ });
+ };
+
+ const wizard = runBulkScanWizard(dependencies, controller.signal);
+ await started.promise;
+ controller.abort(canceled);
+
+ await expect(wizard).rejects.toBe(canceled);
+ expect(prompt.questions).toHaveLength(stage);
+ await expect(lstat(join(root, "security-scans"))).rejects.toMatchObject({
+ code: "ENOENT",
+ });
+ });
});