🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(security): optional captcha/turnstile on sign-in (#1519)
Marc Klingen1d0e36f2e825
diff --git a/.env.prod.example b/.env.prod.exampleindex d1c697f39e..57138276ba 100644--- a/.env.prod.example+++ b/.env.prod.example@@ -90,6 +90,10 @@ SALT="salt"# NEXT_SENTRY_PROJECT=# SENTRY_AUTH_TOKEN=+# Cloudflare Turnstile+# NEXT_PUBLIC_TURNSTILE_SITE_KEY=+# TURNSTILE_SECRET_KEY=+# Betterstack# LANGFUSE_TEAM_BETTERSTACK_TOKEN=diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex c14254a7c6..d49cb00b3d 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -179,6 +179,9 @@ importers:'@langfuse/shared':specifier: workspace:*version: link:../packages/shared+ '@marsidev/react-turnstile':+ specifier: ^0.5.3+ version: 0.5.3([email protected])([email protected])'@next-auth/prisma-adapter':specifier: ^1.0.7version: 1.0.7(@prisma/[email protected])([email protected])@@ -2801,6 +2804,16 @@ packages:call-bind: 1.0.7dev: true+ resolution: {integrity: sha512-lx3p2/56esPt8Ksr37K8uhPt/K4Mg8xaIfCV8MPKmE/1X4aHesRqZok1+L1ySQwsdWoEe5+KJOhBXka8lFBwNg==}+ peerDependencies:+ react: '>=16.8.0'+ react-dom: '>=16.8.0'+ dependencies:+ react: 18.2.0+ react-dom: 18.2.0([email protected])+ dev: false+/@mermaid-js/[email protected]([email protected]):resolution: {integrity: sha512-xZsGR88RsP72SAXyvjU23HzsKHsq6b7/kbnmQrEuIcPrJGPOckFiP9nzHvPh3Z04MjvpDJVjtbIspt2ZQwUzzA==}engines: {node: ^14.13 || >=16.0}@@ -8779,7 +8792,7 @@ packages:eslint: 8.57.0eslint-import-resolver-node: 0.3.9eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/[email protected])([email protected])([email protected])([email protected])- eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected])([email protected])+ eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected])([email protected])([email protected])eslint-plugin-jsx-a11y: 6.8.0([email protected])eslint-plugin-react: 7.34.1([email protected])eslint-plugin-react-hooks: 4.6.0([email protected])@@ -8852,7 +8865,7 @@ packages:enhanced-resolve: 5.16.0eslint: 8.57.0eslint-module-utils: 2.8.1(@typescript-eslint/[email protected])([email protected])([email protected])([email protected])- eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected])([email protected])+ eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected])([email protected])([email protected])fast-glob: 3.3.2get-tsconfig: 4.7.3is-core-module: 2.13.1@@ -8874,7 +8887,7 @@ packages:debug: 4.3.4([email protected])enhanced-resolve: 5.16.0eslint: 8.57.0- eslint-module-utils: 2.8.1(@typescript-eslint/[email protected])([email protected])([email protected])([email protected])+ eslint-module-utils: 2.8.1(@typescript-eslint/[email protected])([email protected])([email protected])eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected])([email protected])fast-glob: 3.3.2get-tsconfig: 4.7.3@@ -8912,6 +8925,35 @@ packages:debug: 3.2.7eslint: 8.57.0eslint-import-resolver-node: 0.3.9+ eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/[email protected])([email protected])([email protected])([email protected])+ transitivePeerDependencies:+ - supports-color+ dev: true++ resolution: {integrity: sha512-rXDXR3h7cs7dy9RNpUlQf80nX31XWJEyGq1tRMo+6GsO5VmTe4UTwtmonAD4ZkAsrfMVDA2wlGJ3790Ys+D49Q==}+ engines: {node: '>=4'}+ peerDependencies:+ '@typescript-eslint/parser': '*'+ eslint: '*'+ eslint-import-resolver-node: '*'+ eslint-import-resolver-typescript: '*'+ eslint-import-resolver-webpack: '*'+ peerDependenciesMeta:+ '@typescript-eslint/parser':+ optional: true+ eslint:+ optional: true+ eslint-import-resolver-node:+ optional: true+ eslint-import-resolver-typescript:+ optional: true+ eslint-import-resolver-webpack:+ optional: true+ dependencies:+ '@typescript-eslint/parser': 6.21.0([email protected])([email protected])+ debug: 3.2.7+ eslint: 8.57.0eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/[email protected])([email protected])([email protected])transitivePeerDependencies:- supports-color@@ -8969,7 +9011,7 @@ packages:ignore: 5.3.1dev: trueresolution: {integrity: sha512-BbPC0cuExzhiMo4Ff1BTVwHpjjv28C5R+btTOGaCRC7UEz801up0JadwkeSk5Ued6TG34uaczuVuH6qyy5YUxw==}engines: {node: '>=4'}peerDependencies:diff --git a/web/package.json b/web/package.jsonindex 933864367f..b16ebf806d 100644--- a/web/package.json+++ b/web/package.json@@ -33,6 +33,7 @@"@langchain/core": "^0.1.49","@langchain/openai": "^0.0.23","@langfuse/shared": "workspace:*",+ "@marsidev/react-turnstile": "^0.5.3","@next-auth/prisma-adapter": "^1.0.7","@radix-ui/react-accordion": "^1.1.2","@radix-ui/react-alert-dialog": "^1.0.5",diff --git a/web/src/env.mjs b/web/src/env.mjsindex fe243a5ee6..18406d243a 100644--- a/web/src/env.mjs+++ b/web/src/env.mjs@@ -66,6 +66,7 @@ export const env = createEnv({// Prompt playgroundOPENAI_API_KEY: z.string().optional(),ANTHROPIC_API_KEY: z.string().optional(),+ TURNSTILE_SECRET_KEY: z.string().optional(),},/**@@ -80,6 +81,7 @@ export const env = createEnv({.optional(),NEXT_PUBLIC_DEMO_PROJECT_ID: z.string().optional(),NEXT_PUBLIC_SIGN_UP_DISABLED: z.enum(["true", "false"]).optional(),+ NEXT_PUBLIC_TURNSTILE_SITE_KEY: z.string().optional(),},/**@@ -139,6 +141,8 @@ export const env = createEnv({// Prompt playgroundOPENAI_API_KEY: process.env.OPENAI_API_KEY,ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY,+ TURNSTILE_SECRET_KEY: process.env.TURNSTILE_SECRET_KEY,+ NEXT_PUBLIC_TURNSTILE_SITE_KEY: process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY,},// Skip validation in Docker builds// DOCKER_BUILD is set in Dockerfilediff --git a/web/src/features/auth/components/AuthCloudPrivacyNotice.tsx b/web/src/features/auth/components/AuthCloudPrivacyNotice.tsxindex 0e657d704f..19a926d839 100644--- a/web/src/features/auth/components/AuthCloudPrivacyNotice.tsx+++ b/web/src/features/auth/components/AuthCloudPrivacyNotice.tsx@@ -2,7 +2,7 @@ import { env } from "@/src/env.mjs";export const CloudPrivacyNotice = ({ action }: { action: string }) =>env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION !== undefined ? (- <div className="mt-10 text-center text-xs text-gray-500">+ <div className="mt-5 text-center text-xs text-gray-500">By {action} you are agreeing to our{" "}<ahref="https://langfuse.com/tos"diff --git a/web/src/pages/auth/sign-in.tsx b/web/src/pages/auth/sign-in.tsxindex f12c1d57d6..a4ab3eca19 100644--- a/web/src/pages/auth/sign-in.tsx+++ b/web/src/pages/auth/sign-in.tsx@@ -26,6 +26,7 @@ import { Divider } from "@tremor/react";import { CloudPrivacyNotice } from "@/src/features/auth/components/AuthCloudPrivacyNotice";import { CloudRegionSwitch } from "@/src/features/auth/components/AuthCloudRegionSwitch";import { PasswordInput } from "@/src/components/ui/password-input";+import { Turnstile } from "@marsidev/react-turnstile";const credentialAuthForm = z.object({email: z.string().email(),@@ -129,6 +130,7 @@ export default function SignIn({ authProviders }: PageProps) {>(null);const posthog = usePostHog();+ const [turnstileToken, setTurnstileToken] = useState<string>();// Credentialsconst credentialsForm = useForm<z.infer<typeof credentialAuthForm>>({@@ -148,6 +150,7 @@ export default function SignIn({ authProviders }: PageProps) {password: values.password,callbackUrl: "/",redirect: false,+ turnstileToken,});if (result?.error) {setCredentialsFormError(result.error);@@ -222,6 +225,17 @@ export default function SignIn({ authProviders }: PageProps) {) : null}<SSOButtons authProviders={authProviders} /></div>+ {env.NEXT_PUBLIC_TURNSTILE_SITE_KEY !== undefined && (+ <>+ <Divider className="text-gray-400" />+ <Turnstile+ siteKey={env.NEXT_PUBLIC_TURNSTILE_SITE_KEY}+ options={{ theme: "light", action: "sign-in" }}+ className="mx-auto"+ onSuccess={setTurnstileToken}+ />+ </>+ )}<CloudPrivacyNotice action="signing in" /></div>diff --git a/web/src/pages/auth/sign-up.tsx b/web/src/pages/auth/sign-up.tsxindex 88ab1e7f85..da294cfcb3 100644--- a/web/src/pages/auth/sign-up.tsx+++ b/web/src/pages/auth/sign-up.tsx@@ -149,6 +149,7 @@ export default function SignIn({ authProviders }: PageProps) {</form></Form><SSOButtons authProviders={authProviders} action="Sign up" />+ <div className="h-5" /><CloudPrivacyNotice action="creating an account" /></div>diff --git a/web/src/server/auth.ts b/web/src/server/auth.tsindex d67e9c78d4..069c8b8321 100644--- a/web/src/server/auth.ts+++ b/web/src/server/auth.ts@@ -31,6 +31,11 @@ const providers: Provider[] = [placeholder: "[email protected]",},password: { label: "Password", type: "password" },+ turnstileToken: {+ label: "Turnstile Token (Captcha)",+ type: "text",+ value: "dummy",+ },},async authorize(credentials, _req) {if (!credentials) throw new Error("No credentials");@@ -39,6 +44,23 @@ const providers: Provider[] = ["Sign in with email and password is disabled for this instance. Please use SSO.",);+ if (env.TURNSTILE_SECRET_KEY && env.NEXT_PUBLIC_TURNSTILE_SITE_KEY) {+ const res = await fetch(+ "https://challenges.cloudflare.com/turnstile/v0/siteverify",+ {+ method: "POST",+ body: `secret=${encodeURIComponent(env.TURNSTILE_SECRET_KEY)}&response=${encodeURIComponent(credentials.turnstileToken)}`,+ headers: {+ "content-type": "application/x-www-form-urlencoded",+ },+ },+ );+ const data = await res.json();+ if (data.success === false) {+ throw new Error("Invalid captcha token");+ }+ }+const blockedDomains =env.AUTH_DOMAINS_WITH_SSO_ENFORCEMENT?.split(",") ?? [];const domain = credentials.email.split("@")[1]?.toLowerCase();