Skip to content

The agent that grows with you

fix: support session mutations on insecure origins

KoNit-Kd41b8aafdc7a
diff --git a/apps/shared/src/session-http-mutations.ts b/apps/shared/src/session-http-mutations.ts
index 621d98363c2..b3c0bc95656 100644
--- a/apps/shared/src/session-http-mutations.ts
+++ b/apps/shared/src/session-http-mutations.ts
@@ -31,7 +31,7 @@ export function createSessionMutationClient() {
|| (snapshot.exists && (!Number.isSafeInteger(snapshot.runtime_generation) || snapshot.runtime_generation! < 0))) {
throw new Error('Session mutation snapshot unavailable; refresh before trying again')
}
- return { request_id: crypto.randomUUID(), expected_revision: snapshot.runtime_revision,
+ return { request_id: newMutationRequestId(), expected_revision: snapshot.runtime_revision,
...(snapshot.exists ? { expected_generation: snapshot.runtime_generation! } : {}) }
})
pending.set(key, prepared)
@@ -54,6 +54,24 @@ export function createSessionMutationClient() {
}
}
+function newMutationRequestId(): string {
+ if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
+ return crypto.randomUUID()
+ }
+ const bytes = new Uint8Array(16)
+ if (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') {
+ crypto.getRandomValues(bytes)
+ } else {
+ for (let index = 0; index < bytes.length; index += 1) {
+ bytes[index] = Math.floor(Math.random() * 256)
+ }
+ }
+ bytes[6] = (bytes[6] & 0x0f) | 0x40
+ bytes[8] = (bytes[8] & 0x3f) | 0x80
+ const hex = Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('')
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`
+}
+
function sessionOwner(key: string): string | null {
try {
const parsed = JSON.parse(key)
diff --git a/web/src/lib/session-mutations.test.ts b/web/src/lib/session-mutations.test.ts
index ef1fc6f55fe..534285cbb78 100644
--- a/web/src/lib/session-mutations.test.ts
+++ b/web/src/lib/session-mutations.test.ts
@@ -44,3 +44,19 @@ it('surfaces revision conflicts and never substitutes missing counters', async (
await expect(api.deleteSession('no-snapshot')).rejects.toThrow(/snapshot/);
expect(fetcher).toHaveBeenCalledTimes(1);
});
+
+it('sends mutations when crypto.randomUUID is unavailable on an insecure origin', async () => {
+ const fetcher = vi.fn(async (_path: string, init: RequestInit) => {
+ if (!init.method) { return Response.json({ exists: true, runtime_revision: 12, runtime_generation: 4 }); }
+ return Response.json({ ok: true });
+ });
+ vi.stubGlobal('fetch', fetcher);
+ vi.stubGlobal('crypto', {
+ getRandomValues: (bytes: Uint8Array) => bytes.fill(0xab),
+ });
+
+ await api.deleteSession('insecure-origin');
+
+ const request = new URL(fetcher.mock.calls[1][0] as string, 'http://localhost');
+ expect(request.searchParams.get('request_id')).toBe('abababab-abab-4bab-abab-abababababab');
+});