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): ignore status markers inside Markdown fences (#1525)

Peter128695d5be4e
diff --git a/sdk/typescript/src/worker-progress.ts b/sdk/typescript/src/worker-progress.ts
index 5f5e40120..453fb28ac 100644
--- a/sdk/typescript/src/worker-progress.ts
+++ b/sdk/typescript/src/worker-progress.ts
@@ -85,19 +85,38 @@ export function scanProgressUpdatesFromEvent(
export function scanProgressUpdatesFromText(output: string): ScanProgress[] {
const updates: ScanProgress[] = [];
- let codeFence = false;
- for (const line of output.split(/\r?\n/u)) {
- if (/^\s*```/u.test(line)) {
- codeFence = !codeFence;
- continue;
- }
- if (codeFence || !line.startsWith(SCAN_PROGRESS_PREFIX)) continue;
+ for (const line of linesOutsideFences(output)) {
+ if (!line.startsWith(SCAN_PROGRESS_PREFIX)) continue;
const progress = scanProgressFromMarker(line);
if (progress !== null) updates.push(progress);
}
return updates;
}
+/** Return lines outside Markdown fences so quoted markers are not read as live. */
+function linesOutsideFences(text: string): string[] {
+ const lines: string[] = [];
+ let fence: { marker: string; length: number } | null = null;
+ for (const line of text.split(/\r?\n/u)) {
+ const delimiter = /^\s*(`{3,}|~{3,})(.*)$/u.exec(line);
+ if (delimiter !== null) {
+ const run = delimiter[1]!;
+ if (fence === null) {
+ fence = { marker: run[0]!, length: run.length };
+ } else if (
+ run[0] === fence.marker &&
+ run.length >= fence.length &&
+ delimiter[2]!.trim() === ""
+ ) {
+ fence = null;
+ }
+ continue;
+ }
+ if (fence === null) lines.push(line);
+ }
+ return lines;
+}
+
function scanProgressFromMarker(marker: string): ScanProgress | null {
const payload = parseJson(() => marker.slice(SCAN_PROGRESS_PREFIX.length));
if (
@@ -171,9 +190,9 @@ function dispatchStatus(
item: Readonly<Record<string, unknown>>,
): ScanWorkerStatus | null {
if (typeof item["text"] !== "string") return null;
- const markers = item["text"]
- .split(/\r?\n/u)
- .filter((line) => line.startsWith(WORKER_STATUS_PREFIX));
+ const markers = linesOutsideFences(item["text"]).filter((line) =>
+ line.startsWith(WORKER_STATUS_PREFIX),
+ );
const marker = markers[0];
if (markers.length !== 1 || marker === undefined) return null;
const payload = parseJson(() => marker.slice(WORKER_STATUS_PREFIX.length));
diff --git a/sdk/typescript/tests-ts/worker-progress.test.ts b/sdk/typescript/tests-ts/worker-progress.test.ts
index 557559fa0..ef41e6f77 100644
--- a/sdk/typescript/tests-ts/worker-progress.test.ts
+++ b/sdk/typescript/tests-ts/worker-progress.test.ts
@@ -341,6 +341,60 @@ describe("worker progress events", () => {
).toEqual([progress]);
});
+ test("ignores progress and dispatch markers inside nested or tilde fences", () => {
+ const progress = {
+ phase: "discovery" as const,
+ filesCompleted: 3,
+ filesTotal: 8,
+ };
+ const progressMarker = `CODEX_SECURITY_SCAN_PROGRESS ${JSON.stringify(progress)}`;
+ const liveProgress = { ...progress, filesCompleted: 4 };
+ const liveMarker = `CODEX_SECURITY_SCAN_PROGRESS ${JSON.stringify(liveProgress)}`;
+ const dispatchMarker = `CODEX_SECURITY_WORKER_STATUS ${JSON.stringify({
+ phase: "file_review",
+ planned: 6,
+ started: 3,
+ })}`;
+ // A four-backtick fence is not closed by an inner three-backtick line.
+ expect(
+ scanProgressUpdatesFromEvent(
+ commandEvent(
+ "read the scan workflow",
+ [
+ "````markdown",
+ "```text",
+ progressMarker,
+ "```",
+ "````",
+ liveMarker,
+ ].join("\n"),
+ ),
+ ),
+ ).toEqual([liveProgress]);
+ // A tilde fence is not closed by a backtick fence.
+ expect(
+ scanProgressUpdatesFromEvent(
+ commandEvent(
+ "read the scan workflow",
+ [
+ "~~~text",
+ progressMarker,
+ "```",
+ progressMarker,
+ "~~~",
+ liveMarker,
+ ].join("\n"),
+ ),
+ ),
+ ).toEqual([liveProgress]);
+ // Dispatch markers inside a fenced block are not live worker status.
+ expect(
+ workerStatusFromEvent(
+ messageEvent(["Example:", "```text", dispatchMarker, "```"].join("\n")),
+ ),
+ ).toBeNull();
+ });
+
test("rejects malformed or overstated file progress", () => {
for (const text of [
'CODEX_SECURITY_SCAN_PROGRESS {"phase":"discovery","filesCompleted":9,"filesTotal":8}',