mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
- Google sign-in, an emailed sign-in code and a reset link confirm an account's address. On an account whose address was never confirmed, each first clears what was set up on it (sessions, password, passkeys, two-factor, API tokens, unused links) in the same D1 batch, and the app says once what went. - A new session or credential is written only while the account's token_version is still the one its check read. - Confirm and change-of-address links take effect only with their own account's session. Opened elsewhere, they finish after signing in there. A browser that starts local-first opens the sign-in for them, and the reset page works there too. - Google sign-in keeps and matches addresses in lower case. A reset link is refused once it has expired. An expired API token stops counting against the plan's limit. Deleting an account deletes its sessions.
101 lines
3.5 KiB
TypeScript
101 lines
3.5 KiB
TypeScript
/**
|
|
* Custom-report ownership: these used to live in a per-isolate in-memory Map keyed by a Date.now()
|
|
* id with no ownership check (an IDOR). They now persist in D1 (custom_reports) scoped by user_id.
|
|
* Runs the real worker in workerd against a local D1 built from worker/migrations/.
|
|
*/
|
|
import { env, SELF } from 'cloudflare:test';
|
|
import { beforeEach, describe, expect, it } from 'vitest';
|
|
import { sessionCookie } from './helpers/session';
|
|
|
|
let cookieA = '';
|
|
let cookieB = '';
|
|
|
|
beforeEach(async () => {
|
|
for (const t of ['custom_reports', 'users']) {
|
|
await env.DB.prepare(`DELETE FROM ${t}`).run();
|
|
}
|
|
await env.DB.batch([
|
|
env.DB.prepare(
|
|
"INSERT INTO users (id, email, auth_provider, token_version) VALUES (1, 'a@example.com', 'password', 1)"
|
|
),
|
|
env.DB.prepare(
|
|
"INSERT INTO users (id, email, auth_provider, token_version) VALUES (2, 'b@example.com', 'password', 1)"
|
|
),
|
|
]);
|
|
cookieA = (await sessionCookie(1, 'password', env)).split(';')[0];
|
|
cookieB = (await sessionCookie(2, 'password', env)).split(';')[0];
|
|
});
|
|
|
|
function req(cookie: string, path: string, init: RequestInit = {}): Promise<Response> {
|
|
return SELF.fetch(`https://example.com${path}`, {
|
|
...init,
|
|
headers: {
|
|
Cookie: cookie,
|
|
'Content-Type': 'application/json',
|
|
...((init.headers as Record<string, string>) ?? {}),
|
|
},
|
|
});
|
|
}
|
|
const body = (res: Response) => res.json() as Promise<Record<string, unknown>>;
|
|
|
|
describe('custom reports — ownership (IDOR fix)', () => {
|
|
it('requires authentication', async () => {
|
|
const res = await SELF.fetch('https://example.com/api/reports/custom', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name: 'x' }),
|
|
});
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it("a user cannot read, update, or delete another user's report", async () => {
|
|
const created = await body(
|
|
await req(cookieA, '/api/reports/custom', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ name: 'Mine' }),
|
|
})
|
|
);
|
|
expect(created.id as number).toBeGreaterThan(0);
|
|
const id = created.id;
|
|
|
|
// Owner can read; the other user gets 404 (not the other tenant's data).
|
|
expect((await req(cookieA, `/api/reports/custom/${id}`)).status).toBe(200);
|
|
expect((await req(cookieB, `/api/reports/custom/${id}`)).status).toBe(404);
|
|
expect(
|
|
(
|
|
await req(cookieB, `/api/reports/custom/${id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify({ name: 'Hacked' }),
|
|
})
|
|
).status
|
|
).toBe(404);
|
|
expect((await req(cookieB, `/api/reports/custom/${id}`, { method: 'DELETE' })).status).toBe(
|
|
404
|
|
);
|
|
|
|
// The owner's report is untouched.
|
|
const still = await body(await req(cookieA, `/api/reports/custom/${id}`));
|
|
expect(still.name).toBe('Mine');
|
|
});
|
|
|
|
it('persists in D1 and /saved lists only the requesting user reports', async () => {
|
|
await req(cookieA, '/api/reports/save', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ name: 'A-report', params: { x: 1 } }),
|
|
});
|
|
await req(cookieB, '/api/reports/save', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ name: 'B-report' }),
|
|
});
|
|
|
|
const savedA = (await body(await req(cookieA, '/api/reports/saved'))) as {
|
|
reports: Array<{ name: string }>;
|
|
};
|
|
expect(savedA.reports.map((r) => r.name)).toEqual(['A-report']);
|
|
const savedB = (await body(await req(cookieB, '/api/reports/saved'))) as {
|
|
reports: Array<{ name: string }>;
|
|
};
|
|
expect(savedB.reports.map((r) => r.name)).toEqual(['B-report']);
|
|
});
|
|
});
|