From ceea657ccf638f01f8a60a1cda50768353f1df63 Mon Sep 17 00:00:00 2001 From: eafonyang Date: Tue, 25 Aug 2026 16:47:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=80=B3=E6=9C=BA=E9=98=BB=E6=8A=97=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=EF=BC=9A=E8=BF=87=E6=BB=A4=E4=BA=86=E9=98=BB=E6=8A=97?= =?UTF-8?q?=3D0=EF=BC=8C=E8=80=B3=E6=9C=BA=E5=9E=8B=E5=8F=B7=E4=B9=B1?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/handler/impedance.go | 47 +++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/internal/handler/impedance.go b/internal/handler/impedance.go index be8b822..e918f46 100644 --- a/internal/handler/impedance.go +++ b/internal/handler/impedance.go @@ -56,6 +56,15 @@ func (h *ImpedanceHandler) ReportImpedance(c *gin.Context) { return } + // 品牌/型号含乱码或控制字符、超长属于无效数据,静默丢弃 + if !isValidHeadphoneText(brand) || !isValidHeadphoneText(headphoneModel) { + c.JSON(http.StatusOK, gin.H{ + "code": 200, + "msg": "操作成功", + }) + return + } + impedance, err := strconv.Atoi(valueStr) if err != nil { c.JSON(http.StatusOK, gin.H{ @@ -65,18 +74,27 @@ func (h *ImpedanceHandler) ReportImpedance(c *gin.Context) { return } + // 阻抗值为 0 属于无效数据,静默丢弃 + if impedance == 0 { + c.JSON(http.StatusOK, gin.H{ + "code": 200, + "msg": "操作成功", + }) + return + } + brandNorm := normalizeHeadphoneText(brand) modelNorm := normalizeHeadphoneText(headphoneModel) impedanceInfo := map[string]any{ - "mac_addr": mac, - "device_model": deviceModel, - "impedance_ohm": impedance, - "headphone_brand": brand, - "headphone_model": headphoneModel, - "headphone_brand_norm": brandNorm, - "headphone_model_norm": modelNorm, - "ip_addr": clientIP, + "mac_addr": mac, + "device_model": deviceModel, + "impedance_ohm": impedance, + "headphone_brand": brand, + "headphone_model": headphoneModel, + "headphone_brand_norm": brandNorm, + "headphone_model_norm": modelNorm, + "ip_addr": clientIP, } jsonData, err := json.Marshal(impedanceInfo) @@ -110,6 +128,19 @@ func normalizeHeadphoneText(s string) string { return strings.ToLower(strings.TrimSpace(s)) } +// isValidHeadphoneText 校验耳机品牌/型号:拒绝含乱码字符(U+FFFD)、控制字符及超长(>100 字符)的输入 +func isValidHeadphoneText(s string) bool { + if len(s) > 100 { + return false + } + for _, r := range s { + if r == '\ufffd' || r < 0x20 || r == 0x7f { + return false + } + } + return true +} + func impedanceRedisField(mac, brandNorm, modelNorm string) string { return fmt.Sprintf("%s|%s|%s", mac, brandNorm, modelNorm) }