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

[Bug 1753838] Harden trailing slashes redirect #360

Merged
merged 4 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you verified that this will fix the issue?
This seems like a good candidate for a unit test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added unit tests now - thanks.
Running locally, I reproduced the issue reported in bug 1753838 with requests like http://localhost:9876/%0a/www.google.com/. Without changes, that redirects to www.google.com; with this patch, pollbot reports:
{"status": 404, "message": "Page '/www.google.com' not found"}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to hear, thank you!

return web.json_response({
"status": 404,
"message": "Page '{}' not found".format(request.path)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ 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