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

1.7 KB
Raw Blame
import { gitText } from "./support/shell.js";
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, expect, test } from "bun:test";
import {
  DiffTarget,
  normalizeTarget,
  validateCommittedDiffCheckout,
} from "../src/targets.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";

const { temporaryDirectory, cleanup } = createApiTestFixtures(
  "codex-security-large-targets-",
);

afterEach(cleanup);

function git(repo: string, ...args: string[]): string {
  return gitText(args, { cwd: repo, maxBuffer: Infinity }).trim();
}

test("validates committed diffs with tracked-file output larger than 1 MB", async () => {
  const root = await temporaryDirectory();
  const repo = join(root, "repo");
  await mkdir(repo);

  git(repo, "init", "-b", "main");
  git(repo, "config", "user.email", "[email protected]");
  git(repo, "config", "user.name", "Test");

  const trackedDirectory = join(repo, "tracked");
  await mkdir(trackedDirectory);
  // Exceed the byte limit without creating paths that are too long on Windows.
  const utf8Stem = "界".repeat(60);
  await Promise.all(
    Array.from({ length: 6_000 }, (_, index) =>
      writeFile(
        join(
          trackedDirectory,
          `${index.toString().padStart(4, "0")}-${utf8Stem}.ts`,
        ),
        "",
      ),
    ),
  );

  git(repo, "add", ".");
  git(repo, "commit", "--quiet", "-m", "large tracked tree");

  const tracked = git(repo, "ls-files", "-t", "-z");
  expect(Buffer.byteLength(tracked, "utf8")).toBeGreaterThan(1024 * 1024);

  const target = await normalizeTarget(repo, DiffTarget.refs({ base: "HEAD" }));
  await expect(
    validateCommittedDiffCheckout(repo, target),
  ).resolves.toBeUndefined();
});