Skip to content

Commit 00d4b68

Browse files
Validate timestamps before storing and comparing them
- Reject invalid session exp claims - Use safer DateTime parsing across diagnostics, adapter, and settings
1 parent c1f7919 commit 00d4b68

4 files changed

Lines changed: 25 additions & 28 deletions

File tree

‎apps/server/src/auth/Layers/SessionCredentialService.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,19 @@ export const makeSessionCredentialService = Effect.gen(function* () {
312312
});
313313
}
314314

315+
const expiresAt = DateTime.make(claims.exp);
316+
if (Option.isNone(expiresAt)) {
317+
return yield* new SessionCredentialError({
318+
message: "Invalid `exp` claim",
319+
});
320+
}
321+
315322
return {
316323
sessionId: claims.sid,
317324
token,
318325
method: claims.method,
319326
client: toClientMetadata(row.value.client),
320-
expiresAt: DateTime.makeUnsafe(claims.exp),
327+
expiresAt: expiresAt.value,
321328
subject: claims.sub,
322329
role: claims.role,
323330
} satisfies VerifiedSession;

‎apps/server/src/diagnostics/TraceDiagnostics.ts‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,8 @@ function unixNanoToDateTime(value: unknown): DateTime.Utc | null {
9090
const text = toStringValue(value);
9191
if (!text) return null;
9292

93-
try {
94-
const millis = Number(BigInt(text) / 1_000_000n);
95-
if (!Number.isFinite(millis)) return null;
96-
return DateTime.makeUnsafe(millis);
97-
} catch {
98-
return null;
99-
}
100-
}
101-
102-
function isAfter(left: DateTime.Utc, right: DateTime.Utc): boolean {
103-
return DateTime.toEpochMillis(left) > DateTime.toEpochMillis(right);
104-
}
105-
106-
function isBefore(left: DateTime.Utc, right: DateTime.Utc): boolean {
107-
return DateTime.toEpochMillis(left) < DateTime.toEpochMillis(right);
93+
const millis = Number(BigInt(text) / 1_000_000n);
94+
return Option.getOrNull(DateTime.make(millis));
10895
}
10996

11097
function readExitTag(exit: unknown): string | null {
@@ -254,10 +241,11 @@ export function aggregateTraceDiagnostics(
254241

255242
recordCount += 1;
256243
firstSpanAt =
257-
startedAt && (firstSpanAt === null || isBefore(startedAt, firstSpanAt))
244+
startedAt && (firstSpanAt === null || DateTime.isLessThan(startedAt, firstSpanAt))
258245
? startedAt
259246
: firstSpanAt;
260-
lastSpanAt = lastSpanAt === null || isAfter(endedAt, lastSpanAt) ? endedAt : lastSpanAt;
247+
lastSpanAt =
248+
lastSpanAt === null || DateTime.isGreaterThan(endedAt, lastSpanAt) ? endedAt : lastSpanAt;
261249

262250
const exitTag = readExitTag(parsed.exit);
263251
const isFailure = exitTag === "Failure";
@@ -289,7 +277,7 @@ export function aggregateTraceDiagnostics(
289277

290278
const failureKey = `${name}\0${cause}`;
291279
const existing = failuresByKey.get(failureKey);
292-
const isLatestFailure = !existing || isAfter(endedAt, existing.lastSeenAt);
280+
const isLatestFailure = !existing || DateTime.isGreaterThan(endedAt, existing.lastSeenAt);
293281
failuresByKey.set(failureKey, {
294282
name,
295283
cause,

‎apps/server/src/provider/Layers/OpenCodeAdapter.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
toOpenCodeQuestionAnswers,
4949
type OpenCodeServerConnection,
5050
} from "../opencodeRuntime.ts";
51+
import * as Option from "effect/Option";
5152

5253
const PROVIDER = ProviderDriverKind.make("opencode");
5354

@@ -355,12 +356,13 @@ export function appendOpenCodeAssistantTextDelta(
355356
};
356357
}
357358

358-
function isoFromEpochMs(value: number | undefined): string | undefined {
359-
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
360-
return undefined;
361-
}
362-
return DateTime.formatIso(DateTime.makeUnsafe(value));
363-
}
359+
const isoFromEpochMs = (value: number) =>
360+
DateTime.make(value).pipe(
361+
Option.match({
362+
onNone: () => undefined,
363+
onSome: DateTime.formatIso,
364+
}),
365+
);
364366

365367
function messageRoleForPart(
366368
context: OpenCodeSessionContext,
@@ -603,8 +605,8 @@ export function makeOpenCodeAdapter(
603605
turnId,
604606
itemId: part.id,
605607
createdAt:
606-
part.type === "text" || part.type === "reasoning"
607-
? isoFromEpochMs(part.time?.start)
608+
(part.type === "text" || part.type === "reasoning") && part.time !== undefined
609+
? isoFromEpochMs(part.time.start)
608610
: undefined,
609611
raw,
610612
})),

‎apps/web/src/components/settings/SettingsPanels.browser.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function createOutdatedProvider(driver: string): ServerProvider {
262262
}
263263

264264
function makeUtc(value: string) {
265-
return DateTime.makeUnsafe(Date.parse(value));
265+
return DateTime.makeUnsafe(value);
266266
}
267267

268268
function makePairingLink(input: {

0 commit comments

Comments
 (0)