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-10701, fix for urlRewrite encoding issue (#5835) #5855

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
24 changes: 10 additions & 14 deletions gateway/mw_url_rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,10 @@ 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, decodeURL bool) (string, error) {
func (gw *Gateway) urlRewrite(meta *apidef.URLRewriteMeta, r *http.Request) (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 @@ -176,6 +168,14 @@ func (gw *Gateway) urlRewrite(meta *apidef.URLRewriteMeta, r *http.Request, deco
}

matchGroups := meta.MatchRegexp.FindAllStringSubmatch(path, -1)
if len(matchGroups) == 0 && containsEscapedChars(rawPath) {
unescapedPath, err := url.PathUnescape(rawPath)
if err != nil {
return unescapedPath, fmt.Errorf("failed to decode URL path: %s", rawPath)
}

matchGroups = meta.MatchRegexp.FindAllStringSubmatch(unescapedPath, -1)
}

// Make sure it matches the string
log.Debug("Rewriter checking matches, len is: ", len(matchGroups))
Expand Down Expand Up @@ -206,10 +206,6 @@ func (gw *Gateway) urlRewrite(meta *apidef.URLRewriteMeta, r *http.Request, deco

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

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

return newpath, nil
}

Expand Down Expand Up @@ -493,7 +489,7 @@ func (m *URLRewriteMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req
log.Debug(r.URL)
oldPath := r.URL.String()

p, err := m.Gw.urlRewrite(umeta, r, false)
p, err := m.Gw.urlRewrite(umeta, r)
if err != nil {
log.Error(err)
return err, http.StatusInternalServerError
Expand Down
24 changes: 20 additions & 4 deletions gateway/mw_url_rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ var testRewriterData = []struct {
"test/val/(.*)", "change/to/$1",
"/test/val/VALUE", "change/to/VALUE",
},
{
"OneVal Special Case",
"test/val/(.*)", "/test/val/$1",
"/test/val/VALUE%2C", "/test/val/VALUE%2C",
},
{
"OneVal Special Case With Query Param Encoded",
"test/val/(.*)", "/test/val/$1",
"/test/val/VALUE%2C?a=te%2Cst", "/test/val/VALUE%2C?a=te%2Cst",
},
{
"ThreeVals",
"/test/val/(.*)/space/(.*)/and/then/(.*)", "/change/to/$1/$2/$3",
Expand Down Expand Up @@ -105,7 +115,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, false)
got, err := ts.Gw.urlRewrite(tc.meta, r)
if err != nil {
t.Error("compile failed:", err)
}
Expand All @@ -123,7 +133,10 @@ func BenchmarkRewriter(b *testing.B) {
//warm-up regexp caches
for _, tc := range cases {
r := tc.reqMaker()
ts.Gw.urlRewrite(tc.meta, r, false)
_, err := ts.Gw.urlRewrite(tc.meta, r)
if err != nil {
b.Errorf("benchmark failed %s", err.Error())
}
}

b.ReportAllocs()
Expand All @@ -133,7 +146,10 @@ func BenchmarkRewriter(b *testing.B) {
b.StopTimer()
r := tc.reqMaker()
b.StartTimer()
ts.Gw.urlRewrite(tc.meta, r, false)
_, err := ts.Gw.urlRewrite(tc.meta, r)
if err != nil {
b.Errorf("benchmark failed %s", err.Error())
}
}
}
}
Expand Down Expand Up @@ -1091,7 +1107,7 @@ func TestRewriterTriggers(t *testing.T) {
Triggers: tc.triggerConf,
}

got, err := ts.Gw.urlRewrite(&testConf, tc.req, false)
got, err := ts.Gw.urlRewrite(&testConf, tc.req)
if err != nil {
t.Error("compile failed:", err)
}
Expand Down
Loading