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 literal backslashes in POSIX file reads (#2503)
Michael D'Angelofcccbbab4602
diff --git a/sdk/typescript/src/scan-activity.ts b/sdk/typescript/src/scan-activity.tsindex c78facdf4..08e2e7f3a 100644--- a/sdk/typescript/src/scan-activity.ts+++ b/sdk/typescript/src/scan-activity.ts@@ -427,7 +427,7 @@ function windowsActivityRoot(path: string): string {.replace(/[A-Z]/gu, (letter) => letter.toLowerCase());}-function repositoryPathMatcher(repository: string, literal = false) {+function repositoryPathMatcher(repository: string, literal: boolean) {const root = windowsActivityRoot(repository);const normalize = (path: string) =>literal && !root ? path : path.replaceAll("\\", "/");@@ -486,12 +486,13 @@ function commandRepositoryPaths(shell: unknown,): string[] {const posixShell = typeof shell === "string" && POSIX_SHELL.test(shell);+ const posixPaths = posixShell && windowsActivityRoot(repository) === "";const paths = new Set<string>();const roots = ["$CODEX_SECURITY_REPOSITORY","${CODEX_SECURITY_REPOSITORY}",repository,- ].map((root) => repositoryPathMatcher(root));+ ].map((root) => repositoryPathMatcher(root, posixPaths));for (const token of command.match(posixShell ? POSIX_TOKEN : SHELL_TOKEN) ??[]) {let value = token;@@ -501,10 +502,12 @@ function commandRepositoryPaths(? first: undefined;const hasQuote = /['"]/u.test(token);- const posixLiteral = posixShell && (hasQuote || /\\[ \t]/u.test(token));+ const hasEscape = posixPaths+ ? token.includes("\\")+ : /\\[ \t]/u.test(token);+ const posixLiteral = posixShell && (hasQuote || hasEscape);if (posixLiteral) {- // Unquoted paths with escaped blanks only support blank escapes.- if (!hasQuote && /\\(?![ \t])/u.test(token)) continue;+ if (!posixPaths && !hasQuote && /\\(?![ \t])/u.test(token)) continue;// A single-quoted or backslash-escaped dollar sign is literal.const variable =/^"?(\$(?:CODEX_SECURITY_REPOSITORY|\{CODEX_SECURITY_REPOSITORY\}))/u.exec(@@ -517,7 +520,7 @@ function commandRepositoryPaths(value = (variable ?? "") + literal;} else if (quote) value = value.slice(1, -1);const raw = posixLiteral ? value : value.replaceAll('\\"', '"');- value = raw.replaceAll("\\", "/");+ value = posixPaths ? raw : raw.replaceAll("\\", "/");if (process.platform === "win32" && quote !== "'") {value = value.replace(/^\$(?:env:CODEX_SECURITY_REPOSITORY|\{env:CODEX_SECURITY_REPOSITORY\})\//iu,@@ -553,16 +556,16 @@ function argumentRepositoryPaths(value: unknown, repository: string): string[] {function operandRepositoryPaths(candidates: unknown[],repository: string,- literal = false,+ skipRoot = false,): string[] {const paths = new Set<string>();- const relativeTo = repositoryPathMatcher(repository, literal);+ const relativeTo = repositoryPathMatcher(repository, true);for (const candidate of candidates) {if (typeof candidate !== "string") continue;const path = relativeTo(candidate);if (path !== null &&- (!literal || path !== "") &&+ (!skipRoot || path !== "") &&!path.split("/").includes(".."))paths.add(path);diff --git a/sdk/typescript/tests-ts/scan-activity.test.ts b/sdk/typescript/tests-ts/scan-activity.test.tsindex ec6bc489d..dee72e02c 100644--- a/sdk/typescript/tests-ts/scan-activity.test.ts+++ b/sdk/typescript/tests-ts/scan-activity.test.ts@@ -109,6 +109,11 @@ describe("scan activity", () => {["sh -c \"cat '\\$CODEX_SECURITY_REPOSITORY/src/file.ts'\"", undefined, []],["ls '/repo/src/'", "sh", ["src"]],['cat "/repo/src/result:42:7"', "sh", ["src/result:42:7"]],+ [+ "/bin/sh -c \"cat '/repo/logs\\\\annual.txt'\"",+ undefined,+ ["logs\\annual.txt"],+ ],["cat /repo/src/result:42:7", "sh", ["src/result"]],['cat "/repo/src/result:42:7"', undefined, ["src/result"]],['cat "/repo/src/result:42:7"', "/usr/bin/pwsh", ["src/result"]],@@ -131,17 +136,50 @@ describe("scan activity", () => {["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"],+ ["two words\\ with.txt", "quoted path.ts", "my\tfile.ts"],+ ],+ [+ "cat /repo/my\\ file.ts\\\\sibling /repo/safe.ts",+ "/repo",+ ["my file.ts\\sibling", "safe.ts"],+ ],+ [+ "cat '/work\\tree/logs\\annual.txt' '/work\\tree/logs/annual.txt' '/work/tree/other.txt'",+ "/work\\tree",+ ["logs\\annual.txt", "logs/annual.txt"],],- ["cat /repo/my\\ file.ts\\\\sibling /repo/safe.ts", "/repo", ["safe.ts"]],] as const)(- "decodes unquoted blanks in recorded POSIX commands: %s",+ "decodes literal paths in recorded POSIX commands: %s",(cmd, repository, paths) => {for (const shell of ["sh", "/bin/bash", "/bin/zsh"])expectCommandPaths(cmd, shell, paths, repository);},);+ test("keeps distinct POSIX filenames in structured tool paths", () => {+ const repository = "/work\\tree";+ const args = {+ paths: [+ repository + "/logs\\annual.txt",+ repository + "/logs/annual.txt",+ "/work/tree/other.txt",+ ],+ };+ for (const activity of [+ scanActivityFromEvent(toolEvent("read_file", args), repository),+ scanActivityFromSessionEvent(+ sessionEvent({+ type: "function_call",+ call_id: "read",+ name: "read_file",+ arguments: JSON.stringify(args),+ }),+ repository,+ ),+ ])+ expect(activity?.paths).toEqual(["logs\\annual.txt", "logs/annual.txt"]);+ });+test.each([undefined, "/usr/bin/pwsh", "unknown"])("preserves ordinary token boundaries for shell %p",(shell) => {