Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

uploads: Reject non-UTF-8 temporary access tokens.

sdivyanshu9054ff1b9093ba
diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py
index b4bc9fb56fd..18a0f5c8478 100644
--- a/zerver/tests/test_upload.py
+++ b/zerver/tests/test_upload.py
@@ -395,8 +395,11 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
self.assertEqual(response.status_code, 404)
def test_serve_local_file_unauthed_invalid_token(self) -> None:
- result = self.client_get("/user_uploads/temporary/badtoken/file.png")
- self.assert_json_error(result, "Invalid token")
+ # "FF" is valid hexadecimal, unlike "badtoken", but decodes to invalid UTF-8.
+ for token in ["badtoken", "FF"]:
+ with self.subTest(token=token):
+ result = self.client_get(f"/user_uploads/temporary/{token}/file.png")
+ self.assert_json_error(result, "Invalid token")
def test_serve_local_file_unauthed_altered_filename(self) -> None:
self.login("hamlet")
diff --git a/zerver/views/upload.py b/zerver/views/upload.py
index adcbe10c38a..b218a52f04c 100644
--- a/zerver/views/upload.py
+++ b/zerver/views/upload.py
@@ -395,7 +395,7 @@ def get_file_path_id_from_token(token: str) -> str | None:
path_id = signer.unsign(
signed_data, max_age=timedelta(seconds=settings.SIGNED_ACCESS_TOKEN_VALIDITY_IN_SECONDS)
)
- except (BadSignature, binascii.Error):
+ except (BadSignature, UnicodeDecodeError, binascii.Error):
return None
return path_id