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): exercise external import flag values at their parser (#2075)
Michael D'Angelo08763e603b9b
diff --git a/sdk/typescript/tests-ts/cli-external-imports-feature.test.ts b/sdk/typescript/tests-ts/cli-external-imports-feature.test.tsindex 3f4437552..d89de04ef 100644--- a/sdk/typescript/tests-ts/cli-external-imports-feature.test.ts+++ b/sdk/typescript/tests-ts/cli-external-imports-feature.test.ts@@ -33,36 +33,6 @@ const publishLocal = ["--yes",];-for (const value of [undefined, "", "0", "true"]) {- test(`external commands are absent from discovery without opt-in (${String(value)})`, async () => {- const environment = { CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: value };- for (const [group, command] of commands) {- const cli = createCliTest(main);- expect(- await cli.runCli([group!, "--help"], dependencies({ environment })),- ).toBe(0);- expect(cli.stdout.text()).not.toMatch(- new RegExp(`^\\s+${command}\\s`, "m"),- );- const schema = createCliTest(main);- expect(- await schema.runCli(- [group!, command!, "--schema"],- dependencies({ environment }),- ),- ).not.toBe(0);- }- const github = createCliTest(main);- expect(- await github.runCli(- ["import", "github", "--help"],- dependencies({ environment }),- ),- ).toBe(0);- expect(github.stdout.text()).not.toContain("publish findings");- });-}-test("disabled commands distinguish discovery requests from filter values", async () => {const deps = dependencies({ environment: {} });for (const flag of ["--schema", "--llms-full", "--help"]) {@@ -309,15 +279,13 @@ test("opt-in exposes the commands per invocation without affecting concurrent diexpect(new RegExp(`^\\s+${command}\\s`, "m").test(cli.stdout.text()),).toBe(enabled);- if (enabled) {- const help = createCliTest(main);- expect(- await help.runCli(- [group!, command!, "--schema"],- dependencies({ environment }),- ),- ).toBe(0);- }+ const schema = createCliTest(main);+ const status = await schema.runCli(+ [group!, command!, "--schema"],+ dependencies({ environment }),+ );+ if (enabled) expect(status).toBe(0);+ else expect(status).not.toBe(0);}}),);@@ -340,5 +308,7 @@ test("established scan publishing, status, scan import, and GitHub collection stexpect(cli.stdout.text()).toContain(`Usage: codex-security ${command.join(" ")} `,);+ if (command[0] === "import" && command[1] === "github")+ expect(cli.stdout.text()).not.toContain("publish findings");}});diff --git a/sdk/typescript/tests-ts/external-findings-publish.test.ts b/sdk/typescript/tests-ts/external-findings-publish.test.tsindex b7d0604cb..ba4ff139d 100644--- a/sdk/typescript/tests-ts/external-findings-publish.test.ts+++ b/sdk/typescript/tests-ts/external-findings-publish.test.ts@@ -61,41 +61,38 @@ const normalized = (id = "vendor-1", severity = "high") => ({},});-for (const value of [undefined, "", "0", "true", " 1 "]) {- test(`external publication stays disabled without exact opt-in (${JSON.stringify(value)})`, async () => {- const root = await mkdtemp(join(tmpdir(), "external-import-disabled-"));- cleanups.push(() => rm(root, { recursive: true, force: true }));- const state = join(root, "state");- let sideEffects = 0;- const disabled = {- environment: {- CODEX_SECURITY_STATE_DIR: state,- CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: value,- },- credentials: async () => {- sideEffects++;- throw new Error("Unexpected credential read");- },- fetch: async () => {- sideEffects++;- throw new Error("Unexpected Cloud request");- },- };- // The missing path would fail differently if the file reader ran first.- await expect(- prepareExternalPublication(join(root, "missing.json"), options, disabled),- ).rejects.toThrow("CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS=1");- await expect(- prepareNormalizedExternalPublication(- { read: 0, findings: [], excluded: [] },- options,- disabled,- ),- ).rejects.toThrow("CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS=1");- expect(sideEffects).toBe(0);- await expect(readdir(state)).rejects.toThrow("ENOENT");- });-}+test("external publication requires opt-in before reading input or accessing Cloud", async () => {+ const root = await mkdtemp(join(tmpdir(), "external-import-disabled-"));+ cleanups.push(() => rm(root, { recursive: true, force: true }));+ const state = join(root, "state");+ let sideEffects = 0;+ const disabled = {+ environment: {+ CODEX_SECURITY_STATE_DIR: state,+ },+ credentials: async () => {+ sideEffects++;+ throw new Error("Unexpected credential read");+ },+ fetch: async () => {+ sideEffects++;+ throw new Error("Unexpected Cloud request");+ },+ };+ // The missing path would fail differently if the file reader ran first.+ await expect(+ prepareExternalPublication(join(root, "missing.json"), options, disabled),+ ).rejects.toThrow("CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS=1");+ await expect(+ prepareNormalizedExternalPublication(+ { read: 0, findings: [], excluded: [] },+ options,+ disabled,+ ),+ ).rejects.toThrow("CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS=1");+ expect(sideEffects).toBe(0);+ await expect(readdir(state)).rejects.toThrow("ENOENT");+});test("concurrent external publications use their own opt-in environments", async () => {const f = await fixture();diff --git a/sdk/typescript/tests-ts/external-imports-feature.test.ts b/sdk/typescript/tests-ts/external-imports-feature.test.tsindex c0a778058..82e76b176 100644--- a/sdk/typescript/tests-ts/external-imports-feature.test.ts+++ b/sdk/typescript/tests-ts/external-imports-feature.test.ts@@ -1,94 +1,54 @@-import { spawnSync } from "node:child_process";-import { fileURLToPath } from "node:url";import { expect, test } from "bun:test";+import { externalImportsEnabled } from "../src/external-imports-feature.js";+import { runTestInSubprocess } from "./support/test-subprocess.js";-test.each(["win32", "linux", "darwin"])(- "external import opt-in follows %s environment key semantics",- (platform) => {- const result = spawnSync(- process.execPath,- [- "-e",- `-const { strict: assert } = await import("node:assert");-const { importGitHubAdvisories } = await import(process.argv[2]);-Object.defineProperty(process, "platform", { value: process.argv[1] });const flag = "CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS";-const environments = [- { Codex_Security_Enable_External_Imports: "1" },- { CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: "1" },- { Codex_Security_Enable_External_Imports: "0" },- { Codex_Security_Enable_External_Imports: "true" },- { Codex_Security_Enable_External_Imports: "1", CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: undefined },- { codex_security_enable_external_imports: "1", Codex_Security_Enable_External_Imports: "0" },- { codex_security_enable_external_imports: "0", Codex_Security_Enable_External_Imports: "1" },- Object.create({ CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: "1" }),- Object.assign(Object.create({ CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: "0" }), {- Codex_Security_Enable_External_Imports: "1",- }),- Object.defineProperty({}, flag, { value: "1" }),- Object.defineProperty({ [flag.toLowerCase()]: "1" }, flag, { value: "0" }),- Object.defineProperty({ [flag.toLowerCase()]: "1" }, flag, { value: undefined }),- {},-];-async function importAdvisories(environment) {- let requests = 0;- try {- const advisories = await importGitHubAdvisories(- { repository: "example/project" },- { environment, createGitHub: async () => ({ request: async () => {- requests++;- return { data: [], headers: {} };- } }) },- );- return { enabled: true, advisories, requests };- } catch (error) {- return { enabled: false, message: error.message, requests };+const mixed = "Codex_Security_Enable_External_Imports";+const lower = flag.toLowerCase();++test.each([undefined, "", "0", "true", " 1 ", "1"])(+ "requires exact opt-in for %p",+ (value) => {+ expect(externalImportsEnabled({ [flag]: value })).toBe(value === "1");+ },+);++test("resolves mixed-case and inherited settings on the host platform", () => {+ const windows = process.platform === "win32";+ for (const [environment, expected] of [+ [{ [mixed]: "1" }, windows],+ [{ [mixed]: "0" }, false],+ [{ [mixed]: "true" }, false],+ [{ [mixed]: "1", [flag]: undefined }, false],+ [{ [lower]: "1", [mixed]: "0" }, false],+ [{ [lower]: "0", [mixed]: "1" }, windows],+ [Object.create({ [flag]: "1" }), true],+ [Object.assign(Object.create({ [flag]: "0" }), { [mixed]: "1" }), false],+ [Object.defineProperty({}, flag, { value: "1" }), true],+ [Object.defineProperty({ [lower]: "1" }, flag, { value: "0" }), false],+ [+ Object.defineProperty({ [lower]: "1" }, flag, { value: undefined }),+ windows,+ ],+ [{}, false],+ ] as const) {+ expect(externalImportsEnabled(environment)).toBe(expected);}-}-const results = await Promise.all(environments.map(importAdvisories));-if (process.platform === "win32") {- const mixed = "Codex_Security_Enable_External_Imports";- for (const name of Object.keys(process.env)) if (name.toUpperCase() === flag) delete process.env[name];- for (const hostKey of [flag, mixed, flag.toLowerCase()]) {+});++const overlay = "prefers a caller's own Windows setting over process.env";+test.skipIf(process.platform !== "win32")(overlay, () => {+ if (runTestInSubprocess(import.meta.path, overlay)) return;+ for (const name of Object.keys(process.env))+ if (name.toUpperCase() === flag) delete process.env[name];+ for (const host of [flag, mixed, lower]) {for (const own of ["0", "1"]) {- process.env[hostKey] = own === "0" ? "1" : "0";- const overlay = Object.create(process.env, { [mixed]: { value: own, enumerable: true } });- assert.equal((await importAdvisories(overlay)).requests, Number(own), hostKey);+ process.env[host] = own === "0" ? "1" : "0";+ const environment = Object.create(process.env, {+ [mixed]: { value: own, enumerable: true },+ });+ expect(externalImportsEnabled(environment)).toBe(own === "1");}- delete process.env[hostKey];+ delete process.env[host];}-}-console.log(JSON.stringify(results));-`,- platform,- fileURLToPath(new URL("../src/github-advisories.ts", import.meta.url)),- ],- { encoding: "utf8" },- );- expect(result.status, result.stderr).toBe(0);- const enabled = { enabled: true, advisories: [], requests: 1 };- const disabled = {- enabled: false,- message: expect.stringContaining(- "CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS=1",- ),- requests: 0,- };- expect(JSON.parse(result.stdout)).toEqual([- platform === "win32" ? enabled : disabled,- enabled,- disabled,- disabled,- disabled,- disabled,- platform === "win32" ? enabled : disabled,- enabled,- disabled,- enabled,- disabled,- platform === "win32" ? enabled : disabled,- disabled,- ]);- },-);+});