From 4b54dafe35c40f0affdd3721b7bfcbbd2089413d Mon Sep 17 00:00:00 2001 From: William Grzybowski Date: Sat, 9 Jun 2018 05:49:20 -0400 Subject: [PATCH] Do not use translated path (#3051) * Do not use translated path Redirect would fail in case we have encoded url, e.g. /foo/bar%2Fbar -> /foo/bar/bar/ * Use request.raw_path as mandated by @asvetlov --- CHANGES/3051.bugfix | 1 + CONTRIBUTORS.txt | 1 + aiohttp/web_middlewares.py | 2 +- tests/test_web_middleware.py | 10 ++++++++-- 4 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 CHANGES/3051.bugfix diff --git a/CHANGES/3051.bugfix b/CHANGES/3051.bugfix new file mode 100644 index 00000000000..7d35089224c --- /dev/null +++ b/CHANGES/3051.bugfix @@ -0,0 +1 @@ +Make redirect with `normalize_path_middleware` work when using url encoded paths. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index c9f32f9866c..f4c130b9eec 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -206,6 +206,7 @@ Vladyslav Bondar W. Trevor King Will McGugan Willem de Groot +William Grzybowski Wilson Ong Wei Lin Weiwei Wang diff --git a/aiohttp/web_middlewares.py b/aiohttp/web_middlewares.py index a85af8f821d..57b735c1c70 100644 --- a/aiohttp/web_middlewares.py +++ b/aiohttp/web_middlewares.py @@ -73,7 +73,7 @@ async def impl(request, handler): resolves, request = await _check_request_resolves( request, path) if resolves: - raise redirect_class(request.path + query) + raise redirect_class(request.raw_path) return await handler(request) diff --git a/tests/test_web_middleware.py b/tests/test_web_middleware.py index 8cb65759753..b7ff59c22b9 100644 --- a/tests/test_web_middleware.py +++ b/tests/test_web_middleware.py @@ -86,6 +86,8 @@ def wrapper(extra_middlewares): 'GET', '/resource1/a/b', handler) app.router.add_route( 'GET', '/resource2/a/b/', handler) + app.router.add_route( + 'GET', '/resource2/a/b%2Fc/', handler) app.middlewares.extend(extra_middlewares) return aiohttp_client(app, server_kwargs={'skip_url_asserts': True}) return wrapper @@ -101,7 +103,9 @@ class TestNormalizePathMiddleware: ('/resource1?p1=1&p2=2', 200), ('/resource1/?p1=1&p2=2', 404), ('/resource2?p1=1&p2=2', 200), - ('/resource2/?p1=1&p2=2', 200) + ('/resource2/?p1=1&p2=2', 200), + ('/resource2/a/b%2Fc', 200), + ('/resource2/a/b%2Fc/', 200) ]) async def test_add_trailing_when_necessary( self, path, status, cli): @@ -120,7 +124,9 @@ async def test_add_trailing_when_necessary( ('/resource1?p1=1&p2=2', 200), ('/resource1/?p1=1&p2=2', 404), ('/resource2?p1=1&p2=2', 404), - ('/resource2/?p1=1&p2=2', 200) + ('/resource2/?p1=1&p2=2', 200), + ('/resource2/a/b%2Fc', 404), + ('/resource2/a/b%2Fc/', 200) ]) async def test_no_trailing_slash_when_disabled( self, path, status, cli):