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(imports): prepare Veracode selection filters once per report (#2248)

Michael D'Angelo50f926f395f7
diff --git a/sdk/typescript/src/veracode-findings.ts b/sdk/typescript/src/veracode-findings.ts
index 2b78596b9..09a361c14 100644
--- a/sdk/typescript/src/veracode-findings.ts
+++ b/sdk/typescript/src/veracode-findings.ts
@@ -26,7 +26,8 @@ const positive = z.int().positive();
const cvssScore = z
.union([z.string().trim().min(1), z.number()])
.pipe(z.coerce.number<string | number>().min(0).max(10));
-const scores = ["severity", "cvss"] as const;
+const idFields = ["cwe", "finding_category"] as const;
+const scoreFields = ["severity", "cvss"] as const;
const pageMetadata = z.object({
total_elements: count,
number: count,
@@ -233,7 +234,7 @@ function selection(
throw new Error(`Invalid Veracode ${key} selection.`);
query.set(key, flags[0]!.toLowerCase());
}
- for (const key of ["cwe", "finding_category"]) {
+ for (const key of idFields) {
const ids = query
.getAll(key)
.flatMap((value) =>
@@ -241,7 +242,7 @@ function selection(
);
if (ids.length) query.set(key, [...new Set(ids)].sort().join(","));
}
- for (const field of scores) {
+ for (const field of scoreFields) {
for (const key of [field, `${field}_gte`]) {
const values = query.getAll(key);
if (!values.length) continue;
@@ -361,6 +362,82 @@ function readPage(input: unknown) {
return { rows, scope, query, filter, number, size, total, pages };
}
+function rowSelectionValidator(query: URLSearchParams) {
+ const staticOnly = query.has("scan_type");
+ const violates = query.get("violates_policy");
+ const newOnly = query.get("new") === "true";
+ const excludeAnnotations = query.get("include_annot") === "false";
+ const idFilters = idFields.flatMap((field) => {
+ const value = query.get(field);
+ return value === null ? [] : [{ field, ids: new Set(value.split(",")) }];
+ });
+ const scoreFilters = scoreFields.flatMap((field) => {
+ const exact = query.get(field);
+ const minimum = query.get(`${field}_gte`);
+ if (exact === null && minimum === null) return [];
+ return [
+ {
+ field,
+ exact: exact === null ? undefined : Number(exact),
+ minimum: minimum === null ? undefined : Number(minimum),
+ },
+ ];
+ });
+
+ return (row: Record<string, unknown>) => {
+ if (
+ newOnly &&
+ record(row["finding_status"], "Veracode finding_status")["new"] !== true
+ )
+ throw new Error("Veracode finding contradicts the new selection.");
+ if (
+ excludeAnnotations &&
+ Array.isArray(row["annotations"]) &&
+ row["annotations"].length
+ )
+ throw new Error(
+ "Veracode finding contradicts the include_annot selection.",
+ );
+ if (idFilters.length || scoreFilters.length) {
+ const details = record(
+ row["finding_details"],
+ "Veracode finding_details",
+ );
+ for (const { field, ids } of idFilters) {
+ if (row["scan_type"] !== "STATIC") continue;
+ const entry = record(details[field], `Veracode ${field}`);
+ const id = integer(entry["id"], `${field} ID`);
+ if (!ids.has(String(id)))
+ throw new Error(
+ `Veracode finding contradicts the ${field} selection.`,
+ );
+ }
+ for (const { field, exact, minimum } of scoreFilters) {
+ const score =
+ field === "severity"
+ ? integer(details[field], field)
+ : cvssScore.safeParse(details[field]).data;
+ if (
+ score === undefined ||
+ (exact !== undefined && score !== exact) ||
+ (minimum !== undefined && score < minimum)
+ )
+ throw new Error(
+ `Veracode finding contradicts the ${field} selection.`,
+ );
+ }
+ }
+ if (staticOnly && row["scan_type"] !== "STATIC")
+ throw new Error(
+ "Veracode finding contradicts the requested STATIC scan type.",
+ );
+ if (violates !== null && row["violates_policy"] !== (violates === "true"))
+ throw new Error(
+ "Veracode finding contradicts the violates_policy selection.",
+ );
+ };
+}
+
function readReport(input: unknown) {
const pages = (Array.isArray(input) ? input : [input])
.map(readPage)
@@ -381,77 +458,15 @@ function readReport(input: unknown) {
throw new Error(
"Supply every Veracode page exactly once with stable totals, selection, origin and application context.",
);
- const { scope, query } = first;
- const selectedScores = scores.filter(
- (field) => query.has(field) || query.has(`${field}_gte`),
- );
- const staticOnly = query.has("scan_type");
- const violates = query.get("violates_policy");
- const newOnly = query.get("new") === "true";
- const cwes = query.get("cwe")?.split(",");
- const categories = query.get("finding_category")?.split(",");
+ const { scope } = first;
+ const checkSelection = rowSelectionValidator(first.query);
const seen = new Set<string>();
const rows: Record<string, unknown>[] = [];
for (const page of pages) {
for (const value of page.rows) {
const row = record(value, "Veracode finding");
requireApplication(row, scope);
- if (
- newOnly &&
- record(row["finding_status"], "Veracode finding_status")["new"] !== true
- )
- throw new Error("Veracode finding contradicts the new selection.");
- if (
- query.get("include_annot") === "false" &&
- Array.isArray(row["annotations"]) &&
- row["annotations"].length
- )
- throw new Error(
- "Veracode finding contradicts the include_annot selection.",
- );
- if (cwes || categories || selectedScores.length) {
- const details = record(
- row["finding_details"],
- "Veracode finding_details",
- );
- for (const [field, ids] of [
- ["cwe", cwes],
- ["finding_category", categories],
- ] as const) {
- if (!ids || row["scan_type"] !== "STATIC") continue;
- const entry = record(details[field], `Veracode ${field}`);
- const id = integer(entry["id"], `${field} ID`);
- if (!ids.includes(String(id)))
- throw new Error(
- `Veracode finding contradicts the ${field} selection.`,
- );
- }
- for (const field of selectedScores) {
- const score =
- field === "severity"
- ? integer(details[field], field)
- : cvssScore.safeParse(details[field]).data;
- const exact = query.get(field);
- const minimum = query.get(`${field}_gte`);
- if (
- score === undefined ||
- (exact !== null && score !== Number(exact)) ||
- (minimum !== null && score < Number(minimum))
- )
- throw new Error(
- `Veracode finding contradicts the ${field} selection.`,
- );
- }
- }
- if (staticOnly && row["scan_type"] !== "STATIC")
- throw new Error(
- "Veracode finding contradicts the requested STATIC scan type.",
- );
- if (violates !== null && row["violates_policy"] !== (violates === "true"))
- throw new Error(
- "Veracode finding contradicts the violates_policy selection.",
- );
-
+ checkSelection(row);
const issue = row["issue_id"];
if (
typeof row["scan_type"] === "string" &&
diff --git a/sdk/typescript/tests-ts/veracode-findings.test.ts b/sdk/typescript/tests-ts/veracode-findings.test.ts
index 53794f7ea..3b011f2d7 100644
--- a/sdk/typescript/tests-ts/veracode-findings.test.ts
+++ b/sdk/typescript/tests-ts/veracode-findings.test.ts
@@ -291,11 +291,22 @@ test("accepts a scoped native empty page but rejects errors, summaries, Pipeline
test("enforces exact and minimum native severity selections", () => {
const page = veracodePolicyPage();
const self = page._links.self.href;
- for (const filter of ["severity=4", "severity_gte=4", "severity_gte=0"]) {
+ for (const filter of [
+ "severity=4",
+ "severity_gte=4",
+ "severity_gte=0",
+ "severity=4&severity_gte=4",
+ ]) {
page._links.self.href = self + "&" + filter;
expect(normalize(page).findings).toHaveLength(1);
}
- for (const filter of ["severity=3", "severity_gte=5", "severity=6"]) {
+ for (const filter of [
+ "severity=3",
+ "severity_gte=5",
+ "severity=6",
+ "severity=0&severity_gte=0",
+ "severity=4&severity_gte=5",
+ ]) {
page._links.self.href = self + "&" + filter;
expect(() => normalize(page)).toThrow(/severity/);
}