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

ci: pass the selected Invoice Desk target directly to the scan (#2467)

Michael D'Angelo28d25b80d354
diff --git a/.github/scripts/invoice-desk-target.mjs b/.github/scripts/invoice-desk-target.mjs
index fc01ecf2e..775db0def 100644
--- a/.github/scripts/invoice-desk-target.mjs
+++ b/.github/scripts/invoice-desk-target.mjs
@@ -11,35 +11,33 @@ function eligible(pr, repository, sha) {
);
}
-export async function resolveScanTargets(context, getPullRequest) {
- const { eventName, event, repository, sha, target } = context;
- // After approval, keep the scan bound to its original PR and source commit.
- if (target) {
- if (target.pr === 0) return [{ pr: 0, sha }];
- const pr = await getPullRequest(String(target.pr));
- return eligible(pr, repository, target.sha)
- ? [{ pr: pr.number, sha: target.sha }]
- : [];
+export async function resolveScanTarget(context, getPullRequest) {
+ const { eventName, event, repository, sha } = context;
+ // After approval, recheck the same PR and revision selected by the target job.
+ let target = context.target;
+ if (!target) {
+ if (eventName === "schedule") return { pr: 0, sha };
+ if (eventName !== "workflow_dispatch") return null;
+ const { pr_number: number = "", source_sha: sourceSha = "" } =
+ event.inputs ?? {};
+ if (!number && !sourceSha) return { pr: 0, sha };
+ if (!/^[1-9][0-9]*$/.test(number) || !/^[a-f0-9]{40}$/.test(sourceSha)) {
+ throw new Error(
+ "PR scans require both a positive pr_number and a full source_sha.",
+ );
+ }
+ target = { pr: number, sha: sourceSha };
}
- if (eventName === "schedule") return [{ pr: 0, sha }];
- if (eventName !== "workflow_dispatch") return [];
- const { pr_number: number = "", source_sha: sourceSha = "" } =
- event.inputs ?? {};
- if (!number && !sourceSha) return [{ pr: 0, sha }];
- if (!/^[1-9][0-9]*$/.test(number) || !/^[a-f0-9]{40}$/.test(sourceSha)) {
- throw new Error(
- "PR scans require both a positive pr_number and a full source_sha.",
- );
- }
-
- const pr = await getPullRequest(number);
- if (!eligible(pr, repository, sourceSha)) return [];
- return [{ pr: pr.number, sha: sourceSha }];
+ if (target.pr === 0) return { pr: 0, sha };
+ const pr = await getPullRequest(String(target.pr));
+ return eligible(pr, repository, target.sha)
+ ? { pr: pr.number, sha: target.sha }
+ : null;
}
if (isMain(import.meta.url)) {
const repository = process.env.GITHUB_REPOSITORY;
- const targets = await resolveScanTargets(
+ const target = await resolveScanTarget(
{
eventName: process.env.GITHUB_EVENT_NAME,
event: JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, "utf8")),
@@ -62,16 +60,11 @@ if (isMain(import.meta.url)) {
);
appendFileSync(
process.env.GITHUB_OUTPUT,
- `targets=${JSON.stringify(targets)}\n`,
+ `sha=${target?.sha ?? ""}\npr=${target?.pr || ""}\n`,
);
- const message =
- targets.length === 0
- ? "Skipped: no eligible PR revision needs a scan."
- : targets
- .map(
- ({ pr, sha }) => `${pr ? `PR #${pr}` : "Main baseline"} at ${sha}`,
- )
- .join("\n");
+ const message = target
+ ? `${target.pr ? `PR #${target.pr}` : "Main baseline"} at ${target.sha}`
+ : "Skipped: no eligible PR revision needs a scan.";
console.log(message);
if (process.env.GITHUB_STEP_SUMMARY) {
appendFileSync(process.env.GITHUB_STEP_SUMMARY, message + "\n");
diff --git a/.github/scripts/invoice-desk-target.test.mjs b/.github/scripts/invoice-desk-target.test.mjs
index 8cce9de3d..216b12ad1 100644
--- a/.github/scripts/invoice-desk-target.test.mjs
+++ b/.github/scripts/invoice-desk-target.test.mjs
@@ -5,7 +5,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
-import { resolveScanTargets } from "./invoice-desk-target.mjs";
+import { resolveScanTarget } from "./invoice-desk-target.mjs";
const repository = "example/invoices";
const sourceSha = "1".repeat(40);
@@ -29,7 +29,7 @@ function pullRequest(overrides = {}) {
test("initial selection and approval recheck require the selected PR and revision to remain eligible", async () => {
const target = { pr: 7, sha: sourceSha };
- // The approval step must use its matrix target even if dispatch inputs differ.
+ // The approval step must use its original target even if dispatch inputs differ.
const recheck = {
...context,
target: { ...target },
@@ -37,21 +37,21 @@ test("initial selection and approval recheck require the selected PR and revisio
};
for (const request of [context, recheck]) {
for (const [overrides, expected] of [
- [{}, [target]],
+ [{}, target],
[
{
draft: true,
head: { sha: sourceSha, repo: { full_name: "contributor/invoices" } },
},
- [target],
+ target,
],
- [{ state: "closed" }, []],
- [{ base: { ref: "release", repo: { full_name: repository } } }, []],
- [{ base: { ref: "main", repo: { full_name: "other/invoices" } } }, []],
- [{ head: { sha: mainSha } }, []],
- [{ head: { sha: "3".repeat(40) } }, []],
+ [{ state: "closed" }, null],
+ [{ base: { ref: "release", repo: { full_name: repository } } }, null],
+ [{ base: { ref: "main", repo: { full_name: "other/invoices" } } }, null],
+ [{ head: { sha: mainSha } }, null],
+ [{ head: { sha: "3".repeat(40) } }, null],
]) {
- const actual = await resolveScanTargets(request, async (number) => {
+ const actual = await resolveScanTarget(request, async (number) => {
assert.equal(number, "7");
return pullRequest(overrides);
});
@@ -74,10 +74,10 @@ test("manual and scheduled baselines use the trusted workflow commit before and
{ ...scheduled, target: { ...target } },
]) {
assert.deepEqual(
- await resolveScanTargets(request, () =>
+ await resolveScanTarget(request, () =>
assert.fail("unexpected API call"),
),
- [target],
+ target,
);
}
});
@@ -90,7 +90,7 @@ test("incomplete or invalid PR inputs fail instead of falling back to a baseline
{ pr_number: "7", source_sha: "main" },
]) {
await assert.rejects(
- resolveScanTargets({ ...context, event: { inputs } }, () =>
+ resolveScanTarget({ ...context, event: { inputs } }, () =>
assert.fail("unexpected API call"),
),
/require both/,
@@ -114,10 +114,10 @@ test("PR events and completed PR workflows do not queue inference or query GitHu
},
]) {
assert.deepEqual(
- await resolveScanTargets({ ...context, ...trigger }, () =>
+ await resolveScanTarget({ ...context, ...trigger }, () =>
assert.fail("unexpected API call"),
),
- [],
+ null,
trigger.eventName,
);
}
@@ -128,7 +128,6 @@ test("resolver CLI emits the requested target without dispatching another workfl
t.after(() => rmSync(root, { recursive: true, force: true }));
const eventPath = join(root, "event.json");
const outputPath = join(root, "output.txt");
- writeFileSync(eventPath, JSON.stringify(context.event));
const scriptUrl = new URL("./invoice-desk-target.mjs", import.meta.url);
const script = `
import assert from "node:assert/strict";
@@ -143,23 +142,28 @@ test("resolver CLI emits the requested target without dispatching another workfl
process.argv[1] = ${JSON.stringify(fileURLToPath(scriptUrl))};
await import(${JSON.stringify(scriptUrl.href)});
`;
- const result = execFileSync(
- process.execPath,
- ["--input-type=module", "--eval", script],
- {
- encoding: "utf8",
- env: {
- GITHUB_REPOSITORY: repository,
- GITHUB_SHA: mainSha,
- GITHUB_EVENT_NAME: "workflow_dispatch",
- GITHUB_EVENT_PATH: eventPath,
- GITHUB_OUTPUT: outputPath,
+ writeFileSync(eventPath, JSON.stringify(context.event));
+ for (const [eventName, message, output] of [
+ ["workflow_dispatch", /PR #7/, `sha=${sourceSha}\npr=7\n`],
+ ["schedule", /Main baseline/, `sha=${mainSha}\npr=\n`],
+ ["pull_request", /Skipped/, "sha=\npr=\n"],
+ ]) {
+ writeFileSync(outputPath, "");
+ const result = execFileSync(
+ process.execPath,
+ ["--input-type=module", "--eval", script],
+ {
+ encoding: "utf8",
+ env: {
+ GITHUB_REPOSITORY: repository,
+ GITHUB_SHA: mainSha,
+ GITHUB_EVENT_NAME: eventName,
+ GITHUB_EVENT_PATH: eventPath,
+ GITHUB_OUTPUT: outputPath,
+ },
},
- },
- );
- assert.match(result, /PR #7/);
- assert.equal(
- readFileSync(outputPath, "utf8"),
- `targets=${JSON.stringify([{ pr: 7, sha: sourceSha }])}\n`,
- );
+ );
+ assert.match(result, message);
+ assert.equal(readFileSync(outputPath, "utf8"), output);
+ }
});
diff --git a/.github/workflows/invoice-desk-scan.yml b/.github/workflows/invoice-desk-scan.yml
index 310369ee0..3366b4b4c 100644
--- a/.github/workflows/invoice-desk-scan.yml
+++ b/.github/workflows/invoice-desk-scan.yml
@@ -34,7 +34,8 @@ jobs:
contents: read
pull-requests: read
outputs:
- targets: ${{ steps.target.outputs.targets }}
+ sha: ${{ steps.target.outputs.sha }}
+ pr: ${{ steps.target.outputs.pr }}
cache-hit: ${{ steps.cache.outputs.cache-hit }}
steps:
- name: Check out trusted workflow helpers
@@ -56,14 +57,14 @@ jobs:
# Populate only public package downloads, before any PR source or inference.
- name: Restore CLI package download cache
- if: steps.target.outputs.targets != '[]'
+ if: steps.target.outputs.sha != ''
id: cache
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ runner.temp }}/invoice-desk-npm-cache/_cacache
key: invoice-desk-npm-v1-${{ runner.os }}-${{ runner.arch }}-node24-cli${{ env.CODEX_SECURITY_VERSION }}
- name: Populate CLI package download cache
- if: steps.target.outputs.targets != '[]' && steps.cache.outputs.cache-hit != 'true'
+ if: steps.target.outputs.sha != '' && steps.cache.outputs.cache-hit != 'true'
working-directory: ${{ runner.temp }}
run: |
npm install --prefix "$RUNNER_TEMP/codex-security-cli" \
@@ -72,24 +73,20 @@ jobs:
--registry=https://registry.npmjs.org/ \
"@openai/codex-security@$CODEX_SECURITY_VERSION"
- name: Save CLI package download cache
- if: steps.target.outputs.targets != '[]' && steps.cache.outputs.cache-hit != 'true'
+ if: steps.target.outputs.sha != '' && steps.cache.outputs.cache-hit != 'true'
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ runner.temp }}/invoice-desk-npm-cache/_cacache
key: invoice-desk-npm-v1-${{ runner.os }}-${{ runner.arch }}-node24-cli${{ env.CODEX_SECURITY_VERSION }}
scan:
- name: "OpenAI source scan (${{ matrix.target.pr && format('PR #{0}', matrix.target.pr) || 'main' }})"
+ name: "OpenAI source scan (${{ needs.target.outputs.pr && format('PR #{0}', needs.target.outputs.pr) || 'main' }})"
needs: target
- if: needs.target.outputs.targets != '[]'
- strategy:
- fail-fast: false
- matrix:
- target: ${{ fromJSON(needs.target.outputs.targets) }}
+ if: needs.target.outputs.sha != ''
# Rejected requests must not cancel an eligible scan or its approval request.
# A delayed target job must not cancel a scan of a different revision.
concurrency:
- group: invoice-desk-scan-${{ matrix.target.pr && format('pr-{0}', matrix.target.pr) || 'main' }}-${{ matrix.target.sha }}
+ group: invoice-desk-scan-${{ needs.target.outputs.pr && format('pr-{0}', needs.target.outputs.pr) || 'main' }}-${{ needs.target.outputs.sha }}
cancel-in-progress: true
environment: codex-security-smoke-test
runs-on: ubuntu-24.04
@@ -99,8 +96,8 @@ jobs:
contents: read
pull-requests: read
env:
- SOURCE_SHA: ${{ matrix.target.sha }}
- PR_NUMBER: ${{ matrix.target.pr }}
+ SOURCE_SHA: ${{ needs.target.outputs.sha }}
+ PR_NUMBER: ${{ needs.target.outputs.pr || '0' }}
steps:
- name: Check out trusted workflow helpers
@@ -132,7 +129,7 @@ jobs:
- name: Check out the exact source commit
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
- ref: ${{ matrix.target.sha }}
+ ref: ${{ needs.target.outputs.sha }}
path: source
persist-credentials: false
- name: Extract committed application files without running PR code
@@ -151,7 +148,7 @@ jobs:
rm -rf -- "$GITHUB_WORKSPACE/source" "$GITHUB_WORKSPACE/trusted-workflow"
- name: Scan with OpenAI inference
id: scan
- if: steps.recheck.outputs.targets != '[]'
+ if: steps.recheck.outputs.sha != ''
working-directory: ${{ runner.temp }}/invoice-desk
shell: bash
env:
@@ -267,7 +264,7 @@ jobs:
if: ${{ !cancelled() && steps.scan.outputs.exit-code != '' }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
- name: invoice-desk-scan-${{ matrix.target.pr || 'main' }}-${{ github.run_id }}-${{ github.run_attempt }}
+ name: invoice-desk-scan-${{ needs.target.outputs.pr || 'main' }}-${{ github.run_id }}-${{ github.run_attempt }}
retention-days: 7
if-no-files-found: warn
path: |
diff --git a/sdk/typescript/tests-ts/invoice-desk-workflow.test.ts b/sdk/typescript/tests-ts/invoice-desk-workflow.test.ts
index aef6f7d48..ccb91725f 100644
--- a/sdk/typescript/tests-ts/invoice-desk-workflow.test.ts
+++ b/sdk/typescript/tests-ts/invoice-desk-workflow.test.ts
@@ -53,19 +53,19 @@ test("Invoice Desk cancels only duplicate scans of an eligible target revision",
const job = workflow.jobs.scan;
expect(job.needs).toBe("target");
const selected = new Function("needs", `return (${job.if});`);
- expect(selected({ target: { outputs: { targets: "[]" } } })).toBe(false);
+ expect(selected({ target: { outputs: { sha: "" } } })).toBe(false);
expect(
selected({
target: {
- outputs: { targets: JSON.stringify([{ pr: 7, sha: sourceSha }]) },
+ outputs: { pr: "7", sha: sourceSha },
},
}),
).toBe(true);
expect(job.concurrency["cancel-in-progress"]).toBe(true);
const group = (pr: number, sha: string) =>
job.concurrency.group.replace(/\$\{\{(.*?)\}\}/g, (_, expression: string) =>
- new Function("matrix", "format", `return (${expression});`)(
- { target: { pr, sha } },
+ new Function("needs", "format", `return (${expression});`)(
+ { target: { outputs: { pr: pr ? String(pr) : "", sha } } },
(template: string, value: unknown) =>
template.replace("{0}", String(value)),
),