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
test(sdk): simplify saved-scan and credential fixtures (#2039)
Michael D'Angelobb599dc8b41d
diff --git a/sdk/typescript/tests-ts/cli-authentication.test.ts b/sdk/typescript/tests-ts/cli-authentication.test.tsindex a0555996b..612176e53 100644--- a/sdk/typescript/tests-ts/cli-authentication.test.ts+++ b/sdk/typescript/tests-ts/cli-authentication.test.ts@@ -25,7 +25,6 @@ import {} from "../src/index.js";import {codexSecurityCredentialAllowsAmbientImport,- prepareCodexSecurityCredentialHome,setCodexSecurityCredentialLogout,} from "../src/runtime.js";import {@@ -83,6 +82,12 @@ function dependencies(});}+async function createCredentialHome() {+ const home = join(stateDirectory, "codex-home");+ await mkdir(home, { mode: 0o700 });+ return home;+}+describe("CLI authentication", () => {test("delegates login and logout without overriding managed credential storage", async () => {const cases = [@@ -108,11 +113,7 @@ describe("CLI authentication", () => {});test("uses the same stable credential home for login, status, and logout", async () => {- const expectedHome = await realpath(- await prepareCodexSecurityCredentialHome({- CODEX_SECURITY_STATE_DIR: stateDirectory,- }),- );+ const expectedHome = join(stateDirectory, "codex-home");for (const argv of [["login"], ["login", "status"], ["logout"]] as const) {const { runCli } = createCliTest(main);@@ -1261,9 +1262,7 @@ describe("CLI authentication", () => {'{"auth_mode":"chatgpt"}\n',);- const credentialHome = await prepareCodexSecurityCredentialHome({- CODEX_SECURITY_STATE_DIR: stateDirectory,- });+ const credentialHome = await createCredentialHome();await setCodexSecurityCredentialLogout(credentialHome, true);const { runCli } = createCliTest(main);@@ -1840,8 +1839,7 @@ describe("skill authentication", () => {CODEX_HOME: join(stateDirectory, "ambient"),CODEX_SECURITY_STATE_DIR: stateDirectory,};- const credentialHome =- await prepareCodexSecurityCredentialHome(environment);+ const credentialHome = await createCredentialHome();await writeFile(join(credentialHome, "auth.json"),JSON.stringify({@@ -1915,9 +1913,7 @@ describe("skill authentication", () => {await mkdir(repository);const ambientHome = join(stateDirectory, "ambient");await mkdir(ambientHome);- const credentialHome = await prepareCodexSecurityCredentialHome({- CODEX_SECURITY_STATE_DIR: stateDirectory,- });+ const credentialHome = await createCredentialHome();await writeFile(join(credentialHome, "config.toml"),[@@ -2089,8 +2085,7 @@ describe("skill authentication", () => {CODEX_SECURITY_STATE_DIR: stateDirectory,};await mkdir(ambientHome);- const credentialHome =- await prepareCodexSecurityCredentialHome(environment);+ const credentialHome = join(stateDirectory, "codex-home");const stored = JSON.stringify({auth_mode: "apikey",OPENAI_API_KEY: "SYNTHETIC_STORED_KEY",@@ -2134,7 +2129,7 @@ describe("skill authentication", () => {? { OPENAI_API_KEY: "SYNTHETIC_SESSION_KEY" }: {}),};- const home = await prepareCodexSecurityCredentialHome(environment);+ const home = await createCredentialHome();const sourceHome =authSource === "shared" ? home : environment.CODEX_HOME;await mkdir(environment.CODEX_HOME, { recursive: true });@@ -2279,7 +2274,7 @@ describe("skill authentication", () => {CODEX_HOME: join(stateDirectory, "ambient"),CODEX_SECURITY_STATE_DIR: stateDirectory,};- const home = await prepareCodexSecurityCredentialHome(environment);+ const home = join(stateDirectory, "codex-home");const preload = join(stateDirectory, "native-status.mjs");const marker = join(home, "status-home");await writeFile(diff --git a/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts b/sdk/typescript/tests-ts/saved-scan-bootstrap.test.tsindex 67e958266..840351795 100644--- a/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts+++ b/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts@@ -26,6 +26,7 @@ import { screeningPairSlot } from "../src/deduplication/deduplication-reviewer.jimport { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";import { createApiTestFixtures } from "./support/temporary-directories.js";import { rejecting } from "./support/errors.js";+import { scanRegistrationArguments } from "./support/workbench-command.js";import { runWorkbenchFixtureCommands } from "./support/workbench-fixtures.js";const { temporaryDirectory, cleanup } = createApiTestFixtures(@@ -33,7 +34,7 @@ const { temporaryDirectory, cleanup } = createApiTestFixtures();afterEach(cleanup);-async function fixture(git = false, separateSetupCommands = false) {+async function workspace(git = false) {const root = await temporaryDirectory();const repository = join(root, "repository");await mkdir(join(repository, "src"), { recursive: true });@@ -71,21 +72,15 @@ async function fixture(git = false, separateSetupCommands = false) {CODEX_SECURITY_STATE_DIR: join(root, "state"),};const python = await resolvePluginPython({ environment });+ return { root, repository, environment, python };+}++async function fixture(git = false, separateSetupCommands = false) {+ const base = await workspace(git);+ const { root, repository, environment, python } = base;const options = { environment, python, pluginRoot: PLUGIN_ROOT };- const registrationArguments = (scanDir: string) => [- "register-cli-scan",- "--repository",- repository,- "--scan-dir",- scanDir,- "--recipe-json",- JSON.stringify({- config: {},- mode: "standard",- repository,- target: { kind: "repository", paths: [] },- }),- ];+ const registrationArguments = (scanDir: string) =>+ scanRegistrationArguments(repository, scanDir);async function scan(name: string, registered?: JsonObject) {const scanDir = join(root, name);if (registered === undefined) {@@ -199,10 +194,7 @@ async function fixture(git = false, separateSetupCommands = false) {})),};return {- root,- repository,- environment,- python,+ ...base,first,second,embedding,@@ -702,7 +694,7 @@ test("latest explains the explicit scan-ID fallback when history protects the hotest.skipIf(process.platform === "win32")("Python rediscovery keeps protecting the caller checkout when a saved target is supplied",async () => {- const f = await fixture();+ const f = await workspace();const caller = join(f.root, "caller-checkout");await mkdir(caller);await mkdir(join(caller, ".git"));@@ -778,7 +770,7 @@ test.skipIf(process.platform === "win32")(test.skipIf(process.platform === "win32")("an unrelated working directory does not exclude trusted Python",async () => {- const f = await fixture();+ const f = await workspace();const source = new URL("../src/runtime.ts", import.meta.url).href;for (const cwd of ["/", f.root]) {const result = spawnSync(@@ -809,7 +801,11 @@ for (const entry of [test.skipIf(process.platform === "win32")(`non-Git caller PATH is protected before Python probes through ${entry}`,async () => {- const f = await fixture();+ const saved =+ entry === "bootstrap" || entry === "helper"+ ? await fixture()+ : undefined;+ const f = saved ?? (await workspace());const caller = join(f.root, "non-git-caller");const callerBin = join(caller, "bin");const trustedBin = join(f.root, "trusted-bin");@@ -840,13 +836,14 @@ for (const entry of [import assert from "node:assert/strict";import { realpath } from "node:fs/promises";const { resolvePluginPython, runWorkbench } = await import(${JSON.stringify(runtime)});+ const scanId = ${JSON.stringify(saved?.first.scanId)};const options = { environment: process.env, protectedRoot: ${JSON.stringify(f.repository)}, pluginRoot: ${JSON.stringify(PLUGIN_ROOT)} };if (${JSON.stringify(entry)} === "bootstrap") {const { savedScanWorkbench } = await import(${JSON.stringify(bootstrap)});- const workbench = await savedScanWorkbench(${JSON.stringify(f.first.scanId)}, { ...options, currentDirectory: ${JSON.stringify(caller)} });- assert.equal((await workbench(["get-scan", "--scan-id", ${JSON.stringify(f.first.scanId)}])).scan.scanId, ${JSON.stringify(f.first.scanId)});+ const workbench = await savedScanWorkbench(scanId, { ...options, currentDirectory: ${JSON.stringify(caller)} });+ assert.equal((await workbench(["get-scan", "--scan-id", scanId])).scan.scanId, scanId);} else if (${JSON.stringify(entry)} === "helper") {- assert.equal((await runWorkbench(options, ["get-scan", "--scan-id", ${JSON.stringify(f.first.scanId)}])).scan.scanId, ${JSON.stringify(f.first.scanId)});+ assert.equal((await runWorkbench(options, ["get-scan", "--scan-id", scanId])).scan.scanId, scanId);} else {const selected = await resolvePluginPython({ ...options, managedRuntimeRoots: [], ...(${JSON.stringify(entry)} === "configured dot-name resolver" ? { configuredPath: ".python" } : {}) });assert.equal(await realpath(selected), await realpath(${JSON.stringify(f.python)}));@@ -876,7 +873,7 @@ for (const entry of [test.skipIf(process.platform === "win32")("Python discovery protects the enclosing checkout of a nested target",async () => {- const f = await fixture();+ const f = await workspace();const target = join(f.repository, "component");await mkdir(target);await mkdir(join(f.repository, ".git"));@@ -1057,7 +1054,8 @@ test.skipIf(process.platform === "win32")(test.skipIf(process.platform === "win32")("legacy workflow state resolves its bound target before Python discovery",async () => {- const f = await fixture();+ const f = await workspace();+ const scanId = "11111111-1111-4111-8111-111111111111";const state = join(f.root, "legacy-state");await mkdir(state);const db = new Database(join(state, "workbench.sqlite3"));@@ -1065,13 +1063,10 @@ test.skipIf(process.platform === "win32")(db.exec("CREATE TABLE scans (id TEXT PRIMARY KEY, target_path TEXT NOT NULL); CREATE TABLE finding_workflows (id TEXT PRIMARY KEY, state_json TEXT NOT NULL)",);- db.query("INSERT INTO scans VALUES (?, ?)").run(- f.first.scanId,- f.repository,- );+ db.query("INSERT INTO scans VALUES (?, ?)").run(scanId, f.repository);db.query("INSERT INTO finding_workflows VALUES (?, ?)").run("legacy-workflow",- JSON.stringify({ scanId: f.first.scanId }),+ JSON.stringify({ scanId }),);} finally {db.close();