Skip to content
18 changes: 18 additions & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ var SMTPAccount = ""
var SMTPFrom = ""
var SMTPToken = ""

// SMS 短信通知系统级配置
var SMSProvider = "" // 短信服务商: aliyun, tencent, custom
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
var SMSAliyunAccessKeyId = "" // 阿里云 AccessKeyId
var SMSAliyunAccessKeySecret = "" // 阿里云 AccessKeySecret
var SMSAliyunSignName = "" // 阿里云短信签名
var SMSAliyunTemplateCode = "" // 阿里云短信模板Code
var SMSSendCloudSmsUser = "" // SendCloud smsUser
var SMSSendCloudSmsKey = "" // SendCloud smsKey
var SMSSendCloudTemplateId = "" // SendCloud 短信模板ID
var SMSTencentSecretId = "" // 腾讯云 SecretId
var SMSTencentSecretKey = "" // 腾讯云 SecretKey
var SMSTencentSmsSdkAppId = "" // 腾讯云 SmsSdkAppId
var SMSTencentSignName = "" // 腾讯云短信签名
var SMSTencentTemplateId = "" // 腾讯云短信模板ID
var SMSCustomUrl = "" // 通用HTTP接口地址
var SMSCustomMethod = "POST" // 通用HTTP请求方法
var SMSCustomTemplate = "" // 通用HTTP请求模板

var GitHubClientId = ""
var GitHubClientSecret = ""
var LinuxDOClientId = ""
Expand Down
8 changes: 7 additions & 1 deletion controller/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ func GetOptions(c *gin.Context) {
strings.HasSuffix(k, "Secret") ||
strings.HasSuffix(k, "Key") ||
strings.HasSuffix(k, "secret") ||
strings.HasSuffix(k, "api_key") {
strings.HasSuffix(k, "api_key") ||
// SMS 敏感凭证字段
k == "SMSAliyunAccessKeyId" ||
k == "SMSSendCloudSmsUser" ||
k == "SMSTencentSecretId" ||
k == "SMSCustomUrl" ||
k == "SMSCustomTemplate" {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
continue
}
options = append(options, &model.Option{
Expand Down
36 changes: 35 additions & 1 deletion controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -1097,6 +1098,7 @@ type UpdateUserSettingRequest struct {
GotifyUrl string `json:"gotify_url,omitempty"`
GotifyToken string `json:"gotify_token,omitempty"`
GotifyPriority int `json:"gotify_priority,omitempty"`
SmsPhoneNumber string `json:"sms_phone_number,omitempty"`
UpstreamModelUpdateNotifyEnabled *bool `json:"upstream_model_update_notify_enabled,omitempty"`
AcceptUnsetModelRatioModel bool `json:"accept_unset_model_ratio_model"`
RecordIpLog bool `json:"record_ip_log"`
Expand All @@ -1110,7 +1112,7 @@ func UpdateUserSetting(c *gin.Context) {
}

// 验证预警类型
if req.QuotaWarningType != dto.NotifyTypeEmail && req.QuotaWarningType != dto.NotifyTypeWebhook && req.QuotaWarningType != dto.NotifyTypeBark && req.QuotaWarningType != dto.NotifyTypeGotify {
if req.QuotaWarningType != dto.NotifyTypeEmail && req.QuotaWarningType != dto.NotifyTypeWebhook && req.QuotaWarningType != dto.NotifyTypeBark && req.QuotaWarningType != dto.NotifyTypeGotify && req.QuotaWarningType != dto.NotifyTypeSms {
common.ApiErrorI18n(c, i18n.MsgSettingInvalidType)
return
}
Expand Down Expand Up @@ -1183,6 +1185,33 @@ func UpdateUserSetting(c *gin.Context) {
}
}

// 如果是SMS类型,验证手机号和系统SMS配置
if req.QuotaWarningType == dto.NotifyTypeSms {
if req.SmsPhoneNumber == "" {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "手机号码不能为空",
})
return
}
// 验证手机号格式:允许国际号码格式,7-15位数字,可选+前缀
phoneRegex := regexp.MustCompile(`^\+?[0-9]{7,15}$`)
if !phoneRegex.MatchString(req.SmsPhoneNumber) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "手机号码格式不正确",
})
return
}
if common.SMSProvider == "" {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "系统未配置短信服务,请联系管理员",
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return
}
}

userId := c.GetInt("id")
user, err := model.GetUserById(userId, true)
if err != nil {
Expand Down Expand Up @@ -1234,6 +1263,11 @@ func UpdateUserSetting(c *gin.Context) {
}
}

// 如果是SMS类型,添加手机号到设置中
if req.QuotaWarningType == dto.NotifyTypeSms {
settings.SmsPhoneNumber = req.SmsPhoneNumber
}

// 更新用户设置
user.SetSetting(settings)
if err := user.Update(false); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions dto/user_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ type UserSetting struct {
SidebarModules string `json:"sidebar_modules,omitempty"` // SidebarModules 左侧边栏模块配置
BillingPreference string `json:"billing_preference,omitempty"` // BillingPreference 扣费策略(订阅/钱包)
Language string `json:"language,omitempty"` // Language 用户语言偏好 (zh, en)
SmsPhoneNumber string `json:"sms_phone_number,omitempty"` // SmsPhoneNumber 接收短信的手机号码
}

var (
NotifyTypeEmail = "email" // Email 邮件
NotifyTypeWebhook = "webhook" // Webhook
NotifyTypeBark = "bark" // Bark 推送
NotifyTypeGotify = "gotify" // Gotify 推送
NotifyTypeSms = "sms" // SMS 短信通知
)
48 changes: 48 additions & 0 deletions model/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ func InitOptionMap() {
common.OptionMap["SMTPToken"] = ""
common.OptionMap["SMTPSSLEnabled"] = strconv.FormatBool(common.SMTPSSLEnabled)
common.OptionMap["SMTPForceAuthLogin"] = strconv.FormatBool(common.SMTPForceAuthLogin)
common.OptionMap["SMSProvider"] = ""
common.OptionMap["SMSAliyunAccessKeyId"] = ""
common.OptionMap["SMSAliyunAccessKeySecret"] = ""
common.OptionMap["SMSAliyunSignName"] = ""
common.OptionMap["SMSAliyunTemplateCode"] = ""
common.OptionMap["SMSSendCloudSmsUser"] = ""
common.OptionMap["SMSSendCloudSmsKey"] = ""
common.OptionMap["SMSSendCloudTemplateId"] = ""
common.OptionMap["SMSTencentSecretId"] = ""
common.OptionMap["SMSTencentSecretKey"] = ""
common.OptionMap["SMSTencentSmsSdkAppId"] = ""
common.OptionMap["SMSTencentSignName"] = ""
common.OptionMap["SMSTencentTemplateId"] = ""
common.OptionMap["SMSCustomUrl"] = ""
common.OptionMap["SMSCustomMethod"] = "POST"
common.OptionMap["SMSCustomTemplate"] = ""
common.OptionMap["Notice"] = ""
common.OptionMap["About"] = ""
common.OptionMap["HomePageContent"] = ""
Expand Down Expand Up @@ -333,6 +349,38 @@ func updateOptionMap(key string, value string) (err error) {
common.SMTPFrom = value
case "SMTPToken":
common.SMTPToken = value
case "SMSProvider":
common.SMSProvider = value
case "SMSAliyunAccessKeyId":
common.SMSAliyunAccessKeyId = value
case "SMSAliyunAccessKeySecret":
common.SMSAliyunAccessKeySecret = value
case "SMSAliyunSignName":
common.SMSAliyunSignName = value
case "SMSAliyunTemplateCode":
common.SMSAliyunTemplateCode = value
case "SMSSendCloudSmsUser":
common.SMSSendCloudSmsUser = value
case "SMSSendCloudSmsKey":
common.SMSSendCloudSmsKey = value
case "SMSSendCloudTemplateId":
common.SMSSendCloudTemplateId = value
case "SMSTencentSecretId":
common.SMSTencentSecretId = value
case "SMSTencentSecretKey":
common.SMSTencentSecretKey = value
case "SMSTencentSmsSdkAppId":
common.SMSTencentSmsSdkAppId = value
case "SMSTencentSignName":
common.SMSTencentSignName = value
case "SMSTencentTemplateId":
common.SMSTencentTemplateId = value
case "SMSCustomUrl":
common.SMSCustomUrl = value
case "SMSCustomMethod":
common.SMSCustomMethod = value
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
case "SMSCustomTemplate":
common.SMSCustomTemplate = value
Comment on lines +364 to +403

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Enumerate which SMS keys are actually filtered by controller/option.go and which are returned.
rg -nP -C3 '(SMSAliyunAccessKeyId|SMSSendCloudSmsUser|SMSTencentSecretId|SMSCustomUrl|SMSCustomTemplate|HasSuffix\(k,\s*"(Secret|Key|Token|secret|api_key)"\))' controller/option.go
echo "---"
# Find any other place SMS* options are read/written (to confirm no additional exposure paths)
rg -nP -C2 '\bSMS[A-Z][A-Za-z]+' --type=go

Repository: QuantumNous/new-api

Length of output: 7675


Fix credential exposure: SMSTencentSmsSdkAppId is returned unfiltered in GET /api/option/.

The filtering logic in controller/option.go (lines 69–79) uses suffix matching (*Secret, *Key, Token) and explicitly lists 5 SMS keys to exclude. However, SMSTencentSmsSdkAppId matches none of these rules and is returned to the frontend. While nominally a tenant ID rather than a secret, it identifies the Tencent application and should be filtered. Also verify that other unfiltered SMS keys (SMSAliyunSignName, SMSAliyunTemplateCode, SMSSendCloudTemplateId, SMSTencentSignName, SMSTencentTemplateId, SMSCustomMethod) do not require filtering based on your threat model.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@model/option.go` around lines 352 - 383, The GET /api/option filtering in
controller/option.go currently omits SMSTencentSmsSdkAppId from the
sensitive-keys filter; update the filter used in the handler (the function that
builds the options response for GET /api/option) to include
"SMSTencentSmsSdkAppId" so it is excluded from responses, and also review/add
any of the other SMS fields from model/option.go (SMSAliyunSignName,
SMSAliyunTemplateCode, SMSSendCloudTemplateId, SMSTencentSignName,
SMSTencentTemplateId, SMSCustomMethod) to the same filter if your threat model
requires them to be hidden. Ensure you modify the same string-list or matching
logic used for keys ending with Secret/Key/Token so the change follows the
existing filtering approach.

case "ServerAddress":
system_setting.ServerAddress = value
case "WorkerUrl":
Expand Down
5 changes: 5 additions & 0 deletions service/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ func checkAndSendQuotaNotify(relayInfo *relaycommon.RelayInfo, quota int, preCon
// Bark推送使用简短文本,不支持HTML
content = "{{value}},剩余额度:{{value}},请及时充值"
values = []interface{}{prompt, logger.FormatQuota(relayInfo.UserQuota)}
} else if notifyType == dto.NotifyTypeSms {
// SMS短信使用模板变量:values[0]=当前余额, values[1]=告警阈值
content = ""
remainQuota := relayInfo.UserQuota - consumeQuota
values = []interface{}{logger.FormatQuota(remainQuota), logger.FormatQuota(threshold)}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
} else if notifyType == dto.NotifyTypeGotify {
content = "{{value}},当前剩余额度为 {{value}},请及时充值。"
values = []interface{}{prompt, logger.FormatQuota(relayInfo.UserQuota)}
Expand Down
Loading
Loading