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

Avoid infinite redirection #4833

Merged
merged 2 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion readthedocs/core/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,16 @@ def server_error_404(request, exception=None, template_name='404.html'): # pyli
Marking exception as optional to make /404/ testing page to work.
"""
response = get_redirect_response(request, path=request.get_full_path())

if response:
return response
if response.url == request.build_absolute_uri():
# check that we do have a response and avoid infinite redirect
log.warning(
'Infinite Redirect: FROM URL is the same than TO URL. url=%s',
response.url,
)
else:
return response
r = render(request, template_name)
r.status_code = 404
return r
Expand Down
28 changes: 28 additions & 0 deletions readthedocs/rtd_tests/tests/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,34 @@ def setUp(self):
self.pip = Project.objects.get(slug='pip')
self.pip.versions.create_latest()

@override_settings(USE_SUBDOMAIN=True)
def test_redirect_prefix_infinite(self):
"""
Avoid infinite redirects.

If the URL hit is the same that the URL returned for redirection, we
return a 404.

These examples comes from this issue:
* https://github.com/rtfd/readthedocs.org/issues/4673
"""
Redirect.objects.create(
project=self.pip, redirect_type='prefix',
from_url='/',
)
r = self.client.get('/redirect', HTTP_HOST='pip.readthedocs.org')
self.assertEqual(r.status_code, 302)
self.assertEqual(
r['Location'], 'http://pip.readthedocs.org/en/latest/redirect.html')

r = self.client.get('/redirect/', HTTP_HOST='pip.readthedocs.org')
self.assertEqual(r.status_code, 302)
self.assertEqual(
r['Location'], 'http://pip.readthedocs.org/en/latest/redirect/')

r = self.client.get('/en/latest/redirect/', HTTP_HOST='pip.readthedocs.org')
self.assertEqual(r.status_code, 404)

@override_settings(USE_SUBDOMAIN=True)
def test_redirect_root(self):
Redirect.objects.create(
Expand Down