Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(environment): reject out-of-range clock offsets

morluto54c666a5ae13
diff --git a/src/domain/analysisView/environmentStateView.test.ts b/src/domain/analysisView/environmentStateView.test.ts
index f92775ee8..f010cb1a2 100644
--- a/src/domain/analysisView/environmentStateView.test.ts
+++ b/src/domain/analysisView/environmentStateView.test.ts
@@ -1224,3 +1224,17 @@ it("refuses a view clock spelling wider than nine fraction digits", () => {
false,
);
});
+
+it.each(["+00:60", "+24:00", "+99:99"])(
+ "refuses a view clock spelling with invalid zone %s",
+ (zone) => {
+ const item = stateOf().views.find((view) => view.shape === "item")!;
+ const view = {
+ ...item,
+ clock_formats: {
+ created: { kind: "iso", separator: "T", fraction: 0, zone },
+ },
+ };
+ expect(environmentStateFields.views.safeParse([view]).success).toBe(false);
+ },
+);
diff --git a/src/domain/analysisView/environmentStateView.ts b/src/domain/analysisView/environmentStateView.ts
index 544fb89f8..b2191e2fa 100644
--- a/src/domain/analysisView/environmentStateView.ts
+++ b/src/domain/analysisView/environmentStateView.ts
@@ -8,6 +8,7 @@ import type {
UnsignedAnalysisView,
} from "./analysisView.js";
import { EnvironmentStateAccumulator } from "./EnvironmentStateAccumulator.js";
+import { isClockZone } from "./environmentStateWriteValues.js";
import {
groupCaptureRecords,
templateRulesSchema,
@@ -95,7 +96,7 @@ const clockFormatSchema = z.discriminatedUnion("kind", [
kind: z.literal("iso"),
separator: z.enum(["T", " "]),
fraction: z.number().int().nonnegative().max(9),
- zone: z.string(),
+ zone: z.string().refine(isClockZone),
}),
z.strictObject({ kind: z.literal("epoch"), unit: z.enum(["s", "ms"]) }),
]);
diff --git a/src/domain/analysisView/environmentStateWriteValues.test.ts b/src/domain/analysisView/environmentStateWriteValues.test.ts
index af90721dd..dc4ef3c56 100644
--- a/src/domain/analysisView/environmentStateWriteValues.test.ts
+++ b/src/domain/analysisView/environmentStateWriteValues.test.ts
@@ -38,6 +38,11 @@ it.each<[string, number, ClockFormat, number?]>([
["2026-03-04T05:06:07.123456Z", WITH_MS, iso("T", 6, "Z"), 456000],
["2026-03-04T07:06:07.123+02:00", WITH_MS, iso("T", 3, "+02:00")],
["2026-03-03T23:36:07-0530", WHOLE_SECOND, iso("T", 0, "-0530")],
+ [
+ "2000-01-01T12:00:00+23:59",
+ Date.UTC(1999, 11, 31, 12, 1),
+ iso("T", 0, "+23:59"),
+ ],
["2026-03-04T05:06:07", WHOLE_SECOND, iso("T", 0, "")],
["2024-02-29 00:00:00Z", Date.UTC(2024, 1, 29), iso(" ", 0, "Z")],
])(
@@ -54,6 +59,9 @@ it.each([
"2026-03-04T24:00:00Z",
"2026-03-04T05:60:00Z",
"2026-03-04T05:06:60Z",
+ "2026-03-04T05:06:07+00:60",
+ "2026-03-04T05:06:07+24:00",
+ "2026-03-04T05:06:07+99:99",
"2026-03-04",
"2026-03-04T05:06Z",
"2026-03-04T05:06:07+05",
diff --git a/src/domain/analysisView/environmentStateWriteValues.ts b/src/domain/analysisView/environmentStateWriteValues.ts
index 5b3c4a6d1..6048e6491 100644
--- a/src/domain/analysisView/environmentStateWriteValues.ts
+++ b/src/domain/analysisView/environmentStateWriteValues.ts
@@ -23,14 +23,28 @@ const ISO_INSTANT =
const MINUTE_MS = 60_000;
/** Minutes east of UTC that a zone suffix names; none and `Z` are UTC. */
-const zoneMinutes = (zone: string): number => {
+const zoneMinutes = (zone: string): number | undefined => {
if (zone === "" || zone === "Z") return 0;
+ if (!/^[+-]\d{2}:?\d{2}$/u.test(zone)) return undefined;
const digits = zone.slice(1).replace(":", "");
- const minutes =
- Number(digits.slice(0, 2)) * 60 + Number(digits.slice(2, 4) || "0");
+ const hours = Number(digits.slice(0, 2));
+ const remainder = Number(digits.slice(2, 4));
+ if (hours > 23 || remainder > 59) return undefined;
+ const minutes = hours * 60 + remainder;
return zone.startsWith("-") ? -minutes : minutes;
};
+/** Whether a stored ISO clock format has a supported RFC 3339 offset suffix. */
+export const isClockZone = (zone: string): boolean =>
+ zoneMinutes(zone) !== undefined;
+
+const zoneMinutesForFormat = (zone: string): number => {
+ const minutes = zoneMinutes(zone);
+ if (minutes === undefined)
+ throw new TypeError(`Invalid clock zone in a derived format: ${zone}`);
+ return minutes;
+};
+
/** The instant of an ISO-8601 date-time and how it is spelled, or undefined when it is not one. */
const readIsoClock = (value: string): ClockReading | undefined => {
const match = ISO_INSTANT.exec(value);
@@ -40,6 +54,8 @@ const readIsoClock = (value: string): ClockReading | undefined => {
.map((part) => (part === "T" || part === " " ? part : Number(part)));
const fraction = match[8] ?? "";
const zone = match[9] ?? "";
+ const offsetMinutes = zoneMinutes(zone);
+ if (offsetMinutes === undefined) return undefined;
const local = Date.UTC(
year as number,
(month as number) - 1,
@@ -60,7 +76,7 @@ const readIsoClock = (value: string): ClockReading | undefined => {
)
return undefined;
return {
- ms: local - zoneMinutes(zone) * MINUTE_MS,
+ ms: local - offsetMinutes * MINUTE_MS,
subMillisecond: Number(fraction.padEnd(9, "0").slice(3)),
format: {
kind: "iso",
@@ -109,7 +125,7 @@ const isoText = (
export const formatClock = (ms: number, format: ClockFormat): JsonValue => {
if (format.kind === "epoch")
return format.unit === "s" ? Math.floor(ms / 1000) : ms;
- const at = new Date(ms + zoneMinutes(format.zone) * MINUTE_MS);
+ const at = new Date(ms + zoneMinutesForFormat(format.zone) * MINUTE_MS);
const digits = `${pad(at.getUTCMilliseconds(), 3)}${"0".repeat(9)}`.slice(
0,
format.fraction,
@@ -138,7 +154,9 @@ export const respellClock = (
0,
clock.format.fraction,
);
- const at = new Date(clock.ms - millis + zoneMinutes(format.zone) * MINUTE_MS);
+ const at = new Date(
+ clock.ms - millis + zoneMinutesForFormat(format.zone) * MINUTE_MS,
+ );
return isoText(
at,
format.separator,