Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🚑️ Disable captcha when the Turnstile pair is incomplete (#2705)

Luke Vellad25d051a0a2a
diff --git a/apps/web/src/app/[locale]/(auth)/login/components/login-email-form.tsx b/apps/web/src/app/[locale]/(auth)/login/components/login-email-form.tsx
index 5540f9d0e..c0702c04b 100644
--- a/apps/web/src/app/[locale]/(auth)/login/components/login-email-form.tsx
+++ b/apps/web/src/app/[locale]/(auth)/login/components/login-email-form.tsx
@@ -21,11 +21,11 @@ import * as z from "zod";
import { setVerificationEmail } from "@/app/[locale]/(auth)/login/actions";
import { Trans, useTranslation } from "@/i18n/client";
import { authClient } from "@/lib/auth-client";
+import { useFeatureFlag } from "@/lib/feature-flags/client";
import { validateRedirectUrl } from "@/lib/utils/redirect";
import { trpc } from "@/trpc/client";
const turnstileSiteKey = process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY;
-const isTurnstileEnabled = !!turnstileSiteKey;
function useLoginWithEmailSchema() {
const { t } = useTranslation();
@@ -42,6 +42,8 @@ export function LoginWithEmailForm({
}: {
isRegistrationEnabled: boolean;
}) {
+ const isCaptchaEnabled = useFeatureFlag("captcha");
+ const isTurnstileEnabled = isCaptchaEnabled && !!turnstileSiteKey;
const router = useRouter();
const loginWithEmailSchema = useLoginWithEmailSchema();
const searchParams = useSearchParams();
@@ -266,7 +268,7 @@ export function LoginWithEmailForm({
{form.formState.errors.root?.message ? (
<FormMessage>{form.formState.errors.root.message}</FormMessage>
) : null}
- {showCaptcha && turnstileSiteKey ? (
+ {showCaptcha && isTurnstileEnabled && turnstileSiteKey ? (
<Turnstile
ref={turnstileRef}
siteKey={turnstileSiteKey}
diff --git a/apps/web/src/app/[locale]/e/[id]/components/rsvp-verify-email.tsx b/apps/web/src/app/[locale]/e/[id]/components/rsvp-verify-email.tsx
index 566d8c40a..13fbc27e4 100644
--- a/apps/web/src/app/[locale]/e/[id]/components/rsvp-verify-email.tsx
+++ b/apps/web/src/app/[locale]/e/[id]/components/rsvp-verify-email.tsx
@@ -26,9 +26,9 @@ import * as z from "zod";
import { InputOTP } from "@/components/input-otp";
import { Trans, useTranslation } from "@/i18n/client";
import { authClient } from "@/lib/auth-client";
+import { useFeatureFlag } from "@/lib/feature-flags/client";
const turnstileSiteKey = process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY;
-const isTurnstileEnabled = !!turnstileSiteKey;
const otpSchema = z.object({ otp: z.string().regex(/^\d{6}$/) });
@@ -39,6 +39,8 @@ export function RsvpVerifyEmail({
email: string;
name: string;
}) {
+ const isCaptchaEnabled = useFeatureFlag("captcha");
+ const isTurnstileEnabled = isCaptchaEnabled && !!turnstileSiteKey;
const { t, i18n } = useTranslation();
const router = useRouter();
const dialog = useDialog();
@@ -169,7 +171,7 @@ export function RsvpVerifyEmail({
/>
</DialogDescription>
</DialogHeader>
- {turnstileSiteKey && !otpSent ? (
+ {isTurnstileEnabled && turnstileSiteKey && !otpSent ? (
<Turnstile
ref={turnstileRef}
siteKey={turnstileSiteKey}
diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.ts
index 14f3d97d3..6592057ae 100644
--- a/apps/web/src/lib/auth.ts
+++ b/apps/web/src/lib/auth.ts
@@ -57,18 +57,20 @@ if (env.OIDC_ISSUER_URL) {
}
// The site key renders the widget and the secret key verifies its tokens;
-// with only one of them set, captcha is either silently skipped or blocks
-// every OTP send. Fail at boot instead.
+// captcha only works with both, so a half-configured pair disables it.
+const isCaptchaEnabled =
+ !!env.TURNSTILE_SECRET_KEY && !!env.NEXT_PUBLIC_TURNSTILE_SITE_KEY;
+
if (!!env.TURNSTILE_SECRET_KEY !== !!env.NEXT_PUBLIC_TURNSTILE_SITE_KEY) {
- throw new Error(
- "NEXT_PUBLIC_TURNSTILE_SITE_KEY and TURNSTILE_SECRET_KEY must be set together",
+ logger.warn(
+ "Captcha is disabled: NEXT_PUBLIC_TURNSTILE_SITE_KEY and TURNSTILE_SECRET_KEY must be set together",
);
}
// Conditional plugins are typed as BetterAuthPlugin[] — they don't add user
// fields so losing their specific types doesn't affect session type inference.
const conditionalPlugins: BetterAuthPlugin[] = [
- ...(env.TURNSTILE_SECRET_KEY
+ ...(isCaptchaEnabled && env.TURNSTILE_SECRET_KEY
? [
captcha({
provider: "cloudflare-turnstile",
diff --git a/apps/web/src/lib/feature-flags/config.ts b/apps/web/src/lib/feature-flags/config.ts
index a4f9fd3fb..eba099908 100644
--- a/apps/web/src/lib/feature-flags/config.ts
+++ b/apps/web/src/lib/feature-flags/config.ts
@@ -15,6 +15,10 @@ export const featureFlagConfig: FeatureFlagConfig = {
billing: isBillingEnabled,
feedback: isFeedbackEnabled,
emailLogin: isEmailLoginEnabled,
+ // Both halves of the Turnstile pair are needed: the site key renders the
+ // widget and the secret verifies its tokens. With only one set, captcha
+ // is disabled rather than half working.
+ captcha: !!env.TURNSTILE_SECRET_KEY && !!env.NEXT_PUBLIC_TURNSTILE_SITE_KEY,
registration: isEmailLoginEnabled && isRegistrationEnabled,
calendars: isCalendarsEnabled,
eventTypes: isEventTypesEnabled,
diff --git a/apps/web/src/lib/feature-flags/types.ts b/apps/web/src/lib/feature-flags/types.ts
index d939c7a19..9f4defa2c 100644
--- a/apps/web/src/lib/feature-flags/types.ts
+++ b/apps/web/src/lib/feature-flags/types.ts
@@ -3,6 +3,7 @@ export interface FeatureFlagConfig {
billing: boolean;
feedback: boolean;
emailLogin: boolean;
+ captcha: boolean;
registration: boolean;
calendars: boolean;
eventTypes: boolean;