Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/auth/account-endpoints.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
31 changes: 31 additions & 0 deletions src/auth/account-endpoints.ts
Original file line number Diff line number Diff line change
@@ -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",
];
22 changes: 22 additions & 0 deletions src/auth/identity.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
? {
Expand Down
Loading