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

refactor!: consolidate scan, workbench and release rules (#2452)

Michael D'Angelo884b7b657a1d
diff --git a/.github/scripts/release-progress.mjs b/.github/scripts/release-progress.mjs
index d1e254cf3..4bdebf455 100644
--- a/.github/scripts/release-progress.mjs
+++ b/.github/scripts/release-progress.mjs
@@ -269,6 +269,22 @@ async function awaitingEnvironment(github, run, environment) {
);
}
+async function progressBody(github, runs) {
+ const jobs = Object.fromEntries(
+ await Promise.all(
+ Object.entries(runs).map(async ([name, run]) => [
+ name,
+ await runJobs(github, run.id),
+ ]),
+ ),
+ );
+ const [awaitingNpm, awaitingContainer] = await Promise.all([
+ awaitingEnvironment(github, runs["node-release"], "npm"),
+ awaitingEnvironment(github, runs["node-github-release"], "container"),
+ ]);
+ return renderProgress(runs, jobs, awaitingNpm, awaitingContainer);
+}
+
export async function updateReleaseProgress(
github,
runId,
@@ -352,19 +368,7 @@ export async function updateReleaseProgress(
incoming.id >= last.id)))
)
runs[name] = incoming;
- const jobs = Object.fromEntries(
- await Promise.all(
- Object.entries(runs).map(async ([name, run]) => [
- name,
- await runJobs(github, run.id),
- ]),
- ),
- );
- const [awaitingNpm, awaitingContainer] = await Promise.all([
- awaitingEnvironment(github, runs["node-release"], "npm"),
- awaitingEnvironment(github, runs["node-github-release"], "container"),
- ]);
- const body = renderProgress(runs, jobs, awaitingNpm, awaitingContainer);
+ const body = await progressBody(github, runs);
if (previous?.body === body)
return { action: "unchanged", pull: pull.number };
await github.request(
@@ -383,18 +387,7 @@ export async function watchReleaseProgress(github, runId, wait = setTimeout) {
const run = await github.request("GET", `actions/runs/${runId}`);
const name = workflowName(run);
if (name === "node-ci" || !(await releaseCommit(github, run))) return;
- const jobs = await runJobs(github, run.id);
- const awaitingApproval = await awaitingEnvironment(
- github,
- run,
- name === "node-release" ? "npm" : "container",
- );
- const current = renderProgress(
- { [name]: run },
- { [name]: jobs },
- name === "node-release" && awaitingApproval,
- name === "node-github-release" && awaitingApproval,
- );
+ const current = await progressBody(github, { [name]: run });
if (current !== previous) {
await github.request(
"POST",
diff --git a/.github/scripts/test-verify-pr-source.mjs b/.github/scripts/test-verify-pr-source.mjs
index cf13639a3..d91e1031c 100644
--- a/.github/scripts/test-verify-pr-source.mjs
+++ b/.github/scripts/test-verify-pr-source.mjs
@@ -193,6 +193,31 @@ test("pending sources settle, genuine red remains red, and newer attempts cannot
}
});
+test("a cancelled metadata edit preserves a source failure without qualifying a success", async () => {
+ const cancelled = edit(5, { conclusion: "cancelled" });
+ const failed = run(2, { conclusion: "failure" });
+ const pending = run(2, { status: "in_progress", conclusion: null });
+ assert.equal(
+ await fixture(
+ [
+ [pending, cancelled],
+ [failed, cancelled],
+ ],
+ {
+ wait: true,
+ },
+ ).check(),
+ "failed",
+ );
+ for (const history of [
+ [run(2), cancelled],
+ [run(2, { conclusion: "cancelled" }), cancelled],
+ [failed, cancelled, edit(8)],
+ ]) {
+ assert.equal(await fixture([history]).check(), "full");
+ }
+});
+
test("retrying a failed edit cannot replace its full checks with older green results", async () => {
const retry = fixture([[run(2)]], { runAttempt: 2 });
assert.equal(await retry.check(), "full");
diff --git a/.github/scripts/verify-pr-source.mjs b/.github/scripts/verify-pr-source.mjs
index d494f7c70..4c840a305 100644
--- a/.github/scripts/verify-pr-source.mjs
+++ b/.github/scripts/verify-pr-source.mjs
@@ -107,7 +107,10 @@ function sourceCandidate(runs, { pr, sha, base }) {
return "full";
if (runs.some((r) => r.status !== "completed")) return "pending";
if (runs.every(successful)) return first;
- if (runs.every((r) => r.conclusion === "failure")) {
+ if (
+ first.conclusion === "failure" &&
+ later.every((r) => ["failure", "cancelled"].includes(r.conclusion))
+ ) {
console.log(`Failed source CI: ${first.html_url}/attempts/1`);
return "failed";
}
diff --git a/.github/workflows/node-release-cut.yml b/.github/workflows/node-release-cut.yml
index 532e3f57a..ef7a13dc1 100644
--- a/.github/workflows/node-release-cut.yml
+++ b/.github/workflows/node-release-cut.yml
@@ -147,7 +147,6 @@ jobs:
{
printf 'changed=true\n'
- printf 'version=%s\n' "$version"
printf 'tag=npm-v%s\n' "$version"
} >> "$GITHUB_OUTPUT"
diff --git a/.github/workflows/node-release-labels.yml b/.github/workflows/node-release-labels.yml
index ae3d7cb31..008bb9433 100644
--- a/.github/workflows/node-release-labels.yml
+++ b/.github/workflows/node-release-labels.yml
@@ -31,50 +31,9 @@ jobs:
run: |
set -euo pipefail
- release_note_label() {
- local type="$1" breaking_marker="$2" scope="$3"
- if [[ "$breaking_marker" == "!" ]]; then
- printf '%s\n' breaking-change
- return 0
- fi
-
- case "$type" in
- feat)
- printf '%s\n' enhancement
- ;;
- fix)
- printf '%s\n' bug
- ;;
- docs)
- printf '%s\n' documentation
- ;;
- release | test)
- printf '%s\n' skip-release-notes
- ;;
- chore)
- if [[ "$scope" == "(release)" ]]; then
- printf '%s\n' skip-release-notes
- fi
- ;;
- *)
- return 0
- ;;
- esac
- }
-
- title_sentinel="__CODEX_SECURITY_TITLE_END__"
- current_title="$(
- gh api "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER" \
- --jq '.title | @base64' |
- node --input-type=module --eval '
- import { readFileSync } from "node:fs";
- process.stdout.write(
- Buffer.from(readFileSync(0, "utf8"), "base64"),
- );
- ' &&
- printf '%s' "$title_sentinel"
- )"
- current_title="${current_title%"$title_sentinel"}"
+ # The suffix preserves trailing newlines through shell command substitution.
+ current_title="$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER" --jq '.title + "."')"
+ current_title="${current_title%.}"
conventional_title='^([a-z][a-z0-9-]*)(\([a-z0-9][a-z0-9._/-]*\))?(!)?:[ ]([^[:space:]].*[^[:space:]]|[^[:space:]])$'
if [[ "$current_title" == *$'\n'* ||
"$current_title" == *$'\r'* ||
@@ -82,10 +41,22 @@ jobs:
echo "Pull request title must follow <type>[optional scope][!]: <description>." >&2
exit 1
fi
- type="${BASH_REMATCH[1]}"
- scope="${BASH_REMATCH[2]:-}"
- breaking_marker="${BASH_REMATCH[3]:-}"
- label="$(release_note_label "$type" "$breaking_marker" "$scope")"
+ label=""
+ if [[ "${BASH_REMATCH[3]}" == "!" ]]; then
+ label=breaking-change
+ else
+ case "${BASH_REMATCH[1]}" in
+ feat) label=enhancement ;;
+ fix) label=bug ;;
+ docs) label=documentation ;;
+ release | test) label=skip-release-notes ;;
+ chore)
+ if [[ "${BASH_REMATCH[2]}" == "(release)" ]]; then
+ label=skip-release-notes
+ fi
+ ;;
+ esac
+ fi
current_labels="$(
gh api "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/labels" \
--jq '.[].name'
@@ -103,7 +74,7 @@ jobs:
label_already_present=false
manual_category_present=false
- bot_labels=""
+ bot_labels=()
while IFS= read -r current_label; do
case "$current_label" in
breaking-change | enhancement | bug | documentation | skip-release-notes)
@@ -122,24 +93,24 @@ jobs:
manual_category_present=true
fi
else
- bot_labels+="$current_label"$'\n'
+ bot_labels+=("$current_label")
fi
;;
esac
done <<< "$current_labels"
- while IFS= read -r current_label; do
- if [[ -z "$current_label" ]]; then
- continue
- fi
- if [[ "$manual_category_present" == true || "$current_label" != "$label" ]]; then
- gh api --method DELETE \
- "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/labels/$current_label" \
- --silent
- else
- label_already_present=true
- fi
- done <<< "$bot_labels"
+ # Bash 3.2 treats an empty array as unset under nounset.
+ if (( ${#bot_labels[@]} )); then
+ for current_label in "${bot_labels[@]}"; do
+ if [[ "$manual_category_present" == true || "$current_label" != "$label" ]]; then
+ gh api --method DELETE \
+ "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/labels/$current_label" \
+ --silent
+ else
+ label_already_present=true
+ fi
+ done
+ fi
if [[ -z "$label" ]]; then
echo "No automatic release-note category applies."
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 577d1f1b1..59eb6b124 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -46,6 +46,11 @@ plugin helper, then regenerate its SDK copy. Historical finding normalization is
maintained in the SDK.
`pnpm --dir sdk/typescript run types` checks freshness.
+The SDK build reads Deep Scan setting names from
+`plugins/codex-security/mcp-app/src/deep-scan-settings.ts`. If you build the
+SDK from a sparse checkout, include this file alongside the plugin defaults
+and severity rubric.
+
Model-based evaluations live in [`evals/`](evals/README.md), outside the plugin
source. CI runs deterministic triage checks and the Deep Scan iteration and
publication tests in the MCP suite.
diff --git a/docker/verify-container-release-version.sh b/docker/verify-container-release-version.sh
index c394be096..4a02bc717 100644
--- a/docker/verify-container-release-version.sh
+++ b/docker/verify-container-release-version.sh
@@ -26,14 +26,12 @@ if ! version_status="$(
if length == 0 or any(.[]; type != "array") then
error("Container package versions must contain at least one JSON array.")
else
- [.[][] | select(any(.metadata.container.tags[]?; . == $version))] as $existing |
- [.[][] | select(any(.metadata.container.tags[]?; . == ("sha-" + $sha)))] as $source |
+ [.[][] | select(any(.metadata.container.tags[]?;
+ . == $version or . == ("sha-" + $sha)))] as $immutable |
any(.[][]; any(.metadata.container.tags[]?;
stable_version > ($version | stable_version))) as $backfill |
- if (($existing + $source) | length) > 0 and (
- ($digest | test("^sha256:[a-fA-F0-9]{64}$") | not) or
- any(($existing + $source)[]; .name != $digest)
- ) then "published"
+ if any($immutable[]; .name != $digest or ($digest | test("^sha256:[a-fA-F0-9]{64}$") | not))
+ then "published"
elif $backfill then "backfill"
else
# An index can lose its numeric alias while it still owns latest.
@@ -64,16 +62,8 @@ case "$version_status" in
esac
case "$version_status" in
- latest|backfill)
- if [ -n "${GITHUB_OUTPUT:-}" ]; then
- publish_latest=false
- if [ "$version_status" = latest ]; then
- publish_latest=true
- fi
- printf 'publish_latest=%s\n' "$publish_latest" >> "$GITHUB_OUTPUT"
- fi
- exit 0
- ;;
+ latest) publish_latest=true ;;
+ backfill) publish_latest=false ;;
published)
printf '%s\n' "::error::Container version $version or its source tag already exists; retries require a matching verified digest." >&2
exit 1
@@ -83,3 +73,6 @@ case "$version_status" in
exit 1
;;
esac
+if [ -n "${GITHUB_OUTPUT:-}" ]; then
+ printf 'publish_latest=%s\n' "$publish_latest" >> "$GITHUB_OUTPUT"
+fi
diff --git a/docs/ci.md b/docs/ci.md
index 01e79d013..344d28f35 100644
--- a/docs/ci.md
+++ b/docs/ci.md
@@ -32,10 +32,11 @@ merge whose first parent is the current tip of that same branch. A stale merge
or a retarget with different parents needs a fresh PR event after the merge ref
updates. Reopening the PR or pushing a commit starts one.
Only the original source run and an uninterrupted sequence of successful edits
-can carry a green result. A cancelled or retried run, a changed target, missing
-evidence or conflicting results require full qualification. Use **Re-run all
-jobs** to retry an edit. A partial retry cannot refresh a previous attempt's
-required checks.
+can carry a green result. A failed source run stays red if all subsequent edits
+failed or were cancelled for that same target. A cancelled source run, a retry,
+a changed target, missing evidence or conflicting results require full
+qualification. Use **Re-run all jobs** to retry an edit. A partial retry cannot
+refresh a previous attempt's required checks.
## Required gate
diff --git a/plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs b/plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs
index 7d60d4f91..e8979962a 100644
--- a/plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs
+++ b/plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs
@@ -27,16 +27,14 @@ export async function buildMcpApp({ output, native = "universal" }) {
const contract = JSON.parse(
await readFile(join(root, "../plugin-files.json"), "utf8"),
);
- const nativeFiles = contract.shippedExact.filter((path) =>
- path.startsWith("mcp/native/"),
+ const nativeFiles = contract.shippedExact.filter(
+ (file) =>
+ file.startsWith("mcp/native/") &&
+ (!hostTarget ||
+ !file.endsWith(".node") ||
+ file.startsWith(`mcp/native/${hostTarget}/`)),
);
- if (
- hostTarget &&
- !nativeFiles.some(
- (path) =>
- path.startsWith(`mcp/native/${hostTarget}/`) && path.endsWith(".node"),
- )
- ) {
+ if (hostTarget && !nativeFiles.some((file) => file.endsWith(".node"))) {
throw new Error(`Unsupported native target: ${hostTarget}.`);
}
@@ -53,13 +51,6 @@ export async function buildMcpApp({ output, native = "universal" }) {
await writeRuntime("server", "main.ts");
for (const file of nativeFiles) {
const path = file.slice("mcp/native/".length);
- if (
- hostTarget &&
- path.endsWith(".node") &&
- !path.startsWith(`${hostTarget}/`)
- ) {
- continue;
- }
const destination = join(mcpDir, "native", path);
await mkdir(dirname(destination), { recursive: true });
try {
diff --git a/plugins/codex-security/mcp-app/src/deep-scan-settings.ts b/plugins/codex-security/mcp-app/src/deep-scan-settings.ts
new file mode 100644
index 000000000..7fbf8f177
--- /dev/null
+++ b/plugins/codex-security/mcp-app/src/deep-scan-settings.ts
@@ -0,0 +1,28 @@
+// SDK name, legacy TOML key, project key, and CLI flag (when exposed).
+export const DEEP_SCAN_SETTINGS = [
+ ["workers", "workers", "workers", "--workers"],
+ ["subagents", "subagents", "subagents_per_worker", "--subagents"],
+ [
+ "stopAfterNoNew",
+ "stop_after_no_new",
+ "stop_after_no_new",
+ "--stop-after-no-new",
+ ],
+ [
+ "stopAfterConsecutiveErrors",
+ "stop_after_consecutive_errors",
+ "stop_after_consecutive_errors",
+ null,
+ ],
+ [
+ "maxDiscoveryRuns",
+ "max_discovery_runs",
+ "max_discovery_runs",
+ "--max-discovery-runs",
+ ],
+ ["maxTimeHours", "max_time_hours", "max_time_hours", "--max-time-hours"],
+] as const;
+
+export function hoursConflict(hours: number, prior = hours) {
+ return Math.abs(prior - hours) > Number.EPSILON * Math.max(prior, hours);
+}
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/executor.ts b/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
index 5341a19b1..e5acb8fb9 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
@@ -32,6 +32,7 @@ import {
preflightProviderDefinitions,
profileConfigOverrides,
} from "../../../scripts/codex_profile.mjs";
+import { DEEP_SCAN_SETTINGS } from "../deep-scan-settings.js";
import { executablePathForSpawn } from "../executable-path.js";
import { writePrivateFile } from "./artifacts.js";
import { workerTokenUsage } from "./usage-receipt.js";
@@ -186,16 +187,11 @@ export class CodexSdkWorkerExecutor implements CodexWorkerExecutor {
await writePrivateFile(
deepConfigFile,
stringifyToml({
- deep_scan: {
- workers: config.workers,
- subagents: config.subagents,
- stop_after_no_new: config.stopAfterNoNew,
- stop_after_consecutive_errors: config.stopAfterConsecutiveErrors,
- max_discovery_runs: config.maxDiscoveryRuns,
- ...(config.maxTimeHours === undefined
- ? {}
- : { max_time_hours: config.maxTimeHours }),
- },
+ deep_scan: Object.fromEntries(
+ DEEP_SCAN_SETTINGS.filter(
+ ([name]) => config[name] !== undefined,
+ ).map(([name, key]) => [key, config[name]]),
+ ),
}),
);
for (const name of Object.keys(childEnv)) {
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/project-config.ts b/plugins/codex-security/mcp-app/src/deep-scan/project-config.ts
index 31a997fff..7b24e3ea7 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/project-config.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/project-config.ts
@@ -3,21 +3,13 @@ import { homedir } from "node:os";
import { extname, isAbsolute, resolve } from "node:path";
import Ajv, { type ValidateFunction } from "ajv";
import { parseDocument } from "yaml";
+import { DEEP_SCAN_SETTINGS, hoursConflict } from "../deep-scan-settings.js";
import schema from "./project-config.schema.json";
let validate: ValidateFunction | undefined;
-const fields = {
- workers: "workers",
- subagents_per_worker: "subagents",
- stop_after_no_new: "stopAfterNoNew",
- stop_after_consecutive_errors: "stopAfterConsecutiveErrors",
- max_discovery_runs: "maxDiscoveryRuns",
- max_time_hours: "maxTimeHours",
-} as const;
-
export interface DeepScanProjectConfig {
path: string;
- settings: Partial<Record<(typeof fields)[keyof typeof fields], number>>;
+ settings: Partial<Record<(typeof DEEP_SCAN_SETTINGS)[number][0], number>>;
}
/** Read only an explicitly selected file; repository configuration is never discovered. */
@@ -64,7 +56,7 @@ export async function readDeepScanProjectConfig(
scan?: {
mode?: string;
max_time_minutes?: number;
- deep?: Record<keyof typeof fields, number>;
+ deep?: Record<(typeof DEEP_SCAN_SETTINGS)[number][2], number>;
};
[key: string]: unknown;
};
@@ -85,8 +77,8 @@ export async function readDeepScanProjectConfig(
);
}
const settings: DeepScanProjectConfig["settings"] = {};
- for (const [field, name] of Object.entries(fields)) {
- const value = input.scan?.deep?.[field as keyof typeof fields];
+ for (const [name, , field] of DEEP_SCAN_SETTINGS) {
+ const value = input.scan?.deep?.[field];
if (value !== undefined) settings[name] = value;
}
const minutes = input.scan?.max_time_minutes;
@@ -96,11 +88,7 @@ export async function readDeepScanProjectConfig(
"Deep Scan scan.max_time_minutes must be no greater than 5760 minutes.",
);
const hours = minutes / 60;
- if (
- settings.maxTimeHours !== undefined &&
- Math.abs(settings.maxTimeHours - hours) >
- Number.EPSILON * Math.max(settings.maxTimeHours, hours)
- ) {
+ if (hoursConflict(hours, settings.maxTimeHours)) {
throw new Error(
"Conflicting scan.max_time_minutes and scan.deep.max_time_hours settings.",
);
diff --git a/plugins/codex-security/mcp-app/src/tracking-contract-compat.ts b/plugins/codex-security/mcp-app/src/tracking-contract-compat.ts
index d6e0fded3..215893d27 100644
--- a/plugins/codex-security/mcp-app/src/tracking-contract-compat.ts
+++ b/plugins/codex-security/mcp-app/src/tracking-contract-compat.ts
@@ -14,81 +14,7 @@ export function adaptTrackingSchema(
): void {
const findingProperties = ["properties", "findings", "items", "properties"];
const scanProperties = ["properties", "scan", "properties"];
- const patterns =
- filename === "findings.json"
- ? [
- {
- path: [
- ...findingProperties,
- "codeEvidence",
- "items",
- "properties",
- "id",
- ],
- before: "^[a-z0-9][a-z0-9._/-]*$",
- after: "^[a-z0-9][a-z0-9._/-]*(?:\\n)?$",
- },
- {
- path: [...findingProperties, "writeup", "properties", "reportPath"],
- before: "^findings/([a-z0-9][a-z0-9._-]*)/\\1\\.md$",
- after: "^findings/([a-z0-9][a-z0-9._-]*)/\\1\\.md(?:\\n)?$",
- },
- ]
- : filename === "scan-manifest.json"
- ? [
- {
- path: [
- ...scanProperties,
- "artifacts",
- "items",
- "properties",
- "path",
- ],
- before: "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*\\\\).+$",
- after:
- "^(?!/)(?![^\\n]*(?:^|/)\\.\\.(?:/|$))(?![^\\n]*\\\\)[^\\n]+$",
- },
- {
- path: [
- ...scanProperties,
- "target",
- "properties",
- "snapshotDigest",
- ],
- before: "^codex-security-snapshot/v1:sha256:[a-f0-9]{64}$",
- after: "^codex-security-snapshot/v1:sha256:[a-f0-9]{64}(?:\\n)?$",
- },
- {
- path: [
- ...scanProperties,
- "threatModel",
- "anyOf",
- 1,
- "properties",
- "content",
- ],
- before: "^[\\s\\S]*\\S[\\s\\S]*$",
- after: "^[\\s\\S]*[^\\p{White_Space}\\u001c-\\u001f][\\s\\S]*$",
- },
- ]
- : filename === "coverage.json"
- ? [
- {
- path: [
- "properties",
- "surfaces",
- "items",
- "properties",
- "receiptRefs",
- "items",
- ],
- before: "^(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*\\\\)artifacts/.+$",
- after:
- "^(?![^\\n]*(?:^|/)\\.\\.(?:/|$))(?![^\\n]*\\\\)artifacts/[^\\n]+$",
- },
- ]
- : [];
- for (const { path, before, after } of patterns) {
+ function adapt(path: (string | number)[], before: string, after: string) {
let field: unknown = schema;
for (const key of path) {
field =
@@ -102,6 +28,41 @@ export function adaptTrackingSchema(
field["pattern"] = after;
}
}
+
+ if (filename === "findings.json") {
+ adapt(
+ [...findingProperties, "codeEvidence", "items", "properties", "id"],
+ "^[a-z0-9][a-z0-9._/-]*$",
+ "^[a-z0-9][a-z0-9._/-]*(?:\\n)?$",
+ );
+ adapt(
+ [...findingProperties, "writeup", "properties", "reportPath"],
+ "^findings/([a-z0-9][a-z0-9._-]*)/\\1\\.md$",
+ "^findings/([a-z0-9][a-z0-9._-]*)/\\1\\.md(?:\\n)?$",
+ );
+ } else if (filename === "scan-manifest.json") {
+ adapt(
+ [...scanProperties, "artifacts", "items", "properties", "path"],
+ "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*\\\\).+$",
+ "^(?!/)(?![^\\n]*(?:^|/)\\.\\.(?:/|$))(?![^\\n]*\\\\)[^\\n]+$",
+ );
+ adapt(
+ [...scanProperties, "target", "properties", "snapshotDigest"],
+ "^codex-security-snapshot/v1:sha256:[a-f0-9]{64}$",
+ "^codex-security-snapshot/v1:sha256:[a-f0-9]{64}(?:\\n)?$",
+ );
+ adapt(
+ [...scanProperties, "threatModel", "anyOf", 1, "properties", "content"],
+ "^[\\s\\S]*\\S[\\s\\S]*$",
+ "^[\\s\\S]*[^\\p{White_Space}\\u001c-\\u001f][\\s\\S]*$",
+ );
+ } else if (filename === "coverage.json") {
+ adapt(
+ ["properties", "surfaces", "items", "properties", "receiptRefs", "items"],
+ "^(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*\\\\)artifacts/.+$",
+ "^(?![^\\n]*(?:^|/)\\.\\.(?:/|$))(?![^\\n]*\\\\)artifacts/[^\\n]+$",
+ );
+ }
}
/** Preserve tracking's whitespace and reserved-name checks on the original path. */
diff --git a/plugins/codex-security/mcp-app/src/workbench/local-dedupe.ts b/plugins/codex-security/mcp-app/src/workbench/local-dedupe.ts
index d18c41a00..456fc2577 100644
--- a/plugins/codex-security/mcp-app/src/workbench/local-dedupe.ts
+++ b/plugins/codex-security/mcp-app/src/workbench/local-dedupe.ts
@@ -302,15 +302,9 @@ function validVector(
numbers.push(number);
}
if (maximum === 0) return false;
+ // This estimate only selects the exact check below, with a twofold margin.
let sum = 0;
- let correction = 0;
- for (const number of numbers) {
- const scaled = number / maximum;
- const term = scaled * scaled - correction;
- const next = sum + term;
- correction = next - sum - term;
- sum = next;
- }
+ for (const number of numbers) sum += (number / maximum) ** 2;
if (maximum * Math.sqrt(sum) < Number.MAX_VALUE / 2) return true;
// Near overflow, a rounded sqrt/product can reject a norm that rounds to
// MAX_VALUE. Compare exact squares in units of 2^-2148 with the overflow midpoint.
diff --git a/plugins/codex-security/mcp-app/src/workbench/migrations.ts b/plugins/codex-security/mcp-app/src/workbench/migrations.ts
index 883962849..721021b53 100644
--- a/plugins/codex-security/mcp-app/src/workbench/migrations.ts
+++ b/plugins/codex-security/mcp-app/src/workbench/migrations.ts
@@ -27,16 +27,29 @@ function columns(database: DatabaseSync, table: string): Set<string> {
);
}
-function addColumn(database: DatabaseSync, statement: string): void {
+function addColumn(database: DatabaseSync, statement: string): boolean {
const [, table, column] = /^ALTER TABLE\s+(\w+)\s+ADD COLUMN\s+(\w+)/u.exec(
statement,
)!;
- if (!columns(database, table).has(column)) database.exec(statement);
+ if (columns(database, table).has(column)) return false;
+ database.exec(statement);
+ return true;
}
-function repairAdditive(database: DatabaseSync, version: number): void {
+function applyRepairStatement(database: DatabaseSync, statement: string): void {
+ if (statement.startsWith("ALTER TABLE ")) addColumn(database, statement);
+ else
+ database.exec(
+ statement.replace(
+ /^CREATE (UNIQUE INDEX|INDEX|TABLE) /u,
+ "CREATE $1 IF NOT EXISTS ",
+ ),
+ );
+}
+
+function repairSchema(database: DatabaseSync, version: number): void {
for (const statement of migration(version).statements)
- addColumn(database, statement);
+ applyRepairStatement(database, statement);
}
function normalizeHistory(database: DatabaseSync): void {
@@ -45,7 +58,7 @@ function normalizeHistory(database: DatabaseSync): void {
.prepare(query.join("\n"))
.all({ ":history": repairHistory })) {
if (operation === "error") throw new Error(String(value));
- if (operation === "additive") repairAdditive(database, Number(value));
+ if (operation === "additive") repairSchema(database, Number(value));
else if (operation === "column") addColumn(database, String(value));
else if (operation === "timestamp")
database.prepare(String(value)).run(new Date().toISOString());
@@ -74,22 +87,9 @@ function repairDeepScan(database: DatabaseSync): void {
"deep_scan_dedup_inputs",
];
if (!missingOwner && expected.every((name) => existing.has(name))) return;
- addColumn(
- database,
- "ALTER TABLE scans ADD COLUMN deep_scan_owner_thread_id TEXT;",
- );
for (const statement of migration(11).statements) {
- if (
- statement.startsWith("ALTER TABLE scans") ||
- (statement.startsWith("UPDATE scans") && !missingOwner)
- )
- continue;
- database.exec(
- statement.replace(
- /^CREATE (UNIQUE INDEX|INDEX|TABLE) /u,
- "CREATE $1 IF NOT EXISTS ",
- ),
- );
+ if (statement.startsWith("UPDATE scans") && !missingOwner) continue;
+ applyRepairStatement(database, statement);
if (
statement.startsWith("UPDATE scans") &&
scanColumns.has("continuation_thread_id")
@@ -113,16 +113,7 @@ function repairStableTargets(database: DatabaseSync): boolean {
existing.length === 2
)
return false;
- for (const statement of migration(16).statements) {
- if (statement.startsWith("ALTER TABLE")) addColumn(database, statement);
- else
- database.exec(
- statement.replace(
- /^CREATE (TABLE|INDEX) /u,
- "CREATE $1 IF NOT EXISTS ",
- ),
- );
- }
+ repairSchema(database, 16);
database.exec(
"UPDATE scans SET target_id = NULL WHERE target_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM security_targets WHERE security_targets.id = scans.target_id)",
);
@@ -222,13 +213,9 @@ export function applyMigrations(
let backfill = false;
for (const item of selected) {
if (item.version === 6) {
- addColumn(
- database,
- "ALTER TABLE workspaces ADD COLUMN thread_id TEXT;",
- );
- database.exec(
- "CREATE INDEX IF NOT EXISTS workspaces_by_thread_and_updated_at ON workspaces(thread_id, updated_at DESC)",
- );
+ // Preserve the original one-line index declaration in sqlite_schema.
+ for (const statement of migration(6).statements)
+ applyRepairStatement(database, statement.replaceAll("\n", " "));
} else if (item.version === 16) backfill = repairStableTargets(database);
else if (applied.has(item.version)) {
if (
@@ -236,7 +223,7 @@ export function applyMigrations(
2, 12, 13, 26, 28, 31, 32, 42, 47, 50, 54, 55, 58, 59, 60, 61,
].includes(item.version)
)
- repairAdditive(database, item.version);
+ repairSchema(database, item.version);
else if (item.version === 11) repairDeepScan(database);
} else {
database.exec(item.statements.join("\n"));
@@ -251,15 +238,8 @@ export function applyMigrations(
}
}
if (applied.has(27)) {
- const [threshold, count] = migration(27).statements;
- const missing = !columns(database, "deep_scan_runs").has(
- "stop_after_consecutive_errors",
- );
- addColumn(database, threshold);
- if (missing)
- database.exec(
- "UPDATE deep_scan_runs SET stop_after_consecutive_errors = stop_after_no_new",
- );
+ const [threshold, count, backfillThreshold] = migration(27).statements;
+ if (addColumn(database, threshold)) database.exec(backfillThreshold);
addColumn(database, count);
}
if (backfill) backfillTargets(database);
diff --git a/plugins/codex-security/mcp-app/src/workbench/severity.ts b/plugins/codex-security/mcp-app/src/workbench/severity.ts
index a9f09045b..403c9c4e2 100644
--- a/plugins/codex-security/mcp-app/src/workbench/severity.ts
+++ b/plugins/codex-security/mcp-app/src/workbench/severity.ts
@@ -208,14 +208,14 @@ export function readSeverityClassification(
try {
database.exec("PRAGMA busy_timeout = 5000");
return transaction(database, "BEGIN", () => {
- if (
- !database
- .prepare(
- "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'scan_severity_classifications'",
- )
- .get()
- )
- return {};
+ const tables = database
+ .prepare(
+ `SELECT name FROM sqlite_master WHERE type = 'table'
+ AND name IN ('scan_severity_classifications', 'scan_severity_assessments')`,
+ )
+ .all()
+ .map((row) => row.name);
+ if (!tables.includes("scan_severity_classifications")) return {};
const columns = database
.prepare("PRAGMA table_info(scan_severity_classifications)")
.all()
@@ -234,18 +234,13 @@ export function readSeverityClassification(
const classification = parseJson(String(row.value)) as {
findingIds: string[];
};
- const hasScanAssessments = database
- .prepare(
- "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'scan_severity_assessments'",
- )
- .get();
return {
scanId,
...classification,
assessments: assessments(
database,
classification.findingIds,
- hasScanAssessments ? scanId : undefined,
+ tables.includes("scan_severity_assessments") ? scanId : undefined,
),
};
});
diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_context_snapshot_cases.ts b/plugins/codex-security/mcp-app/tests/deep_scan_context_snapshot_cases.ts
index ce064cbdb..073b425a6 100644
--- a/plugins/codex-security/mcp-app/tests/deep_scan_context_snapshot_cases.ts
+++ b/plugins/codex-security/mcp-app/tests/deep_scan_context_snapshot_cases.ts
@@ -74,7 +74,8 @@ export async function testDeepScanContextSnapshots() {
async (userContext, index) => {
const severityRubric = userContext === undefined ? undefined : policy;
const deepConfig = path.join(fixture.root, `deep-${index}.toml`);
- const maxTimeHours = 1.5 + index;
+ const maxTimeMinutes = 15.5 + index;
+ const maxTimeHours = maxTimeMinutes / 60;
await writeFile(deepConfig, `[deep_scan]\nmax_time_hours = 48\n`);
const command = async (args: string[], input?: string) => {
const execution = promisify(execFile)(
@@ -100,8 +101,8 @@ export async function testDeepScanContextSnapshots() {
projectConfigPath,
JSON.stringify({
scan: {
+ max_time_minutes: maxTimeMinutes,
deep: {
- max_time_hours: maxTimeHours,
subagents_per_worker: index,
},
},
@@ -246,6 +247,7 @@ export async function testDeepScanContextSnapshots() {
editedContext,
]);
// Reload through a new store to model a coordinator restart after editing.
+ await writeFile(deepConfig, "[deep_scan]\nmax_time_hours = 23\n");
store = new WorkbenchDeepScanStore(command);
run = await store.begin({
scanId: run.scanId,
@@ -328,7 +330,6 @@ export async function testDeepScanContextSnapshots() {
);
assert.match(resumed.invocation.stdin, /record_codex_security_/);
}
- await writeFile(deepConfig, "[deep_scan]\nmax_time_hours = 48\n");
const restarted = await new WorkbenchDeepScanStore(command).begin({
scanId: run.scanId,
threadId,
diff --git a/plugins/codex-security/scripts/deep_scan_workbench.py b/plugins/codex-security/scripts/deep_scan_workbench.py
index 6c49d158e..e60098c2a 100644
--- a/plugins/codex-security/scripts/deep_scan_workbench.py
+++ b/plugins/codex-security/scripts/deep_scan_workbench.py
@@ -1694,20 +1694,9 @@ def fail_deep_scan_locked(
(args.deep_status, message, manifest_path, timestamp, timestamp, scan_id),
)
cancel_active_workers(connection, scan_id, timestamp)
- parent_update = connection.execute(
- """
- UPDATE scans
- SET status = 'failed', failure_message = ?, completed_at = ?, updated_at = ?
- WHERE id = ? AND status = 'running'
- """,
- (message, timestamp, timestamp, scan_id),
- )
- if parent_update.rowcount != 1:
+ if not saved_results.mark_running_scan_failed(connection, scan_id, message, timestamp):
raise SystemExit("Deep Scan failure could not be persisted to its parent scan.")
- connection.execute(
- "UPDATE scan_progress SET updated_at = ? WHERE scan_id = ?",
- (timestamp, scan_id),
- )
+ saved_results.touch_scan_progress(connection, scan_id, timestamp)
connection.commit()
except BaseException:
connection.rollback()
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.py
index 38ca38cdd..9b55b1d47 100644
--- a/plugins/codex-security/scripts/finalize_scan_contract.py
+++ b/plugins/codex-security/scripts/finalize_scan_contract.py
@@ -2727,9 +2727,9 @@ def _validate_existing_seal(
raise ContractError(f"{context}: sealed artifact changed or is missing")
-def _read_sealed_artifacts(
+def _read_scan_artifacts(
scan_dir: Path, scan: dict[str, Any]
-) -> tuple[dict[str, Any], dict[str, Any], bytes]:
+) -> tuple[dict[str, Any], dict[str, Any], dict[str, bytes]]:
findings, findings_bytes = _read_scan_local_json_bytes(
scan_dir, scan["findingsRef"], scan["findingsRef"]
)
@@ -2741,8 +2741,15 @@ def _read_sealed_artifacts(
contents[scan["coverageRef"]] = coverage_bytes
else:
coverage = review_state(scan)
+ return findings, coverage, contents
+
+
+def _read_sealed_artifacts(
+ scan_dir: Path, scan: dict[str, Any]
+) -> tuple[dict[str, Any], dict[str, Any], bytes]:
+ findings, coverage, contents = _read_scan_artifacts(scan_dir, scan)
_validate_existing_seal(scan_dir, scan, artifact_contents=contents)
- return findings, coverage, findings_bytes
+ return findings, coverage, contents[scan["findingsRef"]]
def _read_sealed_scan(
@@ -3102,19 +3109,14 @@ def _prepare_scan_finalization(
_populate_unsealed_manifest_envelope(manifest, scan, completion_binding)
_validate_contract_refs(scan)
if draft_documents is None:
- findings, findings_input_bytes = _read_scan_local_json_bytes(
- scan_dir, scan["findingsRef"], scan["findingsRef"]
- )
- if "coverageRef" in scan:
- coverage, coverage_input_bytes = _read_scan_local_json_bytes(
- scan_dir, scan["coverageRef"], scan["coverageRef"]
- )
- else:
- coverage = review_state(scan)
- coverage_input_bytes = _json_bytes(coverage)
+ findings, coverage, contents = _read_scan_artifacts(scan_dir, scan)
else:
findings, coverage = copy.deepcopy(draft_documents[1:])
- findings_input_bytes, coverage_input_bytes = _json_bytes(findings), _json_bytes(coverage)
+ contents = {scan["findingsRef"]: _json_bytes(findings)}
+ if "coverageRef" in scan:
+ contents[scan["coverageRef"]] = _json_bytes(coverage)
+ if "coverageRef" not in scan:
+ _json_bytes(coverage)
if not was_sealed:
# Preserve legacy sealed bundles, but do not publish a completeness claim.
coverage.pop("completeness", None)
@@ -3138,14 +3140,7 @@ def _prepare_scan_finalization(
raise ContractError(
f"coverage.mode: must match selected scan mode {expected_coverage_mode}"
)
- _validate_existing_seal(
- scan_dir,
- scan,
- artifact_contents={
- scan["findingsRef"]: findings_input_bytes,
- **({scan["coverageRef"]: coverage_input_bytes} if "coverageRef" in scan else {}),
- },
- )
+ _validate_existing_seal(scan_dir, scan, artifact_contents=contents)
scan["sealedAt"] = _require_str(scan, "completedAt", "manifest.scan")
_validate_target(_require_dict(scan, "target", "manifest.scan"))
_validate_completion_binding(manifest, findings, coverage, completion_binding)
diff --git a/plugins/codex-security/scripts/workbench/saved_sources.py b/plugins/codex-security/scripts/workbench/saved_sources.py
index d8c018a1e..b1fdb29e9 100644
--- a/plugins/codex-security/scripts/workbench/saved_sources.py
+++ b/plugins/codex-security/scripts/workbench/saved_sources.py
@@ -56,23 +56,20 @@ def _source_digests(value: Any, error: str) -> dict[str, str]:
return value
-def _retained_source_state(value: Any) -> tuple[dict[str, str], str | None]:
- """Return the saved source digests and the optional path supplying the threat model.
-
- Accept both a legacy path-to-digest map and an envelope containing sources.
- Raise ContractError if the model points outside that envelope's source set.
- """
- if isinstance(value, dict) and isinstance(value.get("sources"), dict):
- sources = _source_digests(
- value["sources"], "Saved stopped-scan source digests are malformed."
- )
- model_source = value.get("threatModelSource")
- if "threatModelSource" in value and (
- not isinstance(model_source, str) or model_source not in sources
- ):
- raise ContractError("Saved stopped-scan model source is outside its checkpoint set.")
- return sources, model_source
- return _source_digests(value, "Saved stopped-scan source digests are malformed."), None
+def _retained_source_state(raw: str | None) -> dict[str, Any]:
+ """Decode stopped-scan sources, preserving the distinction between absent and frozen empty."""
+ if raw is None:
+ return {}
+ state = json.loads(raw)
+ if not isinstance(state, dict) or not isinstance(state.get("sources"), dict):
+ state = {"sources": state} # Legacy rows store only the path-to-digest map.
+ sources = _source_digests(state["sources"], "Saved stopped-scan source digests are malformed.")
+ model_source = state.get("threatModelSource")
+ if "threatModelSource" in state and (
+ not isinstance(model_source, str) or model_source not in sources
+ ):
+ raise ContractError("Saved stopped-scan model source is outside its checkpoint set.")
+ return state
def _retained_checkpoint_state(
diff --git a/plugins/codex-security/scripts/workbench_constants.py b/plugins/codex-security/scripts/workbench_constants.py
index cc1452c6e..befd0c27c 100644
--- a/plugins/codex-security/scripts/workbench_constants.py
+++ b/plugins/codex-security/scripts/workbench_constants.py
@@ -36,6 +36,14 @@ FINDING_ATTACK_PATH_PREVIEW_BYTES = 4_000
FINDING_CODE_EVIDENCE_LIMIT = 4
FINDING_CODE_EVIDENCE_SNIPPET_BYTES = 1_500
FINDING_EVIDENCE_EXCERPT_BYTES = 8_000
+FINDING_LOCATION_ORDER_SQL = "CASE WHEN role = 'root_control' THEN 0 ELSE 1 END, sort_order"
+# The outer query binds finding_occurrences as occurrences.
+PRIMARY_FINDING_LOCATION_SQL = f"""
+ FROM finding_locations
+ WHERE occurrence_id = occurrences.id
+ ORDER BY {FINDING_LOCATION_ORDER_SQL}
+ LIMIT 1
+"""
FINDING_LOCATIONS_LIMIT = 8
FINDING_TITLE_BYTES = 512
FINDING_SUMMARY_BYTES = 2_000
diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.py
index 1ee0159bd..8842b9f24 100644
--- a/plugins/codex-security/scripts/workbench_db.py
+++ b/plugins/codex-security/scripts/workbench_db.py
@@ -68,6 +68,7 @@ from workbench_constants import (
DELIVERED_ACTION_LEASE_SECONDS,
FINDING_ABSOLUTE_PATH_BYTES,
FINDING_LEVEL_BYTES,
+ FINDING_LOCATION_ORDER_SQL,
FINDING_LOCATION_PATH_BYTES,
FINDING_LOCATION_ROLE_BYTES,
FINDING_LOCATIONS_LIMIT,
@@ -2861,11 +2862,11 @@ def finding_result(
except SystemExit:
target = None
for row in connection.execute(
- """
+ f"""
SELECT relative_path, start_line, end_line, role
FROM finding_locations
WHERE occurrence_id = ?
- ORDER BY CASE WHEN role = 'root_control' THEN 0 ELSE 1 END, sort_order
+ ORDER BY {FINDING_LOCATION_ORDER_SQL}
LIMIT ?
""",
(occurrence["id"], FINDING_LOCATIONS_LIMIT),
diff --git a/plugins/codex-security/scripts/workbench_feedback.py b/plugins/codex-security/scripts/workbench_feedback.py
index 605a8a792..ea5452480 100644
--- a/plugins/codex-security/scripts/workbench_feedback.py
+++ b/plugins/codex-security/scripts/workbench_feedback.py
@@ -15,6 +15,7 @@ from workbench_constants import (
FINDING_LOCATION_PATH_BYTES,
FINDING_SUMMARY_BYTES,
FINDING_TITLE_BYTES,
+ PRIMARY_FINDING_LOCATION_SQL,
)
from workbench_validation import bounded_output_text
@@ -26,7 +27,7 @@ def get_scan_feedback(connection: sqlite3.Connection, scan: sqlite3.Row) -> dict
false positive with a nonblank reason. Prefer the root-control location in its preview.
"""
rows = connection.execute(
- """
+ f"""
WITH ranked_decisions AS (
SELECT findings.id AS finding_id, findings.fingerprint, findings.rule_id,
findings.identity_anchor, findings.identity_instance, occurrences.title,
@@ -48,12 +49,7 @@ def get_scan_feedback(connection: sqlite3.Connection, scan: sqlite3.Row) -> dict
JOIN scans AS source_scans ON source_scans.id = occurrences.scan_id
LEFT JOIN finding_triage AS triage ON triage.occurrence_id = occurrences.id
JOIN finding_locations AS locations ON locations.id = (
- SELECT candidate.id
- FROM finding_locations AS candidate
- WHERE candidate.occurrence_id = occurrences.id
- ORDER BY CASE WHEN candidate.role = 'root_control' THEN 0 ELSE 1 END,
- candidate.sort_order
- LIMIT 1
+ SELECT id {PRIMARY_FINDING_LOCATION_SQL}
)
WHERE source_scans.target_id = ?
AND source_scans.id != ?
diff --git a/plugins/codex-security/scripts/workbench_native_indexes.py b/plugins/codex-security/scripts/workbench_native_indexes.py
index 98c7a7501..2bd0c96fa 100644
--- a/plugins/codex-security/scripts/workbench_native_indexes.py
+++ b/plugins/codex-security/scripts/workbench_native_indexes.py
@@ -12,7 +12,12 @@ from typing import Any
# Some plugin hosts launch Python with safe-path isolation enabled.
sys.path.insert(0, str(Path(__file__).resolve().parent))
import workbench_scan_history as scan_history
-from workbench_constants import FINDING_SUMMARY_BYTES, FINDING_TITLE_BYTES, FINDINGS_PAGE_MAX
+from workbench_constants import (
+ FINDING_SUMMARY_BYTES,
+ FINDING_TITLE_BYTES,
+ FINDINGS_PAGE_MAX,
+ PRIMARY_FINDING_LOCATION_SQL,
+)
from workbench_validation import bounded_output_text, timestamp_key
@@ -108,7 +113,7 @@ def _indexed_findings(connection: sqlite3.Connection) -> Iterator[dict[str, Any]
grouped: dict[tuple[str, str], list[sqlite3.Row]] = {}
for row in connection.execute(
- """
+ f"""
SELECT
occurrences.id AS occurrence_id,
occurrences.finding_id,
@@ -125,15 +130,7 @@ def _indexed_findings(connection: sqlite3.Connection) -> Iterator[dict[str, Any]
triage.updated_at AS decision_updated_at,
occurrences.title,
occurrences.summary,
- (
- SELECT locations.relative_path
- FROM finding_locations AS locations
- WHERE locations.occurrence_id = occurrences.id
- ORDER BY
- CASE WHEN locations.role = 'root_control' THEN 0 ELSE 1 END,
- locations.sort_order
- LIMIT 1
- ) AS location_path
+ (SELECT relative_path {PRIMARY_FINDING_LOCATION_SQL}) AS location_path
FROM finding_occurrences AS occurrences
JOIN scans ON scans.id = occurrences.scan_id
JOIN security_targets AS targets ON targets.id = scans.target_id
diff --git a/plugins/codex-security/scripts/workbench_publication.py b/plugins/codex-security/scripts/workbench_publication.py
index b87630493..42b606ad1 100644
--- a/plugins/codex-security/scripts/workbench_publication.py
+++ b/plugins/codex-security/scripts/workbench_publication.py
@@ -28,6 +28,7 @@ from finalize_scan_contract import (
write_sarif_projection,
)
from validate_scan_contract import validate_contract
+from workbench_constants import PRIMARY_FINDING_LOCATION_SQL
def destination_key(destination: Any) -> str:
@@ -422,7 +423,7 @@ def write_csv_export(
def finding_export_rows(connection: sqlite3.Connection, scan_id: str) -> sqlite3.Cursor:
return connection.execute(
- """
+ f"""
SELECT
occurrences.id AS occurrence_id,
occurrences.finding_id,
@@ -440,15 +441,8 @@ def finding_export_rows(connection: sqlite3.Connection, scan_id: str) -> sqlite3
FROM finding_occurrences AS occurrences
LEFT JOIN finding_triage AS triage ON triage.occurrence_id = occurrences.id
LEFT JOIN finding_locations AS locations
- ON locations.occurrence_id = occurrences.id
- AND locations.sort_order = (
- SELECT primary_location.sort_order
- FROM finding_locations AS primary_location
- WHERE primary_location.occurrence_id = occurrences.id
- ORDER BY
- CASE WHEN primary_location.role = 'root_control' THEN 0 ELSE 1 END,
- primary_location.sort_order
- LIMIT 1
+ ON locations.id = (
+ SELECT id {PRIMARY_FINDING_LOCATION_SQL}
)
WHERE occurrences.scan_id = ?
ORDER BY occurrences.created_at, occurrences.id
diff --git a/plugins/codex-security/scripts/workbench_saved_results.py b/plugins/codex-security/scripts/workbench_saved_results.py
index de37e6c3d..83ebfd87e 100644
--- a/plugins/codex-security/scripts/workbench_saved_results.py
+++ b/plugins/codex-security/scripts/workbench_saved_results.py
@@ -159,7 +159,7 @@ def _saved_results_changed(db: Any, connection: Any, scan: Any) -> bool:
if manifest_path is None:
if frozen_sources is not None:
- return bool(_retained_source_state(json.loads(frozen_sources))[0])
+ return bool(_retained_source_state(frozen_sources)["sources"])
return has_saved_source()
if scan["seal_manifest_digest"] is None:
try:
@@ -233,24 +233,24 @@ def _recovery_source_digests(
db: Any, connection: Any, scan: Any
) -> tuple[dict[str, str], bool, dict[str, str | None]]:
scan_dir = db.require_canonical_scan_directory(Path(scan["scan_dir"]))
- frozen_sources: dict[str, str] | None = None
- include_parent = True
- raw_frozen_sources = scan["retained_source_digests_json"]
- if raw_frozen_sources is not None:
- retained = json.loads(raw_frozen_sources)
- frozen_sources, model_source = _retained_source_state(retained)
- if isinstance(retained.get("sources"), dict) and retained.get("sourceTimes") is not None:
- _restore_frozen_model_capture(
- scan_dir, scan["id"], frozen_sources, retained["sourceTimes"], model_source
- )
- _freeze_source_times(
- scan_dir,
- scan["id"],
- frozen_sources,
- retained["sourceTimes"],
- retained.get("parentContext"),
- )
- include_parent = False
+ retained = _retained_source_state(scan["retained_source_digests_json"])
+ frozen_sources = retained.get("sources")
+ include_parent = frozen_sources is None
+ if retained.get("sourceTimes") is not None:
+ _restore_frozen_model_capture(
+ scan_dir,
+ scan["id"],
+ frozen_sources,
+ retained["sourceTimes"],
+ retained.get("threatModelSource"),
+ )
+ _freeze_source_times(
+ scan_dir,
+ scan["id"],
+ frozen_sources,
+ retained["sourceTimes"],
+ retained.get("parentContext"),
+ )
checkpoint_heads, pending_recovery = _retained_checkpoint_state(scan)
if pending_recovery:
return frozen_sources, False, checkpoint_heads
@@ -574,19 +574,13 @@ def preserve_scan_results_locked(
scan = db.require_scan(connection, scan_id)
if scan["status"] != "failed":
return False
- frozen_source_digests: dict[str, str] | None = None
checkpoint_heads, pending_recovery = _retained_checkpoint_state(scan)
- model_source: list[str] = []
- saved_model_source: str | None = None
raw_frozen_sources = scan["retained_source_digests_json"]
- frozen_source_times = None
- if raw_frozen_sources is not None:
- retained = json.loads(raw_frozen_sources)
- frozen_source_digests, saved_model_source = _retained_source_state(retained)
- if isinstance(retained.get("sources"), dict):
- frozen_source_times = retained.get("sourceTimes")
- if saved_model_source is not None:
- model_source.append(saved_model_source)
+ retained = _retained_source_state(raw_frozen_sources)
+ frozen_source_digests = retained.get("sources")
+ saved_model_source = retained.get("threatModelSource")
+ model_source = [saved_model_source] if saved_model_source is not None else []
+ frozen_source_times = retained.get("sourceTimes")
canonical_sources = frozen_source_digests
if recovery_source_digests is not None:
frozen_source_digests = recovery_source_digests
@@ -1238,6 +1232,26 @@ def _scan_draft_contents_digest(documents: dict[str, bytes | None]) -> str:
return digest.hexdigest()
+def mark_running_scan_failed(
+ connection: sqlite3.Connection, scan_id: str, message: str | None, timestamp: str
+) -> bool:
+ updated = connection.execute(
+ """
+ UPDATE scans SET status = 'failed', failure_message = ?, completed_at = ?, updated_at = ?
+ WHERE id = ? AND status = 'running'
+ """,
+ (message, timestamp, timestamp, scan_id),
+ )
+ return updated.rowcount == 1
+
+
+def touch_scan_progress(connection: sqlite3.Connection, scan_id: str, timestamp: str) -> bool:
+ updated = connection.execute(
+ "UPDATE scan_progress SET updated_at = ? WHERE scan_id = ?", (timestamp, scan_id)
+ )
+ return updated.rowcount == 1
+
+
def fail_scan(db: Any, connection: Any, args: Any) -> dict[str, Any]:
with db.scan_completion_lock(db.require_uuid(args.scan_id, "scan-id")):
return fail_scan_locked(db, connection, args)
@@ -1262,22 +1276,10 @@ def fail_scan_locked(db: Any, connection: Any, args: Any) -> dict[str, Any]:
error_message="Scan failure is owned by another continuation.",
)
message = db.optional_text(args.message)
- updated = connection.execute(
- """
- UPDATE scans
- SET status = 'failed', failure_message = ?, completed_at = ?, updated_at = ?
- WHERE id = ? AND status = 'running'
- """,
- (message, timestamp, timestamp, scan["id"]),
- )
- if updated.rowcount != 1:
+ if not mark_running_scan_failed(connection, scan_id, message, timestamp):
raise SystemExit("Only a running scan can be marked failed.")
db.deep_scan.fail_from_parent_scan(connection, scan["id"], message, timestamp)
- progress_updated = connection.execute(
- "UPDATE scan_progress SET updated_at = ? WHERE scan_id = ?",
- (timestamp, scan["id"]),
- )
- if progress_updated.rowcount != 1:
+ if not touch_scan_progress(connection, scan_id, timestamp):
raise SystemExit("Codex Security scan progress not found.")
record_stopped_scan_usage(db, connection, scan_id, cost_json)
preserve_stopped_results_after_transition(db, connection, scan["id"])
@@ -1321,11 +1323,7 @@ def cancel_scan_locked(db: Any, connection: Any, args: Any) -> dict[str, Any]:
if updated.rowcount != 1:
raise SystemExit("Only a running scan can be canceled.")
db.deep_scan.cancel_from_parent_scan(connection, scan["id"], timestamp)
- progress_updated = connection.execute(
- "UPDATE scan_progress SET updated_at = ? WHERE scan_id = ?",
- (timestamp, scan["id"]),
- )
- if progress_updated.rowcount != 1:
+ if not touch_scan_progress(connection, scan_id, timestamp):
raise SystemExit("Codex Security scan progress not found.")
record_stopped_scan_usage(db, connection, scan_id, cost_json)
preserve_stopped_results_after_transition(db, connection, scan["id"])
diff --git a/plugins/codex-security/scripts/workbench_scan_history.py b/plugins/codex-security/scripts/workbench_scan_history.py
index 3315ca0d8..78df631a9 100644
--- a/plugins/codex-security/scripts/workbench_scan_history.py
+++ b/plugins/codex-security/scripts/workbench_scan_history.py
@@ -25,7 +25,7 @@ from finalize_scan_contract import (
)
from report_projection import SEVERITY_ORDER
from review_state import review_state
-from workbench_constants import ARTIFACTS, FINDINGS_PAGE_MAX
+from workbench_constants import ARTIFACTS, FINDINGS_PAGE_MAX, PRIMARY_FINDING_LOCATION_SQL
from workbench_scan_start import scan_target_identity
from workbench_scan_usage import stored_scan_cost_fields
from workbench_source_scopes import public_scan_recipe
@@ -1294,17 +1294,10 @@ def finding_occurrence_conditions(
def _scan_findings(connection: sqlite3.Connection, scan_id: str) -> dict[str, sqlite3.Row]:
"""Index one scan's finding rows by the occurrence IDs used in comparisons."""
rows = connection.execute(
- """
+ f"""
SELECT occurrences.*,
COALESCE(triage.status, 'open') AS triage_status, triage.close_reason,
- (
- SELECT locations.relative_path
- FROM finding_locations AS locations
- WHERE locations.occurrence_id = occurrences.id
- ORDER BY CASE WHEN locations.role = 'root_control' THEN 0 ELSE 1 END,
- locations.sort_order
- LIMIT 1
- ) AS relative_path
+ (SELECT relative_path {PRIMARY_FINDING_LOCATION_SQL}) AS relative_path
FROM finding_occurrences AS occurrences
LEFT JOIN finding_triage AS triage ON triage.occurrence_id = occurrences.id
WHERE occurrences.scan_id = ?
diff --git a/plugins/codex-security/scripts/workbench_schema.py b/plugins/codex-security/scripts/workbench_schema.py
index 990829735..2d3aa8c1a 100644
--- a/plugins/codex-security/scripts/workbench_schema.py
+++ b/plugins/codex-security/scripts/workbench_schema.py
@@ -8,6 +8,7 @@ from pathlib import Path
_SHARED = Path(__file__).resolve().parents[1] / "shared"
_HISTORY = json.loads((_SHARED / "workbench-migrations.json").read_text(encoding="utf-8"))
+_STATEMENTS = {migration["version"]: migration["statements"] for migration in _HISTORY}
_REPAIR_HISTORY = json.dumps(_HISTORY)
_REPAIR_PLAN = json.loads((_SHARED / "workbench-history-repairs.json").read_text(encoding="utf-8"))
MIGRATIONS = tuple(
@@ -77,12 +78,14 @@ def apply_migrations(
should_backfill_targets = False
for version, name, sql in migrations:
if version == 6:
- repair_thread_scoped_workspaces_migration(connection)
+ # Preserve the original one-line index declaration in sqlite_schema.
+ for statement in _STATEMENTS[6]:
+ apply_repair_statement(connection, statement.replace("\n", " "))
elif version == 16:
should_backfill_targets = repair_stable_targets_migration(connection)
elif version in applied:
if version in (2, 12, 13, 26, 28, 31, 32, 42, 47, 50, 54, 55, 58, 59, 60, 61):
- repair_additive_migration(connection, version)
+ repair_schema(connection, version)
elif version == 11:
repair_deep_scan_migration(connection)
else:
@@ -112,7 +115,7 @@ def normalize_pre_release_migrations(connection: sqlite3.Connection, timestamp:
if operation == "error":
raise SystemExit(value)
if operation == "additive":
- repair_additive_migration(connection, value)
+ repair_schema(connection, value)
elif operation == "column":
add_migration_column(connection, value)
else:
@@ -140,19 +143,10 @@ def repair_deep_scan_migration(connection: sqlite3.Connection) -> None:
if not owner_column_missing and expected_objects <= existing_objects:
return
- if owner_column_missing:
- add_column_if_missing(connection, "scans", "deep_scan_owner_thread_id", "TEXT")
- migration_sql = next(sql for version, _, sql in MIGRATIONS if version == 11)
- for statement in sql_statements(migration_sql):
- if statement.startswith("ALTER TABLE scans"):
- continue
+ for statement in _STATEMENTS[11]:
if statement.startswith("UPDATE scans") and not owner_column_missing:
continue
- for prefix in ("CREATE UNIQUE INDEX ", "CREATE INDEX ", "CREATE TABLE "):
- if statement.startswith(prefix):
- statement = statement.replace(prefix, f"{prefix}IF NOT EXISTS ", 1)
- break
- connection.execute(statement)
+ apply_repair_statement(connection, statement)
if statement.startswith("UPDATE scans") and "continuation_thread_id" in scan_columns:
connection.execute(
"UPDATE scans SET deep_scan_owner_thread_id = continuation_thread_id "
@@ -162,25 +156,10 @@ def repair_deep_scan_migration(connection: sqlite3.Connection) -> None:
def repair_deep_scan_failure_counter_migration(connection: sqlite3.Connection) -> None:
- threshold_column, count_column, _backfill = sql_statements(
- next(sql for version, _, sql in MIGRATIONS if version == 27)
- )
- columns = {row["name"] for row in connection.execute("PRAGMA table_info(deep_scan_runs)")}
- threshold_missing = "stop_after_consecutive_errors" not in columns
- add_migration_column(connection, threshold_column)
- if threshold_missing:
- connection.execute(
- "UPDATE deep_scan_runs SET stop_after_consecutive_errors = stop_after_no_new"
- )
- add_migration_column(connection, count_column)
-
-
-def repair_thread_scoped_workspaces_migration(connection: sqlite3.Connection) -> None:
- add_column_if_missing(connection, "workspaces", "thread_id", "TEXT")
- connection.execute(
- "CREATE INDEX IF NOT EXISTS workspaces_by_thread_and_updated_at "
- "ON workspaces(thread_id, updated_at DESC)"
- )
+ threshold, count, backfill = _STATEMENTS[27]
+ if add_migration_column(connection, threshold):
+ connection.execute(backfill)
+ add_migration_column(connection, count)
def repair_stable_targets_migration(connection: sqlite3.Connection) -> bool:
@@ -204,14 +183,7 @@ def repair_stable_targets_migration(connection: sqlite3.Connection) -> bool:
):
return False
- migration_sql = next(sql for version, _, sql in MIGRATIONS if version == 16)
- for statement in sql_statements(migration_sql):
- if statement.startswith(("ALTER TABLE workspaces", "ALTER TABLE scans")):
- add_migration_column(connection, statement)
- continue
- statement = statement.replace("CREATE TABLE ", "CREATE TABLE IF NOT EXISTS ", 1)
- statement = statement.replace("CREATE INDEX ", "CREATE INDEX IF NOT EXISTS ", 1)
- connection.execute(statement)
+ repair_schema(connection, 16)
connection.execute(
"""
UPDATE scans
@@ -225,26 +197,31 @@ def repair_stable_targets_migration(connection: sqlite3.Connection) -> bool:
return True
-def repair_additive_migration(connection: sqlite3.Connection, version: int) -> None:
- """Replay the column additions from one historical migration, skipping columns already present."""
- sql = next(sql for migration_version, _, sql in MIGRATIONS if migration_version == version)
- for statement in sql_statements(sql):
+def repair_schema(connection: sqlite3.Connection, version: int) -> None:
+ for statement in _STATEMENTS[version]:
+ apply_repair_statement(connection, statement)
+
+
+def apply_repair_statement(connection: sqlite3.Connection, statement: str) -> None:
+ if statement.startswith("ALTER TABLE "):
add_migration_column(connection, statement)
+ return
+ for prefix in ("CREATE UNIQUE INDEX ", "CREATE INDEX ", "CREATE TABLE "):
+ if statement.startswith(prefix):
+ statement = statement.replace(prefix, f"{prefix}IF NOT EXISTS ", 1)
+ break
+ connection.execute(statement)
-def add_migration_column(connection: sqlite3.Connection, statement: str) -> None:
+def add_migration_column(connection: sqlite3.Connection, statement: str) -> bool:
_, _, table, _, _, column, definition = statement.removesuffix(";").split(None, 6)
+ columns = {row["name"] for row in connection.execute(f"PRAGMA table_info({table})")}
+ if column in columns:
+ return False
# Preserve the compact declarations used by historical repairs, including CHECK errors.
definition = " ".join(definition.split()).replace("( ", "(").replace(" )", ")")
- add_column_if_missing(connection, table, column, definition)
-
-
-def add_column_if_missing(
- connection: sqlite3.Connection, table: str, column: str, definition: str
-) -> None:
- columns = {row["name"] for row in connection.execute(f"PRAGMA table_info({table})")}
- if column not in columns:
- connection.execute(f"ALTER TABLE {table} ADD COLUMN {column} {definition}")
+ connection.execute(f"ALTER TABLE {table} ADD COLUMN {column} {definition}")
+ return True
def sql_statements(script: str) -> list[str]:
diff --git a/plugins/codex-security/tests/test_checkpoint_publication_authority.py b/plugins/codex-security/tests/test_checkpoint_publication_authority.py
index 9237bd49a..2d406170b 100644
--- a/plugins/codex-security/tests/test_checkpoint_publication_authority.py
+++ b/plugins/codex-security/tests/test_checkpoint_publication_authority.py
@@ -1669,12 +1669,10 @@ def test_inherited_model_uses_same_selection_on_initial_and_frozen_publication(
replayed = workbench_db.execute(
"SELECT retained_source_digests_json FROM scans WHERE id = ?", (scan.scan_id,)
).fetchone()[0]
- assert (
- saved._retained_source_state(json.loads(replayed))[0]
- == saved._retained_source_state(json.loads(retained))[0]
- )
+ latest = saved._retained_source_state(replayed)
+ assert latest["sources"] == saved._retained_source_state(retained)["sources"]
if legacy_format:
- model_source = saved._retained_source_state(json.loads(replayed))[1]
+ model_source = latest.get("threatModelSource")
if model_source is not None:
assert (
json.loads((scan.scan_dir / model_source).read_text())["threatModel"]["summary"]
diff --git a/sdk/typescript/scripts/build-plugin.mjs b/sdk/typescript/scripts/build-plugin.mjs
index 40ec5d8ce..054e67e66 100644
--- a/sdk/typescript/scripts/build-plugin.mjs
+++ b/sdk/typescript/scripts/build-plugin.mjs
@@ -81,11 +81,7 @@ export async function buildBundledPlugin({
source = join(repositoryRoot, "plugins", "codex-security"),
} = {}) {
const contract = JSON.parse(await readFile(contractPath, "utf8"));
- const files = pluginContractFiles(contract);
- files.forEach(validatePath);
- if (new Set(files).size !== files.length) {
- throw new Error("Plugin projection contract contains duplicate paths.");
- }
+ const files = pluginContractFiles(contract, validatePath);
const copiedPaths = files.filter((path) => !path.startsWith("mcp/"));
const sourceFiles = await Promise.all(
@@ -115,7 +111,7 @@ export async function buildBundledPlugin({
const previous = join(staging, "previous");
let preservePrevious = false;
try {
- if (files.some((path) => path.startsWith("mcp/"))) {
+ if (copiedPaths.length < files.length) {
const mcpApp = join(source, "mcp-app");
execFileSync(
process.execPath,
diff --git a/sdk/typescript/scripts/check-package.mjs b/sdk/typescript/scripts/check-package.mjs
index 071681810..1189a0456 100644
--- a/sdk/typescript/scripts/check-package.mjs
+++ b/sdk/typescript/scripts/check-package.mjs
@@ -117,30 +117,19 @@ function validatePackagePaths(entries) {
"package/_bundled_plugin/.codex-plugin/plugin.json",
];
- for (const file of required) {
- if (!files.has(file)) throw new Error(`npm tarball is missing ${file}.`);
+ const allowedFiles = new Set();
+ function requireFile(path) {
+ if (!files.has(path)) throw new Error(`npm tarball is missing ${path}.`);
+ allowedFiles.add(path);
}
+ for (const path of required) requireFile(path);
const contract = JSON.parse(readFileSync(contractPath, "utf8"));
const pluginPaths = pluginContractFiles(contract);
- if (new Set(pluginPaths).size !== pluginPaths.length) {
- throw new Error("Plugin projection contract contains duplicate paths.");
- }
-
- const pluginEntries = new Set();
- for (const file of pluginPaths) {
- const pluginArchivePath = `package/_bundled_plugin/${file}`;
- pluginEntries.add(pluginArchivePath);
- if (!files.has(pluginArchivePath)) {
- throw new Error(`npm tarball is missing ${pluginArchivePath}.`);
- }
- }
- const distFiles = new Set(packageDistFiles);
- for (const file of distFiles) {
- if (!files.has(file)) throw new Error(`npm tarball is missing ${file}.`);
- }
- const allowedFiles = new Set([...required, ...distFiles, ...pluginEntries]);
+ for (const path of pluginPaths)
+ requireFile(`package/_bundled_plugin/${path}`);
+ for (const path of packageDistFiles) requireFile(path);
for (const file of [...allowedFiles]) {
const parts = file.split("/");
for (let index = 1; index < parts.length; index++) {
@@ -215,19 +204,15 @@ function privateExtractionDirectories(directory) {
}
function extractedArchiveFiles() {
- const expectedPaths = new Map();
- for (const { path } of rawEntries) {
- const directory = path.endsWith("/");
- const extractedPath = directory ? path.slice(0, -1) : path;
- const type = directory ? "directory" : "file";
- const previousType = expectedPaths.get(extractedPath);
- if (previousType !== undefined && previousType !== type) invalidTarEntry();
- expectedPaths.set(extractedPath, type);
- const parts = extractedPath.split("/");
- for (let index = 1; index < parts.length; index++) {
- const directory = parts.slice(0, index).join("/");
- if (storedArchive.files.has(directory)) invalidTarEntry();
- expectedPaths.set(directory, "directory");
+ const expectedSizes = new Map(
+ entries.map((path, index) => [path, listingSizes[index]]),
+ );
+ for (const path of entries) {
+ const parts = path.split("/");
+ for (let depth = 1; depth < parts.length; depth++) {
+ const parent = parts.slice(0, depth).join("/");
+ if (expectedSizes.has(parent)) invalidTarEntry();
+ expectedSizes.set(parent + "/", 0);
}
}
@@ -237,7 +222,8 @@ function extractedArchiveFiles() {
try {
chmodSync(extractionRoot, 0o700);
const fileList = join(extractionRoot, "members.list");
- writeFileSync(fileList, [...storedArchive.files.keys(), ""].join("\0"));
+ const fileNames = entries.filter((path) => !path.endsWith("/"));
+ writeFileSync(fileList, [...fileNames, ""].join("\0"));
try {
tar([
"-m",
@@ -260,47 +246,33 @@ function extractedArchiveFiles() {
rmSync(fileList);
const archiveFiles = new Map();
- let expandedBytes = 0;
function visit(directory, relative = "") {
for (const name of readdirSync(directory)) {
const path = relative === "" ? name : `${relative}/${name}`;
- const expectedType = expectedPaths.get(path);
- if (expectedType === undefined) invalidTarEntry();
const extractedPath = join(extractionRoot, path);
const stats = lstatSync(extractedPath);
- expectedPaths.delete(path);
-
- if (expectedType === "directory") {
+ if (expectedSizes.delete(path + "/")) {
if (!stats.isDirectory()) invalidTarEntry();
visit(extractedPath, path);
continue;
}
-
- const stored = storedArchive.files.get(path);
if (
!stats.isFile() ||
stats.nlink !== 1 ||
- stats.size !== stored?.length ||
- stats.size > MAX_EXPANDED_ASSET_BYTES ||
- expandedBytes > MAX_EXPANDED_ASSET_BYTES - stats.size
+ stats.size !== expectedSizes.get(path)
) {
invalidTarEntry();
}
- expandedBytes += stats.size;
+ expectedSizes.delete(path);
chmodSync(extractedPath, 0o600);
const contents = readFileSync(extractedPath);
- if (!contents.equals(stored)) invalidTarEntry();
+ if (!storedArchive.files.get(path)?.equals(contents)) invalidTarEntry();
archiveFiles.set(path, contents);
}
}
visit(extractionRoot);
- if (
- expectedPaths.size !== 0 ||
- archiveFiles.size !== storedArchive.files.size
- ) {
- invalidTarEntry();
- }
+ if (expectedSizes.size !== 0) invalidTarEntry();
return archiveFiles;
} finally {
rmSync(extractionRoot, { force: true, recursive: true });
diff --git a/sdk/typescript/scripts/generate-plugin-defaults.mjs b/sdk/typescript/scripts/generate-plugin-defaults.mjs
index 909447851..d26295583 100644
--- a/sdk/typescript/scripts/generate-plugin-defaults.mjs
+++ b/sdk/typescript/scripts/generate-plugin-defaults.mjs
@@ -26,13 +26,17 @@ async function generate(parser, source, ...outputs) {
const deepDefaults = JSON.parse(
await readFile(new URL("scripts/deep_scan_defaults.json", plugin), "utf8"),
);
+const deepSettings = await readFile(
+ new URL("mcp-app/src/deep-scan-settings.ts", plugin),
+ "utf8",
+);
const rubricPath = "references/default-severity-rubric.md";
const rubric = await readFile(new URL(rubricPath, plugin), "utf8");
for (const [filename, source] of [
[
"deep-scan-defaults.ts",
- `// Generated from the plugin deep_scan_defaults.json. Run pnpm build.\nexport const DEFAULT_DEEP_SCAN_SETTINGS = ${JSON.stringify(deepDefaults)} as const;\n`,
+ `// Generated from the plugin Deep Scan defaults and settings. Run pnpm build.\nexport const DEFAULT_DEEP_SCAN_SETTINGS = ${JSON.stringify(deepDefaults)} as const;\n\n${deepSettings}`,
],
[
"default-severity-rubric.ts",
diff --git a/sdk/typescript/scripts/plugin-contract.mjs b/sdk/typescript/scripts/plugin-contract.mjs
index 3c0fdd2e3..064f7b503 100644
--- a/sdk/typescript/scripts/plugin-contract.mjs
+++ b/sdk/typescript/scripts/plugin-contract.mjs
@@ -1,4 +1,4 @@
-export function pluginContractFiles(contract) {
+export function pluginContractFiles(contract, validatePath) {
const publicManifest = ".codex-plugin/plugin.json";
const { externalOwnedExact, shippedExact } = contract;
if (
@@ -15,8 +15,13 @@ export function pluginContractFiles(contract) {
);
}
- return [
+ const files = [
publicManifest,
...shippedExact.filter((path) => !path.startsWith("sdk/")),
];
+ if (validatePath) files.forEach(validatePath);
+ if (new Set(files).size !== files.length) {
+ throw new Error("Plugin projection contract contains duplicate paths.");
+ }
+ return files;
}
diff --git a/sdk/typescript/scripts/release-automation.mjs b/sdk/typescript/scripts/release-automation.mjs
index 1b821c65f..f0be4bb2a 100644
--- a/sdk/typescript/scripts/release-automation.mjs
+++ b/sdk/typescript/scripts/release-automation.mjs
@@ -601,6 +601,25 @@ function readProvenance(verified) {
* @param {WorkflowRelease} expected
*/
export function verifySignatureAudit(report, archive, expected) {
+ return verifyAuditForRun(report, archive, expected, String(expected.runId));
+}
+
+/**
+ * @param {ReleaseMetadata} report
+ * @param {Uint8Array} archive
+ * @param {RepositoryRelease} expected
+ */
+export function verifyRecoveredSignatureAudit(report, archive, expected) {
+ return verifyAuditForRun(report, archive, expected, null);
+}
+
+/**
+ * @param {ReleaseMetadata} report
+ * @param {Uint8Array} archive
+ * @param {RepositoryRelease} expected
+ * @param {string | null} expectedRunId Null recovers the original signed run.
+ */
+function verifyAuditForRun(report, archive, expected, expectedRunId) {
if (
!Array.isArray(report?.invalid) ||
!Array.isArray(report.missing) ||
@@ -701,21 +720,24 @@ export function verifySignatureAudit(report, archive, expected) {
expected.gitHead,
);
- const runId = String(expected.runId);
- if (!/^[1-9][0-9]*$/u.test(runId)) {
+ if (expectedRunId !== null && !/^[1-9][0-9]*$/u.test(expectedRunId)) {
throw new Error(
"Verified provenance requires a valid release workflow run.",
);
}
const invocation = statement.predicate?.runDetails?.metadata?.invocationId;
- const invocationPrefix = `${repository}/actions/runs/${runId}/attempts/`;
- if (
- typeof invocation !== "string" ||
- !invocation.startsWith(invocationPrefix) ||
- !/^[1-9][0-9]*$/u.test(invocation.slice(invocationPrefix.length))
- ) {
+ const prefix = `${repository}/actions/runs/`;
+ const match =
+ typeof invocation === "string" && invocation.startsWith(prefix)
+ ? /^([1-9][0-9]*)\/attempts\/([1-9][0-9]*)$/u.exec(
+ invocation.slice(prefix.length),
+ )
+ : null;
+ if (!match || (expectedRunId !== null && match[1] !== expectedRunId)) {
throw new Error(
- "Verified SLSA provenance must identify the successful release run.",
+ expectedRunId === null
+ ? "Verified SLSA provenance must identify the protected release run."
+ : "Verified SLSA provenance must identify the successful release run.",
);
}
@@ -739,66 +761,12 @@ export function verifySignatureAudit(report, archive, expected) {
version,
gitHead: expected.gitHead,
repository: expected.repository,
- runId,
- runAttempt: invocation.slice(invocationPrefix.length),
+ runId: match[1],
+ runAttempt: match[2],
sha512,
};
}
-/**
- * @param {ReleaseMetadata} report
- * @param {Uint8Array} archive
- * @param {RepositoryRelease} expected
- */
-export function verifyRecoveredSignatureAudit(report, archive, expected) {
- const version = releaseVersion({
- name: packageName,
- version: expected.version,
- });
- if (
- typeof expected.repository !== "string" ||
- !/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/u.test(expected.repository)
- ) {
- throw new Error("Verified provenance requires an exact GitHub repository.");
- }
-
- const verified = Array.isArray(report?.verified)
- ? report.verified.find(
- (candidate) =>
- candidate?.name === packageName && candidate.version === version,
- )
- : undefined;
- if (verified === undefined) {
- throw new Error(
- "The published package must have a cryptographically verified attestation.",
- );
- }
-
- const { statement } = readProvenance(verified);
-
- const prefix = `https://github.com/${expected.repository}/actions/runs/`;
- const invocation = statement?.predicate?.runDetails?.metadata?.invocationId;
- if (typeof invocation !== "string" || !invocation.startsWith(prefix)) {
- throw new Error(
- "Verified SLSA provenance must identify the protected release run.",
- );
- }
- const match = /^([1-9][0-9]*)\/attempts\/([1-9][0-9]*)$/u.exec(
- invocation.slice(prefix.length),
- );
- if (match === null) {
- throw new Error(
- "Verified SLSA provenance must identify the protected release run.",
- );
- }
-
- return verifySignatureAudit(report, archive, {
- ...expected,
- version,
- runId: match[1],
- });
-}
-
/**
* @param {ReleaseMetadata} release
* @param {Uint8Array} archive
@@ -874,17 +842,14 @@ function main() {
return;
}
- if (command === "require-published-increase" && process.argv.length === 4) {
- const publishedVersions = JSON.parse(readFileSync(0, "utf8"));
- console.log(
- requirePublishedReleaseIncrease(process.argv[3], publishedVersions),
- );
- return;
- }
-
- if (command === "release-mode" && process.argv.length === 4) {
- const publishedVersions = JSON.parse(readFileSync(0, "utf8"));
- console.log(publishedReleaseMode(process.argv[3], publishedVersions));
+ const historyCommands = {
+ "require-published-increase": requirePublishedReleaseIncrease,
+ "release-mode": publishedReleaseMode,
+ };
+ if (process.argv.length === 4 && Object.hasOwn(historyCommands, command)) {
+ const input = JSON.parse(readFileSync(0, "utf8"));
+ const result = historyCommands[command](process.argv[3], input);
+ console.log(result);
return;
}
diff --git a/sdk/typescript/scripts/smoke-package.mjs b/sdk/typescript/scripts/smoke-package.mjs
index ba0983a10..83859f513 100644
--- a/sdk/typescript/scripts/smoke-package.mjs
+++ b/sdk/typescript/scripts/smoke-package.mjs
@@ -18,6 +18,7 @@ import { isAbsolute, join, relative, resolve, sep } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { packageSmokeTimeouts } from "./package-smoke-timeouts.mjs";
import { resolveNpm } from "./package-smoke-npm.mjs";
+import { pluginContractFiles } from "./plugin-contract.mjs";
const {
commandTimeoutMs: PACKAGE_SMOKE_TIMEOUT_MS,
@@ -297,27 +298,7 @@ assert.equal(
`Packed npm tarball is not a regular file: ${archive}.`,
);
-const publicPluginManifest = ".codex-plugin/plugin.json";
-assert.ok(
- Array.isArray(pluginContract.externalOwnedExact) &&
- pluginContract.externalOwnedExact.includes(publicPluginManifest),
- "Plugin contract must include its public manifest.",
-);
-assert.ok(
- Array.isArray(pluginContract.shippedExact) &&
- pluginContract.shippedExact.every((path) => typeof path === "string"),
- "Plugin contract must declare its shipped files.",
-);
-
-const expectedPluginFiles = [
- publicPluginManifest,
- ...pluginContract.shippedExact.filter((path) => !path.startsWith("sdk/")),
-].sort();
-assert.equal(
- new Set(expectedPluginFiles).size,
- expectedPluginFiles.length,
- "Plugin contract must not contain duplicate installed paths.",
-);
+const expectedPluginFiles = pluginContractFiles(pluginContract).sort();
const consumer = await mkdtemp(join(tmpdir(), "codex-security-package-"));
try {
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index f9a9ac0d7..e2f1dfe73 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -428,6 +428,7 @@ import {
meetsSeverity,
type FailureSeverity,
type ResolvedScanSettings,
+ type ScanPromptSettings,
} from "./scan-settings.js";
const PROGRESS_REFRESH_MILLISECONDS = 1_000;
@@ -473,22 +474,26 @@ const SKILL_CONFIG_OPTIONS = MODEL_OPTIONS.extend({
.describe(SKILL_CODEX_OVERRIDE_DESCRIPTION),
});
type SkillConfiguration = z.infer<typeof SKILL_CONFIG_OPTIONS>;
-const CODEX_OVERRIDE_DESCRIPTION =
- 'Repeat TOML KEY=VALUE; e.g. model_reasoning_effort="high" or features.multi_agent_v2.max_concurrent_threads_per_session=4.';
-const PLUGIN_PATH_DESCRIPTION =
- "Codex Security plugin directory or ZIP (default: bundled plugin).";
-const PYTHON_PATH_DESCRIPTION =
- "Python interpreter (default: PYTHON or automatic discovery).";
-const RUNTIME_OPTION_SCHEMAS = {
- pluginPath: optionValue("--plugin-path")
- .optional()
- .describe(PLUGIN_PATH_DESCRIPTION),
- python: optionValue("--python").optional().describe(PYTHON_PATH_DESCRIPTION),
- codex: z
- .array(optionValue("--codex"))
- .default([])
- .describe(CODEX_OVERRIDE_DESCRIPTION),
-};
+const RUNTIME_OPTION_SCHEMAS = runtimeOptions(optionValue);
+
+function runtimeOptions(value: typeof optionValue) {
+ return {
+ pluginPath: value("--plugin-path")
+ .optional()
+ .describe(
+ "Codex Security plugin directory or ZIP (default: bundled plugin).",
+ ),
+ python: value("--python")
+ .optional()
+ .describe("Python interpreter (default: PYTHON or automatic discovery)."),
+ codex: z
+ .array(value("--codex"))
+ .default([])
+ .describe(
+ 'Repeat TOML KEY=VALUE; e.g. model_reasoning_effort="high" or features.multi_agent_v2.max_concurrent_threads_per_session=4.',
+ ),
+ };
+}
const SKILL_AUTH_OPTION = z
.enum(SCAN_AUTH_MODES)
.default("auto")
@@ -1217,7 +1222,6 @@ interface ScanArguments extends ResolvedScanSettings {
dryRun: boolean;
parentScanId?: string;
expectedPluginVersion?: string;
- includeFailureResults?: boolean;
}
interface ScanOutcome {
@@ -2402,6 +2406,16 @@ export async function main(
}
return finishScan(outcome, format, incurError, code);
};
+ const runScan = (format: string, args: ScanArguments) =>
+ withTerminalErrorsHandled(errorOutput, () =>
+ executeScan(
+ args,
+ errorOutput,
+ dependencies,
+ format !== "json" && format !== "jsonl",
+ includeFailureResults(format),
+ ),
+ );
const runImport = (options: ImportScanOptions) =>
withTerminalErrorsHandled(errorOutput, () =>
runScanImport(options, errorOutput, dependencies),
@@ -2944,14 +2958,8 @@ export async function main(
);
}
scanArguments = await prepareScanArgumentsFromRecipe(
- saved["recipe"],
+ saved,
"resume",
- {
- scanPrompt:
- typeof saved["userContext"] === "string"
- ? saved["userContext"]
- : undefined,
- },
dependencies.currentDirectory(),
dependencies.environment,
);
@@ -2967,8 +2975,6 @@ export async function main(
(saved["recipe"] as JsonObject)["scanInputs"],
);
}
- // Resume uses the installed engine with the saved recipe and checkpoints.
- scanArguments.expectedPluginVersion = undefined;
scanArguments.verbose = options.verbose;
scanArguments.showCost = options.showCost;
} catch (error) {
@@ -2981,13 +2987,7 @@ export async function main(
"SCAN_RESUME_UNAVAILABLE",
);
}
- scanArguments.includeFailureResults = includeFailureResults(format);
- const outcome = await runScan(
- scanArguments,
- errorOutput,
- dependencies,
- format !== "json" && format !== "jsonl",
- );
+ const outcome = await runScan(format, scanArguments);
return finishScanCommand(outcome, format, incurError);
},
})
@@ -3051,11 +3051,11 @@ export async function main(
if (scanId === undefined) return;
let scanArguments: ScanArguments;
try {
- const { recipe, scanId: resolvedScanId } =
- await dependencies.runWorkbench(
- workbenchArgs("get-scan-recipe", { scanId }),
- );
- const parentScanId = resolvedScanId as string;
+ const saved = await dependencies.runWorkbench(
+ workbenchArgs("get-scan-recipe", { scanId }),
+ );
+ const recipe = saved["recipe"];
+ const parentScanId = saved["scanId"] as string;
if (
isJsonObject(recipe) &&
recipe["import"] !== undefined &&
@@ -3097,33 +3097,15 @@ export async function main(
);
}
scanArguments = await prepareScanArgumentsFromRecipe(
- recipe,
+ saved,
"rerun",
- {
- scanPromptFile:
- options.scanPromptFile === undefined
- ? undefined
- : resolveCliPath(
- dependencies.currentDirectory(),
- options.scanPromptFile,
- ),
- validationPromptFile:
- options.validationPromptFile === undefined
- ? undefined
- : resolveCliPath(
- dependencies.currentDirectory(),
- options.validationPromptFile,
- ),
- },
dependencies.currentDirectory(),
dependencies.environment,
+ options,
);
scanArguments.verbose = options.verbose;
scanArguments.showCost = options.showCost;
scanArguments.parentScanId = parentScanId;
- scanArguments.rerunInputIdentity = isJsonObject(recipe)
- ? (recipe["scanInputs"] ?? null)
- : null;
} catch (error) {
const message = errorMessage(error);
errorOutput.write(`codex-security: ${diagnosticLines(message)}\n`);
@@ -3134,13 +3116,7 @@ export async function main(
"SCAN_REPLAY_UNAVAILABLE",
);
}
- scanArguments.includeFailureResults = includeFailureResults(format);
- const outcome = await runScan(
- scanArguments,
- errorOutput,
- dependencies,
- format !== "json" && format !== "jsonl",
- );
+ const outcome = await runScan(format, scanArguments);
return finishScanCommand(outcome, format, incurError);
},
})
@@ -4908,12 +4884,8 @@ export async function main(
if (argumentError !== undefined) return fail(argumentError, 2);
const directory = dependencies.currentDirectory();
const codexOverrides = await parseScanCodexOverrides(
- options.codex,
- options.model,
- options.effort,
- options.provider,
- undefined,
- scanCodexHome(dependencies.environment),
+ options,
+ dependencies.environment,
);
const outcome = await withTerminalErrorsHandled(errorOutput, () =>
runPolicyCommand(
@@ -5216,12 +5188,9 @@ export async function main(
maxCostUsd: options.maxCost,
maxTimeMinutes: options.maxTime,
codexOverrides: await parseScanCodexOverrides(
- options.codex,
- options.model,
- options.effort,
- options.provider,
+ options,
+ dependencies.environment,
project?.input.codex,
- scanCodexHome(dependencies.environment),
),
},
directory,
@@ -5237,36 +5206,30 @@ export async function main(
"--validate cannot be combined with a scan cost limit because standalone validation is not cost-tracked.",
);
}
- outcome = await runScan(
- {
- ...settings,
- rubricPath:
- options.rubric === undefined
- ? undefined
- : resolveCliPath(directory, options.rubric),
- codexOverrides: config.codexOverrides,
- projectConfig: provenance,
- workflowId: options.workflowId,
- safetyIdentifier: options.safetyIdentifier,
- verbose: options.verbose,
- repository: args.repository,
- archiveExisting: options.archiveExisting,
- pluginPath: options.pluginPath,
- pythonPath: options.python,
- patch: options.patch,
- validate: options.validate,
- patchSeverity: options.patchSeverity,
- createPr: options.createPr,
- showCost: options.showCost,
- headless: options.headless,
- dryRun: options.dryRun,
- mock: options.mock,
- includeFailureResults: includeFailureResults(format),
- },
- errorOutput,
- dependencies,
- format !== "json" && format !== "jsonl",
- );
+ outcome = await runScan(format, {
+ ...settings,
+ rubricPath:
+ options.rubric === undefined
+ ? undefined
+ : resolveCliPath(directory, options.rubric),
+ codexOverrides: config.codexOverrides,
+ projectConfig: provenance,
+ workflowId: options.workflowId,
+ safetyIdentifier: options.safetyIdentifier,
+ verbose: options.verbose,
+ repository: args.repository,
+ archiveExisting: options.archiveExisting,
+ pluginPath: options.pluginPath,
+ pythonPath: options.python,
+ patch: options.patch,
+ validate: options.validate,
+ patchSeverity: options.patchSeverity,
+ createPr: options.createPr,
+ showCost: options.showCost,
+ headless: options.headless,
+ dryRun: options.dryRun,
+ mock: options.mock,
+ });
} catch (error) {
const message = errorMessage(error);
errorOutput.write(`${diagnosticLines(message)}\n`);
@@ -5845,12 +5808,9 @@ export async function main(
knowledgeBasePaths: options.knowledgeBase,
maxCostUsd: options.maxCost,
codexOverrides: await parseScanCodexOverrides(
- options.codex,
- options.model,
- options.effort,
- options.provider,
+ options,
+ dependencies.environment,
project?.input.codex,
- scanCodexHome(dependencies.environment),
),
},
directory,
@@ -6061,16 +6021,7 @@ export async function main(
maxCost: ScanSettingsSchema.shape.maxCostUsd.describe(
"Stop each repository attempt if estimated USD cost exceeds AMOUNT.",
),
- pluginPath: z
- .string()
- .min(1)
- .optional()
- .describe(PLUGIN_PATH_DESCRIPTION),
- python: z.string().min(1).optional().describe(PYTHON_PATH_DESCRIPTION),
- codex: z
- .array(z.string().min(1))
- .default([])
- .describe(CODEX_OVERRIDE_DESCRIPTION),
+ ...runtimeOptions(() => z.string().min(1)),
}),
examples: [
{
@@ -6174,12 +6125,9 @@ export async function main(
codexOverrides: mergeCodexOverrides(
resolved.config.codexOverrides,
await parseScanCodexOverrides(
- options.codex,
- options.model,
- options.effort,
- options.provider,
+ options,
+ dependencies.environment,
project?.input.codex,
- scanCodexHome(dependencies.environment),
),
),
pluginPath: options.pluginPath,
@@ -6246,14 +6194,8 @@ export async function main(
return undefined;
}
const recipe = await prepareScanArgumentsFromRecipe(
- saved["recipe"],
+ saved,
"resume",
- {
- scanPrompt:
- typeof saved["userContext"] === "string"
- ? saved["userContext"]
- : undefined,
- },
currentDirectory,
dependencies.environment,
);
@@ -7414,70 +7356,24 @@ export async function main(
const commands = Cli.toCommands.get(cli)!;
const valueOptions = cliValueOptions(commands);
- function isScanImportCommand(argv: readonly string[]): boolean {
- const commandIndex = cliCommandIndex(argv);
- return argv[commandIndex] === "scan" && argv[commandIndex + 1] === "import";
- }
-
- function normalizeScanImportArguments(
- argv: readonly string[],
- ): readonly string[] {
- if (!isScanImportCommand(argv)) return argv;
- const normalized: string[] = [];
- const subcommandIndex = cliCommandIndex(argv) + 1;
- for (let index = 0; index < argv.length; index += 1) {
- const argument = argv[index]!;
- const next = argv[index + 1];
- // Incur reserves bare --json for output, but parses --json=FILE normally.
- if (
- index > subcommandIndex &&
- argument === "--json" &&
- next !== undefined &&
- !next.startsWith("--") &&
- next !== "-h"
- ) {
- normalized.push(`--json=${next}`);
- index += 1;
- } else {
- normalized.push(argument);
- }
- }
- return normalized;
- }
-
- function cliCommandIndex(argv: readonly string[]): number {
- return argv.findIndex((value, index) => {
- if (value.startsWith("-")) return false;
- return index === 0 || !valueOptions.has(argv[index - 1]!);
- });
- }
-
- function defaultListCommand(argv: readonly string[]): readonly string[] {
- const commandIndex = cliCommandIndex(argv);
- if (
- commandIndex < 0 ||
- !["scans", "findings"].includes(argv[commandIndex]!) ||
- argv.includes("--help") ||
- argv.includes("-h")
- ) {
- return argv;
- }
- const following = argv[commandIndex + 1];
- if (following !== undefined && !following.startsWith("-")) return argv;
- return [
- ...argv.slice(0, commandIndex + 1),
- "list",
- ...argv.slice(commandIndex + 1),
- ];
+ const commandIndex = argv.findIndex((value, index) => {
+ if (value.startsWith("-")) return false;
+ return index === 0 || !valueOptions.has(argv[index - 1]!);
+ });
+ const command = argv[commandIndex];
+ const following = argv[commandIndex + 1];
+ const scanImport = command === "scan" && following === "import";
+ if (
+ (command === "scans" || command === "findings") &&
+ !argv.includes("--help") &&
+ !argv.includes("-h") &&
+ (following === undefined || following.startsWith("-"))
+ ) {
+ argv = argv.toSpliced(commandIndex + 1, 0, "list");
}
- function validateCliArguments(
- argv: readonly string[],
- positionals: string[],
- ): string | undefined {
+ function validateCliArguments(): string | undefined {
if (argv.includes("--help") || argv.includes("-h")) return undefined;
- const commandIndex = cliCommandIndex(argv);
- const command = argv[commandIndex];
if (command === undefined || !commands.has(command)) return undefined;
const structuredOutput = argv.some(
(value, index) =>
@@ -7541,7 +7437,6 @@ export async function main(
return "Markdown output is not supported for scan results.";
}
}
- const scanImport = isScanImportCommand(argv);
const nestedCommand =
scanImport ||
command === "scans" ||
@@ -7634,20 +7529,38 @@ export async function main(
return `Unexpected positional argument for ${command}${subcommand === undefined ? "" : ` ${subcommand}`}.`;
}
}
- argv = normalizeScanImportArguments(defaultListCommand(argv));
- const frameworkArgv = argv.flatMap((argument) => {
+ const firstImportOption = scanImport ? commandIndex + 2 : argv.length;
+ const frameworkArgv: string[] = [];
+ for (let index = 0; index < argv.length; index++) {
+ const argument = argv[index]!;
+ const next = argv[index + 1];
const separator = argument.indexOf("=");
if (
- separator < 0 ||
- !FRAMEWORK_VALUE_OPTIONS.has(argument.slice(0, separator))
- )
- return [argument];
- return [argument.slice(0, separator), argument.slice(separator + 1)];
- });
+ index >= firstImportOption &&
+ argument === "--json" &&
+ next !== undefined &&
+ !next.startsWith("--") &&
+ next !== "-h"
+ ) {
+ // Incur reserves bare --json for output, but parses --json=FILE normally.
+ frameworkArgv.push(`--json=${next}`);
+ index++;
+ } else if (
+ separator >= 0 &&
+ FRAMEWORK_VALUE_OPTIONS.has(argument.slice(0, separator))
+ ) {
+ frameworkArgv.push(
+ argument.slice(0, separator),
+ argument.slice(separator + 1),
+ );
+ } else {
+ frameworkArgv.push(argument);
+ }
+ }
const frameworkFlags = frameworkOutputFlags(frameworkArgv);
const policyFullOutput =
- argv[cliCommandIndex(argv)] === "policy" && argv.includes("--full-output");
- const argumentError = validateCliArguments(argv, positionals);
+ command === "policy" && argv.includes("--full-output");
+ const argumentError = validateCliArguments();
if (argumentError !== undefined && !policyFullOutput) {
errorOutput.write(`codex-security: ${argumentError}\n`);
return 2;
@@ -7676,7 +7589,7 @@ export async function main(
// Incur cannot mount a command with both a handler and subcommands.
// Select the nested import route while preserving scan [repository].
- if (isScanImportCommand(argv)) {
+ if (scanImport) {
cli.command(
Cli.create("scan", {
description: "Run or import a saved scan.",
@@ -7949,18 +7862,14 @@ async function runScanImport(
}
async function prepareScanArgumentsFromRecipe(
- recipe: JsonValue | undefined,
+ { recipe, userContext }: JsonObject,
operation: "resume" | "rerun",
+ directory: string,
+ environment: CliDependencies["environment"],
{
- scanPrompt,
scanPromptFile,
validationPromptFile,
- }: Pick<
- ScanArguments,
- "scanPrompt" | "scanPromptFile" | "validationPromptFile"
- >,
- directory: string,
- environment: CliDependencies["environment"],
+ }: Pick<ScanPromptSettings, "scanPromptFile" | "validationPromptFile"> = {},
): Promise<ScanArguments> {
if (recipe === undefined || !isJsonObject(recipe)) {
throw new CodexSecurityError(
@@ -8115,6 +8024,10 @@ async function prepareScanArgumentsFromRecipe(
"The saved scan recipe contains invalid launch settings.",
);
}
+ const scanPrompt =
+ operation === "resume" && typeof userContext === "string"
+ ? userContext
+ : undefined;
const prompts = await resolveScanPrompts(
{ scanPrompt, scanPromptFile, validationPromptFile },
repository,
@@ -8166,8 +8079,12 @@ async function prepareScanArgumentsFromRecipe(
maxTimeMinutes,
dryRun: false,
...(recipe["mock"] === true ? { mock: true } : {}),
+ ...(operation === "rerun"
+ ? { rerunInputIdentity: recipe["scanInputs"] ?? null }
+ : {}),
+ // Resumes use the installed engine with the saved checkpoints.
expectedPluginVersion:
- typeof recipe["pluginVersion"] === "string"
+ operation === "rerun" && typeof recipe["pluginVersion"] === "string"
? recipe["pluginVersion"]
: undefined,
};
@@ -10329,17 +10246,6 @@ async function chooseInteractiveAuthentication(
);
}
-async function runScan(
- arguments_: ScanArguments,
- errorOutput: Writable,
- dependencies: CliDependencies,
- interactive = true,
-): Promise<ScanOutcome> {
- return await withTerminalErrorsHandled(errorOutput, () =>
- executeScan(arguments_, errorOutput, dependencies, interactive),
- );
-}
-
async function withTerminalErrorsHandled<T>(
errorOutput: Writable,
operation: () => Promise<T>,
@@ -10361,7 +10267,8 @@ async function executeScan(
arguments_: ScanArguments,
errorOutput: Writable,
dependencies: CliDependencies,
- interactive = true,
+ interactive: boolean,
+ retainFailureResults: boolean,
): Promise<ScanOutcome> {
let registeredScan:
Parameters<NonNullable<ScanOptions["onScanRegistered"]>>[0] | undefined;
@@ -11260,10 +11167,7 @@ async function executeScan(
}
let failureData: Record<string, unknown> | undefined;
- if (
- arguments_.includeFailureResults &&
- (failed || requestedSignal !== null)
- ) {
+ if (retainFailureResults && (failed || requestedSignal !== null)) {
if (result !== null) {
failureData = result.toJSON();
if (validation !== undefined)
@@ -12265,13 +12169,15 @@ function resolveCliScope(
}
async function parseScanCodexOverrides(
- values: readonly string[],
- model: string | undefined,
- effort: ModelCliOptions["effort"],
- provider: "openai" | "amazon-bedrock" | ExternalModelProvider | undefined,
- defaults: JsonObject | undefined,
- profileHome: string,
+ options: ModelCliOptions & {
+ codex: readonly string[];
+ provider: z.infer<typeof PROVIDER_OPTION>;
+ },
+ environment: NodeJS.ProcessEnv,
+ defaults?: JsonObject,
): Promise<JsonObject> {
+ const { codex: values, model, effort, provider } = options;
+ const profileHome = scanCodexHome(environment);
if (!isExternalModelProvider(provider) && provider !== "amazon-bedrock") {
return parseCodexOverrides(values, model, effort, provider, defaults);
}
diff --git a/sdk/typescript/src/custom-publish.ts b/sdk/typescript/src/custom-publish.ts
index 1432c920c..44ce5cb66 100644
--- a/sdk/typescript/src/custom-publish.ts
+++ b/sdk/typescript/src/custom-publish.ts
@@ -71,15 +71,7 @@ export async function publishScanToCustomInternal(
telemetry.notify(result.telemetry!);
return result;
} catch (error) {
- telemetry.notify(
- telemetry.snapshot(
- telemetry.outcome === "indeterminate"
- ? "indeterminate"
- : options.signal?.aborted
- ? "canceled"
- : "failed",
- ),
- );
+ telemetry.notifyFailure(options);
throw error;
}
}
diff --git a/sdk/typescript/src/deep-config.ts b/sdk/typescript/src/deep-config.ts
index b3534a5b3..71013b75a 100644
--- a/sdk/typescript/src/deep-config.ts
+++ b/sdk/typescript/src/deep-config.ts
@@ -4,11 +4,14 @@ import { basename, dirname, join } from "node:path";
import { parse as parseToml, type TomlTable } from "smol-toml";
import { writeCodexConfig, type JsonObject } from "./config.js";
import { canonicalConfigPath } from "./runtime.js";
-import { DEFAULT_DEEP_SCAN_SETTINGS } from "./deep-scan-defaults.js";
+import {
+ DEEP_SCAN_SETTINGS,
+ DEFAULT_DEEP_SCAN_SETTINGS,
+ hoursConflict,
+} from "./deep-scan-defaults.js";
import { CodexSecurityError } from "./errors.js";
import {
DEFAULT_SCAN_MODE,
- DEEP_SCAN_SETTINGS,
DeepScanSettingsSchema,
ScanSettingsSchema,
type DeepScanOptions,
@@ -24,7 +27,6 @@ export interface ResolvedDeepScanConfig {
sources: DeepScanSources;
source: string;
document: TomlTable;
- overrides: DeepScanOptions;
}
export function deepScanOptions(
@@ -54,11 +56,7 @@ export function deepScanOptions(
);
}
const hours = minutes.data / 60;
- if (
- selected.maxTimeHours !== undefined &&
- Math.abs(selected.maxTimeHours - hours) >
- Number.EPSILON * Math.max(selected.maxTimeHours, hours)
- ) {
+ if (hoursConflict(hours, selected.maxTimeHours)) {
throw new CodexSecurityError(
"Conflicting maxTimeMinutes / --max-time and maxTimeHours / --max-time-hours settings.",
);
@@ -119,26 +117,23 @@ export async function resolveDeepScanConfig(
`Unknown Codex Security Deep Scan configuration ${unknown.join(", ")} in ${source}.`,
);
}
- const settings = {
- ...DEFAULT_DEEP_SCAN_SETTINGS,
- } as Required<DeepScanOptions>;
+ const settings: Required<DeepScanOptions> = { ...DEFAULT_DEEP_SCAN_SETTINGS };
const sources = {} as DeepScanSources;
for (const [name, key] of DEEP_SCAN_SETTINGS) {
- let value = Object.hasOwn(configured, key)
- ? configured[key]
- : DEFAULT_DEEP_SCAN_SETTINGS[name];
- if (name === "workers" && value === "auto")
- value = DEFAULT_DEEP_SCAN_SETTINGS.workers;
- if (explicit[name] !== undefined) value = explicit[name];
+ const legacy = Object.hasOwn(configured, key) ? configured[key] : undefined;
+ const override = explicit[name];
sources[name] =
- explicit[name] !== undefined
+ override !== undefined
? "override"
- : Object.hasOwn(configured, key)
+ : legacy !== undefined
? "legacy"
: "default";
+ const value = override ?? legacy ?? settings[name];
settings[name] = requireDeepScanValue(
name,
- value,
+ name === "workers" && value === "auto"
+ ? DEFAULT_DEEP_SCAN_SETTINGS.workers
+ : value,
sources[name] === "legacy" ? `${key} in ${source}` : name,
);
}
@@ -147,7 +142,6 @@ export async function resolveDeepScanConfig(
sources,
source,
document,
- overrides: explicit,
};
}
@@ -160,29 +154,24 @@ export async function writeDeepScanConfig(
canonicalConfigPath(resolved.source),
runtimeConfigPath(destination),
]);
- let document = resolved.document;
- const sameFile = source === target;
- if (sameFile) {
- if (Object.keys(resolved.overrides).length === 0) return;
- document = await readDeepScanDocument(destination);
- }
- // An isolated runtime needs a complete snapshot. An ambient file keeps
- // inherited defaults unset so future releases can still update them.
- const settings = sameFile ? resolved.overrides : resolved.settings;
- const retainAmbientSettings =
- sameFile &&
- DEEP_SCAN_SETTINGS.some(([name]) => settings[name] === undefined);
+ const ambient = source === target;
+ const fields = DEEP_SCAN_SETTINGS.filter(
+ ([name]) => !ambient || resolved.sources[name] === "override",
+ );
+ if (fields.length === 0) return;
+ const document = ambient
+ ? await readDeepScanDocument(destination)
+ : resolved.document;
+ // A complete snapshot replaces the Deep table. Partial ambient writes preserve other keys.
await writeCodexConfig(destination, {
...document,
...(workerRuntime === undefined ? {} : { worker_runtime: workerRuntime }),
deep_scan: {
- ...(retainAmbientSettings
+ ...(fields.length < DEEP_SCAN_SETTINGS.length
? (document["deep_scan"] as TomlTable | undefined)
: {}),
...Object.fromEntries(
- DEEP_SCAN_SETTINGS.filter(([name]) => settings[name] !== undefined).map(
- ([name, key]) => [key, settings[name]],
- ),
+ fields.map(([name, key]) => [key, resolved.settings[name]]),
),
},
} as JsonObject);
diff --git a/sdk/typescript/src/deep-scan-defaults.ts b/sdk/typescript/src/deep-scan-defaults.ts
index 57b167f63..4740f8f86 100644
--- a/sdk/typescript/src/deep-scan-defaults.ts
+++ b/sdk/typescript/src/deep-scan-defaults.ts
@@ -1,4 +1,4 @@
-// Generated from the plugin deep_scan_defaults.json. Run pnpm build.
+// Generated from the plugin Deep Scan defaults and settings. Run pnpm build.
export const DEFAULT_DEEP_SCAN_SETTINGS = {
workers: 4,
subagents: 3,
@@ -7,3 +7,32 @@ export const DEFAULT_DEEP_SCAN_SETTINGS = {
maxDiscoveryRuns: 40,
maxTimeHours: 96,
} as const;
+
+// SDK name, legacy TOML key, project key, and CLI flag (when exposed).
+export const DEEP_SCAN_SETTINGS = [
+ ["workers", "workers", "workers", "--workers"],
+ ["subagents", "subagents", "subagents_per_worker", "--subagents"],
+ [
+ "stopAfterNoNew",
+ "stop_after_no_new",
+ "stop_after_no_new",
+ "--stop-after-no-new",
+ ],
+ [
+ "stopAfterConsecutiveErrors",
+ "stop_after_consecutive_errors",
+ "stop_after_consecutive_errors",
+ null,
+ ],
+ [
+ "maxDiscoveryRuns",
+ "max_discovery_runs",
+ "max_discovery_runs",
+ "--max-discovery-runs",
+ ],
+ ["maxTimeHours", "max_time_hours", "max_time_hours", "--max-time-hours"],
+] as const;
+
+export function hoursConflict(hours: number, prior = hours) {
+ return Math.abs(prior - hours) > Number.EPSILON * Math.max(prior, hours);
+}
diff --git a/sdk/typescript/src/external-findings-publish.ts b/sdk/typescript/src/external-findings-publish.ts
index ca8444220..c4a5a94b7 100644
--- a/sdk/typescript/src/external-findings-publish.ts
+++ b/sdk/typescript/src/external-findings-publish.ts
@@ -10,6 +10,7 @@ import {
rm,
} from "node:fs/promises";
import { join } from "node:path";
+import { nullIfMissingFile } from "./targets.js";
import { setTimeout as delay } from "node:timers/promises";
import {
isBoxedPrimitive,
@@ -490,10 +491,7 @@ function sameFindingSelection(
}
async function readOptionalFile(path: string): Promise<string | undefined> {
- return readFile(path, "utf8").catch((error: NodeJS.ErrnoException) => {
- if (error.code === "ENOENT") return undefined;
- throw error;
- });
+ return (await readFile(path, "utf8").catch(nullIfMissingFile)) ?? undefined;
}
export function externalPublicationStateDirectory(
@@ -542,16 +540,14 @@ function validatePublicationReceipt(
batch: FindingImportRequest,
): FindingImportReceipt {
const receipt = validateImportReceipt(value);
- const batchItems = new Map(batch.items.map((item) => [item.client_id, item]));
+ const remaining = new Set(batch.items.map((item) => item.client_id));
if (
receipt.id !== batch.request_id ||
wireJson(receipt.repository) !== wireJson(batch.repository) ||
wireJson(receipt.source) !== wireJson(batch.source) ||
receipt.item_count !== batch.items.length ||
receipt.results.length !== batch.items.length ||
- new Set(receipt.results.map((item) => item.client_id)).size !==
- batch.items.length ||
- receipt.results.some((item) => !batchItems.has(item.client_id))
+ receipt.results.some((item) => !remaining.delete(item.client_id))
)
throw new CodexSecurityError(
"Cloud returned a receipt for a different publication.",
@@ -603,12 +599,7 @@ async function withImportLocks<T>(
try {
for (const key of [...new Set(keys)].sort()) {
const path = join(state, `${key}.lock.sqlite`);
- const metadata = await lstat(path).catch(
- (error: NodeJS.ErrnoException) => {
- if (error.code === "ENOENT") return undefined;
- throw error;
- },
- );
+ const metadata = await lstat(path).catch(nullIfMissingFile);
if (metadata && !metadata.isFile())
throw new CodexSecurityError(
"The publication lock is not a regular file.",
@@ -1569,6 +1560,9 @@ async function preparePublicationInput(
? (url: string) => cloudSourceKey(url, "github")
: cloudRepositoryUrlKey;
const state = externalPublicationStateDirectory(environment);
+ function pendingFile(key: string) {
+ return join(state, `${key}.pending.json`);
+ }
let previousFindings: ExternalFindings | undefined;
async function previousFindingsIfSaved(
input: PublicationSelection,
@@ -1578,12 +1572,7 @@ async function preparePublicationInput(
return previousFindings;
// An empty state directory (or just this current receipt) needs only the
// current reader for its preview.
- const entries = await readdir(state).catch(
- (error: NodeJS.ErrnoException) => {
- if (error.code === "ENOENT") return [];
- throw error;
- },
- );
+ const entries = (await readdir(state).catch(nullIfMissingFile)) ?? [];
if (
entries.some(
(name) =>
@@ -1773,9 +1762,7 @@ async function preparePublicationInput(
if (projectionKeys.has(candidateKey)) continue;
// New imports use the first reconstructed projection; an exact pending key
// takes precedence, including an earlier SDK's unmodified original input.
- capturedPending = await readOptionalFile(
- join(state, candidateKey + ".pending.json"),
- );
+ capturedPending = await readOptionalFile(pendingFile(candidateKey));
if (projection === resolved || capturedPending !== undefined)
projection = candidate;
projectionKeys.add(candidateKey);
@@ -1809,7 +1796,7 @@ async function preparePublicationInput(
key: string,
context: () => Promise<PrivateImportContext>,
): Promise<{ pending: string | undefined } | undefined> {
- const pending = await readOptionalFile(join(state, key + ".pending.json"));
+ const pending = await readOptionalFile(pendingFile(key));
if (pending !== undefined) return { pending };
const saved = await readOptionalFile(
join(state, key + ".private-import.json"),
@@ -1894,9 +1881,7 @@ async function preparePublicationInput(
originalWireKey,
localeKey(original.parsed.findings),
])) {
- const bytes = await readOptionalFile(
- join(state, `${pathKey}.pending.json`),
- );
+ const bytes = await readOptionalFile(pendingFile(pathKey));
if (bytes === undefined) continue;
const record = JSON.parse(bytes) as SavedSubmission | null;
if (
@@ -2158,11 +2143,7 @@ async function preparePublicationInput(
content: SavedSubmission,
retire: () => Promise<void>,
) {
- if (
- content.requests.some(
- (batch) => batch.repository.reset_marker !== destination.reset_marker,
- )
- ) {
+ if (wasReset(content)) {
if (hosted) await confirmReset();
await retire();
throw new CloudImportError(
@@ -2171,6 +2152,20 @@ async function preparePublicationInput(
);
}
}
+ function retirePendingIfReset(
+ content: SavedSubmission,
+ keys: string[],
+ path: string,
+ remove = () => rm(path),
+ ) {
+ return retireIfReset(content, () =>
+ underLocks(keys, async () => {
+ const bytes = await readOptionalFile(path);
+ if (bytes !== undefined && sameSubmission(JSON.parse(bytes), content))
+ await remove();
+ }),
+ );
+ }
function readSaved(
content: SavedSubmission | null | undefined,
): SavedSubmission {
@@ -2302,7 +2297,7 @@ async function preparePublicationInput(
}
const key = scopeKey(currentIdentity);
const stagingKey = originalKey ?? key;
- const canonicalPath = join(state, `${key}.pending.json`);
+ const canonicalPath = pendingFile(key);
const lookupPath = join(state, `${key}.lookup.json`);
// A lookup only suggests a regular file to validate. A missing or stale hint
// leaves the same on-disk discovery available as before.
@@ -2327,7 +2322,7 @@ async function preparePublicationInput(
let recovered: SavedSubmission | undefined;
for (const legacyKey of legacyKeys) {
if (legacyKey === key || legacyKey === originalKey) continue;
- const legacyPath = join(state, `${legacyKey}.pending.json`);
+ const legacyPath = pendingFile(legacyKey);
const captured = await readOptionalFile(legacyPath);
if (captured !== undefined) {
// Preserve requests staged by the earlier locale-based filename format.
@@ -2625,10 +2620,7 @@ async function preparePublicationInput(
state,
undefined,
true,
- ).catch((error: NodeJS.ErrnoException) => {
- if (error.code === "ENOENT") return undefined;
- throw error;
- });
+ ).catch(nullIfMissingFile);
if (!directory) return undefined;
const known = new Set(
[key, originalKey, knownFileKey].filter((value) => value !== undefined),
@@ -2642,7 +2634,7 @@ async function preparePublicationInput(
selected?: Checkpoint,
contents?: string,
): Promise<Checkpoint | undefined> {
- const path = join(state, `${fileKey}.pending.json`);
+ const path = pendingFile(fileKey);
const serialized =
contents ??
(selected?.fileKey === key && !known.has(fileKey)
@@ -2684,16 +2676,7 @@ async function preparePublicationInput(
throw new CodexSecurityError(
"The pending request and this preview have different repository reset markers. Review the destination again.",
);
- await retireIfReset(record, () =>
- underLocks([fileKey], async () => {
- const bytes = await readOptionalFile(path);
- if (
- bytes !== undefined &&
- sameSubmission(JSON.parse(bytes), record)
- )
- await rm(path);
- }),
- );
+ await retirePendingIfReset(record, [fileKey], path);
}
throw new CodexSecurityError(originalHostedRetry);
}
@@ -2760,9 +2743,7 @@ async function preparePublicationInput(
if (!selected && knownFileKey !== undefined && !hosted) return undefined;
const hint = await lookupFileKey();
if (hint && !known.has(hint)) {
- const contents = await readOptionalRegularFile(
- join(state, `${hint}.pending.json`),
- );
+ const contents = await readOptionalRegularFile(pendingFile(hint));
if (contents !== undefined) {
selected = await readCandidate(hint, selected, contents);
known.add(hint);
@@ -2779,12 +2760,10 @@ async function preparePublicationInput(
selected.content.inputProjection?.identityFormat === 2)
)
return selected;
- const entries = await readdir(state, { withFileTypes: true }).catch(
- (error: NodeJS.ErrnoException) => {
- if (error.code === "ENOENT") return [];
- throw error;
- },
- );
+ const entries =
+ (await readdir(state, { withFileTypes: true }).catch(
+ nullIfMissingFile,
+ )) ?? [];
for (const entry of entries) {
if (!entry.isFile() || !/^[a-f0-9]{64}\.pending\.json$/u.test(entry.name))
continue;
@@ -2850,7 +2829,7 @@ async function preparePublicationInput(
}
let selected = await selectedCheckpoint(recovered ? key : undefined, true);
const fileKey = selected?.fileKey ?? (recovered ? key : stagingKey);
- const pendingPath = join(state, `${fileKey}.pending.json`);
+ const pendingPath = pendingFile(fileKey);
async function rememberFile() {
if (fileKey !== key && (await lookupFileKey()) !== fileKey)
await writeAtomicJson(lookupPath, fileKey).catch(() => undefined);
@@ -2894,18 +2873,8 @@ async function preparePublicationInput(
const saved = selected?.content ?? recovered;
if (historicalOnly && !saved) throw unmatchedPreviousNumbers();
if (!parsed.findings.length) throw new NoPublishableFindingsError(parsed);
- if (saved) {
- await retireIfReset(saved, () =>
- underLocks(lockKeys, async () => {
- const current = await readOptionalFile(pendingPath);
- if (
- current !== undefined &&
- sameSubmission(JSON.parse(current) as SavedSubmission, saved)
- )
- await removePending();
- }),
- );
- }
+ if (saved)
+ await retirePendingIfReset(saved, lockKeys, pendingPath, removePending);
if (saved?.inspectorProjectMapRequired && !inspectorProjectMap)
throw new CodexSecurityError(
"Supply the verified Inspector project binding again to resume.",
@@ -3718,7 +3687,7 @@ async function preparePublicationInput(
if (!submission.receipts?.length) throw error;
throw interruptedPreview(errorMessage(error), error);
});
- const pending = selected?.content;
+ let pending = selected?.content;
const current = pending ?? submission;
const adoptProjectMap =
submission.inspectorProjectMapRequired &&
@@ -3758,14 +3727,13 @@ async function preparePublicationInput(
submission.approvedRepositoryPlatform === "other" &&
!current.approvedRepositoryPlatform;
if (adoptStrictRepository) current.approvedRepositoryPlatform = "other";
- const receipts = (current.receipts ?? []).map(validateImportReceipt);
- if (receipts.length > requests.length)
+ const cached = (current.receipts ?? []).map(validateImportReceipt);
+ if (cached.length > requests.length)
throw new CodexSecurityError(
"Saved publication has more receipts than requests.",
);
const acknowledged: FindingImportReceipt[] = [];
let verified = 0;
- let persisted = pending !== undefined;
try {
if (selected?.checkedCanonical) current.canonicalChecked = true;
if (pending === undefined) {
@@ -3778,9 +3746,8 @@ async function preparePublicationInput(
if (
otherKey !== key &&
otherKey !== fileKey &&
- (await readOptionalFile(
- join(state, otherKey + ".pending.json"),
- )) !== undefined
+ (await readOptionalFile(pendingFile(otherKey))) !==
+ undefined
)
throw new CodexSecurityError(
"Another publication prepared this input. Run the command again to review and resume that saved request.",
@@ -3793,10 +3760,9 @@ async function preparePublicationInput(
}
await writeAtomicJson(pendingPath, current);
});
- persisted = true;
+ pending = current;
} else if (adoptProjectMap || adoptStrictRepository) {
await writeAtomicJson(pendingPath, current);
- persisted = true;
} else if (selected?.checkedCanonical) {
// The existing approval and receipts are usable if caching the scan fails.
await writeAtomicJson(pendingPath, current).catch(() => undefined);
@@ -3810,7 +3776,7 @@ async function preparePublicationInput(
let acknowledgedCount = 0;
for (const [batchIndex, batch] of requests.entries()) {
const receipt = validatePublicationReceipt(
- receipts[batchIndex] ??
+ cached[batchIndex] ??
(await request(`${importPrefix}/finding_imports`, batch)),
batch,
);
@@ -3821,20 +3787,19 @@ async function preparePublicationInput(
completed: acknowledgedCount,
total: direct.findings.length,
});
- if (batchIndex === receipts.length) {
- receipts.push(receipt);
+ if (batchIndex >= cached.length) {
// Acknowledged batches must not consume another POST quota on retry.
// Readback can still resume after this checkpoint without replaying writes.
await writeAtomicJson(pendingPath, {
...submission,
canonicalChecked: current.canonicalChecked,
- receipts,
+ receipts: acknowledged,
});
}
}
// Verify readable source records without mistaking a newer concurrent
// observation for failure of the original, immutable import receipt.
- const readbacks = receipts.flatMap((receipt, batchIndex) => {
+ const readbacks = acknowledged.flatMap((receipt, batchIndex) => {
const batch = requests[batchIndex]!;
const batchItems = new Map(
batch.items.map((item) => [item.client_id, item]),
@@ -3893,16 +3858,16 @@ async function preparePublicationInput(
} catch (error) {
if (error instanceof CloudImportError && error.status === 409) {
await removePending();
- persisted = false;
+ pending = undefined;
}
- const message = `${error instanceof Error ? error.message : String(error)} ${error instanceof CloudImportError && error.status === 409 ? "The old submission was retired. Rediscover and approve a fresh publication." : persisted ? `Repeat the same command to resume the saved request. Saved submission: ${pendingPath}` : "No resumable request was saved. Correct the error and review the input before retrying."}`;
+ const message = `${error instanceof Error ? error.message : String(error)} ${error instanceof CloudImportError && error.status === 409 ? "The old submission was retired. Rediscover and approve a fresh publication." : pending ? `Repeat the same command to resume the saved request. Saved submission: ${pendingPath}` : "No resumable request was saved. Correct the error and review the input before retrying."}`;
throw new ExternalPublicationError(
message,
{
...result(acknowledged, verified),
status: "interrupted",
error: message,
- ...(persisted ? { savedSubmission: pendingPath } : {}),
+ ...(pending ? { savedSubmission: pendingPath } : {}),
},
error,
);
diff --git a/sdk/typescript/src/external-findings-read.ts b/sdk/typescript/src/external-findings-read.ts
index 9ae6ea919..1bc0fb8a8 100644
--- a/sdk/typescript/src/external-findings-read.ts
+++ b/sdk/typescript/src/external-findings-read.ts
@@ -225,18 +225,6 @@ export async function readExternalFindings(
if (target === "cloud") requireCloudReader(options);
return { findings: await readTenableFindings(path, url) };
}
- if (provider === "trivy") {
- const args = [
- await readScannerJson(path),
- target,
- options.trivyScanRoot,
- ] as const;
- return withReplay(
- normalizeTrivyFindings(...args),
- needsPreviousTrivyProjection,
- () => normalizePreviousTrivyFindings(...args),
- );
- }
if (provider === "sonar") {
const findings = await readSonarFindings(path, url, target);
const binding = findings.report?.["repositoryUrl"];
@@ -257,20 +245,6 @@ export async function readExternalFindings(
const findings = await normalizeSnykFile(input, target, references);
return { findings, sourceRepositories: snykFileRepositories(input) };
}
- if (provider === "endor") {
- const input = await readScannerJson(path);
- const findings = normalizeEndorFindings(input, target);
- return {
- ...withReplay(findings, hasPreviousCloudProjection, () =>
- normalizePreviousEndorFindings(input, target),
- ),
- sourceRepositories:
- sarifFileRepositories(input, "Endor") ??
- findings.findings.flatMap((row) =>
- localFindingRepositories(provider, row),
- ),
- };
- }
if (provider === "semgrep") {
const input = await readSemgrepReport(path);
const findings = normalizeSemgrepFindings(
@@ -303,9 +277,28 @@ export async function readExternalFindings(
sourceRepositories: [...wiz.sourceRepositories, ...sourceRepositories],
};
}
- const mapping =
- options.inspectorProjectMap && new Map(options.inspectorProjectMap);
const input = await readScannerJson(path);
+ if (provider === "trivy") {
+ const args = [input, target, options.trivyScanRoot] as const;
+ return withReplay(
+ normalizeTrivyFindings(...args),
+ needsPreviousTrivyProjection,
+ () => normalizePreviousTrivyFindings(...args),
+ );
+ }
+ if (provider === "endor") {
+ const findings = normalizeEndorFindings(input, target);
+ return {
+ ...withReplay(findings, hasPreviousCloudProjection, () =>
+ normalizePreviousEndorFindings(input, target),
+ ),
+ sourceRepositories:
+ sarifFileRepositories(input, "Endor") ??
+ findings.findings.flatMap((row) =>
+ localFindingRepositories(provider, row),
+ ),
+ };
+ }
const [normalize, previous, couldDiffer] = (
{
inspector: [
@@ -330,7 +323,7 @@ export async function readExternalFindings(
],
} as const
)[provider];
- const args = [input, url, target, mapping] as const;
+ const args = [input, url, target, options.inspectorProjectMap] as const;
return {
...withReplay(normalize(...args), couldDiffer, () => previous(...args)),
sourceRepositories: [{ url }],
diff --git a/sdk/typescript/src/private-finding-import.ts b/sdk/typescript/src/private-finding-import.ts
index 1326e2e7d..ee457d352 100644
--- a/sdk/typescript/src/private-finding-import.ts
+++ b/sdk/typescript/src/private-finding-import.ts
@@ -295,7 +295,7 @@ const checkpointSchema = z.object({
accountId: z.string(),
contextVersion: z.string(),
sha256: z.string(),
- source: z.object({ provider: z.string(), source_key: z.string() }),
+ source: jobSchema.shape.source,
repository: z.object({ id: z.string(), repo_connector_id: z.string() }),
uploadRequestId: z.uuid(),
jobRequestId: z.uuid(),
@@ -311,6 +311,24 @@ const checkpointSchema = z.object({
previousReceipt: jobSchema.refine(unsuccessfulJob).optional(),
});
+function matchesJobScope(
+ job: PrivateImportJob,
+ scope: Pick<
+ z.infer<typeof checkpointSchema>,
+ "format" | "source" | "repository"
+ >,
+) {
+ const [target] = job.default_targets;
+ return (
+ job.input.format === scope.format &&
+ job.source.provider === scope.source.provider &&
+ job.source.source_key === scope.source.source_key &&
+ job.default_targets.length === 1 &&
+ target?.repo_id === scope.repository.id &&
+ target.repo_connector_id === scope.repository.repo_connector_id
+ );
+}
+
export function unsuccessfulJob(job: PrivateImportJob | undefined) {
return job?.status === "failed" || job?.status === "cancelled";
}
@@ -333,47 +351,24 @@ export function readPrivateCheckpoint(
) {
if (serialized === undefined) return;
const checkpoint = parsePrivateCheckpoint(serialized);
- const {
- uploadRequestId,
- jobRequestId,
- jobApiVersion,
- approvedRepositoryUrl,
- approvedRepositoryPlatform,
- fileId,
- jobId,
- receipt,
- previousReceipt,
- contextVersion,
- format,
- sha256,
- ...scope
- } = checkpoint;
- const {
- contextVersion: currentContext,
- format: selectedFormat,
- sha256: selectedDigest,
- ...selectedScope
- } = expected;
- const sameContext = contextVersion === currentContext;
+ const { fileId, jobId, receipt, previousReceipt } = checkpoint;
+ const { apiBaseUrl, accountId, source, repository } = checkpoint;
+ const { contextVersion, format, sha256, ...scope } = expected;
+ const sameContext = checkpoint.contextVersion === contextVersion;
if (
- canonicalJson(scope) !== canonicalJson(selectedScope) ||
- (sameContext && (format !== selectedFormat || sha256 !== selectedDigest)) ||
+ canonicalJson({ apiBaseUrl, accountId, source, repository }) !==
+ canonicalJson(scope) ||
+ (sameContext &&
+ (checkpoint.format !== format || checkpoint.sha256 !== sha256)) ||
(jobId && !fileId) ||
[receipt, previousReceipt].some(
- (job) =>
- job &&
- (job.input.format !== format ||
- canonicalJson(job.source) !== canonicalJson(scope.source) ||
- job.default_targets.length !== 1 ||
- job.default_targets[0]?.repo_id !== scope.repository.id ||
- job.default_targets[0]?.repo_connector_id !==
- scope.repository.repo_connector_id),
+ (job) => job && !matchesJobScope(job, checkpoint),
) ||
(receipt &&
(receipt.id !== jobId ||
- receipt.request_id !== jobRequestId ||
+ receipt.request_id !== checkpoint.jobRequestId ||
receipt.input.file_id !== fileId)) ||
- uploadRequestId === jobRequestId
+ checkpoint.uploadRequestId === checkpoint.jobRequestId
)
throw new CodexSecurityError(
"Saved private import does not match this account, repository or report.",
@@ -523,23 +518,12 @@ export function privateImportClient(
(id !== undefined && job.id !== id) ||
job.request_id !== input.requestId ||
job.input.file_id !== input.fileId ||
- job.input.format !== input.format ||
- job.source.provider !== input.source.provider ||
- job.source.source_key !== input.source.source_key ||
- job.default_targets.length !== 1 ||
- job.default_targets[0]?.repo_id !== input.repository.id ||
- job.default_targets[0]?.repo_connector_id !==
- input.repository.repo_connector_id
+ !matchesJobScope(job, input)
)
throw new CodexSecurityError(
"Cloud returned a job for a different private import.",
);
- if (
- job.status === "completed" ||
- job.status === "failed" ||
- job.status === "cancelled"
- )
- return job;
+ if (job.status === "completed" || unsuccessfulJob(job)) return job;
if (!id) {
id = job.id;
await accepted(id);
diff --git a/sdk/typescript/src/publication-telemetry.ts b/sdk/typescript/src/publication-telemetry.ts
index b44df33ef..8779dd55e 100644
--- a/sdk/typescript/src/publication-telemetry.ts
+++ b/sdk/typescript/src/publication-telemetry.ts
@@ -66,6 +66,16 @@ export class PublicationTelemetryRecorder {
});
}
+ notifyFailure(options: { signal?: AbortSignal }): void {
+ const outcome =
+ this.outcome === "indeterminate"
+ ? "indeterminate"
+ : options.signal?.aborted
+ ? "canceled"
+ : "failed";
+ this.notify(this.snapshot(outcome));
+ }
+
notify(telemetry: PublicationTelemetry): void {
void Promise.resolve()
.then(() => this.observer?.(telemetry))
diff --git a/sdk/typescript/src/publish.ts b/sdk/typescript/src/publish.ts
index 9ffce19f2..8d213b914 100644
--- a/sdk/typescript/src/publish.ts
+++ b/sdk/typescript/src/publish.ts
@@ -265,15 +265,7 @@ export async function publishScanInternal(
telemetry.notify(result.telemetry);
return result;
} catch (error) {
- telemetry.notify(
- telemetry.snapshot(
- telemetry.outcome === "indeterminate"
- ? "indeterminate"
- : options.signal?.aborted
- ? "canceled"
- : "failed",
- ),
- );
+ telemetry.notifyFailure(options);
throw error;
}
}
diff --git a/sdk/typescript/src/scan-settings.ts b/sdk/typescript/src/scan-settings.ts
index 06e042e2f..ab3ef8a02 100644
--- a/sdk/typescript/src/scan-settings.ts
+++ b/sdk/typescript/src/scan-settings.ts
@@ -23,30 +23,7 @@ export const SCAN_SEVERITIES = [
export const FailureSeveritySchema = z.enum(REPORTABLE_SEVERITIES);
export type FailureSeverity = z.infer<typeof FailureSeveritySchema>;
-// SDK name, legacy TOML key, project key, and CLI flag (when exposed).
-export const DEEP_SCAN_SETTINGS = [
- ["workers", "workers", "workers", "--workers"],
- ["subagents", "subagents", "subagents_per_worker", "--subagents"],
- [
- "stopAfterNoNew",
- "stop_after_no_new",
- "stop_after_no_new",
- "--stop-after-no-new",
- ],
- [
- "stopAfterConsecutiveErrors",
- "stop_after_consecutive_errors",
- "stop_after_consecutive_errors",
- null,
- ],
- [
- "maxDiscoveryRuns",
- "max_discovery_runs",
- "max_discovery_runs",
- "--max-discovery-runs",
- ],
- ["maxTimeHours", "max_time_hours", "max_time_hours", "--max-time-hours"],
-] as const;
+export { DEEP_SCAN_SETTINGS } from "./deep-scan-defaults.js";
const positiveIntegerError = "must be a positive integer";
const positiveInteger = z
diff --git a/sdk/typescript/tests-ts/custom-publish.test.ts b/sdk/typescript/tests-ts/custom-publish.test.ts
index 1b76f1eae..bc437b1da 100644
--- a/sdk/typescript/tests-ts/custom-publish.test.ts
+++ b/sdk/typescript/tests-ts/custom-publish.test.ts
@@ -194,35 +194,39 @@ test("does not retry failed uploads or report incomplete receipts as successful"
}
});
-test("keeps an aborted upload outcome indeterminate after submission", async () => {
- const { scan } = await fixture();
- const controller = new AbortController();
- const reason = new Error("Synthetic upload cancellation");
- const observed: PublicationTelemetry[] = [];
- const fetchMock = mock(async () => {
- controller.abort(reason);
- throw reason;
- });
- await expect(
- publishScanToCustom(
- scan,
+test.each([false, true])(
+ "keeps an aborted upload indeterminate after submission (options disposed: %s)",
+ async (disposeOptions) => {
+ const { scan } = await fixture();
+ const controller = new AbortController();
+ const reason = new Error("Synthetic upload cancellation");
+ const observed: PublicationTelemetry[] = [];
+ const { proxy, revoke } = Proxy.revocable(
{
findingsUrl: "http://synthetic.test",
signal: controller.signal,
- onTelemetry: (event) => observed.push(event),
+ onTelemetry: (event: PublicationTelemetry) => observed.push(event),
},
- { fetch: fetchMock },
- ),
- ).rejects.toBe(reason);
- expect(fetchMock).toHaveBeenCalledTimes(1);
- expect(observed).toEqual([
- expect.objectContaining({
- outcome: "indeterminate",
- attempted: true,
- counts: expect.objectContaining({ acknowledged: null, failed: null }),
- }),
- ]);
-});
+ {},
+ );
+ const fetchMock = mock(async () => {
+ controller.abort(reason);
+ if (disposeOptions) revoke();
+ throw reason;
+ });
+ await expect(
+ publishScanToCustom(scan, proxy, { fetch: fetchMock }),
+ ).rejects.toBe(reason);
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ expect(observed).toEqual([
+ expect.objectContaining({
+ outcome: "indeterminate",
+ attempted: true,
+ counts: expect.objectContaining({ acknowledged: null, failed: null }),
+ }),
+ ]);
+ },
+);
test.each(["failed", "canceled"] as const)(
"observes custom publication %s before preparation without replacing the error",
diff --git a/sdk/typescript/tests-ts/package-tar-listing.test.ts b/sdk/typescript/tests-ts/package-tar-listing.test.ts
index 48448f9f9..3b55f937f 100644
--- a/sdk/typescript/tests-ts/package-tar-listing.test.ts
+++ b/sdk/typescript/tests-ts/package-tar-listing.test.ts
@@ -17,6 +17,7 @@ import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import { brotliCompressSync, gzipSync } from "node:zlib";
import { describe, expect, test } from "bun:test";
+import packageJson from "../package.json" with { type: "json" };
import {
archive,
cleanCompressedPayload,
@@ -79,14 +80,9 @@ function packageTar({
const paths = [
"package/package.json",
"package/README.md",
- "package/docs/cli.md",
- "package/docs/findings-service.md",
- "package/docs/dedupe-records.md",
- "package/docs/classify-severity-records.md",
- "package/docs/hosted-scans.md",
- "package/docs/validate-records.md",
- "package/docs/hosted-remediation.md",
- "package/docs/telemetry.md",
+ ...packageJson.files
+ .filter((path) => path.startsWith("docs/"))
+ .map((path) => `package/${path}`),
"package/schemas/hosted-scan-v2.fixture.json",
"package/schemas/project-config.schema.json",
"package/schemas/external-findings.schema.json",
diff --git a/sdk/typescript/tests-ts/release-automation.test.ts b/sdk/typescript/tests-ts/release-automation.test.ts
index 04a6cd68e..fafa1df95 100644
--- a/sdk/typescript/tests-ts/release-automation.test.ts
+++ b/sdk/typescript/tests-ts/release-automation.test.ts
@@ -237,7 +237,7 @@ function runPublisher({
return { status, stdout, stderr, calls };
}
-function releaseLabelGhMock(handlers: readonly string[], shiftEndpoint = true) {
+function releaseLabelGhMock(handlers: readonly string[]) {
return [
"gh() {",
' if [[ "$1" != "api" ]]; then return 64; fi',
@@ -248,12 +248,13 @@ function releaseLabelGhMock(handlers: readonly string[], shiftEndpoint = true) {
" shift 2",
" fi",
' local endpoint="$1"',
- ...(shiftEndpoint ? [" shift"] : []),
+ " shift",
' case "$method $endpoint" in',
- ...handlers,
' "GET repos/test/codex-security/issues/17")',
- " printf '%s' \"$MOCK_PR_TITLE\" | base64",
+ ' [[ $# == 2 && "$1" == --jq ]] || return 64',
+ ` jq ${process.platform === "win32" ? "--binary " : ""}-r "$2" <<< "$MOCK_ISSUE"`,
" ;;",
+ ...handlers,
' "POST repos/test/codex-security/issues/17/labels")',
" printf '%s\\n' \"$@\"",
" ;;",
@@ -631,16 +632,15 @@ function runReleaseLabels(
title: string,
routes: readonly string[],
setup: readonly string[] = [],
- shiftEndpoint = true,
) {
- const mock = [...setup, releaseLabelGhMock(routes, shiftEndpoint)].join("\n");
+ const mock = [...setup, releaseLabelGhMock(routes)].join("\n");
const script = workflowStepShell(
releaseLabelsWorkflow,
"Categorize pull request without checking out its code",
);
return runShell(`${mock}\n${script}`, {
GITHUB_REPOSITORY: "test/codex-security",
- MOCK_PR_TITLE: title,
+ MOCK_ISSUE: JSON.stringify({ title }),
PR_NUMBER: "17",
});
}
@@ -2158,10 +2158,8 @@ ${workflowStepShell(releaseCutWorkflow, "Resolve the stable package version")}
const outputs = readFileSync(outputPath, "utf8");
expect(outputs).toContain(`changed=${changed}`);
if (changed) {
- expect(outputs).toContain(`version=${currentVersion}`);
- expect(outputs).toContain(`tag=npm-v${currentVersion}`);
+ expect(outputs.split("\n")).toContain(`tag=npm-v${currentVersion}`);
} else {
- expect(outputs).not.toContain("version=");
expect(outputs).not.toContain("tag=");
}
},
@@ -4079,16 +4077,13 @@ PATH="$RUNNER_TEMP:$PATH" verify`,
"fix: preserve a trailing carriage return\r",
])("rejects nonconventional pull request title %s", async (title) => {
const result = await runReleaseLabels(title, [
- ' "GET repos/test/codex-security/issues/17")',
- " printf '%s' \"$MOCK_PR_TITLE\" | base64",
- " ;;",
" *)",
" printf '%s\\n' 'unexpected label mutation'",
" return 70",
" ;;",
]);
- expect(result.status).toBe(1);
+ expect(result.status, result.stderr).toBe(1);
expect(result.stderr).toContain(
"Pull request title must follow <type>[optional scope][!]: <description>.",
);
@@ -4136,7 +4131,7 @@ PATH="$RUNNER_TEMP:$PATH" verify`,
],
);
- expect(result.status).toBe(0);
+ expect(result.status, result.stderr).toBe(0);
expect(result.stdout).toContain(
"Preserving existing skip-release-notes label.",
);
@@ -4159,9 +4154,6 @@ PATH="$RUNNER_TEMP:$PATH" verify`,
test("preserves the latest unattributed skip label after earlier automation", async () => {
const result = await runReleaseLabels("feat: customer-visible change", [
- ' "GET repos/test/codex-security/issues/17")',
- " printf '%s' 'feat: customer-visible change' | base64",
- " ;;",
' "GET repos/test/codex-security/issues/17/labels")',
" printf '%s\\n' skip-release-notes",
" ;;",
@@ -4178,7 +4170,7 @@ PATH="$RUNNER_TEMP:$PATH" verify`,
" ;;",
]);
- expect(result.status).toBe(0);
+ expect(result.status, result.stderr).toBe(0);
expect(result.stdout).toContain(
"Preserving existing skip-release-notes label.",
);
@@ -4209,7 +4201,7 @@ PATH="$RUNNER_TEMP:$PATH" verify`,
" ;;",
]);
- expect(result.status).toBe(0);
+ expect(result.status, result.stderr).toBe(0);
expect(result.stdout).toContain(
"removed automatically applied skip-release-notes",
);
@@ -4251,7 +4243,7 @@ PATH="$RUNNER_TEMP:$PATH" verify`,
" ;;",
]);
- expect(result.status).toBe(0);
+ expect(result.status, result.stderr).toBe(0);
expect(result.stdout).toContain(`labels[]=${label}`);
});
@@ -4265,9 +4257,6 @@ PATH="$RUNNER_TEMP:$PATH" verify`,
const result = await runReleaseLabels(
title,
[
- ' "GET repos/test/codex-security/issues/17")',
- " printf '%s' \"$MOCK_PR_TITLE\" | base64",
- " ;;",
' "GET repos/test/codex-security/issues/17/labels")',
" return 0",
" ;;",
@@ -4285,10 +4274,9 @@ PATH="$RUNNER_TEMP:$PATH" verify`,
" *) return 66 ;;",
],
["skip_label_exists=0"],
- false,
);
- expect(result.status).toBe(0);
+ expect(result.status, result.stderr).toBe(0);
expect(result.stdout).toContain("applied skip-release-notes");
},
);
@@ -4318,32 +4306,17 @@ test.skipIf(process.platform === "win32").each([
])(
"uses the manual $manual release category over stale $stale after retitling to $title",
async ({ title, manual, stale }) => {
- const script = workflowStepShell(
- releaseLabelsWorkflow,
- "Categorize pull request without checking out its code",
+ const result = await runReleaseLabels(
+ title,
+ [
+ ` "GET repos/test/codex-security/issues/17/labels") printf '%s\\n' "$MANUAL_LABEL" "$STALE_LABEL" ;;`,
+ ` "GET repos/test/codex-security/issues/17/timeline?per_page=100") printf '%s\\t%s\\n' "$MANUAL_LABEL" synthetic-reviewer "$STALE_LABEL" 'github-actions[bot]' ;;`,
+ ' "DELETE repos/test/codex-security/issues/17/labels/$STALE_LABEL") echo removed-stale-label ;;',
+ ' "DELETE "*) echo removed-manual-label; return 70 ;;',
+ ' "POST "*) echo added-automatic-category ;;',
+ ],
+ [`MANUAL_LABEL=${manual}`, `STALE_LABEL=${stale}`],
);
- const fixture = `gh() {
- case "$*" in
- "api repos/test/codex-security/issues/17 --jq "*) printf '%s' "$MOCK_PR_TITLE" | base64 ;;
- "api repos/test/codex-security/issues/17/labels --jq "*) printf '%s\\n' "$MANUAL_LABEL" "$STALE_LABEL" ;;
- "api repos/test/codex-security/issues/17/timeline?per_page=100 "*) printf '%s\\t%s\\n' "$MANUAL_LABEL" synthetic-reviewer "$STALE_LABEL" 'github-actions[bot]' ;;
- "api --method DELETE repos/test/codex-security/issues/17/labels/$STALE_LABEL --silent") echo removed-stale-label ;;
- "api --method DELETE "*) echo removed-manual-label; return 70 ;;
- "api --method POST "*) echo added-automatic-category ;;
- *) return 65 ;;
- esac
- }`;
- const result = await runCommand(bash, ["-c", `${fixture}\n${script}`], {
- env: {
- ...process.env,
- GITHUB_REPOSITORY: "test/codex-security",
- PR_NUMBER: "17",
- MOCK_PR_TITLE: title,
- MANUAL_LABEL: manual,
- STALE_LABEL: stale,
- },
- timeout: 10_000,
- });
expect(result.status, result.stderr).toBe(0);
expect(result.stdout).toContain(`Preserving existing ${manual} label.`);
expect(result.stdout).toContain("removed-stale-label");