Fix append_slash_redirect when PATH_INFO has internal slashes#2338
Fix append_slash_redirect when PATH_INFO has internal slashes#2338davidism merged 2 commits intopallets:mainfrom dairiki:bug.1972-append_slash_redirect
Conversation
|
Thank you, this finally makes sense. The issue is that |
|
@davidism Yes, I think you have found exactly where the urljoin happens. (That is the correct behavior — relative URLs should be interpreted relative to the full request path. Of note, it would also be correct to pass the relative URL in the Anyhow, I've just rebased this PR onto the current |
It was fixed in werkzeug>=2.1.0. Refs: lektor#997, pallets/werkzeug#2338
(This seems to have been a contentious issue — please don't hate me.)
This addresses an issue — reported before in #1972 — namely that
werkzeug.utils.append_slash_redirectdoes not work correctly ifPATH_INFOcontains internal slashes.Suppose a request comes in for
http://example.com/app/foo/bar. Assume this makes it to the WSGI app withenviron["SCRIPT_NAME"] = "/app"andenviron["PATH_INFO"] = "/foo/bar". The app, in order to append a slash to the URL returnsappend_slash_redirect(environ). As things stand, this issues a redirect to the relative path"foo/bar/". That path gets interpreted relative to the base URL implied by the original request ("http://example.com/app/foo/bar") resulting in a final location of"http://example.com/app/foo/foo/bar/". This is not quite what is wanted — onefoois enough for us.The solution is to redirect to a relative path (as is currently done), but redirect to a relative path that contains only the trailing component PATH_INFO.
This PR fixes that and includes new tests that exercise the problem.
Relation to Prior Work
Note that this issue is subtly different than that purportedly "fixed" by PR #1538. PR #1538, as demonstrated by the tests included in that PR wants to redirect to an absolute path based on
PATH_INFO. This results in (incorrectly) discarding any leading path components that may be in SCRIPT_NAME.There is also #1842. Due to its brevity, it is unclear precisely what issue is being described therein, but the proposed fix is the same as the (incorrect) solution proposed in #1538.
Issues Fixed
append_slash_redirectmisredirects #1972Checklist
CHANGES.rstsummarizing the change and linking to the issue... versionchanged::entries in any relevant code docs.pre-commithooks and fix any issues.pytestandtox, no tests failed.