diff --git a/zerver/lib/url_preview/oembed.py b/zerver/lib/url_preview/oembed.py index b1b0872c6f..16831f50a2 100644 --- a/zerver/lib/url_preview/oembed.py +++ b/zerver/lib/url_preview/oembed.py @@ -1,12 +1,13 @@ from typing import Optional, Dict, Any from pyoembed import oEmbed, PyOembedException +import json def get_oembed_data(url: str, maxwidth: Optional[int]=640, maxheight: Optional[int]=480) -> Optional[Dict[str, Any]]: try: data = oEmbed(url, maxwidth=maxwidth, maxheight=maxheight) - except PyOembedException: + except (PyOembedException, json.decoder.JSONDecodeError): return None oembed_resource_type = data.get('type', '') diff --git a/zerver/tests/test_link_embed.py b/zerver/tests/test_link_embed.py index 78e9692f72..190cc3ef25 100644 --- a/zerver/tests/test_link_embed.py +++ b/zerver/tests/test_link_embed.py @@ -114,6 +114,16 @@ class OembedTestCase(ZulipTestCase): data = get_oembed_data(url) self.assertIsNone(data) + @mock.patch('pyoembed.requests.get') + def test_invalid_json_in_response(self, get: Any) -> None: + get.return_value = response = mock.Mock() + response.headers = {'content-type': 'application/json'} + response.ok = True + response.text = '{invalid json}' + url = 'http://instagram.com/p/BLtI2WdAymy' + data = get_oembed_data(url) + self.assertIsNone(data) + def test_oembed_html(self) -> None: html = '' stripped_html = strip_cdata(html)