Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
uploads: Extra-escape internal S3 paths.
Alex Vandiverd41a00b83b5a
diff --git a/puppet/zulip/files/nginx/zulip-include-frontend/uploads-internal.conf b/puppet/zulip/files/nginx/zulip-include-frontend/uploads-internal.confindex 5e56963894e..41445f1ae93 100644--- a/puppet/zulip/files/nginx/zulip-include-frontend/uploads-internal.conf+++ b/puppet/zulip/files/nginx/zulip-include-frontend/uploads-internal.conf@@ -1,10 +1,15 @@# Handle redirects to S3-location ~ ^/internal/s3/([^/]+)/(.*) {+location ~ ^/internal/s3/(?<s3_hostname>[^/]+)/(?<s3_path>.*) {internal;include /etc/nginx/zulip-include/headers;- set $download_url https://$1/$2;- proxy_set_header Host $1;+ # The components of this path are originally double-URI-escaped+ # (see zerver/view/upload.py). "location" matches are on+ # unescaped values, which fills $s3_path with a properly+ # single-escaped path to pass to the upstream server.+ # (see associated commit message for more details)+ set $download_url https://$s3_hostname/$s3_path;+ proxy_set_header Host $s3_hostname;# Ensure that we only get _one_ of these headers: the one that# Django added, not the one from S3.diff --git a/zerver/views/upload.py b/zerver/views/upload.pyindex cb0dda26eeb..423b9c85b5b 100644--- a/zerver/views/upload.py+++ b/zerver/views/upload.py@@ -99,7 +99,14 @@ def serve_s3(request: HttpRequest, path_id: str, download: bool = False) -> Http# this is acceptable in development.return redirect(url)- response = internal_nginx_redirect("/internal/s3/" + url[len("https://") :])+ # We over-escape the path, to work around it being impossible to+ # get the _unescaped_ new internal request URI in nginx.+ parsed_url = urlparse(url)+ assert parsed_url.hostname is not None+ assert parsed_url.path is not None+ assert parsed_url.query is not None+ escaped_path_parts = parsed_url.hostname + quote(parsed_url.path) + "?" + parsed_url.query+ response = internal_nginx_redirect("/internal/s3/" + escaped_path_parts)patch_disposition_header(response, path_id, download)patch_cache_control(response, private=True, immutable=True)return response