From 89967b563b8b9c7338417e373756370294db75ea Mon Sep 17 00:00:00 2001 From: Gravirei Date: Thu, 2 Jul 2026 16:14:20 +0600 Subject: [PATCH 1/2] feat: require CAPTCHA before sending email verification code on registration --- common/constants.go | 1 + controller/misc.go | 1 + controller/option.go | 9 +++++++ middleware/turnstile-check.go | 9 ++++++- model/option.go | 3 +++ .../src/components/settings/SystemSetting.jsx | 11 +++++++++ web/classic/src/i18n/locales/en.json | 1 + web/classic/src/i18n/locales/fr.json | 1 + web/classic/src/i18n/locales/ja.json | 1 + web/classic/src/i18n/locales/ru.json | 1 + web/classic/src/i18n/locales/vi.json | 1 + web/classic/src/i18n/locales/zh-CN.json | 1 + web/classic/src/i18n/locales/zh-TW.json | 1 + web/classic/src/i18n/locales/zh.json | 1 + .../src/features/auth/hooks/use-turnstile.ts | 8 +++---- .../auth/sign-up/components/sign-up-form.tsx | 8 ++++++- .../auth/bot-protection-section.tsx | 24 +++++++++++++++++++ .../features/system-settings/auth/index.tsx | 1 + .../system-settings/auth/section-registry.tsx | 1 + .../src/features/system-settings/types.ts | 1 + web/default/src/i18n/locales/en.json | 2 ++ web/default/src/i18n/locales/fr.json | 2 ++ web/default/src/i18n/locales/ja.json | 2 ++ web/default/src/i18n/locales/ru.json | 2 ++ web/default/src/i18n/locales/vi.json | 2 ++ web/default/src/i18n/locales/zh.json | 2 ++ 26 files changed, 91 insertions(+), 6 deletions(-) diff --git a/common/constants.go b/common/constants.go index 5a15f7301a85..8caba2a805e0 100644 --- a/common/constants.go +++ b/common/constants.go @@ -89,6 +89,7 @@ var LinuxDOOAuthEnabled = false var WeChatAuthEnabled = false var TelegramOAuthEnabled = false var TurnstileCheckEnabled = false +var RegisterPageWithCaptchaEnabled = false var RegisterEnabled = true var EmailDomainRestrictionEnabled = false // 是否启用邮箱域名限制 diff --git a/controller/misc.go b/controller/misc.go index eada4909a090..5e5fe2864c51 100644 --- a/controller/misc.go +++ b/controller/misc.go @@ -69,6 +69,7 @@ func GetStatus(c *gin.Context) { "wechat_login": common.WeChatAuthEnabled, "server_address": system_setting.ServerAddress, "turnstile_check": common.TurnstileCheckEnabled, + "register_page_with_captcha": common.RegisterPageWithCaptchaEnabled, "turnstile_site_key": common.TurnstileSiteKey, "docs_link": operation_setting.GetGeneralSetting().DocsLink, "quota_per_unit": common.QuotaPerUnit, diff --git a/controller/option.go b/controller/option.go index a97f07b841b7..a65f8be321f0 100644 --- a/controller/option.go +++ b/controller/option.go @@ -205,6 +205,15 @@ func UpdateOption(c *gin.Context) { "message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!", }) + return + } + case "RegisterPageWithCaptchaEnabled": + if option.Value == "true" && common.TurnstileSiteKey == "" { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": "无法启用注册人机验证,请先填入 Turnstile 校验相关配置信息!", + }) + return } case "TelegramOAuthEnabled": diff --git a/middleware/turnstile-check.go b/middleware/turnstile-check.go index af87fad4423c..9598b2ffe739 100644 --- a/middleware/turnstile-check.go +++ b/middleware/turnstile-check.go @@ -16,7 +16,14 @@ type turnstileCheckResponse struct { func TurnstileCheck() gin.HandlerFunc { return func(c *gin.Context) { - if common.TurnstileCheckEnabled { + enabled := common.TurnstileCheckEnabled + if !enabled && common.RegisterPageWithCaptchaEnabled { + path := c.Request.URL.Path + if path == "/api/verification" || path == "/api/user/register" { + enabled = true + } + } + if enabled { session := sessions.Default(c) turnstileChecked := session.Get("turnstile") if turnstileChecked != nil { diff --git a/model/option.go b/model/option.go index 8e8587f271c8..0c85beb0d66e 100644 --- a/model/option.go +++ b/model/option.go @@ -44,6 +44,7 @@ func InitOptionMap() { common.OptionMap["TelegramOAuthEnabled"] = strconv.FormatBool(common.TelegramOAuthEnabled) common.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(common.WeChatAuthEnabled) common.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(common.TurnstileCheckEnabled) + common.OptionMap["RegisterPageWithCaptchaEnabled"] = strconv.FormatBool(common.RegisterPageWithCaptchaEnabled) common.OptionMap["RegisterEnabled"] = strconv.FormatBool(common.RegisterEnabled) common.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(common.AutomaticDisableChannelEnabled) common.OptionMap["AutomaticEnableChannelEnabled"] = strconv.FormatBool(common.AutomaticEnableChannelEnabled) @@ -296,6 +297,8 @@ func updateOptionMap(key string, value string) (err error) { common.TelegramOAuthEnabled = boolValue case "TurnstileCheckEnabled": common.TurnstileCheckEnabled = boolValue + case "RegisterPageWithCaptchaEnabled": + common.RegisterPageWithCaptchaEnabled = boolValue case "RegisterEnabled": common.RegisterEnabled = boolValue case "EmailDomainRestrictionEnabled": diff --git a/web/classic/src/components/settings/SystemSetting.jsx b/web/classic/src/components/settings/SystemSetting.jsx index de0962c1b0b1..7fed7320932c 100644 --- a/web/classic/src/components/settings/SystemSetting.jsx +++ b/web/classic/src/components/settings/SystemSetting.jsx @@ -78,6 +78,7 @@ const SystemSetting = () => { WeChatServerToken: '', WeChatAccountQRCodeImageURL: '', TurnstileCheckEnabled: '', + RegisterPageWithCaptchaEnabled: '', TurnstileSiteKey: '', TurnstileSecretKey: '', RegisterEnabled: '', @@ -181,6 +182,7 @@ const SystemSetting = () => { case 'TelegramOAuthEnabled': case 'RegisterEnabled': case 'TurnstileCheckEnabled': + case 'RegisterPageWithCaptchaEnabled': case 'EmailDomainRestrictionEnabled': case 'EmailAliasRestrictionEnabled': case 'SMTPSSLEnabled': @@ -1070,6 +1072,15 @@ const SystemSetting = () => { > {t('允许 Turnstile 用户校验')} + + handleCheckboxChange('RegisterPageWithCaptchaEnabled', e) + } + > + {t('注册及发送邮箱验证码时需要 Turnstile 校验')} + { + return !!( + (status?.turnstile_check || status?.register_page_with_captcha) && + status?.turnstile_site_key + ) + }, [status]) const { isTurnstileEnabled, turnstileSiteKey, turnstileToken, setTurnstileToken, validateTurnstile, - } = useTurnstile() + } = useTurnstile({ forceEnable: signupTurnstileRequired }) const { redirectToLogin, handleLoginSuccess } = useAuthRedirect() const { isSending: isSendingCode, diff --git a/web/default/src/features/system-settings/auth/bot-protection-section.tsx b/web/default/src/features/system-settings/auth/bot-protection-section.tsx index 331613934142..5ada2e8a6ef1 100644 --- a/web/default/src/features/system-settings/auth/bot-protection-section.tsx +++ b/web/default/src/features/system-settings/auth/bot-protection-section.tsx @@ -45,6 +45,7 @@ import { useUpdateOption } from '../hooks/use-update-option' const botProtectionSchema = z.object({ TurnstileCheckEnabled: z.boolean(), + RegisterPageWithCaptchaEnabled: z.boolean(), TurnstileSiteKey: z.string().optional(), TurnstileSecretKey: z.string().optional(), }) @@ -112,6 +113,29 @@ export function BotProtectionSection({ )} /> + ( + + + {t('Require CAPTCHA for Registration')} + + {t( + 'Require Cloudflare Turnstile verification specifically for registration and sending email verification codes' + )} + + + + + + + )} + /> + Date: Thu, 2 Jul 2026 16:32:40 +0600 Subject: [PATCH 2/2] fix: scope registration CAPTCHA on /api/verification to unauthenticated requests only --- middleware/turnstile-check.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/middleware/turnstile-check.go b/middleware/turnstile-check.go index 9598b2ffe739..5e0fd4c0a8aa 100644 --- a/middleware/turnstile-check.go +++ b/middleware/turnstile-check.go @@ -19,7 +19,12 @@ func TurnstileCheck() gin.HandlerFunc { enabled := common.TurnstileCheckEnabled if !enabled && common.RegisterPageWithCaptchaEnabled { path := c.Request.URL.Path - if path == "/api/verification" || path == "/api/user/register" { + // /api/user/register is always a registration flow. + // /api/verification is shared: registration (unauthenticated) and + // email binding (authenticated). Only enforce for the former. + if path == "/api/user/register" { + enabled = true + } else if path == "/api/verification" && c.GetHeader("Authorization") == "" { enabled = true } }