From d5ba94082a2f38aa852285f3ae62c184c0fe4358 Mon Sep 17 00:00:00 2001 From: Wesley Aptekar-Cassels Date: Fri, 28 May 2021 11:04:15 -0700 Subject: [PATCH] markdown: Increase max rendered message length to 1MB. This should help with #17425, where messages with lots of LaTeX are lost, due to the large expansion factor. This isn't a total fix for this - large messages with lots of LaTeX can still end up larger than 1MB, and rendering could timeout, but this fix should help significantly. 1MB is still small enough that I don't expect we'll run into any DOS problems - my testing didn't show any problems rendering messages that contain ~1MB of LaTeX. --- zerver/lib/markdown/__init__.py | 4 ++-- zerver/tests/test_markdown.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py index 69c9b99241..a50b0639e1 100644 --- a/zerver/lib/markdown/__init__.py +++ b/zerver/lib/markdown/__init__.py @@ -2612,9 +2612,9 @@ def do_convert( # rest of the codebase from any bugs where we end up rendering # something huge. MAX_MESSAGE_LENGTH = settings.MAX_MESSAGE_LENGTH - if len(rendered_content) > MAX_MESSAGE_LENGTH * 10: + if len(rendered_content) > MAX_MESSAGE_LENGTH * 100: raise MarkdownRenderingException( - f"Rendered content exceeds {MAX_MESSAGE_LENGTH * 10} characters (message {logging_message_id})" + f"Rendered content exceeds {MAX_MESSAGE_LENGTH * 100} characters (message {logging_message_id})" ) return rendered_content except Exception: diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py index edff75c04f..3230bfb497 100644 --- a/zerver/tests/test_markdown.py +++ b/zerver/tests/test_markdown.py @@ -2749,9 +2749,9 @@ class MarkdownErrorTests(ZulipTestCase): @override_settings(MAX_MESSAGE_LENGTH=10) def test_ultra_long_rendering(self) -> None: - """A rendered message with an ultra-long length (> 10 * MAX_MESSAGE_LENGTH) + """A rendered message with an ultra-long length (> 100 * MAX_MESSAGE_LENGTH) throws an exception""" - msg = "mock rendered message\n" * settings.MAX_MESSAGE_LENGTH + msg = "mock rendered message\n" * 10 * settings.MAX_MESSAGE_LENGTH with mock.patch("zerver.lib.markdown.timeout", return_value=msg), mock.patch( "zerver.lib.markdown.markdown_logger"