Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend): turn off the Better Auth endpoints nothing calls (#15967)

Nicholas Tindleb0736bece427
diff --git a/autogpt_platform/frontend/src/lib/auth/__tests__/disabled-paths.test.ts b/autogpt_platform/frontend/src/lib/auth/__tests__/disabled-paths.test.ts
new file mode 100644
index 0000000000..b559616202
--- /dev/null
+++ b/autogpt_platform/frontend/src/lib/auth/__tests__/disabled-paths.test.ts
@@ -0,0 +1,121 @@
+// @vitest-environment node
+import { beforeAll, describe, expect, it, vi } from "vitest";
+import { DISABLED_AUTH_PATHS } from "../disabled-paths";
+
+vi.unmock("@/lib/auth/auth");
+
+vi.mock("pg", () => ({
+ Pool: class {
+ query(): never {
+ throw new Error("no database in this test");
+ }
+ connect(): never {
+ throw new Error("no database in this test");
+ }
+ on() {}
+ end() {}
+ },
+}));
+
+type Auth = typeof import("../auth").auth;
+let auth: Auth;
+
+beforeAll(async () => {
+ vi.stubEnv("BETTER_AUTH_SECRET", "test-secret-".padEnd(48, "x"));
+ vi.stubEnv("BETTER_AUTH_URL", "http://localhost:3000");
+ ({ auth } = await import("../auth"));
+}, 120_000);
+
+// A query string doesn't change the path the router matches.
+const SPELLINGS = [(path: string) => path, (path: string) => `${path}?x=1`];
+
+// Every endpoint the platform serves on purpose. A Better Auth upgrade or a
+// new plugin that mounts another one fails the test below, so each new
+// endpoint is a decision: add it here if something calls it, otherwise to
+// DISABLED_AUTH_PATHS.
+const SERVED_AUTH_PATHS = [
+ "/admin/ban-user",
+ "/admin/create-user",
+ "/admin/get-user",
+ "/admin/list-users",
+ "/admin/unban-user",
+ "/callback/:id",
+ "/change-email",
+ "/error",
+ "/get-session",
+ "/jwks",
+ "/ok",
+ "/request-password-reset",
+ "/reset-password",
+ "/reset-password/:token",
+ "/send-verification-email",
+ "/sign-in/email",
+ "/sign-in/social",
+ "/sign-out",
+ "/sign-up/email",
+ "/supabase-bridge",
+ "/token",
+ "/update-user",
+ "/verify-email",
+];
+
+function mountedEndpoints() {
+ return Object.values(auth.api).flatMap((endpoint) => {
+ const { path, options } = endpoint as unknown as {
+ path?: string;
+ options: { method: string | string[] };
+ };
+ // Server-only endpoints have no path: they can't be requested at all.
+ if (!path) return [];
+ const methods = [options.method].flat();
+ return [{ path, methods: methods.includes("*") ? ["GET"] : methods }];
+ });
+}
+
+async function statusOf(path: string, method: string) {
+ const response = await auth.handler(
+ new Request(`http://localhost:3000/api/auth${path}`, {
+ method,
+ headers: {
+ "content-type": "application/json",
+ origin: "http://localhost:3000",
+ },
+ body: method === "GET" ? undefined : "{}",
+ }),
+ );
+ return response.status;
+}
+
+describe("Better Auth endpoints", () => {
+ it("are each either served on purpose or disabled", () => {
+ const mounted = [...new Set(mountedEndpoints().map(({ path }) => path))];
+ const served = mounted.filter(
+ (path) => !DISABLED_AUTH_PATHS.includes(path),
+ );
+ expect(served.sort()).toEqual([...SERVED_AUTH_PATHS].sort());
+ // And every disabled path is a real endpoint, so none of them is a typo.
+ expect(
+ DISABLED_AUTH_PATHS.filter((path) => !mounted.includes(path)),
+ ).toEqual([]);
+ });
+
+ it.each(DISABLED_AUTH_PATHS)("%s is not served", async (path) => {
+ const endpoint = mountedEndpoints().find((e) => e.path === path);
+ expect(endpoint, path).toBeDefined();
+ for (const spell of SPELLINGS) {
+ for (const method of endpoint?.methods ?? []) {
+ const status = await statusOf(spell(path), method);
+ expect(status, `${method} ${spell(path)}`).toBe(404);
+ }
+ }
+ });
+
+ it.each(SERVED_AUTH_PATHS)("%s is still served", async (path) => {
+ const endpoint = mountedEndpoints().find((e) => e.path === path);
+ expect(endpoint, path).toBeDefined();
+ for (const method of endpoint?.methods ?? []) {
+ const status = await statusOf(path.replace(/:\w+/g, "x"), method);
+ expect(status, `${method} ${path}`).not.toBe(404);
+ }
+ });
+});
diff --git a/autogpt_platform/frontend/src/lib/auth/auth.ts b/autogpt_platform/frontend/src/lib/auth/auth.ts
index 374f23c8a9..84094152c5 100644
--- a/autogpt_platform/frontend/src/lib/auth/auth.ts
+++ b/autogpt_platform/frontend/src/lib/auth/auth.ts
@@ -11,6 +11,7 @@ import { compare, hash } from "bcryptjs";
import { Pool } from "pg";
import { assertSignInAllowed, assertSignUpAllowed } from "./access-blocks";
import { getTrustedClientIP } from "./client-ip";
+import { DISABLED_AUTH_PATHS } from "./disabled-paths";
import { assertNotDisposableEmail } from "./disposable-email";
import { runAfterResponse } from "./background-tasks";
import { mirrorVerifiedEmailToPlatformUser } from "./email-mirror";
@@ -125,6 +126,7 @@ export const auth = betterAuth({
secret: authSecret,
database: authDbPool,
telemetry: { enabled: false },
+ disabledPaths: DISABLED_AUTH_PATHS,
hooks: {
before: createAuthMiddleware(async (ctx) => {
// Before Better Auth looks the address up, so an address that already
diff --git a/autogpt_platform/frontend/src/lib/auth/disabled-paths.ts b/autogpt_platform/frontend/src/lib/auth/disabled-paths.ts
new file mode 100644
index 0000000000..f037369410
--- /dev/null
+++ b/autogpt_platform/frontend/src/lib/auth/disabled-paths.ts
@@ -0,0 +1,37 @@
+// Better Auth endpoints nothing in the platform calls. Mounted, each would
+// still answer any signed-in browser (the /admin ones any admin), so they are
+// switched off. Our own server code calls auth.api directly, which this list
+// doesn't affect.
+export const DISABLED_AUTH_PATHS = [
+ // The admin tools use get-user, list-users, ban-user and unban-user, and the
+ // load tests' accounts come from create-user. Impersonation is our own
+ // header and roles are set in the database. Deleting an account needs the
+ // platform's own clean-up, which deleting only the sign-in record skips.
+ "/admin/has-permission",
+ "/admin/impersonate-user",
+ "/admin/list-user-sessions",
+ "/admin/remove-user",
+ "/admin/revoke-user-session",
+ "/admin/revoke-user-sessions",
+ "/admin/set-role",
+ "/admin/set-user-password",
+ "/admin/stop-impersonating",
+ "/admin/update-user",
+ // No account linking, no session list or revocation, no provider tokens in
+ // the browser, and passwords change through the reset email.
+ "/account-info",
+ "/change-password",
+ "/delete-user",
+ "/delete-user/callback",
+ "/get-access-token",
+ "/link-social",
+ "/list-accounts",
+ "/list-sessions",
+ "/refresh-token",
+ "/revoke-other-sessions",
+ "/revoke-session",
+ "/revoke-sessions",
+ "/unlink-account",
+ "/update-session",
+ "/verify-password",
+];