Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Be more lenient when parsing the version for oEmbed responses. #11065

Merged
merged 4 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/11065.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Be more lenient when parsing oEmbed response versions.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unsure if this should be a bugfix or misc?

1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ files =
tests/rest/client/test_login.py,
tests/rest/client/test_auth.py,
tests/rest/media/v1/test_filepath.py,
tests/rest/media/v1/test_oembed.py,
tests/storage/test_state.py,
tests/storage/test_user_directory.py,
tests/util/test_itertools.py,
Expand Down
13 changes: 6 additions & 7 deletions synapse/rest/media/v1/oembed.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import attr

from synapse.http.client import SimpleHttpClient
from synapse.types import JsonDict
from synapse.util import json_decoder

Expand Down Expand Up @@ -48,7 +47,7 @@ class OEmbedProvider:
requesting/parsing oEmbed content.
"""

def __init__(self, hs: "HomeServer", client: SimpleHttpClient):
def __init__(self, hs: "HomeServer"):
self._oembed_patterns = {}
for oembed_endpoint in hs.config.oembed.oembed_patterns:
api_endpoint = oembed_endpoint.api_endpoint
Expand All @@ -69,7 +68,6 @@ def __init__(self, hs: "HomeServer", client: SimpleHttpClient):
# Iterate through each URL pattern and point it to the endpoint.
for pattern in oembed_endpoint.url_patterns:
self._oembed_patterns[pattern] = api_endpoint
self._client = client

def get_oembed_url(self, url: str) -> Optional[str]:
"""
Expand Down Expand Up @@ -139,10 +137,11 @@ def parse_oembed_response(self, url: str, raw_body: bytes) -> OEmbedResult:
# oEmbed responses *must* be UTF-8 according to the spec.
oembed = json_decoder.decode(raw_body.decode("utf-8"))

# Ensure there's a version of 1.0.
oembed_version = oembed["version"]
if oembed_version != "1.0":
raise RuntimeError(f"Invalid version: {oembed_version}")
# The version is a required string field, but not always provided,
# or sometimes provided as a float. Be lenient.
oembed_version = oembed.get("version", "1.0")
if oembed_version != "1.0" and oembed_version != 1:
raise RuntimeError(f"Invalid oEmbed version: {oembed_version}")

# Ensure the cache age is None or an int.
cache_age = oembed.get("cache_age")
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/media/v1/preview_url_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __init__(
self.primary_base_path = media_repo.primary_base_path
self.media_storage = media_storage

self._oembed = OEmbedProvider(hs, self.client)
self._oembed = OEmbedProvider(hs)

# We run the background jobs if we're the instance specified (or no
# instance is specified, where we assume there is only one instance
Expand Down
51 changes: 51 additions & 0 deletions tests/rest/media/v1/test_oembed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json

from twisted.test.proto_helpers import MemoryReactor

from synapse.rest.media.v1.oembed import OEmbedProvider
from synapse.server import HomeServer
from synapse.types import JsonDict
from synapse.util import Clock

from tests.unittest import HomeserverTestCase


class OEmbedTests(HomeserverTestCase):
def prepare(self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer):
self.oembed = OEmbedProvider(homeserver)

def parse_response(self, response: JsonDict):
return self.oembed.parse_oembed_response(
"https://test", json.dumps(response).encode("utf-8")
)

def test_version(self):
"""Accept versions that are similar to 1.0 as a string or int (or missing)."""
for version in ("1.0", 1.0, 1):
result = self.parse_response({"version": version, "type": "link"})
# An empty Open Graph response is an error, ensure the URL is included.
self.assertIn("og:url", result.open_graph_result)

# A missing version should be treated as 1.0.
result = self.parse_response({"type": "link"})
self.assertIn("og:url", result.open_graph_result)

# Invalid versions should be rejected.
for version in ("2.0", "1", 1.1, 0, None, {}, []):
result = self.parse_response({"version": version, "type": "link"})
# An empty Open Graph response is an error, ensure the URL is included.
self.assertEqual({}, result.open_graph_result)