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(activity): preserve literal paths before display formatting (#1827)

Michael D'Angelo6a772e1ad692
diff --git a/sdk/typescript/src/scan-activity.ts b/sdk/typescript/src/scan-activity.ts
index ee4b9af1b..134d4429e 100644
--- a/sdk/typescript/src/scan-activity.ts
+++ b/sdk/typescript/src/scan-activity.ts
@@ -74,12 +74,12 @@ export function scanActivityFromEvent(
item["type"] === "command_execution" &&
typeof item["command"] === "string"
) {
- const command = displayCommand(item["command"]);
+ const command = unwrapCommand(item["command"]);
return {
id: item["id"],
kind: "command",
status,
- description: command,
+ description: command.replaceAll(/\s+/gu, " "),
paths: commandRepositoryPaths(command, repository),
};
}
@@ -87,14 +87,14 @@ export function scanActivityFromEvent(
if (item["type"] === "mcp_tool_call" && typeof item["tool"] === "string") {
const arguments_ = isRecord(item["arguments"]) ? item["arguments"] : {};
const value = arguments_["cmd"] ?? arguments_["command"];
- const command = typeof value === "string" ? displayCommand(value) : null;
+ const command = typeof value === "string" ? unwrapCommand(value) : null;
return {
id: item["id"],
kind: "tool",
status,
description:
item["tool"] === "exec" && command !== null
- ? command
+ ? command.replaceAll(/\s+/gu, " ")
: toolDescription(item["tool"], arguments_, item["arguments"]),
paths:
item["tool"] === "exec" && command !== null
@@ -118,11 +118,11 @@ export function scanActivityFromEvent(
return null;
}
-function displayCommand(command: string): string {
- const normalized = command.replaceAll(/\s+/gu, " ").trim();
+function unwrapCommand(command: string): string {
+ const normalized = command.trim();
const powershell =
process.platform === "win32"
- ? /^(?:"([^"]+)"|([^\s"'|;&<>]+))\s+(?:-NoProfile\s+)?-Command\s+(['"])(.*)\3$/iu.exec(
+ ? /^(?:"([^"]+)"|([^\s"'|;&<>]+))\s+(?:-NoProfile\s+)?-Command\s+(['"])(.*)\3$/isu.exec(
normalized,
)
: null;
@@ -135,7 +135,7 @@ function displayCommand(command: string): string {
return powershell[4]!;
}
const match =
- /^(?:\/(?:usr\/)?bin\/)?(?:zsh|bash|sh)\s+-[a-z]*c[a-z]*\s+(['"])(.*)$/u.exec(
+ /^(?:\/(?:usr\/)?bin\/)?(?:zsh|bash|sh)\s+-[a-z]*c[a-z]*\s+(['"])(.*)$/su.exec(
normalized,
);
if (match === null) return normalized;
diff --git a/sdk/typescript/tests-ts/scan-activity.test.ts b/sdk/typescript/tests-ts/scan-activity.test.ts
index e3df4d25a..1a2a36c01 100644
--- a/sdk/typescript/tests-ts/scan-activity.test.ts
+++ b/sdk/typescript/tests-ts/scan-activity.test.ts
@@ -261,6 +261,45 @@ describe("scan activity", () => {
],
] as const)("%s", commandActivityCase);
+ test.each([" ", "\u00a0"])(
+ "attributes literal whitespace in paths before formatting descriptions (%j)",
+ (whitespace) => {
+ const repository = `/code/scan${whitespace}project`;
+ const script = `cat "${repository}/src/a${whitespace}b.ts"\n "$CODEX_SECURITY_REPOSITORY/src/a b.ts"`;
+ const commands = [script, `/bin/sh -lc '${script}'`];
+ if (process.platform === "win32") {
+ commands.push(`pwsh -NoProfile -Command '${script}'`);
+ }
+ for (const command of commands) {
+ for (const activity of [
+ scanActivityFromEvent(
+ commandEvent("item.started", command),
+ repository,
+ ),
+ scanActivityFromEvent(
+ toolEvent("exec", { cmd: command }),
+ repository,
+ ),
+ scanActivityFromSessionEvent(
+ sessionEvent({
+ type: "function_call",
+ name: "exec_command",
+ call_id: "worker-command-1",
+ arguments: JSON.stringify({ cmd: command }),
+ }),
+ repository,
+ ),
+ ]) {
+ expect(activity).toMatchObject({
+ description:
+ 'cat "/code/scan project/src/a b.ts" "$CODEX_SECURITY_REPOSITORY/src/a b.ts"',
+ paths: [`src/a${whitespace}b.ts`, "src/a b.ts"],
+ });
+ }
+ }
+ },
+ );
+
test("recognizes repository paths generated for the current shell", () => {
const command = `${process.platform === "win32" ? "Get-Content -LiteralPath" : "cat"} ${shellEnvironmentReference("CODEX_SECURITY_REPOSITORY", "/src/app.ts")}`;
expect(