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
fix(previews): bound JSON prefix sizing by byte budget (#1722)
Michael D'Angelod605dcb08466
diff --git a/plugins/codex-security/scripts/finding_preview.py b/plugins/codex-security/scripts/finding_preview.pyindex 700df8869..cab0dce67 100644--- a/plugins/codex-security/scripts/finding_preview.py+++ b/plugins/codex-security/scripts/finding_preview.py@@ -447,7 +447,9 @@ def consume_json_budget(budget: list[int], size: int) -> bool:def bounded_json_text(value: str, maximum_bytes: int) -> tuple[str, int]:- length = bisect_right(range(len(value) + 1), maximum_bytes, key=lambda n: json_size(value[:n]))+ length = bisect_right(+ range(min(len(value), maximum_bytes) + 1), maximum_bytes, key=lambda n: json_size(value[:n])+ )selected = value[: max(0, length - 1)]return selected, json_size(selected)diff --git a/plugins/codex-security/tests/test_finding_preview.py b/plugins/codex-security/tests/test_finding_preview.pyindex 09832214e..40e1324bd 100644--- a/plugins/codex-security/tests/test_finding_preview.py+++ b/plugins/codex-security/tests/test_finding_preview.py@@ -401,3 +401,78 @@ def test_bounded_finding_details_filters_malformed_evidence_before_limiting() ->)assert bounded["code_evidence"] == [{"id": "valid", "code": "valid_source()"}]+++def test_large_code_preview_bounds_sizing_probes(monkeypatch: pytest.MonkeyPatch) -> None:+ preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))+ project = preview["bounded_finding_details"]+ original_size = project.__globals__["json_size"]+ probe_lengths: list[int] = []++ def observed_size(value: object) -> int:+ if isinstance(value, str):+ probe_lengths.append(len(value))+ return original_size(value)++ monkeypatch.setitem(project.__globals__, "json_size", observed_size)+ finding = {+ "preventiveControls": ["Keep the authorization check centralized."],+ "codeEvidence": [+ {+ "id": "source",+ "label": "Missing check",+ "path": "example.py",+ "startLine": 1,+ "code": "x" * (64 * 1024),+ "explanation": "The resource is returned without authorization.",+ }+ ],+ "attackPath": {"narrative": "The request reaches the protected resource."},+ "validation": {"summary": "An unauthorized request succeeds."},+ "rootCause": {"summary": "The authorization check is missing."},+ "remediationTests": ["Reject an unauthorized request."],+ }+ original = deepcopy(finding)+ expected = {+ "rootCause": original["rootCause"],+ "validation": original["validation"],+ "attackPath": original["attackPath"],+ "codeEvidence": [{**original["codeEvidence"][0], "code": "x" * 1_498}],+ "remediationTests": original["remediationTests"],+ "preventiveControls": original["preventiveControls"],+ }++ bounded = project(finding)++ assert bounded == expected+ assert list(bounded) == list(expected)+ encoded = json.dumps(bounded, separators=(",", ":")).encode("ascii")+ assert encoded == json.dumps(expected, separators=(",", ":")).encode("ascii")+ assert len(encoded) <= preview["FINDING_DETAILS_PREVIEW_BYTES"]+ assert finding == original+ assert probe_lengths+ assert 1_498 in probe_lengths+ assert max(probe_lengths) <= 1_500+++ "value",+ ["", "plain", '"\\\n\t\x00', "é", "😀", "a😀é\n", "\ud800"],+ ids=["empty", "ascii", "escapes", "bmp", "supplementary", "mixed", "lone-surrogate"],+)+def test_bounded_json_text_matches_exhaustive_prefix_oracle(value: str) -> None:+ preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))+ prefixes = [+ (value[:end], len(json.dumps(value[:end]).encode("ascii"))) for end in range(len(value) + 1)+ ]+ budgets = {-3, 0, 1, 2}+ for _, size in prefixes:+ budgets.update((size - 1, size, size + 1))++ for budget in sorted(budgets):+ expected = next((item for item in reversed(prefixes) if item[1] <= budget), ("", 2))+ assert preview["bounded_json_text"](value, budget) == expected, (repr(value), budget)+ if budget <= 0:+ remaining = [budget]+ assert preview["bounded_json_value"](value, remaining) is None+ assert remaining == [budget]