Files
KomediruzeckiandGitHub 76e1efded8 feat(auth): a password account confirms its email before it uses the app (#616)
A password account confirms its email before it signs in or uses the app.

- Create account ends on Check your inbox, which names the address, sends the link again and says
  where to open it. The screen is the same whatever the address.
- A password sign-in works once the address is confirmed, or in the browser that opened the
  account's own confirm link. Until then the right password is refused as a wrong one is, and the
  form offers Send the link again.
- A signed-in account whose address is not confirmed sees Confirm your email instead of the app.
  The Worker answers its sessions and API tokens 403 EMAIL_UNCONFIRMED everywhere but me, logout,
  the signed-in resend, the link's finish route, and the billing portal for an account that pays.
- POST /api/auth/verify-email/resend sends the link again while signed out, with limits and the
  captcha before any look-up. One budget of three confirm links an hour covers every way of
  asking for one.
- Sign-up answers first, then makes the account, its profile and its first link in one batch,
  tried twice. Signing up again for an address waiting for its link mails a fresh link.
- A sign-in code, a reset link and Google sign-in confirm the address. A passkey or two-factor
  does not.
- /api/v1 capabilities, reminders and sheet syncs leave out an account waiting for its link.
- The welcome and confirm mails say the link starts the account and where to sign in after
  opening it. No mail on the sign-up path has an em dash before the shared footer.

No migration.
2026-10-10 18:17:51 +02:00

120 lines
4.4 KiB
TypeScript

/**
* One table-driven sweep: user B's token must not see or touch user A's data through ANY tool.
*
* Written as a loop over the live registry rather than a fixed list, so a tool added later is
* covered the day it is added instead of the day somebody remembers to extend this file.
*/
import { env, SELF } from 'cloudflare:test';
import { beforeAll, describe, expect, it } from 'vitest';
import { mintApiToken } from '../src/apitoken';
import { TOOLS } from '../src/mcp/registry';
// Importing the route module is what registers the tools into TOOLS.
import '../src/mcp';
const A_USER = 9700;
const A_PROFILE = 9701;
const B_USER = 9800;
const B_PROFILE = 9801;
let bToken = '';
/** Minimal valid arguments per tool, all pointed at user A's profile. */
const ARGS: Record<string, Record<string, unknown>> = {
whoami: {},
get_overview: {},
list_transactions: {},
summarize_spending: {},
list_reference_data: {},
get_budgets_and_goals: {},
export_snapshot: {},
prepare_import: { mode: 'preview' },
list_imports: {},
undo_import: { importId: 'a-batch' },
create_transactions: {
transactions: [{ date: '2026-04-01', description: 'x', amount: -1, type: 'expense' }],
},
create_account: { name: 'Intruder' },
categorize_transactions: { transactionIds: [97001], categoryId: 97002 },
upsert_tag_rule: { tagName: 't', name: 'r', criteria: {} },
upsert_budget: { categoryId: 97002, amount: 10, startDate: '2026-04-01' },
};
async function callAsB(name: string, args: Record<string, unknown>): Promise<any> {
const res = await SELF.fetch('https://api.example.com/mcp', {
method: 'POST',
headers: { Authorization: `Bearer ${bToken}`, 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'tools/call',
params: { name, arguments: { ...args, profileId: A_PROFILE } },
}),
});
return ((await res.json()) as any).result;
}
beforeAll(async () => {
for (const [uid, pid, email] of [
[A_USER, A_PROFILE, 'a@example.com'],
[B_USER, B_PROFILE, 'b@example.com'],
] as const) {
await env.DB.prepare(
"INSERT OR IGNORE INTO users (id, email, password_hash, auth_provider, email_verified, token_version, plan) VALUES (?, ?, 'pbkdf2$100000$x$y', 'password', 1, 1, 'advanced')"
)
.bind(uid, email)
.run();
await env.DB.prepare("INSERT OR IGNORE INTO profiles (id, name, user_id) VALUES (?, 'P', ?)")
.bind(pid, uid)
.run();
}
await env.DB.prepare(
"INSERT OR IGNORE INTO categories (id, name, type, profile_id) VALUES (97002, 'A Category', 'expense', ?)"
)
.bind(A_PROFILE)
.run();
await env.DB.prepare(
"INSERT OR IGNORE INTO transactions (id, date, description, amount, type, currency, profile_id, import_id) VALUES (97001, '2026-04-01', 'A secret', -99, 'expense', 'EUR', ?, 'a-batch')"
)
.bind(A_PROFILE)
.run();
bToken = (
await mintApiToken(env.DB, B_USER, {
name: 'b',
scopes: ['read', 'write', 'import'],
defaultProfileId: B_PROFILE,
})
).secret;
});
describe('cross-user isolation', () => {
it('has minimal arguments defined for every registered tool', () => {
expect(TOOLS.length).toBeGreaterThan(0);
const missing = TOOLS.map((t) => t.name).filter((n) => !(n in ARGS));
expect(missing, `add these to ARGS: ${missing.join(', ')}`).toEqual([]);
});
it.each(TOOLS.map((t) => t.name))(
"%s refuses user A's profile when called with user B's token",
async (name) => {
const result = await callAsB(name, ARGS[name] ?? {});
expect(result.isError, `${name} accepted a foreign profileId`).toBe(true);
expect(result.content[0].text).toMatch(/profile/i);
}
);
it("leaves user A's data completely untouched", async () => {
const tx = await env.DB.prepare(
'SELECT description, amount, category_id FROM transactions WHERE id = 97001'
).first<{ description: string; amount: number; category_id: number | null }>();
expect(tx).toMatchObject({ description: 'A secret', amount: -99, category_id: null });
const counts = await env.DB.prepare(
'SELECT (SELECT COUNT(*) FROM accounts WHERE profile_id = ?) AS accounts, (SELECT COUNT(*) FROM budgets WHERE profile_id = ?) AS budgets, (SELECT COUNT(*) FROM tag_rules WHERE profile_id = ?) AS rules'
)
.bind(A_PROFILE, A_PROFILE, A_PROFILE)
.first<{ accounts: number; budgets: number; rules: number }>();
expect(counts).toEqual({ accounts: 0, budgets: 0, rules: 0 });
});
});