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

fix: preserve validation behavior for malformed scan usage (#1781)

Michael D'Angeloa3b41befaadc
diff --git a/plugins/codex-security/scripts/workbench_scan_usage.py b/plugins/codex-security/scripts/workbench_scan_usage.py
index 35a40a9cf..301ff9cbe 100644
--- a/plugins/codex-security/scripts/workbench_scan_usage.py
+++ b/plugins/codex-security/scripts/workbench_scan_usage.py
@@ -885,7 +885,8 @@ def _usage_counts(usage: object) -> dict[str, int] | None:
input_tokens = usage.get("input_tokens")
cached_input_tokens = usage.get("cached_input_tokens", 0)
if (
- cache_write == 0
+ type(cache_write) is int
+ and cache_write == 0
and type(legacy_cache_write) is int
and legacy_cache_write > 0
and type(input_tokens) is int
@@ -929,9 +930,9 @@ def _timestamp(value: object) -> datetime | None:
if value.endswith("Z")
else datetime.fromisoformat(value)
)
- except ValueError:
+ return parsed.astimezone(timezone.utc) if parsed.tzinfo is not None else None
+ except (ValueError, OverflowError):
return None
- return parsed.astimezone(timezone.utc) if parsed.tzinfo is not None else None
def _unavailable_usage(
diff --git a/plugins/codex-security/scripts/workbench_validation.py b/plugins/codex-security/scripts/workbench_validation.py
index 0e38cfe54..7c52f92ed 100644
--- a/plugins/codex-security/scripts/workbench_validation.py
+++ b/plugins/codex-security/scripts/workbench_validation.py
@@ -69,16 +69,19 @@ SCAN_USAGE_TOKEN_KEYS = tuple(TOKEN_FIELDS.values())
def _valid_legacy_scan_cost(cost: object) -> bool:
token_keys = ("inputTokens", "cachedInputTokens", "cacheWriteInputTokens", "outputTokens")
- return (
- isinstance(cost, dict)
- and isinstance(cost.get("model"), str)
- and bool(cost["model"])
- and all(type(cost.get(key)) is int and cost[key] >= 0 for key in token_keys)
- and cost["cachedInputTokens"] + cost["cacheWriteInputTokens"] <= cost["inputTokens"]
- and type(cost.get("estimatedUsd")) in (int, float)
- and math.isfinite(cost["estimatedUsd"])
- and cost["estimatedUsd"] >= 0
- )
+ try:
+ return (
+ isinstance(cost, dict)
+ and isinstance(cost.get("model"), str)
+ and bool(cost["model"])
+ and all(type(cost.get(key)) is int and cost[key] >= 0 for key in token_keys)
+ and cost["cachedInputTokens"] + cost["cacheWriteInputTokens"] <= cost["inputTokens"]
+ and type(cost.get("estimatedUsd")) in (int, float)
+ and math.isfinite(cost["estimatedUsd"])
+ and cost["estimatedUsd"] >= 0
+ )
+ except OverflowError:
+ return False
def _valid_measured_scan_usage(usage: object) -> bool:
@@ -87,7 +90,7 @@ def _valid_measured_scan_usage(usage: object) -> bool:
coverage = usage.get("coverage")
thread_count = usage.get("threadCount")
if (
- coverage not in {"complete", "partial", "unavailable"}
+ coverage not in ("complete", "partial", "unavailable")
or usage.get("source") != "codex_rollout"
or type(thread_count) is not int
or thread_count < 0
diff --git a/plugins/codex-security/tests/test_workbench_scan_usage.py b/plugins/codex-security/tests/test_workbench_scan_usage.py
index 55288ae7c..b8f96436d 100644
--- a/plugins/codex-security/tests/test_workbench_scan_usage.py
+++ b/plugins/codex-security/tests/test_workbench_scan_usage.py
@@ -667,7 +667,17 @@ def test_native_usage_continues_cumulative_baseline_when_recovering_same_turn(
@pytest.mark.parametrize(
- "failure", ["invalid_usage", "invalid_timestamp", "missing_receipt", "truncated"]
+ "failure",
+ [
+ "invalid_usage",
+ "invalid_timestamp",
+ "timestamp_underflow",
+ "timestamp_overflow",
+ "boolean_cache_write",
+ "float_cache_write",
+ "missing_receipt",
+ "truncated",
+ ],
)
def test_native_usage_preserves_partial_measurement_warnings(tmp_path: Path, failure: str) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:00+00:00")
@@ -677,6 +687,15 @@ def test_native_usage_preserves_partial_measurement_warnings(tmp_path: Path, fai
second["payload"]["usage"] = {}
elif failure == "invalid_timestamp":
del second["timestamp"]
+ elif failure == "timestamp_underflow":
+ second["timestamp"] = "0001-01-01T00:00:00+00:01"
+ elif failure == "timestamp_overflow":
+ second["timestamp"] = "9999-12-31T23:59:59-00:01"
+ elif failure in {"boolean_cache_write", "float_cache_write"}:
+ second["payload"]["usage"].update(
+ cache_write_input_tokens=False if failure == "boolean_cache_write" else 0.0,
+ cache_write_tokens=1,
+ )
events = [second] if failure == "missing_receipt" else [first, second]
parent = _rollout(tmp_path, "scan-parent", events)
if failure == "truncated":
@@ -803,6 +822,96 @@ def test_scan_usage_helper_preserves_cached_token_totals(
)
+ ("canonical", "legacy", "expected"),
+ [(0, 12, 12), (0, 0, 0), (5, 12, 5), (False, 12, None), (0.0, 12, None)],
+)
+def test_scan_usage_helper_requires_integer_cache_write_placeholder(
+ canonical: object, legacy: int, expected: int | None
+) -> None:
+ helper = runpy.run_path(
+ str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
+ )
+ usage = {
+ "input_tokens": 120,
+ "cached_input_tokens": 30,
+ "cache_write_input_tokens": canonical,
+ "cache_write_tokens": legacy,
+ "output_tokens": 15,
+ "total_tokens": 135,
+ }
+
+ assert helper["_usage_counts"](usage) == (
+ None if expected is None else _counts(120, 30, 15, cache_write_input_tokens=expected)
+ )
+
+
+ ("value", "expected"),
+ [
+ ("0001-01-01T00:00:00+00:01", None),
+ ("9999-12-31T23:59:59-00:01", None),
+ ("0001-01-01T00:00:00Z", "0001-01-01T00:00:00+00:00"),
+ ("9999-12-31T23:59:59Z", "9999-12-31T23:59:59+00:00"),
+ ("2026-01-01T01:00:00+01:00", "2026-01-01T00:00:00+00:00"),
+ ("2026-01-01T00:00:00", None),
+ ],
+)
+def test_scan_usage_timestamp_handles_utc_boundaries(value: str, expected: str | None) -> None:
+ helper = runpy.run_path(
+ str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
+ )
+ assert helper["_timestamp"](value) == (
+ datetime.fromisoformat(expected) if expected is not None else None
+ )
+
+
[email protected]("amount", [10**400, -(10**400)])
[email protected]("measured", [False, True])
+def test_scan_cost_preserves_diagnostics_for_overflowing_amount(
+ workbench_api, amount: int, measured: bool
+) -> None:
+ cost = {
+ "model": "fixture-model",
+ "inputTokens": 120,
+ "cachedInputTokens": 30,
+ "cacheWriteInputTokens": 12,
+ "outputTokens": 15,
+ "estimatedUsd": amount,
+ }
+ value = (
+ {
+ "usage": {
+ "coverage": "complete",
+ "source": "codex_rollout",
+ "threadCount": 1,
+ **_counts(120, 30, 15),
+ },
+ "cost": cost,
+ }
+ if measured
+ else cost
+ )
+ expected = (
+ "Scan cost includes invalid measured token usage."
+ if measured
+ else "Scan cost must include a model, nonnegative token counts, and an estimated USD amount."
+ )
+ with pytest.raises(SystemExit) as error:
+ workbench_api["parse_scan_cost"](json.dumps(value))
+ assert str(error.value) == expected
+ cost["estimatedUsd"] = 0.5
+ assert json.loads(workbench_api["parse_scan_cost"](json.dumps(value))) == value
+
+
[email protected]("coverage", [[], {}])
+def test_scan_cost_preserves_diagnostics_for_non_scalar_coverage(workbench_api, coverage) -> None:
+ value = {"usage": {"coverage": coverage, "source": "codex_rollout", "threadCount": 0}}
+ with pytest.raises(SystemExit) as error:
+ workbench_api["parse_scan_cost"](json.dumps(value))
+ assert str(error.value) == "Scan cost includes invalid measured token usage."
+
+
@pytest.mark.parametrize("cache_write_field", ["cache_write_input_tokens", "cache_write_tokens"])
def test_completion_includes_cached_and_cache_write_tokens(
tmp_path: Path,