⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(auth): protect invitation acceptance from unverified accounts (#1386)
Tin Severa6866143f1cc
diff --git a/apps/api/src/auth.ts b/apps/api/src/auth.tsindex bdb430fb1..8b39077ee 100644--- a/apps/api/src/auth.ts+++ b/apps/api/src/auth.ts@@ -56,6 +56,13 @@ const isLoginFormDisabled = process.env.DISABLE_LOGIN_FORM === "true";const isEmailOtpSignInDisabled =process.env.DISABLE_EMAIL_OTP_SIGN_IN === "true";+function normalizeInvitationId(value: unknown): string | undefined {+ if (typeof value !== "string") return undefined;+ const normalized = value.trim();+ if (!/^[a-z0-9_-]{1,128}$/i.test(normalized)) return undefined;+ return normalized;+}+const apiUrl = process.env.KANEO_API_URL || "http://localhost:1337";const clientUrl = process.env.KANEO_CLIENT_URL || "http://localhost:5173";const isHttps = apiUrl.startsWith("https://");@@ -481,7 +488,7 @@ export const auth = betterAuth({databaseHooks: {user: {create: {- before: async (user) => {+ before: async (user, ctx) => {// Allow the very first signup through even when registration// is disabled — that's the instance-admin bootstrap flow.// Otherwise a fresh instance with DISABLE_REGISTRATION=true@@ -494,7 +501,15 @@ export const auth = betterAuth({return;}- const result = await checkRegistrationAllowed(user.email);+ const invitationId = normalizeInvitationId(+ ctx?.body?.invitationId ||+ ctx?.query?.invitationId ||+ ctx?.headers?.get("x-invitation-id"),+ );+ const result = await checkRegistrationAllowed(+ user.email,+ invitationId,+ );if (!result.allowed) {throw new APIError("FORBIDDEN", {message: result.reason,@@ -646,10 +661,11 @@ export const auth = betterAuth({ctx.body?.email ||ctx.query?.email ||ctx.headers?.get("x-invitation-email");- const invitationId =+ const invitationId = normalizeInvitationId(ctx.body?.invitationId ||- ctx.query?.invitationId ||- ctx.headers?.get("x-invitation-id");+ ctx.query?.invitationId ||+ ctx.headers?.get("x-invitation-id"),+ );if (ctx.path === "/sign-up/email") {const result = await checkRegistrationAllowed(email, invitationId);diff --git a/apps/api/src/invitation/index.ts b/apps/api/src/invitation/index.tsindex bb9164e09..ce2d234d4 100644--- a/apps/api/src/invitation/index.ts+++ b/apps/api/src/invitation/index.ts@@ -1,3 +1,4 @@+import type { User } from "better-auth/types";import { Hono } from "hono";import { describeRoute, resolver, validator } from "hono-openapi";import * as v from "valibot";@@ -6,6 +7,7 @@ import getUserPendingInvitations from "./controllers/get-user-pending-invitationconst invitation = new Hono<{Variables: {+ user: User | null;userId: string;userEmail: string;};@@ -41,6 +43,11 @@ const invitation = new Hono<{},}),async (c) => {+ const user = c.get("user");+ if (!user?.emailVerified) {+ return c.json([]);+ }+const userEmail = c.get("userEmail");const invitations = await getUserPendingInvitations(userEmail);return c.json(invitations);diff --git a/apps/api/src/utils/check-registration-allowed.ts b/apps/api/src/utils/check-registration-allowed.tsindex 64a7d10ef..57839e5ad 100644--- a/apps/api/src/utils/check-registration-allowed.ts+++ b/apps/api/src/utils/check-registration-allowed.ts@@ -29,11 +29,11 @@ export async function checkRegistrationAllowed(};}- if (!invitationId && !email) {+ if (!invitationId) {return {allowed: false,reason:- "Registration is currently disabled. Please contact your administrator for an invitation.",+ "Registration is currently disabled. Please use a valid invitation link to create an account.",};}@@ -73,7 +73,7 @@ async function findValidInvitation(conditions.push(eq(invitationTable.email, email.toLowerCase()));}- if (!invitationId && !email) {+ if (!invitationId) {return null;}diff --git a/apps/web/src/components/auth/sign-up-form.tsx b/apps/web/src/components/auth/sign-up-form.tsxindex 3d2dffc8e..0a9199074 100644--- a/apps/web/src/components/auth/sign-up-form.tsx+++ b/apps/web/src/components/auth/sign-up-form.tsx@@ -71,15 +71,21 @@ export function SignUpForm({const onSubmit = async (data: SignUpFormValues) => {setIsPending(true);try {+ const headers: Record<string, string> = {};+ if (turnstileToken) {+ headers["x-turnstile-token"] = turnstileToken;+ }+ if (invitationId) {+ headers["x-invitation-id"] = invitationId;+ }+const result = await authClient.signUp.email({email: data.email,name: data.name,password: data.password,},- turnstileToken- ? { headers: { "x-turnstile-token": turnstileToken } }- : undefined,+ Object.keys(headers).length > 0 ? { headers } : undefined,);if (result.error) {