Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging to release-5.3.9: [TT-13021]Transfer encoding fix (#6770) #6776

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions gateway/mw_url_rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package gateway

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/textproto"
"net/url"
Expand Down Expand Up @@ -692,22 +692,38 @@ func checkContextTrigger(r *http.Request, options map[string]apidef.StringRegexM

func checkPayload(r *http.Request, options apidef.StringRegexMap, triggernum int) bool {
contextData := ctxGetData(r)
bodyBytes, _ := ioutil.ReadAll(r.Body)

nopCloseRequestBody(r)
// Read the entire request body
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
log.WithError(err).Error("error reading request body")
return false
}

// Perform regex matching on the request body
matched, matches := options.FindAllStringSubmatch(string(bodyBytes), -1)

if matched {
kn := buildTriggerKey(triggernum, "payload")

if len(matches) == 0 {
// If there are no matches, simply return true
return true
}

// Store the first match in the context data
contextData[kn] = matches[0][0]

// Iterate over all matches and add them to the context data
for i, match := range matches {
if len(match) > 0 {
addMatchToContextData(contextData, match, triggernum, "payload", i)
}
}

// Update the context data with the modified map
ctxSetData(r, contextData)
return true
}

Expand Down
53 changes: 48 additions & 5 deletions gateway/mw_url_rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gateway

import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -158,11 +159,12 @@ func BenchmarkRewriter(b *testing.B) {

func TestRewriterTriggers(t *testing.T) {
type TestDef struct {
name string
pattern, to string
in, want string
triggerConf []apidef.RoutingTrigger
req *http.Request
name string
pattern, to string
in, want string
triggerConf []apidef.RoutingTrigger
req *http.Request
payloadTrigger bool
}

ts := StartTest(nil)
Expand Down Expand Up @@ -194,6 +196,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand All @@ -220,6 +223,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -250,6 +254,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -280,6 +285,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -310,6 +316,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -355,6 +362,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -385,6 +393,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -415,6 +424,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand All @@ -439,6 +449,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand All @@ -463,6 +474,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -490,6 +502,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -520,6 +533,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -550,6 +564,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -584,6 +599,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -618,6 +634,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -652,6 +669,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand All @@ -675,6 +693,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
true,
}
},
func() TestDef {
Expand All @@ -698,6 +717,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
true,
}
},
func() TestDef {
Expand All @@ -721,6 +741,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
true,
}
},
func() TestDef {
Expand All @@ -744,6 +765,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
true,
}
},
func() TestDef {
Expand Down Expand Up @@ -773,6 +795,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
true,
}
},
func() TestDef {
Expand Down Expand Up @@ -802,6 +825,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
true,
}
},
func() TestDef {
Expand All @@ -825,6 +849,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -854,6 +879,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
true,
}
},
func() TestDef {
Expand Down Expand Up @@ -883,6 +909,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
true,
}
},
func() TestDef {
Expand All @@ -906,6 +933,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand All @@ -929,6 +957,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -958,6 +987,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -987,6 +1017,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -1017,6 +1048,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -1044,6 +1076,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand Down Expand Up @@ -1073,6 +1106,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
func() TestDef {
Expand All @@ -1096,6 +1130,7 @@ func TestRewriterTriggers(t *testing.T) {
},
},
r,
false,
}
},
}
Expand All @@ -1113,6 +1148,14 @@ func TestRewriterTriggers(t *testing.T) {
if err != nil {
t.Error("compile failed:", err)
}

//added check to ensure that reading the payload to check for the trigger does not break the request
if tc.payloadTrigger {
body, err := io.ReadAll(tc.req.Body)
assert.NotEqual(t, "", string(body))
assert.NoError(t, err)
}

if got != tc.want {
t.Errorf("rewrite failed, want %q, got %q", tc.want, got)
}
Expand Down
5 changes: 3 additions & 2 deletions gateway/mw_validate_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gateway
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/TykTechnologies/gojsonschema"
Expand Down Expand Up @@ -48,8 +48,9 @@ func (k *ValidateJSON) ProcessRequest(w http.ResponseWriter, r *http.Request, _
}
}

nopCloseRequestBody(r)
// Load input body into gojsonschema
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
return err, http.StatusBadRequest
}
Expand Down
Loading