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(sdk): reuse datetime validation for saved scan timestamps (#1351)

mldangelo-oaid072c96f582d
diff --git a/sdk/typescript/src/contract.ts b/sdk/typescript/src/contract.ts
index ccfbc264a..77efad052 100644
--- a/sdk/typescript/src/contract.ts
+++ b/sdk/typescript/src/contract.ts
@@ -10,6 +10,7 @@ import {
} from "node:fs/promises";
import { isAbsolute, join, posix, resolve } from "node:path";
import Ajv2020, { type ErrorObject } from "ajv/dist/2020.js";
+import { regexes } from "zod";
import { ContractValidationError, abortReason } from "./errors.js";
import { isRecord } from "./record.js";
import type {
@@ -1160,49 +1161,13 @@ function throwIfAborted(signal?: AbortSignal): void {
throw abortReason(signal);
}
+const RFC3339_DATE_TIME = new RegExp(
+ regexes.datetime({ offset: true }).source,
+ "i",
+);
+
function validRfc3339DateTime(value: string): boolean {
- const match =
- /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-](\d{2}):(\d{2}))$/i.exec(
- value,
- );
- if (match === null) return false;
- const year = Number(match[1]);
- const month = Number(match[2]);
- const day = Number(match[3]);
- const hour = Number(match[4]);
- const minute = Number(match[5]);
- const second = Number(match[6]);
- const offsetHour = Number(match[7] ?? 0);
- const offsetMinute = Number(match[8] ?? 0);
- if (
- year < 1 ||
- month < 1 ||
- month > 12 ||
- day < 1 ||
- hour > 23 ||
- minute > 59 ||
- second > 59 ||
- offsetHour > 23 ||
- offsetMinute > 59
- ) {
- return false;
- }
- const leapYear = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
- const daysInMonth = [
- 31,
- leapYear ? 29 : 28,
- 31,
- 30,
- 31,
- 30,
- 31,
- 31,
- 30,
- 31,
- 30,
- 31,
- ][month - 1]!;
- return day <= daysInMonth;
+ return !value.startsWith("0000") && RFC3339_DATE_TIME.test(value);
}
function schemaError(
diff --git a/sdk/typescript/tests-ts/contract.test.ts b/sdk/typescript/tests-ts/contract.test.ts
index 56c7e17c7..d77adbbd0 100644
--- a/sdk/typescript/tests-ts/contract.test.ts
+++ b/sdk/typescript/tests-ts/contract.test.ts
@@ -931,35 +931,28 @@ describe("canonical scan contract", () => {
expect(stderr).toContain("duplicate artifact path");
});
- test("rejects calendar-invalid RFC 3339 timestamps", async () => {
- const scanDir = await copyExample();
- const manifestPath = join(scanDir, "scan-manifest.json");
- const manifest = await readJson(manifestPath);
- manifest["scan"]["completedAt"] = "2026-02-30T18:00:00Z";
- manifest["scan"]["sealedAt"] = "2026-02-30T18:00:00Z";
- await writeJson(manifestPath, manifest);
- await expect(
- loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),
- ).rejects.toThrow("date-time");
-
- manifest["scan"]["completedAt"] = "0000-01-01T00:00:00Z";
- manifest["scan"]["sealedAt"] = "0000-01-01T00:00:00Z";
- await writeJson(manifestPath, manifest);
- await expect(
- loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),
- ).rejects.toThrow("date-time");
- });
-
- test("accepts lowercase RFC 3339 separators", async () => {
+ test.each([
+ ["2026-02-30T18:00:00Z", false],
+ ["0000-01-01T00:00:00Z", false],
+ ["1900-02-29T00:00:00Z", false],
+ ["2026-01-01T24:00:00Z", false],
+ ["2026-01-01T00:00:60Z", false],
+ ["2026-01-01T00:00:00+24:00", false],
+ ["2026-01-01T00:00Z", false],
+ ["2026-05-31t18:09:00z", true],
+ ["0001-01-01T00:00:00Z", true],
+ ["2000-02-29T23:59:59.123456+23:59", true],
+ ["2026-01-01T00:00:00Z\n", false],
+ ] as const)("validates RFC 3339 timestamp %j", async (value, valid) => {
const scanDir = await copyExample();
const manifestPath = join(scanDir, "scan-manifest.json");
const manifest = await readJson(manifestPath);
- manifest["scan"]["completedAt"] = "2026-05-31t18:09:00z";
- manifest["scan"]["sealedAt"] = "2026-05-31t18:09:00z";
+ manifest["scan"]["completedAt"] = value;
+ manifest["scan"]["sealedAt"] = value;
await writeJson(manifestPath, manifest);
- await expect(
- loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),
- ).resolves.toBeDefined();
+ const loaded = loadContract(scanDir, { pluginRoot: PLUGIN_ROOT });
+ if (valid) await expect(loaded).resolves.toBeDefined();
+ else await expect(loaded).rejects.toThrow("date-time");
});
test("requires completed and sealed timestamps to match exactly", async () => {