Skip to content

Commit 034f54d

Browse files
authored
Proxito: always add nginx internal path (#11080)
* Proxito: always add nginx internal path Currently, this allows serving files like: https://docs.readthedocs.io/_/static/proxito-static/javascript/readthedocs-analytics.js We should always add the internal path to avoid serving files over incorrect paths. * Test
1 parent d9c4e3c commit 034f54d

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

readthedocs/proxito/tests/test_full.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,28 @@ def test_serve_static_files(self):
17291729
resp.headers["Cache-Tag"], "project,project:rtd-staticfiles,rtd-staticfiles"
17301730
)
17311731

1732+
@mock.patch(
1733+
"readthedocs.proxito.views.mixins.staticfiles_storage",
1734+
new=StaticFileSystemStorageTest(),
1735+
)
1736+
def test_serve_static_files_internal_nginx_redirect_always_appended(self):
1737+
"""Test for #11080."""
1738+
resp = self.client.get(
1739+
reverse(
1740+
"proxito_static_files",
1741+
args=["proxito-static/javascript/readthedocs-doc-embed.js"],
1742+
),
1743+
headers={"host": "project.readthedocs.io"},
1744+
)
1745+
self.assertEqual(resp.status_code, 200)
1746+
self.assertEqual(
1747+
resp.headers["x-accel-redirect"],
1748+
"/proxito-static/media/proxito-static/javascript/readthedocs-doc-embed.js",
1749+
)
1750+
self.assertEqual(
1751+
resp.headers["Cache-Tag"], "project,project:rtd-staticfiles,rtd-staticfiles"
1752+
)
1753+
17321754
@mock.patch("readthedocs.proxito.views.mixins.staticfiles_storage")
17331755
def test_serve_invalid_static_file(self, staticfiles_storage):
17341756
staticfiles_storage.url.side_effect = Exception

readthedocs/proxito/views/mixins.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import copy
21
import mimetypes
32
from urllib.parse import parse_qsl, urlencode, urlparse
43

@@ -233,22 +232,17 @@ def _serve_file_from_nginx(self, path, root_path):
233232
:param path: The path of the file to serve.
234233
:param root_path: The root path of the internal redirect.
235234
"""
236-
original_path = copy.copy(path)
237-
if not path.startswith(f"/{root_path}/"):
238-
if path[0] == "/":
239-
path = path[1:]
240-
path = f"/{root_path}/{path}"
241-
235+
internal_path = f"/{root_path}/" + path.lstrip("/")
242236
log.debug(
243237
"Nginx serve.",
244-
original_path=original_path,
245-
path=path,
238+
original_path=path,
239+
internal_path=internal_path,
246240
)
247241

248-
content_type, encoding = mimetypes.guess_type(path)
242+
content_type, encoding = mimetypes.guess_type(internal_path)
249243
content_type = content_type or "application/octet-stream"
250244
response = HttpResponse(
251-
f"Serving internal path: {path}", content_type=content_type
245+
f"Serving internal path: {internal_path}", content_type=content_type
252246
)
253247
if encoding:
254248
response["Content-Encoding"] = encoding
@@ -258,11 +252,11 @@ def _serve_file_from_nginx(self, path, root_path):
258252
# as the header value.
259253
# https://github.com/benoitc/gunicorn/issues/1448
260254
# https://docs.djangoproject.com/en/1.11/ref/unicode/#uri-and-iri-handling
261-
x_accel_redirect = iri_to_uri(path)
255+
x_accel_redirect = iri_to_uri(internal_path)
262256
response["X-Accel-Redirect"] = x_accel_redirect
263257

264258
# Needed to strip any GET args, etc.
265-
response.proxito_path = urlparse(path).path
259+
response.proxito_path = urlparse(internal_path).path
266260
return response
267261

268262
def _serve_file_from_python(self, request, path, storage):

0 commit comments

Comments
 (0)