Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions browser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions browser/data-browser/src/components/AccountRecoveryCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
PrfUnsupportedError,
readCachedBackups,
revealSecretFromBackup,
sameAgent,
saveRecoverySecret,
storeCachedRecoverySecret,
type RecoverySecret,
Expand Down Expand Up @@ -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 });

Expand All @@ -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(
Expand Down
74 changes: 74 additions & 0 deletions browser/data-browser/src/helpers/managed/reconcile.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest';
import {
evaluateIdentityReconciliation,
evaluateServerReconciliation,
connectHostedDrive,
localAgentIsDisposable,
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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';

Expand Down
18 changes: 10 additions & 8 deletions browser/data-browser/src/helpers/managed/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -32,17 +32,19 @@ export type IdentityReconcileResult =
function activeEnrollmentAgents(
enrollments: ManagedEnrollmentSummary[],
): string[] {
const agents = new Set<string>();
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;
}

/**
Expand Down Expand Up @@ -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: {
Expand All @@ -94,7 +96,7 @@ export async function evaluateIdentityReconciliation(

if (
enrollmentAgents.length > 0 &&
!enrollmentAgents.includes(localAgentSubject)
!enrollmentAgents.some(agent => sameAgent(agent, localAgentSubject))
) {
return {
ok: false,
Expand All @@ -107,7 +109,7 @@ export async function evaluateIdentityReconciliation(
};
}

if (bindingAgent && bindingAgent !== localAgentSubject) {
if (bindingAgent && !sameAgent(bindingAgent, localAgentSubject)) {
return {
ok: false,
issue: {
Expand Down
7 changes: 5 additions & 2 deletions browser/data-browser/src/helpers/managed/recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion browser/data-browser/src/routes/SyncRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import {
envelopeWrapperKinds,
getRecoverySecret,
readCachedBackups,
sameAgent,
} from '../helpers/managed/recovery';
import { useDriveVault } from '../helpers/managed/useDriveVault';
import { ContainerNarrow } from '../components/Containers';
Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
decryptEnvelopeV2,
decryptEnvelopeWithPasskey,
envelopeWrapperKinds,
sameAgent,
upgradeToEnvelopeV2,
type RecoverySecret,
} from '../../helpers/managed/recovery';
Expand Down Expand Up @@ -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(
Expand Down