OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
#!/usr/bin/env node
import assert from "node:assert/strict";
import { extractTriageResult, parseCaseOutcome } from "./sastbench-result.mts";
import { afterEach as addSastBenchMetrics } from "../assertions/sastbench-metrics.mts";
import verdictAssertion from "../assertions/sastbench-verdict.mts";
function triageResult(verdict: string, inputId = "sastbench-000000") {
return {
schema_version: "triage-finding/v0",
repository: { path: "/target", revision: "a".repeat(40) },
findings: [
{
input_id: inputId,
source_type: "scanner_ticket",
verdict,
evidence: ['Literal ```code``` fence, "quoted" and \\ escaped text'],
},
],
};
}
const fenced = `Summary\n\n\`\`\`json\n${JSON.stringify(triageResult("confirmed"), null, 2)}\n\`\`\``;
assert.equal(extractTriageResult(fenced).findings[0].verdict, "confirmed");
assert.equal(
extractTriageResult(JSON.stringify(triageResult("not_actionable")))
.findings[0].verdict,
"not_actionable",
);
assert.equal(
extractTriageResult("```json\ninvalid\n```\n" + fenced).findings[0].verdict,
"confirmed",
);
for (const output of [
"```json\ninvalid\n```\n" + JSON.stringify(triageResult("confirmed")),
"Summary: " + JSON.stringify(triageResult("confirmed")),
"```json " + JSON.stringify(triageResult("confirmed")) + "```",
"```json\ntruncated output\n".repeat(16_000),
]) {
assert.throws(() => extractTriageResult(output), {
message: "Could not find a parseable triage-finding/v0 JSON result",
});
}
const truncated =
'```json\n{"schema_version":"triage-finding/v0","findings":[{"evidence":["' +
'\\"x'.repeat(32_000);
assert.throws(() => extractTriageResult(truncated), {
message: "Could not find a parseable triage-finding/v0 JSON result",
});
const truncatedMetrics = addSastBenchMetrics(
extensionContext("sastbench-000000", "true_positive", { output: truncated }),
).result;
assert.equal(truncatedMetrics.metadata.sastbench.status, "invalid_output");
assert.equal(truncatedMetrics.namedScores.invalid_output, 1);
for (const output of [
JSON.stringify(triageResult("confirmed")),
JSON.stringify(triageResult("confirmed"), null, 2),
fenced.replace("```json", "```"),
fenced.replaceAll("\n", "\r\n"),
"```typescript\nconst value = 1;\n```\n\n" + fenced,
truncated + "\n```\n" + fenced,
'Prose {"other": true}\n' + fenced.replaceAll("```", " ```"),
'```json\n"invalid\n```\n' + fenced,
]) {
assert.equal(
parseCaseOutcome(output, "sastbench-000000").verdict,
"confirmed",
);
const result = addSastBenchMetrics(
extensionContext("sastbench-000000", "true_positive", { output }),
).result;
assert.equal(result.metadata.sastbench.status, "ok");
assert.equal(result.namedScores.strict_tp, 1);
assert.equal(result.namedScores.strict_fn, 0);
}
assert.throws(
() =>
parseCaseOutcome(
JSON.stringify(triageResult("confirmed", "wrong")),
"sastbench-000000",
),
/input_id mismatch/,
);
assert.throws(
() =>
parseCaseOutcome(
JSON.stringify({ ...triageResult("confirmed"), findings: [] }),
"sastbench-000000",
),
/exactly one finding/,
);
assert.throws(
() =>
parseCaseOutcome(
JSON.stringify(triageResult("unexpected")),
"sastbench-000000",
),
/unsupported verdict/,
);
assert.equal(
verdictAssertion(fenced, {
vars: { case_id: "sastbench-000000" },
test: { metadata: { ground_truth: "true_positive" } },
}).pass,
true,
);
assert.equal(
verdictAssertion(fenced, {
vars: { case_id: "sastbench-000000" },
test: { metadata: { ground_truth: "false_positive" } },
}).pass,
false,
);
function extensionContext(
caseId: string,
groundTruth: string,
{ output, error }: { output?: string; error?: string },
sampleWeight?: number,
) {
return {
test: {
metadata: {
ground_truth: groundTruth,
...(sampleWeight === undefined ? {} : { sample_weight: sampleWeight }),
},
},
result: {
vars: { case_id: caseId },
latencyMs: 1234,
cost: 0.25,
namedScores: { existing_metric: 1 },
response:
output === undefined
? { error }
: {
output,
tokenUsage: { prompt: 10, completion: 5, total: 15, cached: 2 },
sessionId: "session",
},
failureReason: error ? "error" : undefined,
},
};
}
const nativeContexts = [
["tp-confirmed", "true_positive", "confirmed"],
["tp-closed", "true_positive", "not_actionable"],
["tp-review", "true_positive", "needs_review"],
["fp-closed", "false_positive", "not_actionable"],
["fp-confirmed", "false_positive", "confirmed"],
["fp-review", "false_positive", "needs_review"],
].map(([caseId, groundTruth, verdict]) =>
extensionContext(caseId, groundTruth, {
output: JSON.stringify(triageResult(verdict, caseId)),
}),
);
const nativeMetricRows = nativeContexts.map(addSastBenchMetrics);
const totals: Record<string, number> = {};
for (const row of nativeMetricRows) {
for (const [name, value] of Object.entries(row.result.namedScores)) {
totals[name] = (totals[name] || 0) + value;
}
}
assert.deepEqual(
{
tp: totals.strict_tp,
tn: totals.strict_tn,
fp: totals.strict_fp,
fn: totals.strict_fn,
},
{ tp: 1, tn: 1, fp: 2, fn: 2 },
);
assert.deepEqual(
{
tp: totals.decided_tp,
tn: totals.decided_tn,
fp: totals.decided_fp,
fn: totals.decided_fn,
},
{ tp: 1, tn: 1, fp: 1, fn: 1 },
);
assert.equal(totals.decided_cases, 4);
assert.equal(totals.positive_cases, 3);
assert.equal(totals.negative_cases, 3);
assert.equal(totals.retained_positive, 2);
assert.equal(totals.unsafe_closure, 1);
assert.equal(totals.false_alert_auto_closure, 1);
assert.equal(totals.false_alert_escalation, 1);
assert.equal(totals.confirmed_true, 1);
assert.equal(totals.confirmed_total, 2);
assert.equal(totals.abstention, 2);
assert.equal(totals.remaining_analyst_work, 4);
assert.equal(totals.execution_or_parse_error, 0);
assert.equal(totals.existing_metric, 6);
const positiveProviderError = addSastBenchMetrics(
extensionContext("tp-error", "true_positive", { error: "provider failed" }),
).result;
assert.equal(positiveProviderError.namedScores.strict_fn, 1);
assert.equal(positiveProviderError.namedScores.execution_or_parse_error, 1);
assert.equal(positiveProviderError.namedScores.remaining_analyst_work, 1);
assert.equal(positiveProviderError.metadata.sastbench.status, "model_error");
const negativeInvalidOutput = addSastBenchMetrics(
extensionContext("fp-invalid", "false_positive", {
output: "not a triage result",
}),
).result;
assert.equal(negativeInvalidOutput.namedScores.strict_fp, 1);
assert.equal(negativeInvalidOutput.namedScores.execution_or_parse_error, 1);
assert.equal(negativeInvalidOutput.metadata.sastbench.status, "invalid_output");
const weightedPositive = addSastBenchMetrics(
extensionContext(
"weighted-tp",
"true_positive",
{ output: JSON.stringify(triageResult("confirmed", "weighted-tp")) },
2.5,
),
).result;
assert.equal(weightedPositive.namedScores.strict_tp, 2.5);
assert.equal(weightedPositive.namedScores.positive_cases, 2.5);
assert.equal(weightedPositive.namedScores.existing_metric, 1);
const firstResult = nativeMetricRows[0].result;
assert.equal(firstResult.metadata.sastbench.status, "ok");
assert.equal(firstResult.metadata.sastbench.verdict, "confirmed");
assert.equal(firstResult.latencyMs, 1234);
assert.equal(firstResult.cost, 0.25);
assert.equal(firstResult.response, nativeContexts[0].result.response);
assert.match(
negativeInvalidOutput.metadata.sastbench.error!,
/triage-finding\/v0/,
);
assert.equal(positiveProviderError.metadata.sastbench.error, "provider failed");
console.log("sastbench result and native metric tests passed");