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: share final scan schema definitions (#1493)

mldangelo-oai7a1f5964c494
diff --git a/plugins/codex-security/schemas/findings.schema.json b/plugins/codex-security/schemas/findings.schema.json
index 2344b6715..c2012259f 100644
--- a/plugins/codex-security/schemas/findings.schema.json
+++ b/plugins/codex-security/schemas/findings.schema.json
@@ -162,11 +162,7 @@
"minLength": 1
},
"cwe": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
}
}
},
@@ -300,11 +296,7 @@
"minLength": 1
},
"evidenceRefs": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"code": {
"type": "string",
@@ -328,18 +320,10 @@
"minLength": 1
},
"evidenceRefs": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"evidence_refs": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"code": {
"type": "string",
@@ -362,18 +346,10 @@
],
"properties": {
"assertions": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"counterEvidence": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"evidence": {
"type": [
@@ -387,25 +363,13 @@
}
},
"evidenceRefs": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"evidence_refs": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"limitations": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"method": {
"type": "string",
@@ -445,154 +409,40 @@
],
"properties": {
"assumptions": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"blindspots": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"controls": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"dataFlow": {
- "type": [
- "string",
- "object"
- ],
- "minLength": 1,
- "properties": {
- "summary": { "type": "string", "minLength": 1 },
- "source": { "type": "string", "minLength": 1 },
- "sink": { "type": "string", "minLength": 1 },
- "outcome": { "type": "string", "minLength": 1 },
- "transformations": {
- "type": "array",
- "items": { "type": "string", "minLength": 1 }
- },
- "evidenceRefs": {
- "type": "array",
- "items": { "type": "string", "minLength": 1 }
- },
- "evidence_refs": {
- "type": "array",
- "items": { "type": "string", "minLength": 1 }
- }
- }
+ "$ref": "#/$defs/dataFlow"
},
"data_flow": {
- "type": [
- "string",
- "object"
- ],
- "minLength": 1,
- "properties": {
- "summary": { "type": "string", "minLength": 1 },
- "source": { "type": "string", "minLength": 1 },
- "sink": { "type": "string", "minLength": 1 },
- "outcome": { "type": "string", "minLength": 1 },
- "transformations": {
- "type": "array",
- "items": { "type": "string", "minLength": 1 }
- },
- "evidenceRefs": {
- "type": "array",
- "items": { "type": "string", "minLength": 1 }
- },
- "evidence_refs": {
- "type": "array",
- "items": { "type": "string", "minLength": 1 }
- }
- }
+ "$ref": "#/$defs/dataFlow"
},
"dataflow": {
- "type": [
- "string",
- "object"
- ],
- "minLength": 1,
- "properties": {
- "summary": { "type": "string", "minLength": 1 },
- "source": { "type": "string", "minLength": 1 },
- "sink": { "type": "string", "minLength": 1 },
- "outcome": { "type": "string", "minLength": 1 },
- "transformations": {
- "type": "array",
- "items": { "type": "string", "minLength": 1 }
- },
- "evidenceRefs": {
- "type": "array",
- "items": { "type": "string", "minLength": 1 }
- },
- "evidence_refs": {
- "type": "array",
- "items": { "type": "string", "minLength": 1 }
- }
- }
+ "$ref": "#/$defs/dataFlow"
},
"evidenceRefs": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"evidence_refs": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"impact": {
- "type": [
- "string",
- "object",
- "null"
- ],
- "minLength": 1,
- "properties": {
- "level": { "type": "string", "minLength": 1 },
- "rationale": { "type": "string", "minLength": 1 },
- "why": { "type": "string", "minLength": 1 }
- }
+ "$ref": "#/$defs/assessment"
},
"likelihood": {
- "type": [
- "string",
- "object",
- "null"
- ],
- "minLength": 1,
- "properties": {
- "level": { "type": "string", "minLength": 1 },
- "rationale": { "type": "string", "minLength": 1 },
- "why": { "type": "string", "minLength": 1 }
- }
+ "$ref": "#/$defs/assessment"
},
"limitations": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"preconditions": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"reachability": {
"type": [
@@ -608,25 +458,18 @@
"sink": { "type": "string", "minLength": 1 },
"outcome": { "type": "string", "minLength": 1 },
"preconditions": {
- "type": "array",
- "items": { "type": "string", "minLength": 1 }
+ "$ref": "#/$defs/stringList"
},
"evidenceRefs": {
- "type": "array",
- "items": { "type": "string", "minLength": 1 }
+ "$ref": "#/$defs/stringList"
},
"evidence_refs": {
- "type": "array",
- "items": { "type": "string", "minLength": 1 }
+ "$ref": "#/$defs/stringList"
}
}
},
"steps": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"summary": {
"type": "string",
@@ -635,18 +478,10 @@
}
},
"remediationTests": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"preventiveControls": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"provenance": {
"type": "object",
@@ -680,5 +515,49 @@
}
}
}
+ },
+ "$defs": {
+ "dataFlow": {
+ "type": [
+ "string",
+ "object"
+ ],
+ "minLength": 1,
+ "properties": {
+ "summary": { "type": "string", "minLength": 1 },
+ "source": { "type": "string", "minLength": 1 },
+ "sink": { "type": "string", "minLength": 1 },
+ "outcome": { "type": "string", "minLength": 1 },
+ "transformations": {
+ "$ref": "#/$defs/stringList"
+ },
+ "evidenceRefs": {
+ "$ref": "#/$defs/stringList"
+ },
+ "evidence_refs": {
+ "$ref": "#/$defs/stringList"
+ }
+ }
+ },
+ "assessment": {
+ "type": [
+ "string",
+ "object",
+ "null"
+ ],
+ "minLength": 1,
+ "properties": {
+ "level": { "type": "string", "minLength": 1 },
+ "rationale": { "type": "string", "minLength": 1 },
+ "why": { "type": "string", "minLength": 1 }
+ }
+ },
+ "stringList": {
+ "type": "array",
+ "items": {
+ "type": "string",
+ "minLength": 1
+ }
+ }
}
}
diff --git a/plugins/codex-security/schemas/scan-manifest.schema.json b/plugins/codex-security/schemas/scan-manifest.schema.json
index b0b705db5..bf614d85f 100644
--- a/plugins/codex-security/schemas/scan-manifest.schema.json
+++ b/plugins/codex-security/schemas/scan-manifest.schema.json
@@ -146,11 +146,7 @@
"minLength": 1
},
"artifactsReviewed": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"runtimeStatus": {
"type": "string",
@@ -165,11 +161,7 @@
"minLength": 1
},
"limitations": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
}
}
},
@@ -185,39 +177,19 @@
"minLength": 1
},
"assets": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"trustBoundaries": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"attackerCapabilities": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"securityObjectives": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"assumptions": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
}
}
},
@@ -237,18 +209,10 @@
"description": "The modeled source scope, which may differ from the scan scope. Omit when unknown.",
"properties": {
"includePaths": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"excludePaths": {
- "type": "array",
- "items": {
- "type": "string",
- "minLength": 1
- }
+ "$ref": "#/$defs/stringList"
},
"summary": {
"type": "string",
@@ -343,5 +307,14 @@
}
]
}
+ },
+ "$defs": {
+ "stringList": {
+ "type": "array",
+ "items": {
+ "type": "string",
+ "minLength": 1
+ }
+ }
}
}
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.py
index cd02cd0dd..d7c99b106 100644
--- a/plugins/codex-security/scripts/finalize_scan_contract.py
+++ b/plugins/codex-security/scripts/finalize_scan_contract.py
@@ -1120,14 +1120,14 @@ def _recover_unsealed_findings(
continue
try:
_validate_schema_node(
- finding[auxiliary], auxiliary_schema, f"{context}.{auxiliary}"
+ finding[auxiliary], auxiliary_schema, f"{context}.{auxiliary}", schema
)
except ContractError as exc:
finding.pop(auxiliary)
warnings.append(
f"Skipped malformed {auxiliary} for finding {index + 1}: {exc}."
)
- _validate_schema_node(finding, finding_schema, context)
+ _validate_schema_node(finding, finding_schema, context, schema)
except ContractError as exc:
warning = f"Skipped malformed finding {index + 1}: {exc}."
warnings.append(warning)
@@ -1780,16 +1780,22 @@ def _validate_findings(manifest: dict[str, Any], findings: dict[str, Any]) -> No
_require_safe_json_value(findings, "findings.json")
+_SCHEMA_TYPES = {
+ "array": list,
+ "boolean": bool,
+ "integer": int,
+ "number": (int, float),
+ "object": dict,
+ "string": str,
+}
+
+
def _schema_type_matches(value: Any, expected: str) -> bool:
- return {
- "array": isinstance(value, list),
- "boolean": isinstance(value, bool),
- "integer": isinstance(value, int) and not isinstance(value, bool),
- "number": isinstance(value, (int, float)) and not isinstance(value, bool),
- "object": isinstance(value, dict),
- "string": isinstance(value, str),
- "null": value is None,
- }[expected]
+ if expected == "null":
+ return value is None
+ return isinstance(value, _SCHEMA_TYPES[expected]) and (
+ expected not in ("integer", "number") or not isinstance(value, bool)
+ )
def _schema_values_equal(left: Any, right: Any) -> bool:
@@ -1860,6 +1866,8 @@ def _validate_schema_node(
context,
root_schema,
)
+ if len(schema) == 1:
+ return
expected = schema.get("type")
if isinstance(expected, list):
if not any(_schema_type_matches(value, item) for item in expected):
diff --git a/plugins/codex-security/tests/test_finalize_scan_contract.py b/plugins/codex-security/tests/test_finalize_scan_contract.py
index d9950455f..dc5ec9332 100644
--- a/plugins/codex-security/tests/test_finalize_scan_contract.py
+++ b/plugins/codex-security/tests/test_finalize_scan_contract.py
@@ -3085,5 +3085,25 @@ The extraction root is not enforced.
self.assertEqual(line_hashes.call_count, 1)
[email protected](("expected", "value"), [("integer", 1), ("number", 1.5)])
+def test_schema_numeric_types_do_not_accept_booleans(expected, value) -> None:
+ schema = {"type": expected}
+ FINALIZER._validate_schema_node(value, schema, "value")
+ with pytest.raises(FINALIZER.ContractError, match="expected schema type"):
+ FINALIZER._validate_schema_node(True, schema, "value")
+
+
+def test_schema_references_preserve_constraints_siblings_and_cycle_errors() -> None:
+ root = {"$defs": {"text": {"type": "string", "minLength": 1}}}
+ reference = {"$ref": "#/$defs/text"}
+ FINALIZER._validate_schema_node("yes", reference, "value", root)
+ with pytest.raises(FINALIZER.ContractError, match="string is too short"):
+ FINALIZER._validate_schema_node("", reference, "value", root)
+ with pytest.raises(FINALIZER.ContractError, match="string does not match schema pattern"):
+ FINALIZER._validate_schema_node("no", {**reference, "pattern": "^yes$"}, "value", root)
+ with pytest.raises(RecursionError):
+ FINALIZER._validate_schema_node("yes", {"$ref": "#"}, "value")
+
+
if __name__ == "__main__":
main()
diff --git a/sdk/typescript/scripts/generate-models.cjs b/sdk/typescript/scripts/generate-models.cjs
index 303bfd1ea..bcb23faf7 100644
--- a/sdk/typescript/scripts/generate-models.cjs
+++ b/sdk/typescript/scripts/generate-models.cjs
@@ -8,6 +8,20 @@ const schemas = resolve(packageRoot, "../../plugins/codex-security/schemas");
if (!existsSync(schemas)) throw new Error("Could not find the plugin schemas.");
+function modelSchema(value, root = value) {
+ if (Array.isArray(value))
+ return value.map((child) => modelSchema(child, root));
+ if (value === null || typeof value !== "object") return value;
+ if (typeof value.$ref === "string") {
+ return modelSchema(root.$defs[value.$ref.slice("#/$defs/".length)], root);
+ }
+ return Object.fromEntries(
+ Object.entries(value)
+ .filter(([key]) => key !== "allOf" && key !== "$defs")
+ .map(([key, child]) => [key, modelSchema(child, root)]),
+ );
+}
+
async function generate() {
const documents = [
["scan-manifest.schema.json", "ScanManifest"],
@@ -17,9 +31,8 @@ async function generate() {
const models = await Promise.all(
documents.map(async ([filename, name]) => {
// json-schema-to-typescript drops object fields when allOf uses contains or if/then.
- const input = JSON.parse(
- readFileSync(join(schemas, filename), "utf8"),
- (key, value) => (key === "allOf" ? undefined : value),
+ const input = modelSchema(
+ JSON.parse(readFileSync(join(schemas, filename), "utf8")),
);
input.title = name;
return compile(input, name, {
diff --git a/sdk/typescript/src/deduplication/finding-schema.ts b/sdk/typescript/src/deduplication/finding-schema.ts
index 465fde7ea..6f5a41efc 100644
--- a/sdk/typescript/src/deduplication/finding-schema.ts
+++ b/sdk/typescript/src/deduplication/finding-schema.ts
@@ -6,7 +6,8 @@ let schema: object | undefined;
let validator: ValidateFunction<Finding> | undefined;
export function findingSchema(): object {
- return (schema ??= JSON.parse(
+ if (schema) return schema;
+ const document = JSON.parse(
readFileSync(
new URL(
"../../_bundled_plugin/schemas/findings.schema.json",
@@ -14,7 +15,11 @@ export function findingSchema(): object {
),
"utf8",
),
- ).properties.findings.items);
+ );
+ return (schema = {
+ ...document.properties.findings.items,
+ $defs: document.$defs,
+ });
}
export function requireFinding(value: unknown): Finding {
diff --git a/sdk/typescript/src/server/validation.ts b/sdk/typescript/src/server/validation.ts
index bfb26af03..f39e3eb38 100644
--- a/sdk/typescript/src/server/validation.ts
+++ b/sdk/typescript/src/server/validation.ts
@@ -36,6 +36,7 @@ export async function findingsRequestValidator(): Promise<
return new Ajv2020({ strict: false }).compile<FindingsRequest>({
type: "object",
required: ["findings"],
+ $defs: schema.$defs,
properties: {
findings: schema.properties.findings,
repositoryId: { type: "string", minLength: 1, pattern: "^[^\\u0000]*$" },
diff --git a/sdk/typescript/tests-ts/dedupe-records.test.ts b/sdk/typescript/tests-ts/dedupe-records.test.ts
index 2bc289e39..2987649e8 100644
--- a/sdk/typescript/tests-ts/dedupe-records.test.ts
+++ b/sdk/typescript/tests-ts/dedupe-records.test.ts
@@ -1,4 +1,5 @@
import { expect, test, mock } from "bun:test";
+import Ajv2020 from "ajv/dist/2020.js";
import { DeduplicationReviewError } from "../src/errors.js";
import { mkdtemp, readFile, readdir, rm } from "node:fs/promises";
import { spawn } from "node:child_process";
@@ -129,6 +130,12 @@ test("records groups original observations and retrieved neighbors with unique r
requests.push(review);
expect(review.trustedInstructions).toContain("approved repository");
expect(review.findingSchema).toHaveProperty("required");
+ const validateFinding = new Ajv2020({ strict: false }).compile(
+ JSON.parse(JSON.stringify(review.findingSchema)) as object,
+ );
+ const finding = original.observations[0]!.finding;
+ expect(validateFinding(finding)).toBe(true);
+ expect(validateFinding({ ...finding, title: "" })).toBe(false);
expect(review).not.toHaveProperty("validate");
expect(review).not.toHaveProperty("findingIds");
for (const finding of assigned(review))