From f7c1a51efb6d0e578c6f699b6f3498b5930cdd32 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Fri, 23 Sep 2022 14:36:38 -0600 Subject: fastcgi: Redirect using original URI path (fix #5073) --- modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go b/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go index a9e6b22..799050e 100644 --- a/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go +++ b/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go @@ -348,7 +348,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error } redirHandler := caddyhttp.StaticResponse{ StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)), - Headers: http.Header{"Location": []string{"{http.request.uri.path}/"}}, + Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/"}}, } redirRoute := caddyhttp.Route{ MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet}, -- cgit v1.2.3 From e747a9bb12bc644aebce369061a2dc0de567d8d9 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Fri, 23 Sep 2022 16:47:59 -0600 Subject: Fix tests --- caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.txt | 4 ++-- caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.txt | 2 +- caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.txt | 2 +- caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.txt | 2 +- .../integration/caddyfile_adapt/php_fastcgi_try_files_override.txt | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.txt b/caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.txt index bb7c7f7..2fa2b79 100644 --- a/caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.txt +++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.txt @@ -8,7 +8,7 @@ route { } not path */ } - redir @canonicalPath {path}/ 308 + redir @canonicalPath {http.request.orig_uri.path}/ 308 # If the requested file does not exist, try index files @indexFiles { @@ -50,7 +50,7 @@ route { "handler": "static_response", "headers": { "Location": [ - "{http.request.uri.path}/" + "{http.request.orig_uri.path}/" ] }, "status_code": 308 diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.txt b/caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.txt index dbb0871..70a0780 100644 --- a/caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.txt +++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.txt @@ -42,7 +42,7 @@ "handler": "static_response", "headers": { "Location": [ - "{http.request.uri.path}/" + "{http.request.orig_uri.path}/" ] }, "status_code": 308 diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.txt b/caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.txt index 488c525..d90b2cb 100644 --- a/caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.txt +++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.txt @@ -30,7 +30,7 @@ php_fastcgi @api localhost:9000 "handler": "static_response", "headers": { "Location": [ - "{http.request.uri.path}/" + "{http.request.orig_uri.path}/" ] }, "status_code": 308 diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.txt b/caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.txt index 90d1633..a04d66f 100644 --- a/caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.txt +++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.txt @@ -43,7 +43,7 @@ php_fastcgi localhost:9000 { "handler": "static_response", "headers": { "Location": [ - "{http.request.uri.path}/" + "{http.request.orig_uri.path}/" ] }, "status_code": 308 diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.txt b/caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.txt index 4342786..a3381f6 100644 --- a/caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.txt +++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.txt @@ -46,7 +46,7 @@ php_fastcgi localhost:9000 { "handler": "static_response", "headers": { "Location": [ - "{http.request.uri.path}/" + "{http.request.orig_uri.path}/" ] }, "status_code": 308 -- cgit v1.2.3 From 013b5103527cb1646dc717cadf48ad34aceaa77e Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Wed, 28 Sep 2022 00:13:12 -0600 Subject: rewrite: Only trim prefix if matched See #5073 --- modules/caddyhttp/rewrite/rewrite.go | 9 +++++++-- modules/caddyhttp/rewrite/rewrite_test.go | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/modules/caddyhttp/rewrite/rewrite.go b/modules/caddyhttp/rewrite/rewrite.go index 95bc504..31c9778 100644 --- a/modules/caddyhttp/rewrite/rewrite.go +++ b/modules/caddyhttp/rewrite/rewrite.go @@ -383,8 +383,13 @@ func trimPathPrefix(escapedPath, prefix string) string { iPrefix++ } - // found matching prefix, trim it - return escapedPath[iPath:] + // if we iterated through the entire prefix, we found it, so trim it + if iPath >= len(prefix) { + return escapedPath[iPath:] + } + + // otherwise we did not find the prefix + return escapedPath } func reverse(s string) string { diff --git a/modules/caddyhttp/rewrite/rewrite_test.go b/modules/caddyhttp/rewrite/rewrite_test.go index bc20c85..5875983 100644 --- a/modules/caddyhttp/rewrite/rewrite_test.go +++ b/modules/caddyhttp/rewrite/rewrite_test.go @@ -225,6 +225,16 @@ func TestRewrite(t *testing.T) { input: newRequest(t, "GET", "/prefix/foo/bar"), expect: newRequest(t, "GET", "/foo/bar"), }, + { + rule: Rewrite{StripPathPrefix: "/prefix"}, + input: newRequest(t, "GET", "/prefix"), + expect: newRequest(t, "GET", ""), + }, + { + rule: Rewrite{StripPathPrefix: "/prefix"}, + input: newRequest(t, "GET", "/"), + expect: newRequest(t, "GET", "/"), + }, { rule: Rewrite{StripPathPrefix: "/prefix"}, input: newRequest(t, "GET", "/prefix/foo%2Fbar"), -- cgit v1.2.3