Skip to content

Commit 678870f

Browse files
authored
Merge pull request #9048 from readthedocs/humitos/dont-serve-inactive-non-existent-versions
2 parents 052f891 + 75c2467 commit 678870f

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

readthedocs/proxito/tests/test_full.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,31 @@ def test_external_version_serving_old_slugs(self):
198198

199199
# Invalid tests
200200

201+
def test_non_existent_version(self):
202+
url = "/en/non-existent-version/"
203+
host = "project.dev.readthedocs.io"
204+
resp = self.client.get(url, HTTP_HOST=host)
205+
self.assertEqual(resp.status_code, 404)
206+
207+
def test_non_existent_version_with_filename(self):
208+
url = "/en/non-existent-version/doesnt-exist.html"
209+
host = "project.dev.readthedocs.io"
210+
resp = self.client.get(url, HTTP_HOST=host)
211+
self.assertEqual(resp.status_code, 404)
212+
213+
def test_inactive_version(self):
214+
url = "/en/inactive/"
215+
host = "project.dev.readthedocs.io"
216+
fixture.get(
217+
Version,
218+
verbose_name="inactive",
219+
slug="inactive",
220+
active=False,
221+
project=self.project,
222+
)
223+
resp = self.client.get(url, HTTP_HOST=host)
224+
self.assertEqual(resp.status_code, 404)
225+
201226
@override_settings(
202227
RTD_EXTERNAL_VERSION_DOMAIN='dev.readthedocs.build',
203228
)

readthedocs/proxito/views/serve.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,35 @@ def get(self,
8484
version_slug=version_slug,
8585
filename=filename,
8686
)
87-
88-
# All public versions can be cached.
8987
version = final_project.versions.filter(slug=version_slug).first()
90-
if (
91-
self._is_cache_enabled(final_project)
92-
and version and not version.is_private
93-
):
94-
self.cache_request = True
9588

9689
log.bind(
9790
project_slug=final_project.slug,
9891
subproject_slug=subproject_slug,
9992
lang_slug=lang_slug,
10093
version_slug=version_slug,
10194
filename=filename,
102-
cache_request=self.cache_request,
10395
)
96+
97+
# Skip serving versions that are not active (return 404). This is to
98+
# avoid serving files that we have in the storage, but its associated
99+
# version does not exist anymore or it was de-activated.
100+
#
101+
# Note that we want to serve the page when `version is None` because it
102+
# could be a valid URL, like `/` or `` (empty) that does not have a
103+
# version associated to it.
104+
#
105+
# However, if there is a `version_slug` in the URL but there is no
106+
# version on the database we want to return 404.
107+
if (version and not version.active) or (version_slug and not version):
108+
log.warning("Version does not exist or is not active.")
109+
raise Http404("Version does not exist or is not active.")
110+
111+
if self._is_cache_enabled(final_project) and version and not version.is_private:
112+
# All public versions can be cached.
113+
self.cache_request = True
114+
115+
log.bind(cache_request=self.cache_request)
104116
log.debug('Serving docs.')
105117

106118
# Verify if the project is marked as spam and return a 401 in that case

0 commit comments

Comments
 (0)