fix: round 10 critical fixes - WebSocket race, rate limiting, XSS protection, Caddyfile, and input validation

This commit is contained in:
2026-05-20 17:59:22 +08:00
parent a5b5713b29
commit 20cdcc748e
10 changed files with 336 additions and 25 deletions
@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"regexp"
"strings"
"time"
@@ -16,6 +17,9 @@ import (
"github.com/yourname/cyrene-ai/gateway/internal/store"
)
// usernameRegex 用户名格式校验:仅允许字母、数字、下划线,长度 3-32
var usernameRegex = regexp.MustCompile(`^[a-zA-Z0-9_]{3,32}$`)
// AuthHandler 认证处理器
type AuthHandler struct {
cfg *config.Config
@@ -49,6 +53,12 @@ func (h *AuthHandler) Register(c *gin.Context) {
return
}
// 用户名格式校验:仅允许字母、数字、下划线,长度 3-32
if !usernameRegex.MatchString(req.Username) {
c.JSON(http.StatusBadRequest, gin.H{"error": "用户名格式无效:仅允许字母、数字和下划线,长度 3-32 位"})
return
}
// MVP阶段:验证码简单校验 (开发环境接受 "000000")
if req.VerifyCode != "000000" {
c.JSON(http.StatusBadRequest, gin.H{"error": "验证码错误 (开发阶段请使用 000000)"})
@@ -118,6 +128,12 @@ func (h *AuthHandler) Login(c *gin.Context) {
return
}
// 用户名格式校验:仅允许字母、数字、下划线,长度 3-32
if !usernameRegex.MatchString(req.Username) {
c.JSON(http.StatusBadRequest, gin.H{"error": "用户名格式无效"})
return
}
var userID string
// 尝试从 users 表查询用户