Skip to content

Raise 404 at SubdomainMiddleware if the project does not exist. #4795

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

Merged
merged 3 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
2 changes: 2 additions & 0 deletions readthedocs/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def process_request(self, request):
# Support ports during local dev
public_domain in host or public_domain in full_host
):
if not Project.objects.filter(slug=subdomain).exists():
raise Http404(_('Project not found'))
request.subdomain = True
request.slug = subdomain
request.urlconf = SUBDOMAIN_URLCONF
Expand Down
7 changes: 7 additions & 0 deletions readthedocs/rtd_tests/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def test_proper_subdomain(self):
self.assertEqual(request.urlconf, self.urlconf_subdomain)
self.assertEqual(request.slug, 'pip')

@override_settings(PRODUCTION_DOMAIN='readthedocs.org')
def test_wrong_subdomain(self):
http_host = 'xyz-wrong-sub-domain-xyz.readthedocs.org'
request = self.factory.get(self.url, HTTP_HOST=http_host)
with self.assertRaises(Http404):
self.middleware.process_request(request)

@override_settings(PRODUCTION_DOMAIN='readthedocs.org')
def test_restore_urlconf_after_request(self):
"""
Expand Down