diff --git a/docs/Tasktracking/2026-04-27-fix-batch-edit-shouldbindjson.md b/docs/Tasktracking/2026-04-27-fix-batch-edit-shouldbindjson.md index ccd5c797..59e19308 100644 --- a/docs/Tasktracking/2026-04-27-fix-batch-edit-shouldbindjson.md +++ b/docs/Tasktracking/2026-04-27-fix-batch-edit-shouldbindjson.md @@ -13,16 +13,20 @@ Change Type: Fix ## Changes - `web/controller/inbound.go:481`: `c.ShouldBindJSON(&request)` → `c.ShouldBind(&request)` -- `web/controller/inbound.go:477-479`: 为匿名结构体字段添加 `form` 标签,确保 form 绑定能正确映射字段名(`shouldBind` 使用 `form` 标签而非 `json` 标签进行 form 字段映射) +- `web/controller/inbound.go:477-479`: 为匿名结构体字段添加 `form` 标签,确保 form 绑定能正确映射字段名 +- `web/controller/inbound.go:482`: 绑定失败时使用 `invalidFormData` 替代 `inboundUpdateSuccess` 作为错误前缀,避免 "入站连接已成功更新 (错误)" 的语义矛盾 +- `web/service/inbound.go:722,767`: `enable` 字段类型断言改为 comma-ok 模式,非 bool 值时返回明确错误而非 panic ## Impact - `ShouldBind` 会根据 Content-Type 自动选择 form 绑定(浏览器请求)或 JSON 绑定(其他客户端),与项目其他 handler 一致 - 向后兼容:JSON 请求依然能被正确解析(`json` 标签仍保留) +- 绑定失败的错误提示更准确("数据格式错误" 而非 "入站连接已成功更新") +- 恶意构造的请求不再能通过类型断言 panic 导致进程崩溃 - 不影响数据库、API、配置 ## Verification - `gofmt -l -w .` — 无格式问题 -- `go vet ./web/controller/...` — 通过 +- `go vet ./web/controller/... ./web/service/...` — 通过 ## Risks And Follow-Up -- 低风险,与项目现有代码风格一致(model 中所有 struct 字段均同时携带 `json` 和 `form` 标签) +- 低风险,与项目现有代码风格一致 diff --git a/web/controller/inbound.go b/web/controller/inbound.go index cbc032b9..5261c965 100644 --- a/web/controller/inbound.go +++ b/web/controller/inbound.go @@ -479,7 +479,7 @@ func (a *InboundController) batchUpdateInboundClients(c *gin.Context) { UpdateFields string `json:"updateFields" form:"updateFields"` } if err := c.ShouldBind(&request); err != nil { - jsonMsg(c, I18nWeb(c, "pages.inbounds.toasts.inboundUpdateSuccess"), err) + jsonMsg(c, I18nWeb(c, "pages.login.toasts.invalidFormData"), err) return } diff --git a/web/service/inbound.go b/web/service/inbound.go index 98467eaf..8f994abb 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -719,7 +719,11 @@ func (s *InboundService) BatchUpdateInboundClients(inboundID int, clientIDs []st } if _, hasEnable := updatesMap["enable"]; hasEnable { - enableVal := updatesMap["enable"].(bool) + enableVal, ok := updatesMap["enable"].(bool) + if !ok { + tx.Rollback() + return false, common.NewError("enable must be a boolean value") + } for _, email := range oldEmails { if err := tx.Model(&xray.ClientTraffic{}).Where("email = ? AND inbound_id = ?", email, inboundID). Update("enable", enableVal).Error; err != nil { @@ -759,8 +763,11 @@ func (s *InboundService) BatchUpdateInboundClients(inboundID int, clientIDs []st needRestart := false if _, hasEnable := updatesMap["enable"]; hasEnable { + enableVal, ok := updatesMap["enable"].(bool) + if !ok { + return false, common.NewError("enable must be a boolean value") + } s.xrayApi.Init(p.GetAPIPort()) - enableVal := updatesMap["enable"].(bool) for _, email := range oldEmails { if email == "" { continue