From 270b9a18028d898ffe678542acfb22af516c453d Mon Sep 17 00:00:00 2001 From: Harsh Vardhan <184601513+harshvardhan178251@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:31:22 +0530 Subject: [PATCH 1/3] zulip: Add fetch_upload() for downloading uploaded files. --- zulip/tests/test_fetch_upload.py | 80 ++++++++++++++++++++++++++++++++ zulip/zulip/__init__.py | 37 +++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 zulip/tests/test_fetch_upload.py diff --git a/zulip/tests/test_fetch_upload.py b/zulip/tests/test_fetch_upload.py new file mode 100644 index 000000000..82b5c747c --- /dev/null +++ b/zulip/tests/test_fetch_upload.py @@ -0,0 +1,80 @@ +from unittest.mock import MagicMock, patch + +import pytest + +from zulip import Client, ZulipError + + +def make_client() -> Client: + with patch.object( + Client, + "get_server_settings", + return_value={"zulip_version": "test-version", "zulip_feature_level": 1}, + ): + return Client( + email="test@example.com", + api_key="test-api-key", + site="https://zulip.example.com", + client="test-client", + ) + + +def mock_response(status_code: int, content: bytes) -> MagicMock: + response = MagicMock() + response.status_code = status_code + response.content = content + return response + + +class TestFetchUpload: + def test_absolute_url_passthrough(self) -> None: + client = make_client() + client.ensure_session() + assert client.session is not None + with patch.object( + client.session, "get", return_value=mock_response(200, b"file contents") + ) as mock_get: + result = client.fetch_upload("https://zulip.example.com/user_uploads/1/file.pdf") + mock_get.assert_called_once_with( + "https://zulip.example.com/user_uploads/1/file.pdf", timeout=90.0 + ) + assert result == b"file contents" + + def test_root_relative_path_resolved_against_server_root(self) -> None: + client = make_client() + client.ensure_session() + assert client.session is not None + with patch.object( + client.session, "get", return_value=mock_response(200, b"file contents") + ) as mock_get: + result = client.fetch_upload("/user_uploads/1/file.pdf") + mock_get.assert_called_once_with( + "https://zulip.example.com/user_uploads/1/file.pdf", timeout=90.0 + ) + assert result == b"file contents" + + def test_custom_timeout(self) -> None: + client = make_client() + client.ensure_session() + assert client.session is not None + with patch.object( + client.session, "get", return_value=mock_response(200, b"file contents") + ) as mock_get: + client.fetch_upload("/user_uploads/1/file.pdf", timeout=5) + mock_get.assert_called_once_with( + "https://zulip.example.com/user_uploads/1/file.pdf", timeout=5.0 + ) + + def test_non_root_relative_path_rejected(self) -> None: + client = make_client() + with pytest.raises(ZulipError): + client.fetch_upload("user_uploads/1/file.pdf") + + def test_non_200_raises(self) -> None: + client = make_client() + client.ensure_session() + assert client.session is not None + with patch.object( + client.session, "get", return_value=mock_response(403, b"") + ), pytest.raises(ZulipError): + client.fetch_upload("/user_uploads/1/file.pdf") diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index 6d725a10b..7609f0a47 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -876,6 +876,43 @@ def upload_file(self, file: IO[Any]) -> Dict[str, Any]: """ return self.call_endpoint(url="user_uploads", files=[file]) + def fetch_upload(self, url_or_path: str, timeout: Optional[float] = None) -> bytes: + """ + Download an uploaded file, using API authentication. + + Accepts the `/user_uploads/...` path returned as the `url` + field by `upload_file()`, a `/user_uploads/temporary/...` + token URL, or any absolute `http(s)` URL pointing at a Zulip + server. Unlike `call_endpoint()` (which always requests a + `/api/v1/` path), this method is not tied to the API prefix: + `GET /api/v1/user_uploads/...` returns a JSON temporary-access + URL rather than the file, so downloads have to go to the + unprefixed path. + + `timeout` is the request timeout in seconds; defaults to 90, + like long-polling requests. + + Returns the file contents as bytes. + """ + if url_or_path.startswith(("http://", "https://")): + url = url_or_path + elif url_or_path.startswith("/"): + # Absolute path on the Zulip server root, e.g. /user_uploads/... + server_root = self.base_url.removesuffix("/api/") + url = server_root + url_or_path + else: + raise ZulipError( + f"fetch_upload() requires an absolute URL or a server-root-relative path, got {url_or_path!r}" + ) + self.ensure_session() + assert self.session is not None + res = self.session.get(url, timeout=timeout if timeout is not None else 90.0) + if res.status_code != 200: + raise ZulipError( + f"Failed to fetch upload {url_or_path!r}: server returned {res.status_code}" + ) + return res.content + def get_attachments(self) -> Dict[str, Any]: """ Example usage: From 150a60372c9cc5f46104c44d73822b0b23f8974e Mon Sep 17 00:00:00 2001 From: Harsh Vardhan <184601513+harshvardhan178251@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:27:20 +0530 Subject: [PATCH 2/3] zulip: Reject fetch_upload() URLs off the Zulip origin. --- zulip/tests/test_fetch_upload.py | 18 ++++++++++++++++++ zulip/zulip/__init__.py | 21 +++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/zulip/tests/test_fetch_upload.py b/zulip/tests/test_fetch_upload.py index 82b5c747c..57b884c55 100644 --- a/zulip/tests/test_fetch_upload.py +++ b/zulip/tests/test_fetch_upload.py @@ -40,6 +40,24 @@ def test_absolute_url_passthrough(self) -> None: ) assert result == b"file contents" + def test_absolute_url_on_other_origin_rejected(self) -> None: + client = make_client() + client.ensure_session() + assert client.session is not None + with patch.object(client.session, "get") as mock_get: + with pytest.raises(ZulipError): + client.fetch_upload("https://evil.example.com/user_uploads/1/file.pdf") + mock_get.assert_not_called() + + def test_absolute_url_with_different_scheme_rejected(self) -> None: + client = make_client() + client.ensure_session() + assert client.session is not None + with patch.object(client.session, "get") as mock_get: + with pytest.raises(ZulipError): + client.fetch_upload("http://zulip.example.com/user_uploads/1/file.pdf") + mock_get.assert_not_called() + def test_root_relative_path_resolved_against_server_root(self) -> None: client = make_client() client.ensure_session() diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index 7609f0a47..6e1cac486 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -882,23 +882,36 @@ def fetch_upload(self, url_or_path: str, timeout: Optional[float] = None) -> byt Accepts the `/user_uploads/...` path returned as the `url` field by `upload_file()`, a `/user_uploads/temporary/...` - token URL, or any absolute `http(s)` URL pointing at a Zulip - server. Unlike `call_endpoint()` (which always requests a - `/api/v1/` path), this method is not tied to the API prefix: + token URL, or an absolute `http(s)` URL on this Zulip server. + Unlike `call_endpoint()` (which always requests a `/api/v1/` + path), this method is not tied to the API prefix: `GET /api/v1/user_uploads/...` returns a JSON temporary-access URL rather than the file, so downloads have to go to the unprefixed path. + Absolute URLs on other origins are rejected, because the + authenticated session would otherwise send the bot's API key + to a third-party host. + `timeout` is the request timeout in seconds; defaults to 90, like long-polling requests. Returns the file contents as bytes. """ + server_root = self.base_url.removesuffix("/api/") + server_origin = urllib.parse.urlsplit(server_root) if url_or_path.startswith(("http://", "https://")): + parsed = urllib.parse.urlsplit(url_or_path) + if (parsed.scheme.lower(), parsed.netloc.lower()) != ( + server_origin.scheme.lower(), + server_origin.netloc.lower(), + ): + raise ZulipError( + f"fetch_upload() requires an absolute URL on {server_root!r}; refusing to send credentials elsewhere" + ) url = url_or_path elif url_or_path.startswith("/"): # Absolute path on the Zulip server root, e.g. /user_uploads/... - server_root = self.base_url.removesuffix("/api/") url = server_root + url_or_path else: raise ZulipError( From b14867e57c77c468f717dbe94f1094df12495e3b Mon Sep 17 00:00:00 2001 From: Harsh Vardhan <184601513+harshvardhan178251@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:13:04 +0530 Subject: [PATCH 3/3] zulip: Harden fetch_upload() URL handling. Incorporate ideas from Johnboard-cmyk's closed PR #950: strip a leading /api/v1/ prefix (people paste the wrong URL shape and hit the JSON temporary-URL instead of the file), accept bare relative paths under user_uploads/, and wrap connection failures in UnrecoverableNetworkError for consistency with do_api_query(). --- zulip/tests/test_fetch_upload.py | 70 ++++++++++++++++++++++++++++++-- zulip/zulip/__init__.py | 41 +++++++++++++------ 2 files changed, 95 insertions(+), 16 deletions(-) diff --git a/zulip/tests/test_fetch_upload.py b/zulip/tests/test_fetch_upload.py index 57b884c55..8bdb784bc 100644 --- a/zulip/tests/test_fetch_upload.py +++ b/zulip/tests/test_fetch_upload.py @@ -1,8 +1,9 @@ from unittest.mock import MagicMock, patch import pytest +import requests -from zulip import Client, ZulipError +from zulip import Client, UnrecoverableNetworkError, ZulipError def make_client() -> Client: @@ -40,6 +41,33 @@ def test_absolute_url_passthrough(self) -> None: ) assert result == b"file contents" + def test_absolute_url_api_v1_prefix_stripped(self) -> None: + client = make_client() + client.ensure_session() + assert client.session is not None + with patch.object( + client.session, "get", return_value=mock_response(200, b"file contents") + ) as mock_get: + client.fetch_upload( + "https://zulip.example.com/api/v1/user_uploads/1/file.pdf?token=xyz" + ) + mock_get.assert_called_once_with( + "https://zulip.example.com/user_uploads/1/file.pdf?token=xyz", timeout=90.0 + ) + + def test_temporary_token_url(self) -> None: + client = make_client() + client.ensure_session() + assert client.session is not None + with patch.object( + client.session, "get", return_value=mock_response(200, b"file contents") + ) as mock_get: + client.fetch_upload("/user_uploads/temporary/token123/file.pdf") + mock_get.assert_called_once_with( + "https://zulip.example.com/user_uploads/temporary/token123/file.pdf", + timeout=90.0, + ) + def test_absolute_url_on_other_origin_rejected(self) -> None: client = make_client() client.ensure_session() @@ -83,10 +111,34 @@ def test_custom_timeout(self) -> None: "https://zulip.example.com/user_uploads/1/file.pdf", timeout=5.0 ) - def test_non_root_relative_path_rejected(self) -> None: + def test_path_without_leading_slash_accepted(self) -> None: client = make_client() - with pytest.raises(ZulipError): + client.ensure_session() + assert client.session is not None + with patch.object( + client.session, "get", return_value=mock_response(200, b"file contents") + ) as mock_get: client.fetch_upload("user_uploads/1/file.pdf") + mock_get.assert_called_once_with( + "https://zulip.example.com/user_uploads/1/file.pdf", timeout=90.0 + ) + + def test_path_api_v1_prefix_stripped(self) -> None: + client = make_client() + client.ensure_session() + assert client.session is not None + with patch.object( + client.session, "get", return_value=mock_response(200, b"file contents") + ) as mock_get: + client.fetch_upload("/api/v1/user_uploads/1/file.pdf") + mock_get.assert_called_once_with( + "https://zulip.example.com/user_uploads/1/file.pdf", timeout=90.0 + ) + + def test_non_user_uploads_path_rejected(self) -> None: + client = make_client() + with pytest.raises(ZulipError): + client.fetch_upload("random/path/here") def test_non_200_raises(self) -> None: client = make_client() @@ -96,3 +148,15 @@ def test_non_200_raises(self) -> None: client.session, "get", return_value=mock_response(403, b"") ), pytest.raises(ZulipError): client.fetch_upload("/user_uploads/1/file.pdf") + + def test_connection_error_raises_unrecoverable(self) -> None: + client = make_client() + client.ensure_session() + assert client.session is not None + with patch.object( + client.session, + "get", + side_effect=requests.exceptions.ConnectionError("boom"), + ): + with pytest.raises(UnrecoverableNetworkError): + client.fetch_upload("/user_uploads/1/file.pdf") diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index 6e1cac486..04352b1d1 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -880,11 +880,10 @@ def fetch_upload(self, url_or_path: str, timeout: Optional[float] = None) -> byt """ Download an uploaded file, using API authentication. - Accepts the `/user_uploads/...` path returned as the `url` - field by `upload_file()`, a `/user_uploads/temporary/...` + Accepts a path (e.g. "/user_uploads/1/abc123/file.png", as it + appears in message content), a `/user_uploads/temporary/...` token URL, or an absolute `http(s)` URL on this Zulip server. - Unlike `call_endpoint()` (which always requests a `/api/v1/` - path), this method is not tied to the API prefix: + A leading `/api/v1/` prefix is stripped: `GET /api/v1/user_uploads/...` returns a JSON temporary-access URL rather than the file, so downloads have to go to the unprefixed path. @@ -896,7 +895,9 @@ def fetch_upload(self, url_or_path: str, timeout: Optional[float] = None) -> byt `timeout` is the request timeout in seconds; defaults to 90, like long-polling requests. - Returns the file contents as bytes. + Returns the file contents as bytes. Raises UnrecoverableNetworkError + if the request cannot be made, and ZulipError if the server + returns an HTTP error status. """ server_root = self.base_url.removesuffix("/api/") server_origin = urllib.parse.urlsplit(server_root) @@ -909,17 +910,31 @@ def fetch_upload(self, url_or_path: str, timeout: Optional[float] = None) -> byt raise ZulipError( f"fetch_upload() requires an absolute URL on {server_root!r}; refusing to send credentials elsewhere" ) - url = url_or_path - elif url_or_path.startswith("/"): - # Absolute path on the Zulip server root, e.g. /user_uploads/... - url = server_root + url_or_path - else: - raise ZulipError( - f"fetch_upload() requires an absolute URL or a server-root-relative path, got {url_or_path!r}" + path = parsed.path + if path.startswith("/api/v1/"): + path = path[len("/api/v1"):] + url = urllib.parse.urlunsplit( + (parsed.scheme, parsed.netloc, path, parsed.query, "") ) + else: + # Path on this Zulip server (leading/trailing slashes and a + # mistaken /api/v1/ prefix are tolerated). + path = url_or_path.lstrip("/") + if path.startswith("api/v1/"): + path = path[len("api/v1/"):] + if not path.startswith("user_uploads/"): + raise ZulipError( + f"fetch_upload() requires an absolute URL or a path under /user_uploads/, got {url_or_path!r}" + ) + url = server_root + "/" + path self.ensure_session() assert self.session is not None - res = self.session.get(url, timeout=timeout if timeout is not None else 90.0) + try: + res = self.session.get(url, timeout=timeout if timeout is not None else 90.0) + except requests.exceptions.ConnectionError as e: + raise UnrecoverableNetworkError( + "cannot connect to server " + self.base_url + ) from e if res.status_code != 200: raise ZulipError( f"Failed to fetch upload {url_or_path!r}: server returned {res.status_code}"