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): recognize Windows drive and UNC host casing (#545)
Sylvester Kaczmarek07bd29202f02
diff --git a/sdk/typescript/src/scan-activity.ts b/sdk/typescript/src/scan-activity.tsindex 134d4429e..d3d25d4f7 100644--- a/sdk/typescript/src/scan-activity.ts+++ b/sdk/typescript/src/scan-activity.ts@@ -371,30 +371,63 @@ function sessionCallArguments(return isRecord(parsed) ? parsed : {};}+const WINDOWS_DRIVE = /^(?:(?:\\\\|\/\/)[?.][\\/])?[a-z]:(?=[\\/])/i;+const UNC_SERVER = /^(?:\\\\|\/\/)(?:[?.][\\/]UNC[\\/])?[^\\/]+/i;++function windowsActivityRoot(path: string): string {+ // Inspect the original separators: a POSIX directory name can begin with a backslash.+ const root =+ WINDOWS_DRIVE.exec(path)?.[0] ?? UNC_SERVER.exec(path)?.[0] ?? "";+ return root+ .replaceAll("\\", "/")+ .replace(/[A-Z]/gu, (letter) => letter.toLowerCase());+}++function repositoryPathMatcher(repository: string) {+ const prefix = `${repository.replaceAll("\\", "/").replace(/\/$/u, "")}/`;+ const root = windowsActivityRoot(repository);+ 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 => {+ if (+ value.startsWith(prefix) ||+ (root !== "" &&+ value.startsWith(directory, root.length) &&+ windowsActivityRoot(raw) === root)+ ) {+ return value.slice(prefix.length);+ }+ return null;+ };+}+function commandRepositoryPaths(command: string, repository: string): string[] {const paths = new Set<string>();- const repositoryPrefix = repository.replaceAll("\\", "/").replace(/\/$/u, "");+ const roots = [+ "$CODEX_SECURITY_REPOSITORY",+ "${CODEX_SECURITY_REPOSITORY}",+ repository,+ ].map(repositoryPathMatcher);for (const token of command.match(SHELL_TOKEN) ?? []) {let value = token;const singleQuoted = value.startsWith("'") && value.endsWith("'");if ((value.startsWith('"') && value.endsWith('"')) || singleQuoted) {value = value.slice(1, -1);}- value = value.replaceAll('\\"', '"').replaceAll("\\", "/");+ const raw = value.replaceAll('\\"', '"');+ value = raw.replaceAll("\\", "/");if (process.platform === "win32" && !singleQuoted) {value = value.replace(/^\$(?:env:CODEX_SECURITY_REPOSITORY|\{env:CODEX_SECURITY_REPOSITORY\})\//iu,"$CODEX_SECURITY_REPOSITORY/",);}- for (const prefix of [- "$CODEX_SECURITY_REPOSITORY/",- "${CODEX_SECURITY_REPOSITORY}/",- `${repositoryPrefix}/`,- ]) {- if (!value.startsWith(prefix)) continue;- const path = value- .slice(prefix.length)+ for (const relativeTo of roots) {+ const relative = relativeTo(raw, value);+ if (relative === null) continue;+ const path = relative.replace(/["'\r\n].*$/u, "").trim().replace(/\/+$/u, "")@@ -416,14 +449,11 @@ function argumentRepositoryPaths(value: unknown, repository: string): string[] {if (!isRecord(value)) return [];const candidates = [value["path"], value["file_path"], value["paths"]].flat();const paths = new Set<string>();- const prefix = `${repository.replaceAll("\\", "/").replace(/\/$/u, "")}/`;+ const relativeTo = repositoryPathMatcher(repository);for (const candidate of candidates) {if (typeof candidate !== "string") continue;- const normalized = candidate.replaceAll("\\", "/");- if (normalized.startsWith(prefix)) {- const path = normalized.slice(prefix.length);- if (!path.split("/").includes("..")) paths.add(path);- }+ const path = relativeTo(candidate);+ if (path !== null && !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 1a2a36c01..03caa7c56 100644--- a/sdk/typescript/tests-ts/scan-activity.test.ts+++ b/sdk/typescript/tests-ts/scan-activity.test.ts@@ -368,6 +368,12 @@ describe("scan activity", () => {"/code/project",{ paths: [] },],+ [+ "keeps POSIX environment names case-sensitive for Windows repositories",+ 'cat "$CODEX_SECURITY_REPOSITORY/Src/App.ts" "${CODEX_SECURITY_REPOSITORY}/Src/Other.ts" "$codex_security_repository/src/app.ts" "${Codex_Security_Repository}/src/app.ts"',+ "C:\\code\\project",+ { paths: ["Src/App.ts", "Src/Other.ts"] },+ ],["cleans escaped quotes from real wrapped scan commands",'nl -ba "$CODEX_SECURITY_REPOSITORY/commands.py\\" printf "next" "$CODEX_SECURITY_REPOSITORY/server.py\\"',@@ -382,36 +388,65 @@ describe("scan activity", () => {],] as const)("%s", commandActivityCase);- test("keeps Unix and Windows tool paths inside the repository", () => {- for (const [repository, path, paths] of [- ["/code/juice-shop", "/code/juice-shop/../credentials.json", []],- [- "C:\\code\\juice-shop",- "C:\\code\\juice-shop\\..\\credentials.json",- [],- ],- [- "C:\\code\\juice-shop",- "C:\\code\\juice-shop\\routes\\login.ts",- ["routes/login.ts"],- ],- ] as const) {+ test.each([+ ["C:\\Work\\Repo", "C:\\Work\\Repo\\Src\\A.ts", true],+ ["C:\\Work\\Repo", "c:\\Work\\Repo\\Src\\A.ts", true],+ ["C:/", "c:/Src/A.ts", true],+ ["//Server/Share/Repo", "//server/Share/Repo/Src/A.ts", true],+ ["\\\\Server\\Share", "\\\\server\\Share\\Src\\A.ts", true],+ ["//?/UNC/Server/Share/Repo", "//?/UNC/server/Share/Repo/Src/A.ts", true],+ ["//?/C:/Repo", "//?/c:/Repo/Src/A.ts", true],+ ["//Server/Docs/Repo", "//server/docs/Repo/Src/A.ts", false],+ ["//?/UNC/Srv/Docs", "//?/UNC/srv/docs/Src/A.ts", false],+ ["/\\Foo/Repo", "/\\Foo/Repo/Src/A.ts", true],+ ["/\\Foo/Repo", "/\\foo/Repo/Src/A.ts", false],+ ["//Foo/Repo", "/\\foo/Repo/Src/A.ts", false],+ ["/work/Repo", "/work/Repo/Src/A.ts", true],+ ["C:/Work/Repo", "c:/Work/repo/Src/A.ts", false],+ ["C:/Work/Repo", "c:/work/Repo/Src/A.ts", false],+ ["//Server/Share/Repo", "//server/Share/repo/Src/A.ts", false],+ ["//?/UNC/Srv/Share/Repo", "//?/UNC/srv/Share/repo/Src/A.ts", false],+ ["//Server/Share", "//server/share-other/Src/A.ts", false],+ ["//host/K/Repo", "//host/K/Repo/Src/A.ts", false],+ ["//HOST/K/Repo", "//host/K/Repo/Src/A.ts", true],+ ["//Host@SSL/Docs", "//HOST@ssl/Docs/Src/A.ts", true],+ ["//Host@SSL/Docs", "//host@SSL/docs/Src/A.ts", false],+ ["//Host@8080/Docs", "//host@8080/docs/Src/A.ts", false],+ ["//?/UNC/Host@SSL@443/Docs", "//?/UNC/host@SSL@443/docs/Src/A.ts", false],+ ["C:/Work/Repo", "c:/Work/Repo-other/Src/A.ts", false],+ ["C:/", "D:/Src/A.ts", false],+ ["C:Repo", "c:Repo/Src/A.ts", false],+ ["/work/Repo", "/work/repo/Src/A.ts", false],+ ["/work/Repo", "/work/Repo/../Src/A.ts", false],+ ["C:/Work/Repo", "c:\\Work\\Repo\\..\\Src\\A.ts", false],+ ] as const)(+ "%s extracts live and saved activity paths from %s",+ (repository, path, inside) => {+ const paths = inside ? ["Src/A.ts"] : [];+ const cmd = `type "${path}"`;expect(scanActivityFromEvent(toolEvent("read_file", { path }), repository),- ).toMatchObject({ paths: [...paths] });+ ).toMatchObject({ paths });expect(- scanActivityFromSessionEvent(- sessionEvent({- type: "function_call",- name: "read_file",- call_id: "worker-tool-1",- arguments: JSON.stringify({ path }),- }),- repository,- ),- ).toMatchObject({ paths: [...paths] });- }- });+ scanActivityFromEvent(commandEvent("item.started", cmd), repository),+ ).toMatchObject({ paths, description: cmd });++ for (const [name, args] of [+ ["read_file", { path }],+ ["exec_command", { cmd }],+ ] as const) {+ const event = sessionEvent({+ type: "function_call",+ call_id: "worker-call",+ name,+ arguments: JSON.stringify(args),+ });+ expect(scanActivityFromSessionEvent(event, repository)).toMatchObject({+ paths,+ });+ }+ },+ );test("reports tool reads and completed reasoning", () => {expect(