From 6d8bfc2beece34904fc8970864e039d4ab1ce81f Mon Sep 17 00:00:00 2001 From: samwaf Date: Wed, 19 Mar 2025 16:12:49 +0800 Subject: [PATCH] fix: gpt in firefox #207 --- middleware/sec_api_middleware.go | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/middleware/sec_api_middleware.go b/middleware/sec_api_middleware.go index a6640f9..6b49629 100644 --- a/middleware/sec_api_middleware.go +++ b/middleware/sec_api_middleware.go @@ -5,39 +5,33 @@ import ( "SamWaf/wafsec" "bytes" "github.com/gin-gonic/gin" - "io/ioutil" + "io" "net/http" + "strings" ) // SecApi 鉴权中间件 func SecApi() gin.HandlerFunc { return func(c *gin.Context) { - // Read the body and make a backup for later use - bodyBytes, err := ioutil.ReadAll(c.Request.Body) + bodyBytes, err := io.ReadAll(c.Request.Body) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read request body"}) c.Abort() return } - - c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) // Reset the request body to original - - //fmt.Println("Header ", c.Request.Header["Content-Type"]) - // Your preprocessing logic here - // For example, reading raw body and doing some operations - //fmt.Println("Raw body:", string(bodyBytes)) + c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) if c.Request.Header.Get("Content-Type") == "application/x-www-form-urlencoded" { - // Modify the bodyBytes if necessary - // ... - decryptBytes, _ := wafsec.AesDecrypt(string(bodyBytes), global.GWAF_COMMUNICATION_KEY) + decryptBytes, err := wafsec.AesDecrypt(string(bodyBytes), global.GWAF_COMMUNICATION_KEY) + if err == nil { + c.Request.Body = io.NopCloser(bytes.NewBuffer(decryptBytes)) + } - //fmt.Println("Raw body解密:", string(deBytes)) - // Store the modified body back in the request - c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(decryptBytes)) - } else if c.Request.Header.Get("accept") == ", text/event-stream" { - decryptBytes, _ := wafsec.AesDecrypt(string(bodyBytes), global.GWAF_COMMUNICATION_KEY) - c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(decryptBytes)) + } else if strings.Contains(c.Request.Header.Get("accept"), "text/event-stream") { + decryptBytes, err := wafsec.AesDecrypt(string(bodyBytes), global.GWAF_COMMUNICATION_KEY) + if err == nil { + c.Request.Body = io.NopCloser(bytes.NewBuffer(decryptBytes)) + } } c.Next() }