diff options
| author | Adam Soltys <[email protected]> | 2026-12-02 10:34:36 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-12-02 10:34:36 -0700 |
| commit | 374afce1c93e25373eadce258edaa0398fb99faf (patch) | |
| tree | a53eeff7db40f876b7cc55e57b50ebd4d334abb2 /test/migrate-grant.test.ts | |
| parent | d707d2c696184b5ad90132cb0a4b5673d15d6644 (diff) | |
| download | coinos-ui-master.tar.gz coinos-ui-master.zip | |
The /migrate action told the names registrar which v3 identity may claim
a name, using the `username` field from the submitted form. Any logged-in
account could post another user's name and be granted it. Look the name
up from the session (/me) instead, and refuse before sweeping or flagging
anything when there is no session.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VMan6SSfVopM1ZzXKZmLYU
Diffstat (limited to 'test/migrate-grant.test.ts')
| -rw-r--r-- | test/migrate-grant.test.ts | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/test/migrate-grant.test.ts b/test/migrate-grant.test.ts new file mode 100644 index 00000000..af73d4a5 --- /dev/null +++ b/test/migrate-grant.test.ts @@ -0,0 +1,71 @@ +// bun test test/migrate-grant.test.ts +// +// The /migrate action tells the v3 registrar which identity may claim a +// @coinos.io name. That name must come from the logged-in session, never from +// the form, or any logged-in account could be granted someone else's name. +import { beforeEach, describe, expect, mock, test } from "bun:test"; + +const SESSION_USER = "attacker1"; +let me: any = { username: SESSION_USER }; +const posts: [string, any][] = []; + +mock.module("$env/dynamic/private", () => ({ env: { MIGRATE_TOKEN: "t" } })); +mock.module("$lib/utils", () => ({ + auth: () => ({ authorization: "Bearer x" }), + get: async (url: string) => { + if (url !== "/me") throw new Error(`unexpected get ${url}`); + if (!me) throw new Error("unauthorized"); + return me; + }, + post: async (url: string, body: any) => { + posts.push([url, body]); + return {}; + }, +})); +mock.module("@sveltejs/kit", () => ({ redirect: () => {} })); + +const grants: any[] = []; +globalThis.fetch = (async (url: string, init: any) => { + if (String(url).endsWith("/migrate-grant")) grants.push(JSON.parse(init.body)); + return new Response("{}", { status: 200 }); +}) as any; + +const { actions } = await import("../src/routes/(app)/migrate/+page.server.ts"); + +const run = (fields: Record<string, string>) => { + const form = new FormData(); + for (const [k, v] of Object.entries(fields)) form.set(k, v); + return actions.default({ + cookies: { get: () => "x" }, + request: new Request("http://x/migrate", { method: "POST", body: form }), + url: new URL("http://x/migrate"), + } as any); +}; + +beforeEach(() => { + me = { username: SESSION_USER }; + posts.length = 0; + grants.length = 0; +}); + +describe("/migrate grant", () => { + test("grants the session's own name", async () => { + const r = await run({ to: "[email protected]", username: SESSION_USER }); + expect(r).toEqual({ ok: true, sent: 0, released: SESSION_USER }); + expect(grants).toEqual([{ name: SESSION_USER, to: "[email protected]" }]); + }); + + test("ignores a username in the form that isn't the session's", async () => { + const r = await run({ to: "[email protected]", username: "victim" }); + expect(grants.map((g) => g.name)).toEqual([SESSION_USER]); + expect(r.released).toBe(SESSION_USER); + }); + + test("no session: nothing flagged, nothing granted", async () => { + me = null; + const r = await run({ to: "[email protected]", username: "victim" }); + expect(r).toEqual({ error: "not logged in" }); + expect(posts).toEqual([]); + expect(grants).toEqual([]); + }); +}); |
