From 9f272be2fb55391bc34cde9d2db41d065bedeb41 Mon Sep 17 00:00:00 2001 From: Jerome Leclanche Date: Wed, 25 Nov 2026 01:34:57 +0100 Subject: [PATCH] nk-auth: FORM_GATED_AUTH_PATHS for disabledPaths The raw Better Auth endpoints behind nk-forms (sign-up, password reset request, verification resend) skip the bot gate and per-address limits. Sites pass these as disabledPaths: HTTP answers 404, auth.api still runs. The README sends the set-password link server-side. Co-Authored-By: Claude Opus 5.5 --- .changeset/nk-auth-form-gated-paths.md | 5 ++ packages/nk-auth/README.md | 21 +++++++-- packages/nk-auth/src/form-gated.test.ts | 61 +++++++++++++++++++++++++ packages/nk-auth/src/index.ts | 1 + packages/nk-auth/src/options.ts | 15 ++++++ 5 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 .changeset/nk-auth-form-gated-paths.md create mode 100644 packages/nk-auth/src/form-gated.test.ts diff --git a/.changeset/nk-auth-form-gated-paths.md b/.changeset/nk-auth-form-gated-paths.md new file mode 100644 index 0000000..ed414ea --- /dev/null +++ b/.changeset/nk-auth-form-gated-paths.md @@ -0,0 +1,5 @@ +--- +"@ingram-tech/nk-auth": minor +--- + +`FORM_GATED_AUTH_PATHS`: the Better Auth endpoints a site serves through nk-forms (sign-up, password reset request, verification resend). Pass them as `disabledPaths` so the raw HTTP routes cannot bypass the bot gate and per-address limits; server-side `auth.api.*` calls keep working. The README now sends the set-password link server-side to the session's own address. diff --git a/packages/nk-auth/README.md b/packages/nk-auth/README.md index 0931982..6fa669a 100644 --- a/packages/nk-auth/README.md +++ b/packages/nk-auth/README.md @@ -781,16 +781,27 @@ export default async function Security() { **Set a password without a current one.** A social-only user has no current password to verify, so the re-auth is an emailed reset link — clicking it proves -account ownership. Trigger it with the standard client call; there is no separate -"set password" endpoint: +account ownership. Send it server-side, from a session-gated route or server +action, to the session's own address; there is no separate "set password" +endpoint: ```ts -await authClient.requestPasswordReset({ - email, - redirectTo: "/reset-password", // your token-consumer page (below) +"use server"; +const user = await requireUser(); +await auth.api.requestPasswordReset({ + body: { email: user.email, redirectTo: "/reset-password" }, // your token-consumer page (below) }); ``` +**Close the raw endpoints the forms wrap.** A site that serves sign-up and +forgot-password through `@ingram-tech/nk-forms` passes +`disabledPaths: [...FORM_GATED_AUTH_PATHS]` to `betterAuth()`. Otherwise +`POST /auth/sign-up/email`, `/auth/request-password-reset` and +`/auth/send-verification-email` skip the bot gate and the per-address limits, +and anyone can loop them at a victim's inbox. The forms keep working: +`disabledPaths` gates HTTP only, not `auth.api.*`. That is also why the +set-password link above is sent server-side, not through `authClient`. + Better Auth's reset endpoint **creates** the credential when the user has none, so the same flow both resets a forgotten password and sets a first one. That guarantee is pinned by `reset-password.test.ts` against a real instance, so a diff --git a/packages/nk-auth/src/form-gated.test.ts b/packages/nk-auth/src/form-gated.test.ts new file mode 100644 index 0000000..425955c --- /dev/null +++ b/packages/nk-auth/src/form-gated.test.ts @@ -0,0 +1,61 @@ +import { betterAuth } from "better-auth"; +import { memoryAdapter } from "better-auth/adapters/memory"; +import { describe, expect, it } from "vitest"; +import { FORM_GATED_AUTH_PATHS } from "./options.js"; + +// Pins the Better Auth behaviour FORM_GATED_AUTH_PATHS relies on: disabled +// paths refuse HTTP while the server-side API still runs. +describe("FORM_GATED_AUTH_PATHS", () => { + const sent: string[] = []; + const store: Record[]> = { + user: [ + { + id: "u1", + name: "User", + email: "user@example.com", + emailVerified: true, + createdAt: new Date(), + updatedAt: new Date(), + }, + ], + account: [], + session: [], + verification: [], + }; + const auth = betterAuth({ + baseURL: "http://localhost:3000", + secret: "test-secret-at-least-32-chars-long-000", + database: memoryAdapter(store), + disabledPaths: [...FORM_GATED_AUTH_PATHS], + emailAndPassword: { + enabled: true, + sendResetPassword: async ({ user }) => { + sent.push(user.email); + }, + }, + emailVerification: { sendVerificationEmail: async () => {} }, + }); + + it.each(FORM_GATED_AUTH_PATHS)("answers 404 over HTTP on %s", async (path) => { + const response = await auth.handler( + new Request(`http://localhost:3000/api/auth${path}`, { + method: "POST", + headers: { + "content-type": "application/json", + origin: "http://localhost:3000", + }, + body: JSON.stringify({ + email: "user@example.com", + password: "x".repeat(12), + name: "X", + }), + }), + ); + expect(response.status).toBe(404); + }); + + it("still serves the server-side API", async () => { + await auth.api.requestPasswordReset({ body: { email: "user@example.com" } }); + expect(sent).toEqual(["user@example.com"]); + }); +}); diff --git a/packages/nk-auth/src/index.ts b/packages/nk-auth/src/index.ts index ccaa776..3425450 100644 --- a/packages/nk-auth/src/index.ts +++ b/packages/nk-auth/src/index.ts @@ -19,6 +19,7 @@ export { type AuthEmailMessage, type AuthEmailUser, bcryptPassword, + FORM_GATED_AUTH_PATHS, makeEmailSenders, makePasskeyOptions, type PasskeyConfig, diff --git a/packages/nk-auth/src/options.ts b/packages/nk-auth/src/options.ts index 9563605..3bcd37f 100644 --- a/packages/nk-auth/src/options.ts +++ b/packages/nk-auth/src/options.ts @@ -17,6 +17,21 @@ export { uuidGenerateId } from "./id.js"; * presets live in `./jwt` and `./organization`. See the package README. */ +/** + * Better Auth endpoints a site serves through `@ingram-tech/nk-forms` (bot gate, + * per-client and per-address limits). Pass them as `disabledPaths` so the raw + * HTTP routes answer 404 and cannot bypass the form. The forms' server-side + * `auth.api.*` calls keep working: Better Auth checks `disabledPaths` in its + * HTTP router only. A signed-in "send me a set-password link" then goes through + * a session-gated route that calls `auth.api.requestPasswordReset` for the + * session's own email. + */ +export const FORM_GATED_AUTH_PATHS: readonly string[] = [ + "/sign-up/email", + "/request-password-reset", + "/send-verification-email", +]; + /** * `emailAndPassword.password` config that hashes/verifies with bcrypt. *