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
import { runNodePython } from "./support/python-probe.js";
import { useTestPython } from "./support/python.js";
import { nodeCommand } from "./support/shell.js";
import { windowsHelperFixture } from "./windows-helper-command.js";
import { createTemporaryDirectoriesSync } from "./support/temporary-directories.js";
import { git } from "./git-fixture.js";
import { spawnSync } from "node:child_process";
import {
chmodSync,
existsSync,
mkdirSync,
readFileSync,
realpathSync,
rmSync,
symlinkSync,
writeFileSync,
} from "node:fs";
import { basename, dirname, join } from "node:path";
import { afterEach, expect, test } from "bun:test";
import { PLUGIN_ROOT } from "./plugin-root.js";
const pythonExecutable = useTestPython();
const temporaryRoots = createTemporaryDirectoriesSync(true);
const testPosix = process.platform === "win32" ? test.skip : test;
afterEach(temporaryRoots.cleanup);
test("diff previews stay inside the selected repository", () => {
const root = temporaryRoots.create("codex-security-diff-rank-");
const repository = join(root, "repository");
const nested = join(repository, "src", "nested");
mkdirSync(nested, { recursive: true });
git(repository, "init", "-q");
git(repository, "config", "--local", "core.autocrlf", "false");
writeFileSync(join(repository, "src", "handler.py"), "value = 1\n");
writeFileSync(join(repository, "src", "deleted.py"), "removed = True\n");
writeFileSync(join(repository, "src", "entry.py"), "handler.py");
writeFileSync(join(nested, "linked.py"), "value = 1\n");
git(repository, "add", ".");
const originalLink = git(repository, "hash-object", "src/entry.py");
git(
repository,
"update-index",
"--cacheinfo",
`120000,${originalLink},src/entry.py`,
);
git(repository, "commit", "-qm", "base");
const base = git(repository, "rev-parse", "HEAD");
writeFileSync(join(repository, "src", "handler.py"), "value = 2\n");
writeFileSync(join(repository, "src", "entry.py"), "nested/linked.py");
writeFileSync(join(nested, "linked.py"), "value = 2\n");
rmSync(join(repository, "src", "deleted.py"));
git(repository, "add", ".");
const updatedLink = git(repository, "hash-object", "src/entry.py");
git(
repository,
"update-index",
"--cacheinfo",
`120000,${updatedLink},src/entry.py`,
);
git(repository, "commit", "-qm", "selected changes");
const head = git(repository, "rev-parse", "HEAD");
const externalFixture = join(root, "synthetic-fixture");
mkdirSync(externalFixture);
writeFileSync(join(externalFixture, "linked.py"), "synthetic = True\n");
rmSync(nested, { recursive: true });
symlinkSync(externalFixture, nested, "junction");
const output = join(root, "rank-input.jsonl");
const result = spawnSync(
nodeCommand().command,
[
join(PLUGIN_ROOT, "mcp", "helpers.mjs"),
"make-diff-rank-input",
"--repo",
repository,
"--base",
base,
"--head",
head,
"--mode",
"local-patch",
"--out",
output,
],
{ encoding: "utf8" },
);
expect(result.status, result.stderr).toBe(0);
const rows = readFileSync(output, "utf8")
.trim()
.split("\n")
.map((row) => JSON.parse(row) as { path: string; preview: string });
expect(rows.map((row) => row.path)).toEqual([
"src/deleted.py",
"src/entry.py",
"src/handler.py",
"src/nested/linked.py",
]);
expect(rows.find((row) => row.path === "src/handler.py")?.preview).toBe(
"value = 2",
);
expect(rows.find((row) => row.path === "src/nested/linked.py")?.preview).toBe(
"",
);
});
test("preserves Unicode Git paths and legacy-encoded commit metadata", () => {
const root = temporaryRoots.create("codex-security-diff-rank-unicode-");
const repository = join(root, "repository-漢字");
const source = join(repository, "src", "変更.py");
mkdirSync(join(repository, "src"), { recursive: true });
git(repository, "init", "-q");
git(repository, "config", "--local", "core.autocrlf", "false");
writeFileSync(source, "value = 1\n");
git(repository, "add", ".");
git(repository, "commit", "-qm", "base");
const base = git(repository, "rev-parse", "HEAD");
writeFileSync(source, "value = 2\n");
git(repository, "add", ".");
git(repository, "commit", "-qm", "変更");
const head = git(repository, "rev-parse", "HEAD");
const legacyMessage = join(root, "legacy-message");
writeFileSync(legacyMessage, Buffer.from("café\n", "latin1"));
git(
repository,
"-c",
"i18n.commitEncoding=ISO-8859-1",
"commit",
"--allow-empty",
"-q",
"-F",
legacyMessage,
);
const legacyHead = git(repository, "rev-parse", "HEAD");
const output = join(root, "rank-input.jsonl");
const rank = spawnSync(
nodeCommand().command,
[
join(PLUGIN_ROOT, "mcp", "helpers.mjs"),
"make-diff-rank-input",
"--repo",
repository,
"--base",
base,
"--head",
head,
"--out",
output,
],
{ encoding: "utf8" },
);
const probeSource = [
"import json, pathlib, sys",
"sys.path.insert(0, sys.argv[1])",
"import workbench_target as target",
"import workbench_db as db",
"repo = pathlib.Path(sys.argv[2])",
"root, pathspec = target.git_worktree_context(repo)",
"metadata = target.git_target_metadata(repo)",
"diff = db.require_diff_target(repo, 'commit', None, sys.argv[3], None)",
"print(json.dumps({'root': str(root), 'pathspec': pathspec, 'subject': metadata['commitSubject'], 'diff': diff}))",
].join("\n");
const python = pythonExecutable();
expect(python).not.toBeNull();
const probe = runNodePython(python!, [
"-c",
probeSource,
join(PLUGIN_ROOT, "scripts"),
repository,
legacyHead,
]);
expect(rank.status, `${rank.stderr}\n${String(rank.error ?? "")}`).toBe(0);
expect(
readFileSync(output, "utf8")
.trimEnd()
.split("\n")
.map((row) => JSON.parse(row) as { path: string; preview: string })
.map(({ path, preview }) => ({ path, preview })),
).toEqual([{ path: "src/変更.py", preview: "value = 2" }]);
expect(probe.status, probe.stderr).toBe(0);
const target = JSON.parse(probe.stdout) as {
root: string;
pathspec: string;
subject: string;
diff: { kind: string; baseRevision: string; headRevision: string };
};
expect(basename(target.root)).toBe("repository-漢字");
expect(target).toMatchObject({
pathspec: ".",
subject: "café",
diff: { kind: "commit", baseRevision: head, headRevision: legacyHead },
});
});
testPosix(
"uses only the host-selected Git executable for rank and inventory helpers",
() => {
const root = temporaryRoots.create("codex-security-host-git-");
const repository = join(root, "repository");
const shimDirectory = join(repository, "tools");
const shim = join(shimDirectory, "git");
const externalBin = join(root, "external-bin");
const ripgrep = join(externalBin, "rg");
const marker = join(root, "shim-ran");
mkdirSync(shimDirectory, { recursive: true });
mkdirSync(externalBin);
git(repository, "init", "-q");
git(repository, "config", "--local", "core.autocrlf", "false");
writeFileSync(join(repository, "source.py"), "value = 1\n");
git(repository, "add", ".");
git(repository, "commit", "-qm", "base");
const base = git(repository, "rev-parse", "HEAD");
writeFileSync(join(repository, "source.py"), "value = 2\n");
git(repository, "add", ".");
git(repository, "commit", "-qm", "head");
const head = git(repository, "rev-parse", "HEAD");
writeFileSync(join(repository, ".gitignore"), "source.py\n");
writeFileSync(shim, '#!/bin/sh\n: > "$GIT_SHIM_MARKER"\nexit 99\n');
chmodSync(shim, 0o700);
symlinkSync(shim, join(externalBin, "git"));
writeFileSync(ripgrep, "#!/bin/sh\nexit 0\n");
chmodSync(ripgrep, 0o700);
const hostGit = Bun.which("git");
expect(hostGit).not.toBeNull();
const trustedGit = join(
realpathSync(dirname(hostGit!)),
basename(hostGit!),
);
const output = join(root, "output");
const run = (script: string, args: string[], binding = trustedGit) =>
spawnSync(
nodeCommand().command,
[
join(PLUGIN_ROOT, "mcp", "helpers.mjs"),
...(script === "generate-in-scope-files" ? [script] : []),
...args,
"--repo",
repository,
"--out",
output,
],
{
encoding: "utf8",
env: {
...process.env,
PATH: `${externalBin}:${process.env["PATH"] ?? ""}`,
GIT_SHIM_MARKER: marker,
CODEX_SECURITY_GIT: binding,
},
},
);
const rankArguments = [
"make-diff-rank-input",
"--base",
base,
"--head",
head,
];
const unavailable = run("rank-input", rankArguments, "");
expect(unavailable.status).not.toBe(0);
const rejected = run("rank-input", rankArguments, shim);
expect(rejected.status).not.toBe(0);
expect(rejected.stderr).toContain("outside the protected repository");
const rank = run("rank-input", rankArguments);
expect(rank.status, rank.stderr).toBe(0);
for (const diff of [true, false]) {
const inventory = run("generate-in-scope-files", [
"--scope",
".",
...(diff ? ["--diff-base", base, "--diff-head", head] : []),
]);
expect(inventory.status, inventory.stderr).toBe(0);
expect(readFileSync(output, "utf8")).toBe(
diff ? "source.py\n" : "./source.py\n",
);
}
expect(existsSync(marker)).toBe(false);
},
);
for (const mode of ["revisions", "local-patch"] as const)
test.skipIf(process.platform !== "win32")(
`documented Windows ${mode} ranking command preserves literal paths with resolved revisions`,
async () => {
const root = temporaryRoots.create("codex-security-diff-docs-");
const repository = join(root, "repository %USERNAME% !EXPAND! 雪's");
const discovery = join(root, "discovery %USERNAME% !EXPAND! 雪's");
mkdirSync(repository);
mkdirSync(discovery);
git(repository, "init", "-q");
const source = join(repository, "source.py");
writeFileSync(source, "before\n");
git(repository, "add", ".");
git(repository, "commit", "-qm", "Fixture base");
const base = "fixture-%USERNAME%-!EXPAND!-é's";
git(repository, "branch", base);
writeFileSync(source, "after\n");
if (mode === "revisions") {
git(repository, "add", ".");
git(repository, "commit", "-qm", "Fixture head");
}
const launcher = windowsHelperFixture(root, {
CODEX_SECURITY_GIT: Bun.which("git")!,
});
for (const powershell of launcher.powershells) {
const result = await launcher.run(
powershell,
"skills/security-scan/references/scan-artifacts-and-ledger.md",
{
"<plugin_dir>": launcher.plugin,
"<repo_root>": repository,
"<discovery_dir>": discovery,
"<resolved_base_object_id>": git(
repository,
"rev-parse",
"--verify",
"--end-of-options",
`${base}^{commit}`,
),
"<resolved_head_object_id>": git(
repository,
"rev-parse",
"--verify",
"--end-of-options",
"HEAD^{commit}",
),
},
undefined,
root,
undefined,
mode === "revisions" ? 1 : 2,
);
expect(result.status, result.diagnostics).toBe(0);
expect(result.stdout, result.diagnostics).not.toContain(
"expanded-plugin-used",
);
expect(
JSON.parse(readFileSync(join(discovery, "rank_input.jsonl"), "utf8")),
).toMatchObject({ path: "source.py", preview: "after" });
}
},
);