Skip to content

Commit

Permalink
Merge pull request #360 from gbrownmozilla/redirects
Browse files Browse the repository at this point in the history
[Bug 1753838] Harden trailing slashes redirect
  • Loading branch information
gbrownmozilla committed Feb 7, 2022
2 parents 5c05a73 + 0d66516 commit e39d8be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pollbot/middlewares.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from aiohttp import web
import os
import string

logger = logging.getLogger(__package__)

Expand Down Expand Up @@ -60,8 +61,12 @@ async def handle_any(request, response):

async def handle_404(request, response):
if 'json' not in response.headers['Content-Type']:
# This traling slash redirect has caused security issues.
# If it continues to be problematic, consider:
# - only redirect "/v1/.../"?
# - remove the redirect entirely; use duplicate routes instead, in app.py
if request.path.endswith('/'):
return web.HTTPFound('/' + request.path.strip('/'))
return web.HTTPFound('/' + request.path.strip('/'+string.whitespace))
return web.json_response({
"status": 404,
"message": "Page '{}' not found".format(request.path)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ async def test_redirects_strip_leading_slashes(cli):
cli.server.skip_url_asserts = True
resp = await check_response(cli, "//page/", status=302, allow_redirects=False)
assert resp.headers['Location'] == "/page"
# also strip leading and trailing whitespace
resp = await check_response(cli, "/%0a/www.evil.com/", status=302, allow_redirects=False)
assert resp.headers['Location'] == "/www.evil.com"
resp = await check_response(cli, "/%0a /www.evil.com %0a%0b/", status=302,
allow_redirects=False)
assert resp.headers['Location'] == "/www.evil.com"


async def check_yaml_resource(cli, url, filename, **kwargs):
Expand Down

0 comments on commit e39d8be

Please # to comment.