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 1 commit
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
Prev Previous commit
Next Next commit
Support more lenient versioning for oEmbed responses.
  • Loading branch information
clokep committed Oct 12, 2021
commit 58673d4719d4d2656d9489a71a82eacb6e8f4d88
9 changes: 5 additions & 4 deletions synapse/rest/media/v1/oembed.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,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
17 changes: 14 additions & 3 deletions tests/rest/media/v1/test_oembed.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ def parse_response(self, response: JsonDict):
)

def test_version(self):
"""Accept versions are 1.0."""
result = self.parse_response({"version": "1.0", "type": "link"})
# An empty Open Graph response is an error, ensure the URL is included.
"""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)