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(cli): escape component names in terminal output (#2424)

Michael D'Angelo7c5c8e696e34
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 03bbfd106..f9a9ac0d7 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -5937,9 +5937,10 @@ export async function main(
onScanEvent:
dashboard === null
? (event) => {
- const componentName =
+ const componentName = diagnosticValue(
componentNames.get(event.componentId) ??
- event.componentId;
+ event.componentId,
+ );
const line = componentScanEventLine(
componentName,
event,
@@ -5964,7 +5965,7 @@ export async function main(
if (dashboard !== null) dashboard.updateComponent(component);
else
errorOutput.write(
- `codex-security: ${component.name} ${component.status}${component.error === undefined ? "" : `: ${diagnosticValue(component.error)}`}\n`,
+ `codex-security: ${diagnosticValue(component.name)} ${component.status}${component.error === undefined ? "" : `: ${diagnosticValue(component.error)}`}\n`,
);
},
onComplete: (result) => {
diff --git a/sdk/typescript/src/scan-dashboard.ts b/sdk/typescript/src/scan-dashboard.ts
index c14276deb..ecddac4df 100644
--- a/sdk/typescript/src/scan-dashboard.ts
+++ b/sdk/typescript/src/scan-dashboard.ts
@@ -1740,5 +1740,8 @@ function budgetBar(cost: number, limit: number): string {
}
function terminalText(value: string): string {
- return stripAnsi(value).replaceAll(/[\u0000-\u001F\u007F]/gu, " ");
+ return stripAnsi(value).replaceAll(
+ /[\u0000-\u001F\u007F-\u009F\u2028\u2029]/gu,
+ " ",
+ );
}
diff --git a/sdk/typescript/tests-ts/component-scan.test.ts b/sdk/typescript/tests-ts/component-scan.test.ts
index 7bc8f1d7a..2293cb03c 100644
--- a/sdk/typescript/tests-ts/component-scan.test.ts
+++ b/sdk/typescript/tests-ts/component-scan.test.ts
@@ -882,8 +882,14 @@ test.each([
},
);
-test("CLI escapes component failure controls while preserving the saved error", async () => {
+test("CLI escapes component names and errors in headless output and preserves their saved values", async () => {
const paths = await fixture();
+ const name = "API\u001b[2J\nnext\u009bline";
+ const planFile = join(paths.root, "plan.json");
+ await writeFile(
+ planFile,
+ JSON.stringify({ components: [{ name, paths: ["apps/api"] }] }),
+ );
const failure = "Component failed: token=SYNTHETIC_VALUE\u001b[2J\ncontinued";
const { stdout, stderr, runCli } = createCliTest(main);
@@ -892,25 +898,36 @@ test("CLI escapes component failure controls while preserving the saved error",
[
"scan-components",
paths.repository,
- "--component",
- "apps/api",
+ "--components-file",
+ planFile,
+ "--headless",
"--output-dir",
paths.outputDir,
"--json",
],
{
...dependencies({ currentDirectory: paths.root }),
- createSecurity: client(rejecting(failure)),
+ createSecurity: client(async (_repository, options) => {
+ options.onProgress?.({
+ phase: "validation",
+ filesCompleted: 1,
+ filesTotal: 1,
+ });
+ throw new Error(failure);
+ }),
},
),
).toBe(2);
+ expect(stderr.text()).toContain("API [2J next line validating findings");
+ expect(stderr.text()).toContain("API [2J next line failed:");
expect(stderr.text()).toContain(
"Component failed: token=SYNTHETIC_VALUE [2J continued\n",
);
expect(stderr.text()).not.toContain("\u001b");
+ expect(stderr.text()).not.toContain("\u009b");
const result = JSON.parse(stdout.text());
expect(await json(result.summaryPath)).toMatchObject({
- components: [expect.objectContaining({ error: failure })],
+ components: [expect.objectContaining({ name, error: failure })],
});
});
diff --git a/sdk/typescript/tests-ts/scan-dashboard.test.ts b/sdk/typescript/tests-ts/scan-dashboard.test.ts
index c8473353e..1c56843da 100644
--- a/sdk/typescript/tests-ts/scan-dashboard.test.ts
+++ b/sdk/typescript/tests-ts/scan-dashboard.test.ts
@@ -1608,7 +1608,7 @@ describe("live scan dashboard", () => {
{ length: 20 },
(_, index) => ({
id: `c${index}`,
- name: `Component ${index}`,
+ name: `Component\u0085${index}`,
paths: [`src/${index}`],
status: "pending",
outputDir: `/synthetic/${index}`,
@@ -1634,6 +1634,7 @@ describe("live scan dashboard", () => {
.every((line) => line.length <= 80),
).toBe(true);
input.emit("data", "\r");
+ expect(frame()).toContain("project · Component 19");
expect(frame()).not.toContain("COST");
input.emit("data", "\u0003");
expect(onInterrupt).toHaveBeenCalled();