OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(activity): preserve shell-escaped whitespace in repository paths (#2163)
Diyaz Seisenbayevac6a269f0168
diff --git a/sdk/typescript/src/scan-activity.ts b/sdk/typescript/src/scan-activity.tsindex d3d25d4f7..774f8bde6 100644--- a/sdk/typescript/src/scan-activity.ts+++ b/sdk/typescript/src/scan-activity.ts@@ -4,6 +4,8 @@ import { parseJson } from "./value.js";const MAX_ACTIVITY_PATHS = 8;const MAX_PROSE_CHARACTERS = 1_000;const SHELL_TOKEN = /"(?:\\.|[^"\\])*"|'[^']*'|[^\s|;&<>]+/gu;+const POSIX_TOKEN = /"(?:\\.|[^"\\])*"|'[^']*'|(?:\\[\\ \t]|[^\s|;&<>])+/gu;+const POSIX_SHELL = /(?:^|\/)(?:sh|bash|zsh)$/u;export type ScanActivityStatus = "running" | "completed" | "failed";@@ -70,37 +72,35 @@ export function scanActivityFromEvent(? "completed": "running";- if (- item["type"] === "command_execution" &&- typeof item["command"] === "string"- ) {- const command = unwrapCommand(item["command"]);+ const command = item["command"];+ if (item["type"] === "command_execution" && isCommand(command)) {return {id: item["id"],kind: "command",status,- description: command.replaceAll(/\s+/gu, " "),- paths: commandRepositoryPaths(command, repository),+ ...commandDetails(command, repository, item["shell"]),};}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" ? unwrapCommand(value) : null;- return {- id: item["id"],- kind: "tool",- status,- description:- item["tool"] === "exec" && command !== null- ? command.replaceAll(/\s+/gu, " ")- : toolDescription(item["tool"], arguments_, item["arguments"]),- paths:- item["tool"] === "exec" && command !== null- ? commandRepositoryPaths(command, repository)- : argumentRepositoryPaths(arguments_, repository),- };+ const details =+ item["tool"] === "exec" && isCommand(value)+ ? commandDetails(+ value,+ repository,+ arguments_["shell"] ?? item["shell"],+ )+ : {+ description: toolDescription(+ item["tool"],+ arguments_,+ item["arguments"],+ ),+ paths: argumentRepositoryPaths(arguments_, repository),+ };+ return { id: item["id"], kind: "tool", status, ...details };}if (@@ -118,6 +118,33 @@ export function scanActivityFromEvent(return null;}+function isCommand(value: unknown): value is string | string[] {+ return (+ typeof value === "string" ||+ (Array.isArray(value) && value.every((part) => typeof part === "string"))+ );+}++function commandDetails(+ value: string | string[],+ repository: string,+ shell: unknown,+) {+ const text =+ typeof value === "string" ? unwrapCommand(value) : value.join(" ");+ return {+ description: text.replaceAll(/\s+/gu, " "),+ paths:+ typeof value === "string"+ ? commandRepositoryPaths(+ text === value.trim() ? value : text,+ repository,+ shell,+ )+ : literalRepositoryPaths(value, repository),+ };+}+function unwrapCommand(command: string): string {const normalized = command.trim();const powershell =@@ -209,23 +236,16 @@ export function scanActivityFromSessionEvent(arguments_["cmd"] ??arguments_["command"] ??customShellCommand(name, payload["input"]);- const command =- typeof value === "string"- ? value- : Array.isArray(value) && value.every((part) => typeof part === "string")- ? value.join(" ")- : null;if (- command !== null &&+ isCommand(value) &&/^(?:exec|exec_command|shell_command|shell)$/u.test(name)) {- return scanActivityFromEvent(- {- type: "item.started",- item: { id, type: "command_execution", command },- },- repository,- );+ return {+ id,+ kind: "command",+ status: "running",+ ...commandDetails(value, repository, arguments_["shell"]),+ };}return {@@ -383,14 +403,16 @@ function windowsActivityRoot(path: string): string {.replace(/[A-Z]/gu, (letter) => letter.toLowerCase());}-function repositoryPathMatcher(repository: string) {- const prefix = `${repository.replaceAll("\\", "/").replace(/\/$/u, "")}/`;+function repositoryPathMatcher(repository: string, literal = false) {const root = windowsActivityRoot(repository);+ const normalize = (path: string) =>+ literal && !root ? path : path.replaceAll("\\", "/");+ const prefix = `${normalize(repository).replace(/\/$/u, "")}/`;const directory = prefix.slice(root.length);// A recorded UNC path can refer to either an SMB share or a case-sensitive// WebDAV collection. Keep every component after the server exact.- return (raw: string, value = raw.replaceAll("\\", "/")): string | null => {+ return (raw: string, value = normalize(raw)): string | null => {if (value.startsWith(prefix) ||(root !== "" &&@@ -403,19 +425,69 @@ function repositoryPathMatcher(repository: string) {};}-function commandRepositoryPaths(command: string, repository: string): string[] {+function literalRepositoryPaths(argv: string[], repository: string): string[] {+ // The shell's -c argument is source text. Its positional arguments are literal.+ const executable = argv[0] ?? "";+ const posix = POSIX_SHELL.test(executable);+ const powershell = /(?:^|[\\/])(?:pwsh|powershell)(?:\.exe)?$/iu.test(+ executable,+ );+ let operands = argv;+ if (posix || powershell) {+ for (let i = 1; i < argv.length; i++) {+ const part = argv[i]!;+ if (+ !part.startsWith("-") ||+ part === "--" ||+ (powershell && /^-file$/iu.test(part))+ )+ break;+ if (posix && /^-[a-z]*c[a-z]*$/u.test(part)) {+ const script = argv[i + 1] === "--" ? i + 2 : i + 1;+ operands = argv.filter((_, index) => index !== script);+ break;+ }+ if (powershell && /^-command$/iu.test(part)) {+ operands = argv.slice(0, i);+ break;+ }+ }+ }+ return operandRepositoryPaths(operands, repository, true);+}++function commandRepositoryPaths(+ command: string,+ repository: string,+ shell: unknown,+): string[] {+ const escapedBlanks =+ typeof shell === "string" &&+ POSIX_SHELL.test(shell) &&+ /\\[ \t]/u.test(command);const paths = new Set<string>();const roots = ["$CODEX_SECURITY_REPOSITORY","${CODEX_SECURITY_REPOSITORY}",repository,- ].map(repositoryPathMatcher);- for (const token of command.match(SHELL_TOKEN) ?? []) {+ ].map((root) => repositoryPathMatcher(root));+ for (const token of command.match(+ escapedBlanks ? POSIX_TOKEN : SHELL_TOKEN,+ ) ?? []) {let value = token;const singleQuoted = value.startsWith("'") && value.endsWith("'");- if ((value.startsWith('"') && value.endsWith('"')) || singleQuoted) {- value = value.slice(1, -1);- }+ const quoted =+ (value.startsWith('"') && value.endsWith('"')) || singleQuoted;+ const escaped = escapedBlanks && !quoted && /\\[ \t]/u.test(token);+ if (escaped) {+ value = value.replace(/\\([ \t])/gu, "$1");+ // Only the registered repository variable can be resolved in an escaped word.+ const operand = value.replace(+ /^\$(?:CODEX_SECURITY_REPOSITORY|\{CODEX_SECURITY_REPOSITORY\})\//u,+ "",+ );+ if (/["'\\\x60$()*?\[{}]/u.test(operand)) continue;+ } else if (quoted) value = value.slice(1, -1);const raw = value.replaceAll('\\"', '"');value = raw.replaceAll("\\", "/");if (process.platform === "win32" && !singleQuoted) {@@ -427,11 +499,13 @@ function commandRepositoryPaths(command: string, repository: string): string[] {for (const relativeTo of roots) {const relative = relativeTo(raw, value);if (relative === null) continue;- const path = relative- .replace(/["'\r\n].*$/u, "")- .trim()- .replace(/\/+$/u, "")- .replace(/:\d+(?::\d+)?$/u, "");+ const path = escaped+ ? relative+ : relative+ .replace(/["'\r\n].*$/u, "")+ .trim()+ .replace(/\/+$/u, "")+ .replace(/:\d+(?::\d+)?$/u, "");if (path !== "" &&!path.startsWith("../") &&@@ -448,12 +522,25 @@ function commandRepositoryPaths(command: string, repository: string): string[] {function argumentRepositoryPaths(value: unknown, repository: string): string[] {if (!isRecord(value)) return [];const candidates = [value["path"], value["file_path"], value["paths"]].flat();+ return operandRepositoryPaths(candidates, repository);+}++function operandRepositoryPaths(+ candidates: unknown[],+ repository: string,+ literal = false,+): string[] {const paths = new Set<string>();- const relativeTo = repositoryPathMatcher(repository);+ const relativeTo = repositoryPathMatcher(repository, literal);for (const candidate of candidates) {if (typeof candidate !== "string") continue;const path = relativeTo(candidate);- if (path !== null && !path.split("/").includes("..")) paths.add(path);+ if (+ path !== null &&+ (!literal || path !== "") &&+ !path.split("/").includes("..")+ )+ paths.add(path);if (paths.size >= MAX_ACTIVITY_PATHS) break;}return [...paths];diff --git a/sdk/typescript/tests-ts/scan-activity.test.ts b/sdk/typescript/tests-ts/scan-activity.test.tsindex 03caa7c56..f9773a6dd 100644--- a/sdk/typescript/tests-ts/scan-activity.test.ts+++ b/sdk/typescript/tests-ts/scan-activity.test.ts@@ -59,6 +59,126 @@ function commandActivityCase(}describe("scan activity", () => {+ test.each([+ [+ "cat $CODEX_SECURITY_REPOSITORY/src/my\\ file.ts",+ "/repo",+ ["src/my file.ts"],+ ],+ [+ "cat /code/project\\ files/\\ leading\\ file.ts\\ ",+ "/code/project files",+ [" leading file.ts "],+ ],+ [+ "cat \"/repo/two words\\ with.txt\" '/repo/quoted path.ts' /repo/my\\\tfile.ts",+ "/repo",+ ["two words/ with.txt", "quoted path.ts", "my\tfile.ts"],+ ],+ ["cat /repo/my\\ file.ts\\\\sibling /repo/safe.ts", "/repo", ["safe.ts"]],+ ] as const)(+ "decodes unquoted blanks in recorded POSIX commands: %s",+ (cmd, repository, paths) => {+ for (const activity of [+ scanActivityFromEvent(+ {+ type: "item.started",+ item: {+ id: "live",+ type: "command_execution",+ command: cmd,+ shell: "/bin/bash",+ },+ },+ repository,+ ),+ scanActivityFromEvent(+ toolEvent("exec", { cmd, shell: "sh" }),+ repository,+ ),+ scanActivityFromSessionEvent(+ sessionEvent({+ type: "function_call",+ name: "exec_command",+ call_id: "saved",+ arguments: JSON.stringify({ cmd, shell: "/bin/zsh" }),+ }),+ repository,+ ),+ ])+ expect(activity?.paths).toEqual([...paths]);+ },+ );++ test.each([undefined, "/usr/bin/pwsh", "unknown"])(+ "preserves ordinary token boundaries for shell %p",+ (shell) => {+ expect(+ scanActivityFromEvent(+ toolEvent("exec", {+ cmd: "Get-ChildItem /repo/src\\ -File /repo/one.ts",+ shell,+ }),+ "/repo",+ )?.paths,+ ).toEqual(["src", "one.ts"]);+ },+ );++ test.each([+ [+ [+ "grep",+ "-F",+ "foo\\",+ "/repo/my file.ts",+ "/repo/one\\two.ts",+ "$CODEX_SECURITY_REPOSITORY/other.ts",+ ],+ "/repo",+ ["my file.ts", "one\\two.ts"],+ ],+ [+ ["sh", "-e", "-c", "/repo/inside.ts /repo/fake.ts", "/repo/direct.ts"],+ "/repo",+ ["direct.ts"],+ ],+ [+ ["sh", "/repo/run.sh", "-c", "/repo/options.conf"],+ "/repo",+ ["run.sh", "options.conf"],+ ],+ [+ ["pwsh", "-Command", "/repo/script.ps1", "/repo/script text"],+ "/repo",+ [],+ ],+ [+ ["pwsh", "-File", "C:\\repo\\run.ps1", "-Command", "c:\\repo\\settings"],+ "C:\\repo",+ ["run.ps1", "settings"],+ ],+ [+ ["cat", "//server/Share/Repo/my file.ts"],+ "//Server/Share/Repo",+ ["my file.ts"],+ ],+ ] as const)(+ "uses the recorded argv boundaries for %p",+ (command, repository, paths) => {+ expect(+ scanActivityFromSessionEvent(+ sessionEvent({+ type: "local_shell_call",+ call_id: "argv",+ action: { type: "exec", command },+ }),+ repository,+ ),+ ).toMatchObject({ description: command.join(" "), paths: [...paths] });+ },+ );+test("reports repository files as soon as a read command starts", () => {expect(scanActivityFromEvent(