From 0e45fea431cb1ec8a88234de1b7828ece9a69de4 Mon Sep 17 00:00:00 2001 From: dawn Date: Mon, 26 Oct 2026 22:23:05 +0300 Subject: [PATCH] e2e/signup: check a signup lands on /welcome signed in without the pds pages Signed-off-by: dawn --- e2e/harness/api.ts | 7 ++ e2e/harness/processes.ts | 17 ++++- e2e/harness/signin.ts | 37 ++++++++--- e2e/specs/signup.spec.ts | 140 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 191 insertions(+), 10 deletions(-) create mode 100644 e2e/specs/signup.spec.ts diff --git a/e2e/harness/api.ts b/e2e/harness/api.ts index f66db6142..37772f523 100644 --- a/e2e/harness/api.ts +++ b/e2e/harness/api.ts @@ -181,6 +181,13 @@ const admin = async (nsid: string, json: object) => { ); }; +export const preauthorize = (did: string, requestUri: string) => + request(`${config.pdsUrl}/_preauthorize`, { + method: "POST", + basic: `admin:${config.pdsAdminPassword}`, + json: { did, requestUri } + }); + const slug = (text: string) => text.toLowerCase().replace(/[^a-z0-9]+/g, "-"); // no other test shares an account's stars, tickets or rate limits diff --git a/e2e/harness/processes.ts b/e2e/harness/processes.ts index 18f69fc5f..07a6642a8 100644 --- a/e2e/harness/processes.ts +++ b/e2e/harness/processes.ts @@ -7,8 +7,7 @@ const onPc = process.env.E2E_STACK === "pc"; const stateDir = () => { const state = process.env.E2E_PC_STATE; - if (!onPc || !state) - throw new Error("stopping services needs the pc stack, run it with E2E_STACK=pc"); + if (!onPc || !state) throw new Error("this needs the pc stack, run it with E2E_STACK=pc"); return state; }; @@ -63,6 +62,20 @@ export const startProcess = async (name: string, upTo: "started" | "ready" = "re export const isReady = async (name: string) => (await processOf(name)).is_ready === "Ready"; +// deliberi has no resend key here, so the token comes from its db instead of a mail +export const signupToken = (email: string) => { + const db = new DatabaseSync(`${stateDir()}/data/deliberi/deliberi.db`, { readOnly: true }); + try { + const row = db + .prepare("select verification_token as token from signups_inflight where email = ?") + .get(email); + if (typeof row?.token !== "string") throw new Error(`deliberi has no signup for ${email}`); + return row.token; + } finally { + db.close(); + } +}; + // stop spindle first, it reads this at boot and keeps writing it export const rewindJetstreamCursor = (microseconds: number) => { const db = new DatabaseSync(`${stateDir()}/data/spindle/spindle.db`); diff --git a/e2e/harness/signin.ts b/e2e/harness/signin.ts index 87f4253d4..96047331d 100644 --- a/e2e/harness/signin.ts +++ b/e2e/harness/signin.ts @@ -138,9 +138,14 @@ class Jar { } } -const authorize = async (account: Account, dpop: typeof fetch, challenge: string) => { - const [client, server] = await Promise.all([clientMetadata(), authServer()]); - const pds = new URL(server.issuer); +export interface Client { + client_id: string; + redirect_uris: string[]; + scope: string; +} + +const push = async (dpop: typeof fetch, challenge: string, client: Client, loginHint?: string) => { + const server = await authServer(); const state = randomBytes(18).toString("base64url"); const par = ok( "the pds par", @@ -149,7 +154,7 @@ const authorize = async (account: Account, dpop: typeof fetch, challenge: string code_challenge: challenge, code_challenge_method: "S256", state, - login_hint: account.handle, + ...(loginHint ? { login_hint: loginHint } : {}), response_mode: "fragment", response_type: "code", scope: client.scope, @@ -157,12 +162,19 @@ const authorize = async (account: Account, dpop: typeof fetch, challenge: string }), 201 ); + return { server, state, requestUri: par.request_uri as string }; +}; + +const authorize = async (account: Account, dpop: typeof fetch, challenge: string) => { + const client = await clientMetadata(); + const { server, state, requestUri } = await push(dpop, challenge, client, account.handle); + const pds = new URL(server.issuer); // opening the page is what sets the device and csrf cookies the calls need const jar = new Jar(pds.host); const page = `${server.authorization_endpoint}?${new URLSearchParams({ client_id: client.client_id, - request_uri: par.request_uri + request_uri: requestUri })}`; const opened = await send(page, "GET", { "sec-fetch-site": "same-site", @@ -211,9 +223,8 @@ const authorize = async (account: Account, dpop: typeof fetch, challenge: string return { client, server, code, cookies: jar.all }; }; -export const mintSession = async (account: Account): Promise => { - const key = await generateDpopKey(["ES256"]); - const dpop = createDpopFetch({ +const dpopFetch = (key: Awaited>) => + createDpopFetch({ key, nonces: { get: (origin) => nonces.get(origin), @@ -235,6 +246,16 @@ export const mintSession = async (account: Account): Promise => { return response as unknown as Response; } }); + +export const pushedRequest = async (client?: Client) => { + const { challenge } = await generatePkce(); + const dpop = dpopFetch(await generateDpopKey(["ES256"])); + return (await push(dpop, challenge, client ?? (await clientMetadata()))).requestUri; +}; + +export const mintSession = async (account: Account): Promise => { + const key = await generateDpopKey(["ES256"]); + const dpop = dpopFetch(key); const { verifier, challenge } = await generatePkce(); const { client, server, code, cookies } = await authorize(account, dpop, challenge); const token = ok( diff --git a/e2e/specs/signup.spec.ts b/e2e/specs/signup.spec.ts new file mode 100644 index 000000000..1ee0bf94d --- /dev/null +++ b/e2e/specs/signup.spec.ts @@ -0,0 +1,140 @@ +import { expect } from "@playwright/test"; + +import { preauthorize } from "../harness/api"; +import { config, seeded, type Account } from "../harness/config"; +import { button, field, flow, page, testid, where, type Ctx } from "../harness/flow"; +import { request, stamp } from "../harness/net"; +import { signupToken } from "../harness/processes"; +import { forgetTheLogin } from "../harness/shared"; +import { pushedRequest } from "../harness/signin"; + +const COMPLETE_SIGNUP = "/xrpc/org.tangled.temp.account.completeSignup"; +const pdsOrigin = new URL(config.pdsUrl).origin; + +interface Signup { + email: string; + username: string; + made?: Account; + pdsPages: string[]; +} + +const signups = new WeakMap(); +const signupOf = (ctx: Ctx) => { + if (!signups.has(ctx)) { + const username = `signup-${stamp()}`; + signups.set(ctx, { email: `${username}@pds.tngl.boltless.dev`, username, pdsPages: [] }); + } + return signups.get(ctx)!; +}; +const madeBy = (ctx: Ctx) => { + const made = signupOf(ctx).made; + if (!made) throw new Error("the signup made no account yet"); + return made; +}; + +const signupPage = where("the sign up page", () => "/signup"); +const emailedLink = where( + "the link from the signup email", + (ctx) => `/signup/verify?token=${signupToken(signupOf(ctx).email)}` +); +const welcome = where("the welcome page", () => "/welcome"); + +const beginSignup = (name: string) => + flow(name) + .actor("guest", { login: "ui" }) + .open(page.profile) + .step("signs out", forgetTheLogin) + .open(signupPage) + .fill(testid("email"), (ctx) => signupOf(ctx).email) + .click(button("Sign up")) + .sees(testid("checkEmailTitle")) + .open(emailedLink) + .step("notes every page it opens on the pds", async (ctx) => { + ctx.page.on("request", (request) => { + if (request.isNavigationRequest() && new URL(request.url()).origin === pdsOrigin) + signupOf(ctx).pdsPages.push(request.url()); + }); + }) + .fill(field("Username"), (ctx) => signupOf(ctx).username) + .fill(field("Password"), config.password); + +const createAccount = async (ctx: Ctx) => { + const signup = signupOf(ctx); + const [response] = await Promise.all([ + ctx.page.waitForResponse((reply) => new URL(reply.url()).pathname === COMPLETE_SIGNUP), + button("Create account").locate(ctx.page, ctx).click() + ]); + expect(response.status()).toBe(200); + const { did, handle } = await response.json(); + signup.made = { name: signup.username, handle, did }; + ctx.owe(`account ${handle}`, { kind: "account", account: signup.made }); +}; + +beginSignup("signing up signs the new account in to tangled without the pds pages") + .step("creates the account", createAccount) + .at(welcome, 20_000) + .step("the browser holds a session for the new account", async (ctx) => { + const { did } = madeBy(ctx); + const stored = await ctx.page.evaluate(() => ({ + active: localStorage.getItem("tangled.currentDid"), + sessions: Object.keys(JSON.parse(localStorage.getItem("atcute-oauth:sessions") ?? "{}")) + })); + expect(stored).toEqual({ active: did, sessions: [did] }); + }) + .step("it never opened a pds page", async (ctx) => { + expect(signupOf(ctx).pdsPages).toEqual([]); + }); + +beginSignup("a signup the pds can't sign in goes to the pds sign in with the handle filled") + .step("deliberi gets no oauth request, like on a pds without the route", async (ctx) => { + await ctx.page.route(`**${COMPLETE_SIGNUP}`, async (route) => { + const { requestUri: _, ...input } = route.request().postDataJSON(); + await route.continue({ postData: JSON.stringify(input) }); + }); + }) + .step("creates the account", createAccount) + .step("lands on the pds sign in with the handle filled", async (ctx) => { + const { handle } = madeBy(ctx); + await ctx.page.waitForURL( + (url) => url.origin === pdsOrigin && url.pathname === "/oauth/authorize" + ); + await expect + .poll(() => + ctx.page + .locator("input") + .evaluateAll( + (inputs, value) => + inputs.some((input) => (input as HTMLInputElement).value === value), + handle + ) + ) + .toBe(true); + }); + +flow("the pds refuses to sign in an account that isn't new or a client it doesn't list") + .actor("fresh") + .step("an account that signed in before is refused", async () => { + const { status, body } = await preauthorize(seeded.alice.did, await pushedRequest()); + expect(status).toBe(403); + // on a stack up for under 5 minutes alice is still new, but setup already signed her in + expect(["account is not new", "account already authorized a client"]).toContain( + body.message + ); + }) + .step("a client outside the list is refused", async (ctx) => { + const loopback = { + client_id: "http://localhost", + redirect_uris: ["http://127.0.0.1/"], + scope: "atproto" + }; + const { status, body } = await preauthorize(ctx.actor.did, await pushedRequest(loopback)); + expect(status).toBe(403); + expect(body.message).toBe("client is not allowed"); + }) + .step("without the admin password the pds says nothing else", async (ctx) => { + const { status } = await request(`${config.pdsUrl}/_preauthorize`, { + method: "POST", + json: { did: ctx.actor.did, requestUri: await pushedRequest() } + }); + expect(status).toBe(401); + }); -- knotmirror 0.0.1