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 quoted POSIX filenames (#2418)

Jayanth Yarlagaddafb9469c176ec
diff --git a/github-action/scripts/setup-ubuntu-sandbox.sh b/github-action/scripts/setup-ubuntu-sandbox.sh
index 17db9d0ce..24ed38009 100644
--- a/github-action/scripts/setup-ubuntu-sandbox.sh
+++ b/github-action/scripts/setup-ubuntu-sandbox.sh
@@ -7,4 +7,9 @@ $nrconf{override_rc}{qr(^php8\.3-fpm\.service$)} = 0;
EOF
sudo apt-get update
sudo apt-get install --yes bubblewrap apparmor-profiles
-sudo apparmor_parser -r /usr/share/apparmor/extra-profiles/bwrap-userns-restrict
+bwrap_profile=/usr/share/apparmor/extra-profiles/bwrap-userns-restrict
+# Ubuntu 26.04 installs this profile under /etc/apparmor.d.
+if [[ ! -f "$bwrap_profile" ]]; then
+ bwrap_profile=/etc/apparmor.d/bwrap-userns-restrict
+fi
+sudo apparmor_parser -r "$bwrap_profile"
diff --git a/sdk/typescript/src/scan-activity.ts b/sdk/typescript/src/scan-activity.ts
index 774f8bde6..c78facdf4 100644
--- a/sdk/typescript/src/scan-activity.ts
+++ b/sdk/typescript/src/scan-activity.ts
@@ -4,7 +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_TOKEN =
+ /(?:"(?:\\[\s\S]|[^"\\])*"|'[^']*'|\\[\s\S]|[^\s|;&<>'"\\])+/gu;
const POSIX_SHELL = /(?:^|\/)(?:sh|bash|zsh)$/u;
export type ScanActivityStatus = "running" | "completed" | "failed";
@@ -130,22 +131,41 @@ function commandDetails(
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),
- };
+ let text: string;
+ let paths: string[];
+ if (typeof value === "string") {
+ const command = unwrapCommand(value);
+ text = command.text;
+ paths = commandRepositoryPaths(
+ command.script ?? text,
+ repository,
+ shell ?? command.recordedShell,
+ );
+ } else {
+ text = value.join(" ");
+ paths = literalRepositoryPaths(value, repository);
+ }
+ return { description: text.replaceAll(/\s+/gu, " "), paths };
}
-function unwrapCommand(command: string): string {
+/** Decode one literal POSIX word without expanding variables or running a shell. */
+function literalPosixWord(argument: string) {
+ // A sticky match stops at the first unsupported fragment instead of skipping it.
+ const fragment =
+ /'([^']*)'|"((?:\\.|[^"\\$\x60])*)"|\\(.)|[^\s'"\\$\x60|;&<>()*?\[\]{}~]+/suy;
+ let word = "";
+ do {
+ const match = fragment.exec(argument);
+ if (!match) return undefined;
+ const [raw, single, double, escaped] = match;
+ if (double !== undefined)
+ word += double.replace(/\\\n|\\([$\x60"\\])/gu, "$1");
+ else if (escaped !== "\n") word += single ?? escaped ?? raw;
+ } while (fragment.lastIndex < argument.length);
+ return word;
+}
+
+function unwrapCommand(command: string) {
const normalized = command.trim();
const powershell =
process.platform === "win32"
@@ -159,16 +179,20 @@ function unwrapCommand(command: string): string {
powershell[1] ?? powershell[2]!,
)
) {
- return powershell[4]!;
+ return { text: powershell[4]! };
}
const match =
- /^(?:\/(?:usr\/)?bin\/)?(?:zsh|bash|sh)\s+-[a-z]*c[a-z]*\s+(['"])(.*)$/su.exec(
+ /^((?:\/(?:usr\/)?bin\/)?(?:zsh|bash|sh))\s+-[a-z]*c[a-z]*\s+(["'].*)$/su.exec(
normalized,
);
- if (match === null) return normalized;
- const quote = match[1]!;
- const script = match[2]!;
- return script.endsWith(quote) ? script.slice(0, -1) : script;
+ if (match === null) return { text: normalized, script: command };
+ const argument = match[2]!;
+ const decoded = literalPosixWord(argument);
+ return {
+ text: argument.slice(1, argument.endsWith(argument[0]!) ? -1 : undefined),
+ script: decoded,
+ recordedShell: decoded === undefined ? undefined : match[1],
+ };
}
export function scanActivityFromSessionEvent(
@@ -461,36 +485,40 @@ function commandRepositoryPaths(
repository: string,
shell: unknown,
): string[] {
- const escapedBlanks =
- typeof shell === "string" &&
- POSIX_SHELL.test(shell) &&
- /\\[ \t]/u.test(command);
+ const posixShell = typeof shell === "string" && POSIX_SHELL.test(shell);
const paths = new Set<string>();
const roots = [
"$CODEX_SECURITY_REPOSITORY",
"${CODEX_SECURITY_REPOSITORY}",
repository,
].map((root) => repositoryPathMatcher(root));
- for (const token of command.match(
- escapedBlanks ? POSIX_TOKEN : SHELL_TOKEN,
- ) ?? []) {
+ for (const token of command.match(posixShell ? POSIX_TOKEN : SHELL_TOKEN) ??
+ []) {
let value = token;
- const singleQuoted = value.startsWith("'") && value.endsWith("'");
- 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,
- "",
+ const first = token[0]!;
+ const quote =
+ (first === "'" || first === '"') && token.endsWith(first)
+ ? first
+ : undefined;
+ const hasQuote = /['"]/u.test(token);
+ const posixLiteral = posixShell && (hasQuote || /\\[ \t]/u.test(token));
+ if (posixLiteral) {
+ // Unquoted paths with escaped blanks only support blank escapes.
+ if (!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(
+ token,
+ )?.[1];
+ const literal = literalPosixWord(
+ variable ? token.replace(variable, "") : token,
);
- if (/["'\\\x60$()*?\[{}]/u.test(operand)) continue;
- } else if (quoted) value = value.slice(1, -1);
- const raw = value.replaceAll('\\"', '"');
+ if (literal === undefined || literal.startsWith("$")) continue;
+ value = (variable ?? "") + literal;
+ } else if (quote) value = value.slice(1, -1);
+ const raw = posixLiteral ? value : value.replaceAll('\\"', '"');
value = raw.replaceAll("\\", "/");
- if (process.platform === "win32" && !singleQuoted) {
+ if (process.platform === "win32" && quote !== "'") {
value = value.replace(
/^\$(?:env:CODEX_SECURITY_REPOSITORY|\{env:CODEX_SECURITY_REPOSITORY\})\//iu,
"$CODEX_SECURITY_REPOSITORY/",
@@ -499,20 +527,17 @@ function commandRepositoryPaths(
for (const relativeTo of roots) {
const relative = relativeTo(raw, value);
if (relative === null) continue;
- const path = escaped
- ? relative
- : relative
- .replace(/["'\r\n].*$/u, "")
- .trim()
- .replace(/\/+$/u, "")
- .replace(/:\d+(?::\d+)?$/u, "");
- if (
- path !== "" &&
- !path.startsWith("../") &&
- !path.split("/").includes("..")
- ) {
- paths.add(path);
+ // Quotes, edge whitespace and colon-number suffixes can be filename data.
+ let path = relative;
+ if (!posixLiteral) {
+ // An opposite quote inside a quoted filename is literal data.
+ const boundary =
+ quote && !path.includes(quote) ? /[\r\n].*$/u : /["'\r\n].*$/u;
+ path = path.replace(boundary, "").trim();
}
+ path = path.replace(/\/+$/u, "");
+ if (!posixLiteral) path = path.replace(/:\d+(?::\d+)?$/u, "");
+ if (path !== "" && !path.split("/").includes("..")) paths.add(path);
}
if (paths.size >= MAX_ACTIVITY_PATHS) break;
}
diff --git a/sdk/typescript/tests-ts/scan-activity.test.ts b/sdk/typescript/tests-ts/scan-activity.test.ts
index f9773a6dd..4c07f371c 100644
--- a/sdk/typescript/tests-ts/scan-activity.test.ts
+++ b/sdk/typescript/tests-ts/scan-activity.test.ts
@@ -47,6 +47,31 @@ function sessionEvent(
return { type: "response_item", payload };
}
+function expectCommandPaths(
+ cmd: string,
+ shell: string | undefined,
+ paths: readonly string[],
+ repository = "/repo",
+) {
+ const args = { cmd, shell };
+ const live = {
+ type: "item.started",
+ item: { id: "live", type: "command_execution", command: cmd, shell },
+ };
+ const saved = sessionEvent({
+ type: "function_call",
+ name: "exec_command",
+ call_id: "saved",
+ arguments: JSON.stringify(args),
+ });
+ for (const activity of [
+ scanActivityFromEvent(live, repository),
+ scanActivityFromEvent(toolEvent("exec", args), repository),
+ scanActivityFromSessionEvent(saved, repository),
+ ])
+ expect(activity?.paths).toEqual([...paths]);
+}
+
function commandActivityCase(
_name: string,
command: string,
@@ -59,6 +84,39 @@ function commandActivityCase(
}
describe("scan activity", () => {
+ test.each([
+ [
+ '/bin/sh -c "cat \\"/repo/src/O\'Brien.ts\\""',
+ undefined,
+ ["src/O'Brien.ts"],
+ ],
+ [
+ '/bin/sh -c \'cat "$CODEX_SECURITY_REPOSITORY/src/O\'"\'Brien.ts\\""',
+ undefined,
+ ["src/O'Brien.ts"],
+ ],
+ ['cat "/repo/src/O\'Brien.ts"', undefined, ["src/O'Brien.ts"]],
+ ['cat "/repo/src/O\'Brien.ts"', "sh", ["src/O'Brien.ts"]],
+ [`cat '/repo/src/O'"'"'Brien.ts'`, "sh", ["src/O'Brien.ts"]],
+ ["cat /repo/src/O\\'Brien.ts", "sh", ["src/O'Brien.ts"]],
+ [`cat '/repo/src/say "hello".ts'`, "sh", ['src/say "hello".ts']],
+ ['cat "/repo/ leading.ts "', "/bin/bash", [" leading.ts "]],
+ [
+ 'cat "$CODEX_SECURITY_REPOSITORY/src/O\'Brien.ts"',
+ "/bin/zsh",
+ ["src/O'Brien.ts"],
+ ],
+ ["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"]],
+ ["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"]],
+ ] as const)(
+ "attributes quoted filenames in live and saved commands: %s (%p)",
+ (cmd, shell, paths) => expectCommandPaths(cmd, shell, paths),
+ );
+
test.each([
[
"cat $CODEX_SECURITY_REPOSITORY/src/my\\ file.ts",
@@ -79,34 +137,8 @@ describe("scan activity", () => {
] 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]);
+ for (const shell of ["sh", "/bin/bash", "/bin/zsh"])
+ expectCommandPaths(cmd, shell, paths, repository);
},
);