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

7.3 KB
Raw Blame
import { spawnSync } from "node:child_process";
import { mkdtempSync, readFileSync, renameSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, test } from "bun:test";
import { git } from "./git-fixture.js";
import { bashCommand, writeSource } from "./support/shell.js";
import { workflow } from "./support/workflow.js";

const native = workflow("native-artifacts");
const prepare = native.jobs["prepare"]!;
const keyScript = readFileSync(
  new URL("../../../.github/actions/native-cache-key/key.sh", import.meta.url),
  "utf8",
);

test("native reuse hashes committed inputs with Git alone across platforms", () => {
  const root = mkdtempSync(join(tmpdir(), "native-cache-"));
  const repository = join(root, "repository");
  const source = "plugins/codex-security/native/src/lib.rs";
  const helper = "plugins/codex-security/mcp-app/src/native.ts";
  const proof = "plugins/codex-security/native/proof.mjs";
  const proofHelper = ".github/scripts/native-proof-helper.mjs";
  const write = (path: string, contents: string) =>
    writeSource(repository, path, contents);
  const runProof = () =>
    spawnSync(process.execPath, [proof], { cwd: repository }).status;
  write(source, "native source\n");
  write(helper, "helper source\n");
  write(proof, `import "../../../${proofHelper}";\n`);
  write(proofHelper, "process.exitCode = 0;\n");
  write("README.md", "documentation\n");
  git(repository, "init", "-q");
  const commit = () => {
    git(repository, "add", ".");
    git(repository, "commit", "-qm", "fixture");
  };
  const key = (
    week = "2026-43",
    run?: number,
    cached = "",
    admittedWeek = "0",
  ) => {
    const result = spawnSync(
      Bun.which(bashCommand())!,
      [
        "--noprofile",
        "--norc",
        "-c",
        `git() { "$TEST_GIT" "$@"; }\ndate() { printf '%s\\n' "$TEST_WEEK"; }\ntimeout() { printf '%s\\n' "$TEST_CACHED"; }\n${keyScript}`,
        "native-cache-key",
        ...(run === undefined ? [] : ["cold-group"]),
      ],
      {
        cwd: repository,
        encoding: "utf8",
        env: {
          ...process.env,
          // macOS does not ship GNU sha256sum. Only Git and Bash builtins are
          // available here; the workflow's date command is fixed for this test.
          PATH: root,
          TEST_GIT: Bun.which("git")!,
          TEST_WEEK: week.replace("-", ""),
          NATIVE_CACHE_WEEK: admittedWeek,
          TEST_CACHED: cached,
          GITHUB_RUN_NUMBER: String(run),
        },
      },
    );
    expect(result.status, result.stderr).toBe(0);
    return result.stdout.trim();
  };
  try {
    commit();
    const initial = key();
    expect(initial).toMatch(/^key=native-universal-v1-2026-43-[a-f0-9]{40}$/);
    const fingerprint = initial.slice("key=".length);
    expect(runProof()).toBe(0);

    write("README.md", "updated documentation\n");
    for (const script of [
      "invoice-desk-target",
      "check-ci-results",
      "select-ci-checks",
      "test-ci-selection",
      "test-release-guide",
      "verify-pr-source",
    ]) {
      write(`.github/scripts/${script}.mjs`, "// CI only\n");
    }
    write(".github/scripts/test_check_plugin_source_compatibility.mts", "");
    write("plugins/codex-security/mcp-app/tests/support/new.ts", "fixture\n");
    commit();
    expect(key("2026-43", 4, fingerprint)).toBe("week=202643");

    let previous = initial;
    const expectNewKey = () => {
      commit();
      const next = key();
      expect(next).not.toBe(previous);
      previous = next;
    };
    write(proofHelper, "process.exitCode = 1;\n");
    expect(runProof()).toBe(1);
    expectNewKey();
    write(".github/scripts/new-proof-input.mts", "compile me\n");
    expectNewKey();
    write(source, "updated native source\n");
    expectNewKey();
    write(helper, "updated helper source\n");
    expectNewKey();
    renameSync(
      join(repository, source),
      join(repository, "plugins/codex-security/native/src/renamed.rs"),
    );
    expectNewKey();
    const nextWeek = previous
      .slice("key=".length)
      .replace("2026-43", "2026-44");
    const cold = key("2026-44", 15, fingerprint);
    expect(cold).toBe(`week=202644\ncold-group=${nextWeek}-0`);
    expect(key("2026-44", 16, fingerprint)).toBe(cold);
    const admittedWeek = cold.split("\n")[0]!.slice("week=".length);
    expect(key("2026-45", undefined, "", admittedWeek)).toBe(`key=${nextWeek}`);
    expect(key("2026-44", 17, fingerprint)).not.toBe(cold);
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});

test("only exact hits replace fresh native builds and cache failures fall back to proofs", () => {
  const restore = prepare.steps!.find((step) => step.id === "restore")!;
  expect(restore["continue-on-error"]).toBe(true);
  expect(restore.with).not.toHaveProperty("restore-keys");
  const upload = prepare.steps!.find((step) =>
    step.uses?.startsWith("actions/upload-artifact@"),
  )!;
  expect(upload.if).toBe("steps.restore.outputs.cache-hit == 'true'");
  expect(upload.with?.["if-no-files-found"]).toBe("error");
  expect(upload.with?.["name"]).toBe(
    "native-universal-${{ inputs.ref || github.sha }}",
  );
  for (const platform of ["unix", "windows", "musl"]) {
    expect(native.jobs[platform]).toMatchObject({
      needs: "prepare",
      if: "needs.prepare.outputs.cache-hit != 'true'",
    });
  }
});

test("only a successful fresh matrix can save a main-branch cache", () => {
  const bundle = native.jobs["bundle"]!;
  expect(bundle.needs).toEqual(["prepare", "unix", "windows", "musl"]);
  expect(bundle.if).toBe(
    "${{ !cancelled() && (needs.prepare.result != 'success' || needs.prepare.outputs.cache-hit != 'true') }}",
  );
  const save = bundle.steps!.find((step) =>
    step.uses?.startsWith("actions/cache/save@"),
  )!;
  expect(save.if).toBe(
    "github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'schedule') && (inputs.ref == '' || inputs.ref == github.sha)",
  );
  expect(save["continue-on-error"]).toBe(true);
});

test.each([
  ["success", "success", "success", "success", true],
  ["failure", "skipped", "skipped", "skipped", false],
  ["success", "success", "failure", "success", false],
  ["success", "success", "success", "skipped", false],
] as const)(
  "fresh native payload requires successful preparation and proofs (%s, %s, %s, %s)",
  (prepare, unix, windows, musl, passes) => {
    const guard = native.jobs["bundle"]!.steps![0]!.run!;
    const checked = spawnSync(bashCommand(), ["-c", guard], {
      encoding: "utf8",
      env: {
        ...process.env,
        PREPARE_RESULT: prepare,
        UNIX_RESULT: unix,
        WINDOWS_RESULT: windows,
        MUSL_RESULT: musl,
      },
    });
    expect(checked.status === 0, checked.stderr).toBe(passes);
  },
);

test("source CI opts in to native artifact reuse", () => {
  expect(native.on["workflow_call"]).toMatchObject({
    inputs: { "reuse-verified": { default: false } },
  });
  expect(workflow("node-ci").jobs["native"]?.with?.["reuse-verified"]).toBe(
    true,
  );
  for (const caller of ["node-release", "container-publish", "container-ci"]) {
    const build = Object.values(workflow(caller).jobs).find(
      (job) => job.uses === "./.github/workflows/native-artifacts.yml",
    );
    expect(build).toBeDefined();
    expect(build!.with?.["reuse-verified"]).toBeUndefined();
  }
});