From 0eea1613d6bab5e209fbbdaed0fc55dbf9c506d6 Mon Sep 17 00:00:00 2001 From: Joep Meindertsma Date: Fri, 25 Sep 2026 04:18:10 +0000 Subject: [PATCH] Compare agents by identity when checking them against the account A recovery backup saved before the did:ad: to atomic: rename names the agent as did:ad:agent:..., and a secret exported before it still decodes to that spelling, while a newer one decodes to atomic:agent:.... The reconcile gate and the secret sign-in compared the strings, so one agent looked like two: opening the app while signed in to atomic.place archived the local identity and sent the user to sign in, and signing in with the secret then ended the account session, which signs out of atomic.place too because the session cookie is shared. #1664 fixed the same comparison inside recovery.ts. Export its sameAgent and use it in reconcile.ts, the secret sign-in's conflicting-session check, and the two places that match the cached backup to the agent. --- browser/CHANGELOG.md | 7 ++ .../src/components/AccountRecoveryCard.tsx | 5 +- .../src/helpers/managed/reconcile.test.ts | 74 +++++++++++++++++++ .../src/helpers/managed/reconcile.ts | 18 +++-- .../src/helpers/managed/recovery.ts | 7 +- browser/data-browser/src/routes/SyncRoute.tsx | 3 +- .../getting-started/GettingStartedFlow.tsx | 3 +- 7 files changed, 103 insertions(+), 14 deletions(-) diff --git a/browser/CHANGELOG.md b/browser/CHANGELOG.md index 211cfe8199..84ed3dfd9c 100644 --- a/browser/CHANGELOG.md +++ b/browser/CHANGELOG.md @@ -4,6 +4,13 @@ This changelog covers all five packages, as they are (for now) updated as a whol ## UNRELEASED +- Fixed opening the app while signed in to atomic.place sending you to sign + in again, and signing in with your secret then signing you out of + atomic.place. A backup saved before the `did:ad:` → `atomic:` rename names + your agent in the old spelling, and the check that compares it with the + agent on this device compared the text, so one agent looked like two. The + gate then switched identities, and the secret sign-in ended the account + session as if the secret belonged to someone else. - A private workspace the app has to recreate is titled after whoever it belongs to, like the one onboarding and an accepted invitation already make. A returning account on a second device, and a sign-in whose cloud diff --git a/browser/data-browser/src/components/AccountRecoveryCard.tsx b/browser/data-browser/src/components/AccountRecoveryCard.tsx index 9ad08ea89f..ce9d606780 100644 --- a/browser/data-browser/src/components/AccountRecoveryCard.tsx +++ b/browser/data-browser/src/components/AccountRecoveryCard.tsx @@ -30,6 +30,7 @@ import { PrfUnsupportedError, readCachedBackups, revealSecretFromBackup, + sameAgent, saveRecoverySecret, storeCachedRecoverySecret, type RecoverySecret, @@ -172,7 +173,7 @@ export function AccountRecoveryCard({ if ( fromServer && - (!agentSubject || fromServer.agent_subject === agentSubject) + (!agentSubject || sameAgent(fromServer.agent_subject, agentSubject)) ) { setBackup({ phase: 'ready', secret: fromServer, onServer: true }); @@ -181,7 +182,7 @@ export function AccountRecoveryCard({ const cached = readCachedBackups(); const mine = agentSubject - ? cached.find(entry => entry.agent_subject === agentSubject) + ? cached.find(entry => sameAgent(entry.agent_subject, agentSubject)) : cached[cached.length - 1]; setBackup( diff --git a/browser/data-browser/src/helpers/managed/reconcile.test.ts b/browser/data-browser/src/helpers/managed/reconcile.test.ts index cf204c108c..fef9927904 100644 --- a/browser/data-browser/src/helpers/managed/reconcile.test.ts +++ b/browser/data-browser/src/helpers/managed/reconcile.test.ts @@ -1,5 +1,6 @@ import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'; import { + evaluateIdentityReconciliation, evaluateServerReconciliation, connectHostedDrive, localAgentIsDisposable, @@ -16,6 +17,7 @@ import type { ManagedEnrollmentSummary } from './enrollmentApi'; function mockFetch(opts: { account?: { email: string } | null; enrollments?: ManagedEnrollmentSummary[]; + recovery?: { agent_subject: string } | null; }) { globalThis.fetch = vi.fn((input: RequestInfo | URL) => { const url = String(input); @@ -40,6 +42,23 @@ function mockFetch(opts: { } as Response); } + if (url.endsWith('/recovery-secret')) { + if (!opts.recovery) { + return Promise.resolve({ status: 404, ok: false } as Response); + } + + return Promise.resolve({ + ok: true, + status: 200, + json: () => + Promise.resolve({ + owner_email: opts.account?.email, + wrappers: [], + ...opts.recovery, + }), + } as Response); + } + throw new Error(`Unexpected fetch: ${url}`); }) as typeof fetch; } @@ -307,6 +326,61 @@ describe('evaluateServerReconciliation', () => { }); }); +describe('evaluateIdentityReconciliation', () => { + beforeEach(() => + vi.stubEnv('VITE_MANAGED_API_BASE', 'https://portal.example/api'), + ); + afterEach(() => vi.unstubAllEnvs()); + const realFetch = globalThis.fetch; + + afterEach(() => { + globalThis.fetch = realFetch; + vi.restoreAllMocks(); + }); + + // A backup saved before the `did:ad:` → `atomic:` rename, read by an agent + // loaded under the new spelling (or the other way round), is the same + // agent. Calling it a mismatch sent the gate into the account switch and + // the secret sign-in into ending the portal session. + it('treats the legacy did:ad: spelling of the backup agent as the same agent', async () => { + mockFetch({ + account: { email: 'legacy-backup@example.com' }, + recovery: { agent_subject: 'did:ad:agent:abc' }, + enrollments: [enrollment({ agent_subject: 'did:ad:agent:abc' })], + }); + + await expect( + evaluateIdentityReconciliation('atomic:agent:abc'), + ).resolves.toMatchObject({ ok: true }); + }); + + it('treats the new atomic: spelling of the backup agent as the same agent', async () => { + mockFetch({ + account: { email: 'new-backup@example.com' }, + recovery: { agent_subject: 'atomic:agent:abc' }, + enrollments: [enrollment({ agent_subject: 'atomic:agent:abc' })], + }); + + await expect( + evaluateIdentityReconciliation('did:ad:agent:abc'), + ).resolves.toMatchObject({ ok: true }); + }); + + it('still reports a genuinely different agent', async () => { + mockFetch({ + account: { email: 'other-agent@example.com' }, + recovery: { agent_subject: 'did:ad:agent:abc' }, + }); + + await expect( + evaluateIdentityReconciliation('atomic:agent:xyz'), + ).resolves.toMatchObject({ + ok: false, + issue: { reason: 'recovery_agent' }, + }); + }); +}); + describe('localAgentIsDisposable', () => { const personalDrive = 'https://atomicdata.dev/properties/personalDrive'; diff --git a/browser/data-browser/src/helpers/managed/reconcile.ts b/browser/data-browser/src/helpers/managed/reconcile.ts index a21295c2d1..be668f1ed2 100644 --- a/browser/data-browser/src/helpers/managed/reconcile.ts +++ b/browser/data-browser/src/helpers/managed/reconcile.ts @@ -9,7 +9,7 @@ import { getManagedEnrollments, type ManagedEnrollmentSummary, } from './enrollmentApi'; -import { getRecoverySecret } from './recovery'; +import { getRecoverySecret, sameAgent } from './recovery'; import { getManagedAccount, type ManagedAccount } from './session'; export type IdentityMismatchReason = @@ -32,17 +32,19 @@ export type IdentityReconcileResult = function activeEnrollmentAgents( enrollments: ManagedEnrollmentSummary[], ): string[] { - const agents = new Set(); + const agents: string[] = []; for (const enrollment of enrollments) { if (enrollment.status === 'Disabled') continue; - if (enrollment.agent_subject) { - agents.add(enrollment.agent_subject); + const subject = enrollment.agent_subject; + + if (subject && !agents.some(agent => sameAgent(agent, subject))) { + agents.push(subject); } } - return [...agents]; + return agents; } /** @@ -80,7 +82,7 @@ export async function evaluateIdentityReconciliation( return { ok: true, managedAccount }; } - if (recoveryAgent && recoveryAgent !== localAgentSubject) { + if (recoveryAgent && !sameAgent(recoveryAgent, localAgentSubject)) { return { ok: false, issue: { @@ -94,7 +96,7 @@ export async function evaluateIdentityReconciliation( if ( enrollmentAgents.length > 0 && - !enrollmentAgents.includes(localAgentSubject) + !enrollmentAgents.some(agent => sameAgent(agent, localAgentSubject)) ) { return { ok: false, @@ -107,7 +109,7 @@ export async function evaluateIdentityReconciliation( }; } - if (bindingAgent && bindingAgent !== localAgentSubject) { + if (bindingAgent && !sameAgent(bindingAgent, localAgentSubject)) { return { ok: false, issue: { diff --git a/browser/data-browser/src/helpers/managed/recovery.ts b/browser/data-browser/src/helpers/managed/recovery.ts index 9b160f2915..6eaccca8e1 100644 --- a/browser/data-browser/src/helpers/managed/recovery.ts +++ b/browser/data-browser/src/helpers/managed/recovery.ts @@ -58,9 +58,12 @@ export type RecoverySecret = { /** * Backups saved before the `did:ad:` → `atomic:` rename carry the legacy - * spelling of the same agent, so compare identities, not strings. + * spelling of the same agent, and so do secrets exported before it, so + * compare identities, not strings. Every check of "is this the account's + * agent" goes through here: a strict comparison makes one agent look like + * two, and the reconcile gate then switches identities and signs out. */ -function sameAgent(a: string, b: string): boolean { +export function sameAgent(a: string, b: string): boolean { return canonicalIdentifier(a) === canonicalIdentifier(b); } diff --git a/browser/data-browser/src/routes/SyncRoute.tsx b/browser/data-browser/src/routes/SyncRoute.tsx index 274a5f5379..ffcab14d0f 100644 --- a/browser/data-browser/src/routes/SyncRoute.tsx +++ b/browser/data-browser/src/routes/SyncRoute.tsx @@ -85,6 +85,7 @@ import { envelopeWrapperKinds, getRecoverySecret, readCachedBackups, + sameAgent, } from '../helpers/managed/recovery'; import { useDriveVault } from '../helpers/managed/useDriveVault'; import { ContainerNarrow } from '../components/Containers'; @@ -843,7 +844,7 @@ function SyncPage() { const agentSubject = store.getAgent()?.subject; const cached = readCachedBackups(); const mine = agentSubject - ? cached.some(entry => entry.agent_subject === agentSubject) + ? cached.some(entry => sameAgent(entry.agent_subject, agentSubject)) : cached.length > 0; setRecoveryBackup(mine ? 'device-only' : 'none'); diff --git a/browser/data-browser/src/views/getting-started/GettingStartedFlow.tsx b/browser/data-browser/src/views/getting-started/GettingStartedFlow.tsx index d0b570eacd..a469c0dd78 100644 --- a/browser/data-browser/src/views/getting-started/GettingStartedFlow.tsx +++ b/browser/data-browser/src/views/getting-started/GettingStartedFlow.tsx @@ -53,6 +53,7 @@ import { decryptEnvelopeV2, decryptEnvelopeWithPasskey, envelopeWrapperKinds, + sameAgent, upgradeToEnvelopeV2, type RecoverySecret, } from '../../helpers/managed/recovery'; @@ -591,7 +592,7 @@ export function GettingStartedFlow({ try { const stored = await getRecoverySecret(); - if (stored && stored.agent_subject !== agentSubject) { + if (stored && !sameAgent(stored.agent_subject, agentSubject)) { clearManagedAccountBinding(); await logoutManagedSession(); toast(