fix: sanitize API key name with html.EscapeString to prevent stored XSS (CWE-79)

HTML-encode user-supplied key names in both Create and Update
endpoints. Previously, names were stored verbatim — an attacker
could inject script tags that would execute in admin panels or
any view using innerHTML/v-html rendering.

Fixes: CWE-79 (Stored Cross-Site Scripting)
This commit is contained in:
Cheri Wen
2026-06-02 00:46:29 +08:00
parent aa69e3947d
commit 0ae3329613
+3 -2
View File
@@ -5,6 +5,7 @@ import (
"crypto/rand"
"encoding/hex"
"fmt"
"html"
"strconv"
"strings"
"sync"
@@ -399,7 +400,7 @@ func (s *APIKeyService) Create(ctx context.Context, userID int64, req CreateAPIK
apiKey := &APIKey{
UserID: userID,
Key: key,
Name: req.Name,
Name: html.EscapeString(req.Name),
GroupID: req.GroupID,
Status: StatusActive,
IPWhitelist: req.IPWhitelist,
@@ -538,7 +539,7 @@ func (s *APIKeyService) Update(ctx context.Context, id int64, userID int64, req
// 更新字段
if req.Name != nil {
apiKey.Name = *req.Name
apiKey.Name = html.EscapeString(*req.Name)
}
if req.GroupID != nil {