diff --git a/apps/server/src/auth/SessionStore.ts b/apps/server/src/auth/SessionStore.ts index 9fbddc7ae3a1e..c8cfea80c301d 100644 --- a/apps/server/src/auth/SessionStore.ts +++ b/apps/server/src/auth/SessionStore.ts @@ -20,6 +20,7 @@ import * as Layer from "effect/Layer"; import * as PubSub from "effect/PubSub"; import * as Ref from "effect/Ref"; import * as Schema from "effect/Schema"; +import * as SqlClient from "effect/sql/SqlClient"; import * as Stream from "effect/Stream"; import * as Option from "effect/Option"; @@ -530,6 +531,7 @@ const make = Effect.gen(function* () { const serverEnvironment = yield* ServerEnvironment.ServerEnvironmentIdentity; const secretStore = yield* ServerSecretStore.ServerSecretStore; const authSessions = yield* AuthSessions.AuthSessionRepository; + const sql = yield* SqlClient.SqlClient; const signer = yield* PayloadSigner.fromSecret( yield* secretStore.getOrCreateRandom(SIGNING_SECRET_NAME, 32), ); @@ -774,31 +776,36 @@ const make = Effect.gen(function* () { }) : authSessions.create(sessionRecord).pipe(Effect.as([] as ReadonlyArray)) ).pipe(Effect.mapError((cause) => new SessionCredentialIssueError({ sessionId, cause }))); - if (claims.jkt) latestSessionByProofKey.set(claims.jkt, sessionId); - if (replacedSessionIds.length > 0) { - yield* Ref.update(connectedSessionsRef, (current) => { - const next = new Map(current); - for (const replacedSessionId of replacedSessionIds) { - next.delete(replacedSessionId); + // Kin issues inside its own transaction; a rollback must leave nothing announced. + yield* sql.afterCommit( + Effect.gen(function* () { + if (claims.jkt) latestSessionByProofKey.set(claims.jkt, sessionId); + if (replacedSessionIds.length > 0) { + yield* Ref.update(connectedSessionsRef, (current) => { + const next = new Map(current); + for (const replacedSessionId of replacedSessionIds) { + next.delete(replacedSessionId); + } + return next; + }); + yield* Effect.forEach(replacedSessionIds, emitRemoved, { + concurrency: "unbounded", + discard: true, + }); } - return next; - }); - yield* Effect.forEach(replacedSessionIds, emitRemoved, { - concurrency: "unbounded", - discard: true, - }); - } - yield* emitUpsert( - toAuthClientSession({ - sessionId, - subject: claims.sub, - scopes: claims.scopes, - method: claims.method, - client, - issuedAt, - expiresAt, - lastConnectedAt: null, - connected: false, + yield* emitUpsert( + toAuthClientSession({ + sessionId, + subject: claims.sub, + scopes: claims.scopes, + method: claims.method, + client, + issuedAt, + expiresAt, + lastConnectedAt: null, + connected: false, + }), + ); }), ); @@ -1089,12 +1096,13 @@ const make = Effect.gen(function* () { const revokedSessionIds = yield* authSessions .revokeByProofKey({ proofKeyThumbprint, revokedAt }) .pipe(Effect.mapError((cause) => new ProofKeySessionsError({ operation: "revoke", cause }))); - yield* Ref.update(connectedSessionsRef, (current) => { - const next = new Map(current); - for (const revokedSessionId of revokedSessionIds) next.delete(revokedSessionId); - return next; - }); - yield* Effect.forEach(revokedSessionIds, emitRemoved, { discard: true }); + yield* sql.afterCommit( + Ref.update(connectedSessionsRef, (current) => { + const next = new Map(current); + for (const revokedSessionId of revokedSessionIds) next.delete(revokedSessionId); + return next; + }).pipe(Effect.andThen(Effect.forEach(revokedSessionIds, emitRemoved, { discard: true }))), + ); return revokedSessionIds.length; }); diff --git a/apps/server/src/kin/auth/ProofKeySessions.test.ts b/apps/server/src/kin/auth/ProofKeySessions.test.ts index db5cde21b549e..98d3ae738df6f 100644 --- a/apps/server/src/kin/auth/ProofKeySessions.test.ts +++ b/apps/server/src/kin/auth/ProofKeySessions.test.ts @@ -441,6 +441,37 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("Kin proof key se { type: "clientRemoved", sessionId: sibling.sessionId }, ]), ); + + // Replacement and proof-key revoke run the plain store inside Kin's own write, so a + // failure later in that write must take back what the store would have announced. + const phone = yield* sessions.issue({ proofKeyThumbprint: "phone" }); + const linked = yield* sessions.issue({ proofKeyThumbprint: "linked" }); + yield* sessions.markConnected(phone.sessionId); + for (let i = 0; i < 3; i++) + expect((yield* Queue.take(changes)).type).toBe("clientUpserted"); + yield* sql`INSERT INTO kin_orgs (org_id, name, created_at) VALUES ('org', 'Org', '')`; + yield* sql`INSERT INTO kin_people (person_id, org_id, handle, display_name, created_at, access) + VALUES ('asha', 'org', 'asha', 'Asha', '', 'member')`; + yield* sql`INSERT INTO kin_session_bindings (session_id, org_id, person_id, bound_at, parent_session_id) + VALUES (${phone.sessionId}, 'org', 'asha', 0, NULL), (${linked.sessionId}, 'org', 'asha', 0, ${phone.sessionId})`; + yield* sql`DELETE FROM reject_revoke_ids`; + yield* sql`INSERT INTO reject_revoke_ids VALUES (${linked.sessionId})`; + yield* sql`CREATE TRIGGER reject_revoke BEFORE UPDATE ON auth_sessions WHEN NEW.session_id IN (SELECT session_id FROM reject_revoke_ids) BEGIN SELECT RAISE(ABORT, 'cannot revoke'); END`; + expect( + Exit.isFailure( + yield* sessions + .issue({ proofKeyThumbprint: "phone", replaceSessionId: phone.sessionId }) + .pipe(Effect.exit), + ), + ).toBe(true); + expect(Exit.isFailure(yield* sessions.revokeByProofKey("phone").pipe(Effect.exit))).toBe( + true, + ); + expect(yield* Queue.size(changes)).toBe(0); + expect( + (yield* sessions.listActive()).find((session) => session.sessionId === phone.sessionId) + ?.connected, + ).toBe(true); }).pipe(Effect.scoped, Effect.provide(layer)), ); diff --git a/docs/user/kin.md b/docs/user/kin.md index 387bbcc7e5b99..f9dc736372f36 100644 --- a/docs/user/kin.md +++ b/docs/user/kin.md @@ -32,8 +32,9 @@ A join link works once and lasts a week. If yours has expired or was used, ask f Once you're in, you add your other devices yourself. Open **Linked devices** (under your name on the web, in **Settings** on the phone) and choose **Link a device**. Kin shows a link and a QR code that work once, for 10 minutes. Scan the QR code with the new phone, or open the link in the new -browser. Each time a device is linked, Kin tells you in Kin and on your phones that have -notifications on, so you'd hear of one you didn't add. +browser. Each time a device is linked, Kin tells you on your phones that have notifications on, +and in your chat with the Team agent. Without a Team agent, the notice goes to your direct chats. +If you only use a browser and have no direct chats, check **Linked devices** for any you didn't add. A linked device stays signed in for 30 days, then you link it again the same way. You can have 10 devices at a time. Remove a device from the same list to sign it out at once, along with every