diff --git a/src/auth/account-endpoints.test.ts b/src/auth/account-endpoints.test.ts new file mode 100644 index 0000000..c71a55f --- /dev/null +++ b/src/auth/account-endpoints.test.ts @@ -0,0 +1,66 @@ +import { betterAuth } from "better-auth"; +import { memoryAdapter } from "better-auth/adapters/memory"; +import { describe, expect, it } from "vite-plus/test"; +import { unusedAccountPaths } from "./account-endpoints"; + +const baseURL = "https://auth.polinetwork.test"; +const getPaths = new Set(["/account-info", "/delete-user/callback", "/verify-email"]); + +async function signedIn(disabledPaths: string[]) { + const auth = betterAuth({ + baseURL, + secret: "account-endpoints-test-secret-at-least-32-characters", + database: memoryAdapter({ user: [], session: [], account: [], verification: [] }), + emailAndPassword: { enabled: true }, + disabledPaths, + }); + // A server-side call to a closed path still works. + const signUp = await auth.api.signUpEmail({ + body: { email: "member@example.test", name: "Member", password: "test-password-at-least-16" }, + asResponse: true, + }); + expect(signUp.status).toBe(200); + const headers = new Headers({ + cookie: signUp.headers + .getSetCookie() + .map((value) => value.split(";")[0]) + .join("; "), + origin: baseURL, + "content-type": "application/json", + }); + const request = (path: string, body?: unknown) => + auth.handler( + new Request(`${baseURL}/api/auth${path}`, { + method: body === undefined ? "GET" : "POST", + headers, + body: body === undefined ? undefined : JSON.stringify(body), + }), + ); + return { auth, headers, request }; +} + +describe("unused account endpoints", () => { + it("are refused over HTTP", async () => { + const { request } = await signedIn(unusedAccountPaths); + for (const path of unusedAccountPaths) { + const response = await request(path, getPaths.has(path) ? undefined : {}); + expect(response.status, path).toBe(404); + } + expect((await request("/get-session")).status).toBe(200); + }); + + it("do not let a signed-in person rename themselves", async () => { + const open = await signedIn([]); + expect((await open.request("/update-user", { name: "Someone Else" })).status).toBe(200); + + const { auth, headers, request } = await signedIn(unusedAccountPaths); + const response = await request("/update-user", { + name: "Someone Else", + image: "https://example.test/x.png", + }); + expect(response.status).toBe(404); + const session = await auth.api.getSession({ headers }); + expect(session?.user.name).toBe("Member"); + expect(session?.user.image ?? null).toBeNull(); + }); +}); diff --git a/src/auth/account-endpoints.ts b/src/auth/account-endpoints.ts new file mode 100644 index 0000000..f15a919 --- /dev/null +++ b/src/auth/account-endpoints.ts @@ -0,0 +1,31 @@ +/** + * Better Auth's own account endpoints that no page here calls. These paths are closed over HTTP; + * server-side `auth.api` calls are not affected. + * + * - `/update-user`: anyone signed in could set their own name and picture to anything, and that + * name is what applications, administrators and the e-signing picker see. + * - `/update-session`: nothing here lets a session be edited. + * - `/get-access-token`, `/refresh-token`, `/account-info`: they hand the Google, Entra or + * Telegram tokens and profile behind a linked account to the browser. + * - `/delete-user`, `/delete-user/callback`: people are deleted from the user pages, which + * check permissions. Better Auth's own deletion is off. + * - The rest belong to email and password sign-in and email changes, which are off here. + */ +export const unusedAccountPaths = [ + "/update-user", + "/update-session", + "/get-access-token", + "/refresh-token", + "/account-info", + "/delete-user", + "/delete-user/callback", + "/change-email", + "/change-password", + "/sign-up/email", + "/sign-in/email", + "/request-password-reset", + "/reset-password", + "/send-verification-email", + "/verify-email", + "/verify-password", +]; diff --git a/src/auth/identity.integration.test.ts b/src/auth/identity.integration.test.ts index db304b1..4a91e94 100644 --- a/src/auth/identity.integration.test.ts +++ b/src/auth/identity.integration.test.ts @@ -180,6 +180,28 @@ describe.skipIf(!baseURL || !databaseURL || !secret)("identity HTTP integration" expect(response.status).toBe(401); } }); + it("refuses the account endpoints no page uses", async () => { + const { unusedAccountPaths } = await import("./account-endpoints"); + for (const path of unusedAccountPaths) { + const get = ["/account-info", "/delete-user/callback", "/verify-email"].includes(path); + const response = await fetch(`${baseURL}/api/auth${path}`, { + method: get ? "GET" : "POST", + headers, + body: get ? undefined : "{}", + }); + expect(response.status, path).toBe(404); + } + const rename = await fetch(`${baseURL}/api/auth/update-user`, { + method: "POST", + headers, + body: JSON.stringify({ name: "Someone Else", image: "https://example.invalid/x.png" }), + }); + expect(rename.status).toBe(404); + const { rows } = await pool.query(`SELECT name, image FROM "user" WHERE id = $1`, [ + "integration-user", + ]); + expect(rows[0]).toEqual({ name: "Test", image: null }); + }); it("offers discoverable passkey authentication without an email", async () => { const response = await fetch(`${baseURL}/api/auth/passkey/generate-authenticate-options`); expect(response.status).toBe(200); diff --git a/src/auth/index.ts b/src/auth/index.ts index 9cdd88e..9f52047 100644 --- a/src/auth/index.ts +++ b/src/auth/index.ts @@ -31,6 +31,7 @@ import { finishMcpRegistration, mcpAuthorizationRequest, mcpResource } from "./m import { hasIdpPermission } from "./idp-access"; import { canAdministerIdp } from "./oidc-admin"; import { validateClientJwks } from "./client-jwks"; +import { unusedAccountPaths } from "./account-endpoints"; import { rawClientMutationPaths, refusedClientMutationPaths } from "./client-endpoints"; import { apiScopes, @@ -106,7 +107,7 @@ export const auth = betterAuth({ secret: env.BETTER_AUTH_SECRET, database: drizzleAdapter(db, { provider: "pg", schema }), trustedOrigins: [new URL(env.BETTER_AUTH_URL).origin], - disabledPaths: ["/token", "/unlink-account", ...rawClientMutationPaths], + disabledPaths: ["/token", "/unlink-account", ...unusedAccountPaths, ...rawClientMutationPaths], socialProviders: env.GOOGLE_CLIENT_ID && env.GOOGLE_CLIENT_SECRET ? {