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-4-lts: [TT-9327] Decoding the URL request first, before handling any additional logic (#5345) #5736

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
21 changes: 18 additions & 3 deletions gateway/mw_url_rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,18 @@ var envValueMatch = regexp.MustCompile(`\$secret_env.([A-Za-z0-9_\-\.]+)`)
var metaMatch = regexp.MustCompile(`\$tyk_meta.([A-Za-z0-9_\-\.]+)`)
var secretsConfMatch = regexp.MustCompile(`\$secret_conf.([A-Za-z0-9[.\-\_]+)`)

func (gw *Gateway) urlRewrite(meta *apidef.URLRewriteMeta, r *http.Request) (string, error) {
path := r.URL.String()
func (gw *Gateway) urlRewrite(meta *apidef.URLRewriteMeta, r *http.Request, decodeURL bool) (string, error) {
rawPath := r.URL.String()
path := rawPath

if decodeURL {
var err error
path, err = url.PathUnescape(rawPath)
if err != nil {
return rawPath, fmt.Errorf("failed to decode URL path: %s", rawPath)
}
}

log.Debug("Inbound path: ", path)
newpath := path

Expand Down Expand Up @@ -196,6 +206,10 @@ func (gw *Gateway) urlRewrite(meta *apidef.URLRewriteMeta, r *http.Request) (str

newpath = gw.replaceTykVariables(r, newpath, true)

if rawPath == newpath && containsEscapedChars(rawPath) {
newpath, _ = gw.urlRewrite(meta, r, true)
}

return newpath, nil
}

Expand Down Expand Up @@ -478,7 +492,8 @@ func (m *URLRewriteMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req
umeta := meta.(*apidef.URLRewriteMeta)
log.Debug(r.URL)
oldPath := r.URL.String()
p, err := m.Gw.urlRewrite(umeta, r)

p, err := m.Gw.urlRewrite(umeta, r, false)
if err != nil {
log.Error(err)
return err, http.StatusInternalServerError
Expand Down
18 changes: 14 additions & 4 deletions gateway/mw_url_rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ var testRewriterData = []struct {
pattern, to string
in, want string
}{
{
"Encoded",
"/test/payment-intents", "/change/to/me",
"/test/payment%2Dintents", "/change/to/me",
},
{
"MatchEncodedChars",
"^(.+)%2[Dd](.+)$", "/change/to/me",
"/test/payment%2Dintents", "/change/to/me",
},
{
"Straight",
"/test/straight/rewrite", "/change/to/me",
Expand Down Expand Up @@ -95,7 +105,7 @@ func TestRewriter(t *testing.T) {
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
r := tc.reqMaker()
got, err := ts.Gw.urlRewrite(tc.meta, r)
got, err := ts.Gw.urlRewrite(tc.meta, r, false)
if err != nil {
t.Error("compile failed:", err)
}
Expand All @@ -113,7 +123,7 @@ func BenchmarkRewriter(b *testing.B) {
//warm-up regexp caches
for _, tc := range cases {
r := tc.reqMaker()
ts.Gw.urlRewrite(tc.meta, r)
ts.Gw.urlRewrite(tc.meta, r, false)
}

b.ReportAllocs()
Expand All @@ -123,7 +133,7 @@ func BenchmarkRewriter(b *testing.B) {
b.StopTimer()
r := tc.reqMaker()
b.StartTimer()
ts.Gw.urlRewrite(tc.meta, r)
ts.Gw.urlRewrite(tc.meta, r, false)
}
}
}
Expand Down Expand Up @@ -1081,7 +1091,7 @@ func TestRewriterTriggers(t *testing.T) {
Triggers: tc.triggerConf,
}

got, err := ts.Gw.urlRewrite(&testConf, tc.req)
got, err := ts.Gw.urlRewrite(&testConf, tc.req, false)
if err != nil {
t.Error("compile failed:", err)
}
Expand Down
24 changes: 24 additions & 0 deletions gateway/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gateway

import (
"errors"
"net/url"
"os"

"github.com/TykTechnologies/tyk/apidef"
Expand Down Expand Up @@ -158,3 +159,26 @@ func shouldReloadSpec(existingSpec, newSpec *APISpec) bool {

return false
}

// check if 2 maps are the same
func areMapsEqual(a, b map[string]string) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if b[k] != v {
return false
}
}
return true
}

// checks if a string contains escaped characters
func containsEscapedChars(str string) bool {
unescaped, err := url.PathUnescape(str)
if err != nil {
return true
}

return str != unescaped
}
62 changes: 62 additions & 0 deletions gateway/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,65 @@ func Test_shouldReloadSpec(t *testing.T) {
assertionHelper(t, tcs)
})
}

func TestAreMapsEqual(t *testing.T) {
tests := []struct {
name string
map1 map[string]string
map2 map[string]string
expected bool
}{
{
name: "Equal maps",
map1: map[string]string{"key1": "value1", "key2": "value2"},
map2: map[string]string{"key1": "value1", "key2": "value2"},
expected: true,
},
{
name: "Different maps",
map1: map[string]string{"key1": "value1", "key2": "value2"},
map2: map[string]string{"key1": "value1", "key2": "value3"},
expected: false,
},
{
name: "Different sizes",
map1: map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"},
map2: map[string]string{"key1": "value1", "key2": "value2"},
expected: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := areMapsEqual(test.map1, test.map2)
if result != test.expected {
t.Errorf("areMapsEqual() = %v, want %v", result, test.expected)
}
})
}
}

func TestContainsEscapedCharacters(t *testing.T) {
tests := []struct {
value string
expected bool
}{
{
value: "payment%2Dintents",
expected: true,
},
{
value: "payment-intents",
expected: false,
},
}

for _, test := range tests {
t.Run(test.value, func(t *testing.T) {
result := containsEscapedChars(test.value)
if result != test.expected {
t.Errorf("containsEscapedChars() = %v, want %v", result, test.expected)
}
})
}
}
Loading