From 023aa16013b4410ef76238d89524403414d2bc33 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Wed, 18 Dec 2024 19:18:17 +0300 Subject: [PATCH] Merging to release-5.3: [TT-13021], fixed missing lines (#6787) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [TT-13021], fixed missing lines (#6787) ### **User description**
TT-13021
Summary URL Rewrite with `Transfer-Encoding: chunked` Header removes the response payload body
Type Bug Bug
Status In Dev
Points N/A
Labels '24Bugsmash, QA_Fail, customer_bug, jira_escalated
--- ## Description TASK: https://tyktech.atlassian.net/browse/TT-13021 ## Related Issue ## Motivation and Context ## How This Has Been Tested ## Screenshots (if appropriate) ## Types of changes - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Refactoring or add test (improvements in base code or adds test coverage to functionality) ## Checklist - [ ] I ensured that the documentation is up to date - [ ] I explained why this PR updates go.mod in detail with reasoning why it's required - [ ] I would like a code coverage CI quality gate exception and have explained why ___ ### **PR Type** Bug fix ___ ### **Description** - Fixed an issue where the request body was not properly reset after being read, which could cause issues in subsequent processing. - Updated `gateway/mw_url_rewrite.go` to use `io.NopCloser` and `bytes.NewBuffer` for resetting the request body after reading. - Updated `gateway/mw_validate_json.go` to ensure the request body is reset after reading for JSON validation. - Added `bytes` package imports in both files to support the changes. ___ ### **Changes walkthrough** 📝
Relevant files
Bug fix
mw_url_rewrite.go
Fix request body handling in URL rewrite middleware           

gateway/mw_url_rewrite.go
  • Added bytes package import for handling request body.
  • Ensured the request body is reset after reading it using io.NopCloser
    and bytes.NewBuffer.
  • Improved handling of request body to allow further processing after
    reading.
  • +2/-0     
    mw_validate_json.go
    Fix request body handling in JSON validation middleware   

    gateway/mw_validate_json.go
  • Added bytes package import for handling request body.
  • Ensured the request body is reset after reading it using io.NopCloser
    and bytes.NewBuffer.
  • Improved request body handling for JSON validation.
  • +2/-0     
    ___ > 💡 **PR-Agent usage**: Comment `/help "your question"` on any pull request to receive relevant information --- gateway/mw_url_rewrite.go | 2 ++ gateway/mw_validate_json.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/gateway/mw_url_rewrite.go b/gateway/mw_url_rewrite.go index 80a24537d22..e59a4d18ed1 100644 --- a/gateway/mw_url_rewrite.go +++ b/gateway/mw_url_rewrite.go @@ -1,6 +1,7 @@ package gateway import ( + "bytes" "fmt" "io" "net/http" @@ -700,6 +701,7 @@ func checkPayload(r *http.Request, options apidef.StringRegexMap, triggernum int log.WithError(err).Error("error reading request body") return false } + r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) // Perform regex matching on the request body matched, matches := options.FindAllStringSubmatch(string(bodyBytes), -1) diff --git a/gateway/mw_validate_json.go b/gateway/mw_validate_json.go index 87f8e4de3c6..c53f35c6e19 100644 --- a/gateway/mw_validate_json.go +++ b/gateway/mw_validate_json.go @@ -1,6 +1,7 @@ package gateway import ( + "bytes" "errors" "fmt" "io" @@ -54,6 +55,7 @@ func (k *ValidateJSON) ProcessRequest(w http.ResponseWriter, r *http.Request, _ if err != nil { return err, http.StatusBadRequest } + r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) defer r.Body.Close() inputLoader := gojsonschema.NewBytesLoader(bodyBytes)