Skip to content

Commit e764d4a

Browse files
committed
feat: enhance request logging for file uploads in operations log middleware
- Updated logging to filter out binary content for multipart file upload requests. - Implemented request body size limitation to prevent excessive logging. - Removed unused file handling logic to streamline the code.
1 parent df646b3 commit e764d4a

1 file changed

Lines changed: 21 additions & 38 deletions

File tree

‎internal/middleware/operations_log.go‎

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8-
"path/filepath"
98
"strings"
109
"time"
1110

@@ -18,10 +17,6 @@ import (
1817
"github.com/sirupsen/logrus"
1918
)
2019

21-
var allowedFileExts = []string{
22-
"jpg", "jpeg", "png", "pdf", "doc", "docx", "xlsx", "xls", "zip", "rar", "tar", "gz", "7z",
23-
}
24-
2520
func OperationLogs() gin.HandlerFunc {
2621
return func(c *gin.Context) {
2722
if !isModifyMethod(c.Request.Method) {
@@ -31,7 +26,12 @@ func OperationLogs() gin.HandlerFunc {
3126

3227
logrus.Info("开始处理请求:", c.Request.URL.Path, "方法:", c.Request.Method)
3328
requestMessage, _ := processRequestBody(c)
34-
logrus.Info("请求体:", requestMessage)
29+
// 对于文件上传请求,不打印完整请求体(包含二进制内容)
30+
if !isMultipartRequest(c) {
31+
logrus.Info("请求体:", requestMessage)
32+
} else {
33+
logrus.Info("请求体: [文件上传请求,已过滤二进制内容]", requestMessage)
34+
}
3535

3636
writer := newResponseBodyWriter(c)
3737
c.Writer = writer
@@ -55,6 +55,14 @@ func isModifyMethod(method string) bool {
5555
}
5656

5757
func processRequestBody(c *gin.Context) (string, string) {
58+
// 检查是否是 multipart/form-data 请求(文件上传)
59+
if isMultipartRequest(c) {
60+
// 对于 multipart 请求,不读取请求体(包含二进制文件内容)
61+
// 只记录路径信息,避免消耗请求体导致后续无法读取文件
62+
return fmt.Sprintf("[文件上传请求: %s]", c.Request.URL.Path), ""
63+
}
64+
65+
// 对于普通请求,读取请求体
5866
body, err := io.ReadAll(c.Request.Body)
5967
if err != nil {
6068
logrus.Error("读取请求体失败:", err)
@@ -63,43 +71,18 @@ func processRequestBody(c *gin.Context) (string, string) {
6371
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
6472

6573
requestMessage := string(body)
66-
if strings.Contains(c.Request.URL.Path, "file/up") {
67-
requestMessage = handleFileUpload(c)
74+
// 限制请求体大小,避免记录过大的内容
75+
if len(requestMessage) > 10000 {
76+
requestMessage = requestMessage[:10000] + "...[内容过长已截断]"
6877
}
6978

7079
return requestMessage, requestMessage
7180
}
7281

73-
func handleFileUpload(c *gin.Context) string {
74-
file, err := c.FormFile("file")
75-
if err != nil {
76-
return ""
77-
}
78-
79-
fileType := c.PostForm("type")
80-
if fileType == "" {
81-
fileType = "unknown"
82-
}
83-
84-
// 1. 获取安全的基本文件名,去除路径
85-
baseFileName := filepath.Base(file.Filename)
86-
87-
// 2. 净化文件名
88-
filename := utils.SanitizeFilename(baseFileName)
89-
90-
// 3. 二次验证文件名的安全性
91-
if !filepath.IsLocal(filename) {
92-
c.JSON(http.StatusBadRequest, gin.H{"error": "非法的文件名"})
93-
return ""
94-
}
95-
96-
// 4. 验证文件类型
97-
if !utils.ValidateFileExtension(filename, allowedFileExts) {
98-
c.JSON(http.StatusBadRequest, gin.H{"error": "不允许的文件类型"})
99-
return ""
100-
}
101-
102-
return fmt.Sprintf("%s:%s", fileType, filename)
82+
// isMultipartRequest 检查是否是 multipart/form-data 请求
83+
func isMultipartRequest(c *gin.Context) bool {
84+
contentType := c.Request.Header.Get("Content-Type")
85+
return strings.HasPrefix(contentType, "multipart/form-data")
10386
}
10487

10588
func saveOperationLog(c *gin.Context, start time.Time, cost int64, requestMsg, responseMsg string) {

0 commit comments

Comments
 (0)