Skip to content

armgit / fx0

Public Imported from GitHub
0

A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.

TypeScript

Claim a signed-in guest's chats once, then drop the guest cookie

Esteban Suarez92794643c146
diff --git a/lib/route-auth.ts b/lib/route-auth.ts
index 6e37330..c526d91 100644
--- a/lib/route-auth.ts
+++ b/lib/route-auth.ts
@@ -15,7 +15,7 @@ import type { SessionData } from './secure-session'
import type { SessionPage } from './events'
import type { SessionState } from './session'
// @ts-expect-error Node strip-types tests require the source extension.
-import { createGuestSession, getGuestSession, guestUser, trialEnabled, type GuestSession } from './trial.ts'
+import { clearGuestSession, createGuestSession, getGuestSession, guestUser, trialEnabled, type GuestSession } from './trial.ts'
export interface UserRouteContext { session: SessionData; user: AccountUser }
export interface ChatRouteContext { session: SessionData | GuestSession; user: AccountUser }
@@ -71,8 +71,13 @@ export function withChatRoute(body: (request: Request, context: ChatRouteContext
}
if (request.method !== 'GET' && request.method !== 'HEAD') assertSameOrigin(request)
if ('kind' in session) return privateResponse(await body(request, { session, user: guestUser(session) }))
+ // A guest who signs in keeps their chats; the claim runs once, then the cookie goes,
+ // so later requests skip the database write.
const guest = await getGuestSession()
- if (guest) await claimGuestSessions(guest.userId, session.userId)
+ if (guest) {
+ await claimGuestSessions(guest.userId, session.userId)
+ await clearGuestSession()
+ }
return privateResponse(await body(request, { session, user: publicUser(session) }))
} catch (error) { return routeFailure(error) }
})
diff --git a/lib/trial.ts b/lib/trial.ts
index 4b21742..90238cb 100644
--- a/lib/trial.ts
+++ b/lib/trial.ts
@@ -75,6 +75,12 @@ export async function createGuestSession(): Promise<GuestSession> {
return session
}
+// Once a signed-in request has claimed the guest's chats, the guest cookie has no use left
+// (the free prompt allowance is counted per IP, not per guest), so it is dropped.
+export async function clearGuestSession(): Promise<void> {
+ ;(await cookies()).delete(GUEST_COOKIE)
+}
+
// Vercel overwrites this header. Outside Vercel, only local development is
// supported; accepting a caller's forwarded IP there would reset the allowance.
export function trialIpHash(headers: Headers): string {
diff --git a/tests/route-security.test.mjs b/tests/route-security.test.mjs
index a9310bc..b7be32c 100644
--- a/tests/route-security.test.mjs
+++ b/tests/route-security.test.mjs
@@ -14,6 +14,7 @@ const mocks = {
'lib/trial.ts': `
export const trialEnabled = () => !!globalThis.securityTest.trialEnabled;
export const getGuestSession = async () => globalThis.securityTest.guest ?? null;
+ export async function clearGuestSession() { globalThis.securityTest.trace.push('clear-guest'); globalThis.securityTest.guest = null; }
export async function createGuestSession() {
const s = globalThis.securityTest;
if (!s.trialEnabled) throw new Error('Trials disabled');
@@ -581,6 +582,18 @@ test('a capped attachment still schedules durable cleanup after its refusal; del
assert.equal(deleting.trace.includes('cleanup-retry'), true)
})
+test('a guest who signs in has their chats claimed once, then the guest cookie is dropped', async () => {
+ const s = reset({ guest: { kind: 'guest', userId: `guest:${'b'.repeat(32)}`, loginId: 'b'.repeat(32), loginAt: 1000, expiresAt: Date.now() + 86_400_000 } })
+ const claims = () => s.queries.filter(({ text }) => text.startsWith('UPDATE fx0_chat_ownership SET user_id')).length
+ assert.equal((await routes.session.GET(request('session', 'GET'))).status, 200)
+ assert.equal(claims(), 1)
+ assert.deepEqual(s.queries.find(({ text }) => text.startsWith('UPDATE fx0_chat_ownership SET user_id')).values, ['user_a', `guest:${'b'.repeat(32)}`])
+ assert.ok(s.trace.includes('clear-guest'))
+ assert.equal(s.guest, null)
+ assert.equal((await routes.session.GET(request('session', 'GET'))).status, 200)
+ assert.equal(claims(), 1, 'a later request does not repeat the claim')
+})
+
test('the artifact preview paints no white of its own before the design does', async () => {
reset()
const html = await (await routes['artifact-frame'].GET(request('artifact-frame', 'GET', own, undefined, 'null'))).text()