fix: correct error message and add type assertion guard in batch update

This commit is contained in:
root 2026-04-27 10:14:06 +08:00
parent d2b35f9fc4
commit 2e067bf9c3
3 changed files with 17 additions and 6 deletions

View file

@ -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` 标签)
- 低风险,与项目现有代码风格一致

View file

@ -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
}

View file

@ -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