服务端安全加固:多维限流(按 IP/按门店)+ 敏感接口独立速率上限抵御 DDoS/刷接口; 登录暴力破解新增按来源 IP 锁定;反代后正确识别真实客户端 IP; 门店 custom_fields 轻量配置(录入默认值)透传保存。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Y2Wdwo7SmgBJU37cBrkhPK
This commit is contained in:
@@ -42,12 +42,13 @@ func (h *ShopHandler) UpdateInfo(c *gin.Context) {
|
||||
shopID := middleware.GetShopID(c)
|
||||
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
Address string `json:"address"`
|
||||
Phone string `json:"phone"`
|
||||
ManagerName string `json:"manager_name"`
|
||||
LogoURL string `json:"logo_url"`
|
||||
WechatID string `json:"wechat_id"`
|
||||
Name string `json:"name"`
|
||||
Address string `json:"address"`
|
||||
Phone string `json:"phone"`
|
||||
ManagerName string `json:"manager_name"`
|
||||
LogoURL string `json:"logo_url"`
|
||||
WechatID string `json:"wechat_id"`
|
||||
CustomFields map[string]interface{} `json:"custom_fields"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -64,6 +65,19 @@ func (h *ShopHandler) UpdateInfo(c *gin.Context) {
|
||||
if req.LogoURL != "" {
|
||||
updates["logo_url"] = req.LogoURL
|
||||
}
|
||||
// custom_fields 增量 merge:保留已有键(如其它店级配置),只覆盖本次传入的键。
|
||||
if req.CustomFields != nil {
|
||||
var cur model.Shop
|
||||
h.db.Select("custom_fields").Where("id = ?", shopID).First(&cur)
|
||||
merged := model.JSON{}
|
||||
for k, v := range cur.CustomFields {
|
||||
merged[k] = v
|
||||
}
|
||||
for k, v := range req.CustomFields {
|
||||
merged[k] = v
|
||||
}
|
||||
updates["custom_fields"] = merged
|
||||
}
|
||||
if err := h.db.Model(&model.Shop{}).Where("id = ?", shopID).Updates(updates).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/wangjia/jiu/backend/internal/middleware"
|
||||
"github.com/wangjia/jiu/backend/internal/model"
|
||||
"github.com/wangjia/jiu/backend/testutil"
|
||||
)
|
||||
|
||||
func setupShopRouter(db *gorm.DB) *gin.Engine {
|
||||
shopH := NewShopHandler(db)
|
||||
r := gin.New()
|
||||
r.Use(gin.Recovery())
|
||||
api := r.Group("/api/v1")
|
||||
api.Use(middleware.JWT(db))
|
||||
shop := api.Group("/shop")
|
||||
shop.GET("/info", shopH.GetInfo)
|
||||
shop.PUT("/info", shopH.UpdateInfo)
|
||||
return r
|
||||
}
|
||||
|
||||
// TestUpdateInfo_MergesCustomFields 钉死「设为默认」依赖的契约:
|
||||
// 传 custom_fields 会 merge 进本店已有 custom_fields(保留旧键),且只动本店。
|
||||
func TestUpdateInfo_MergesCustomFields(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
db := testutil.SetupTestDB()
|
||||
shop := testutil.CreateTestShop(db, "S_CF1")
|
||||
admin := testutil.CreateTestUser(db, shop.ID, "admin", "pw", "admin")
|
||||
// 预置已有 custom_fields,验证 merge 不抹掉其它键
|
||||
require.NoError(t, db.Model(&model.Shop{}).Where("id = ?", shop.ID).
|
||||
Update("custom_fields", model.JSON{"foo": "bar"}).Error)
|
||||
// 另一店做隔离对照
|
||||
other := testutil.CreateTestShop(db, "S_CF2")
|
||||
require.NoError(t, db.Model(&model.Shop{}).Where("id = ?", other.ID).
|
||||
Update("custom_fields", model.JSON{"x": "y"}).Error)
|
||||
|
||||
r := setupShopRouter(db)
|
||||
token := getAuthToken(admin.ID, shop.ID, "admin")
|
||||
w := makeRequest(r, http.MethodPut, "/api/v1/shop/info", token, jsonBody(
|
||||
"name", "改名后",
|
||||
"custom_fields", map[string]interface{}{"default_series_id": 7},
|
||||
))
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
var got model.Shop
|
||||
require.NoError(t, db.First(&got, shop.ID).Error)
|
||||
assert.Equal(t, "改名后", got.Name)
|
||||
assert.Equal(t, "bar", got.CustomFields["foo"]) // 旧键保留
|
||||
assert.EqualValues(t, 7, got.CustomFields["default_series_id"]) // 新键写入
|
||||
|
||||
// 隔离:另一店的 custom_fields 不受影响
|
||||
var o model.Shop
|
||||
require.NoError(t, db.First(&o, other.ID).Error)
|
||||
assert.Equal(t, "y", o.CustomFields["x"])
|
||||
_, leaked := o.CustomFields["default_series_id"]
|
||||
assert.False(t, leaked)
|
||||
}
|
||||
Reference in New Issue
Block a user