2021-08-31 18:37:07 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2021-08-31 18:37:07 -04:00
|
|
|
#
|
|
|
|
#
|
2023-01-09 09:22:02 -05:00
|
|
|
import html
|
2021-08-31 18:37:07 -04:00
|
|
|
import logging
|
2021-09-21 12:09:57 -04:00
|
|
|
import urllib.parse
|
2023-05-31 13:06:57 -04:00
|
|
|
from typing import TYPE_CHECKING, List, Optional, cast
|
2021-08-31 18:37:07 -04:00
|
|
|
|
|
|
|
import attr
|
|
|
|
|
2023-02-27 08:26:05 -05:00
|
|
|
from synapse.media.preview_html import parse_html_description
|
2021-09-21 12:09:57 -04:00
|
|
|
from synapse.types import JsonDict
|
|
|
|
from synapse.util import json_decoder
|
2021-08-31 18:37:07 -04:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2021-09-22 09:45:20 -04:00
|
|
|
from lxml import etree
|
|
|
|
|
2021-08-31 18:37:07 -04:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-09-21 12:09:57 -04:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
2021-08-31 18:37:07 -04:00
|
|
|
class OEmbedResult:
|
2021-09-21 12:09:57 -04:00
|
|
|
# The Open Graph result (converted from the oEmbed result).
|
|
|
|
open_graph_result: JsonDict
|
2022-01-18 13:20:24 -05:00
|
|
|
# The author_name of the oEmbed result
|
|
|
|
author_name: Optional[str]
|
2021-09-22 09:45:20 -04:00
|
|
|
# Number of milliseconds to cache the content, according to the oEmbed response.
|
2021-09-21 12:09:57 -04:00
|
|
|
#
|
|
|
|
# This will be None if no cache-age is provided in the oEmbed response (or
|
|
|
|
# if the oEmbed response cannot be turned into an Open Graph response).
|
|
|
|
cache_age: Optional[int]
|
2021-08-31 18:37:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
class OEmbedProvider:
|
|
|
|
"""
|
|
|
|
A helper for accessing oEmbed content.
|
|
|
|
|
|
|
|
It can be used to check if a URL should be accessed via oEmbed and for
|
|
|
|
requesting/parsing oEmbed content.
|
|
|
|
"""
|
|
|
|
|
2021-10-13 07:00:07 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-08-31 18:37:07 -04:00
|
|
|
self._oembed_patterns = {}
|
|
|
|
for oembed_endpoint in hs.config.oembed.oembed_patterns:
|
2021-09-08 07:17:52 -04:00
|
|
|
api_endpoint = oembed_endpoint.api_endpoint
|
|
|
|
|
|
|
|
# Only JSON is supported at the moment. This could be declared in
|
|
|
|
# the formats field. Otherwise, if the endpoint ends in .xml assume
|
|
|
|
# it doesn't support JSON.
|
|
|
|
if (
|
|
|
|
oembed_endpoint.formats is not None
|
|
|
|
and "json" not in oembed_endpoint.formats
|
|
|
|
) or api_endpoint.endswith(".xml"):
|
|
|
|
logger.info(
|
|
|
|
"Ignoring oEmbed endpoint due to not supporting JSON: %s",
|
|
|
|
api_endpoint,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Iterate through each URL pattern and point it to the endpoint.
|
2021-08-31 18:37:07 -04:00
|
|
|
for pattern in oembed_endpoint.url_patterns:
|
2021-09-08 07:17:52 -04:00
|
|
|
self._oembed_patterns[pattern] = api_endpoint
|
2021-08-31 18:37:07 -04:00
|
|
|
|
|
|
|
def get_oembed_url(self, url: str) -> Optional[str]:
|
|
|
|
"""
|
|
|
|
Check whether the URL should be downloaded as oEmbed content instead.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
url: The URL to check.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A URL to use instead or None if the original URL should be used.
|
|
|
|
"""
|
|
|
|
for url_pattern, endpoint in self._oembed_patterns.items():
|
|
|
|
if url_pattern.fullmatch(url):
|
2021-09-21 12:09:57 -04:00
|
|
|
# TODO Specify max height / width.
|
|
|
|
|
|
|
|
# Note that only the JSON format is supported, some endpoints want
|
|
|
|
# this in the URL, others want it as an argument.
|
|
|
|
endpoint = endpoint.replace("{format}", "json")
|
|
|
|
|
|
|
|
args = {"url": url, "format": "json"}
|
|
|
|
query_str = urllib.parse.urlencode(args, True)
|
|
|
|
return f"{endpoint}?{query_str}"
|
2021-08-31 18:37:07 -04:00
|
|
|
|
|
|
|
# No match.
|
|
|
|
return None
|
|
|
|
|
2023-05-31 13:06:57 -04:00
|
|
|
def autodiscover_from_html(self, tree: "etree._Element") -> Optional[str]:
|
2021-10-08 14:14:42 -04:00
|
|
|
"""
|
|
|
|
Search an HTML document for oEmbed autodiscovery information.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
tree: The parsed HTML body.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The URL to use for oEmbed information, or None if no URL was found.
|
|
|
|
"""
|
|
|
|
# Search for link elements with the proper rel and type attributes.
|
2023-05-31 13:06:57 -04:00
|
|
|
# Cast: the type returned by xpath depends on the xpath expression: mypy can't deduce this.
|
|
|
|
for tag in cast(
|
|
|
|
List["etree._Element"],
|
|
|
|
tree.xpath("//link[@rel='alternate'][@type='application/json+oembed']"),
|
2021-10-08 14:14:42 -04:00
|
|
|
):
|
|
|
|
if "href" in tag.attrib:
|
2023-05-31 13:06:57 -04:00
|
|
|
return cast(str, tag.attrib["href"])
|
2021-10-08 14:14:42 -04:00
|
|
|
|
|
|
|
# Some providers (e.g. Flickr) use alternative instead of alternate.
|
2023-05-31 13:06:57 -04:00
|
|
|
# Cast: the type returned by xpath depends on the xpath expression: mypy can't deduce this.
|
|
|
|
for tag in cast(
|
|
|
|
List["etree._Element"],
|
|
|
|
tree.xpath("//link[@rel='alternative'][@type='application/json+oembed']"),
|
2021-10-08 14:14:42 -04:00
|
|
|
):
|
|
|
|
if "href" in tag.attrib:
|
2023-05-31 13:06:57 -04:00
|
|
|
return cast(str, tag.attrib["href"])
|
2021-10-08 14:14:42 -04:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2021-09-21 12:09:57 -04:00
|
|
|
def parse_oembed_response(self, url: str, raw_body: bytes) -> OEmbedResult:
|
2021-08-31 18:37:07 -04:00
|
|
|
"""
|
2021-09-21 12:09:57 -04:00
|
|
|
Parse the oEmbed response into an Open Graph response.
|
2021-08-31 18:37:07 -04:00
|
|
|
|
|
|
|
Args:
|
2021-09-21 12:09:57 -04:00
|
|
|
url: The URL which is being previewed (not the one which was
|
|
|
|
requested).
|
|
|
|
raw_body: The oEmbed response as JSON encoded as bytes.
|
2021-08-31 18:37:07 -04:00
|
|
|
|
|
|
|
Returns:
|
2021-09-21 12:09:57 -04:00
|
|
|
json-encoded Open Graph data
|
2021-08-31 18:37:07 -04:00
|
|
|
"""
|
2021-09-08 07:17:52 -04:00
|
|
|
|
2021-09-21 12:09:57 -04:00
|
|
|
try:
|
|
|
|
# oEmbed responses *must* be UTF-8 according to the spec.
|
|
|
|
oembed = json_decoder.decode(raw_body.decode("utf-8"))
|
2022-10-07 09:29:43 -04:00
|
|
|
except ValueError:
|
|
|
|
return OEmbedResult({}, None, None)
|
2021-08-31 18:37:07 -04:00
|
|
|
|
2022-10-07 09:29:43 -04:00
|
|
|
# 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:
|
|
|
|
return OEmbedResult({}, None, None)
|
2021-08-31 18:37:07 -04:00
|
|
|
|
2022-10-07 09:29:43 -04:00
|
|
|
# Attempt to parse the cache age, if possible.
|
|
|
|
try:
|
|
|
|
cache_age = int(oembed.get("cache_age")) * 1000
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
# If the cache age cannot be parsed (e.g. wrong type or invalid
|
|
|
|
# string), ignore it.
|
|
|
|
cache_age = None
|
2022-01-18 13:20:24 -05:00
|
|
|
|
2022-10-07 09:29:43 -04:00
|
|
|
# The oEmbed response converted to Open Graph.
|
|
|
|
open_graph_response: JsonDict = {"og:url": url}
|
2021-08-31 18:37:07 -04:00
|
|
|
|
2022-10-07 09:29:43 -04:00
|
|
|
title = oembed.get("title")
|
|
|
|
if title and isinstance(title, str):
|
2023-01-09 09:22:02 -05:00
|
|
|
# A common WordPress plug-in seems to incorrectly escape entities
|
|
|
|
# in the oEmbed response.
|
|
|
|
open_graph_response["og:title"] = html.unescape(title)
|
2021-09-21 12:09:57 -04:00
|
|
|
|
2022-10-07 09:29:43 -04:00
|
|
|
author_name = oembed.get("author_name")
|
|
|
|
if not isinstance(author_name, str):
|
|
|
|
author_name = None
|
2021-08-31 18:37:07 -04:00
|
|
|
|
2022-10-07 09:29:43 -04:00
|
|
|
# Use the provider name and as the site.
|
|
|
|
provider_name = oembed.get("provider_name")
|
|
|
|
if provider_name and isinstance(provider_name, str):
|
|
|
|
open_graph_response["og:site_name"] = provider_name
|
|
|
|
|
|
|
|
# If a thumbnail exists, use it. Note that dimensions will be calculated later.
|
|
|
|
thumbnail_url = oembed.get("thumbnail_url")
|
|
|
|
if thumbnail_url and isinstance(thumbnail_url, str):
|
|
|
|
open_graph_response["og:image"] = thumbnail_url
|
|
|
|
|
|
|
|
# Process each type separately.
|
|
|
|
oembed_type = oembed.get("type")
|
|
|
|
if oembed_type == "rich":
|
2023-01-09 09:22:02 -05:00
|
|
|
html_str = oembed.get("html")
|
|
|
|
if isinstance(html_str, str):
|
|
|
|
calc_description_and_urls(open_graph_response, html_str)
|
2022-10-07 09:29:43 -04:00
|
|
|
|
|
|
|
elif oembed_type == "photo":
|
|
|
|
# If this is a photo, use the full image, not the thumbnail.
|
|
|
|
url = oembed.get("url")
|
|
|
|
if url and isinstance(url, str):
|
|
|
|
open_graph_response["og:image"] = url
|
|
|
|
|
|
|
|
elif oembed_type == "video":
|
|
|
|
open_graph_response["og:type"] = "video.other"
|
2023-01-09 09:22:02 -05:00
|
|
|
html_str = oembed.get("html")
|
|
|
|
if html_str and isinstance(html_str, str):
|
2021-09-22 09:45:20 -04:00
|
|
|
calc_description_and_urls(open_graph_response, oembed["html"])
|
2022-10-07 09:29:43 -04:00
|
|
|
for size in ("width", "height"):
|
|
|
|
val = oembed.get(size)
|
2023-08-29 09:41:43 -04:00
|
|
|
if type(val) is int: # noqa: E721
|
2022-10-07 09:29:43 -04:00
|
|
|
open_graph_response[f"og:video:{size}"] = val
|
2021-09-22 09:45:20 -04:00
|
|
|
|
2022-10-07 09:29:43 -04:00
|
|
|
elif oembed_type == "link":
|
|
|
|
open_graph_response["og:type"] = "website"
|
2021-08-31 18:37:07 -04:00
|
|
|
|
2022-10-07 09:29:43 -04:00
|
|
|
else:
|
|
|
|
logger.warning("Unknown oEmbed type: %s", oembed_type)
|
2021-08-31 18:37:07 -04:00
|
|
|
|
2022-01-18 13:20:24 -05:00
|
|
|
return OEmbedResult(open_graph_response, author_name, cache_age)
|
2021-08-31 18:37:07 -04:00
|
|
|
|
|
|
|
|
2023-05-31 13:06:57 -04:00
|
|
|
def _fetch_urls(tree: "etree._Element", tag_name: str) -> List[str]:
|
2021-09-22 09:45:20 -04:00
|
|
|
results = []
|
2023-05-31 13:06:57 -04:00
|
|
|
# Cast: the type returned by xpath depends on the xpath expression: mypy can't deduce this.
|
|
|
|
for tag in cast(List["etree._Element"], tree.xpath("//*/" + tag_name)):
|
2021-09-22 09:45:20 -04:00
|
|
|
if "src" in tag.attrib:
|
2023-05-31 13:06:57 -04:00
|
|
|
results.append(cast(str, tag.attrib["src"]))
|
2021-09-22 09:45:20 -04:00
|
|
|
return results
|
|
|
|
|
|
|
|
|
2021-09-21 12:09:57 -04:00
|
|
|
def calc_description_and_urls(open_graph_response: JsonDict, html_body: str) -> None:
|
|
|
|
"""
|
|
|
|
Calculate description for an HTML document.
|
|
|
|
|
|
|
|
This uses lxml to convert the HTML document into plaintext. If errors
|
|
|
|
occur during processing of the document, an empty response is returned.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
open_graph_response: The current Open Graph summary. This is updated with additional fields.
|
|
|
|
html_body: The HTML document, as bytes.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The summary
|
|
|
|
"""
|
|
|
|
# If there's no body, nothing useful is going to be found.
|
|
|
|
if not html_body:
|
|
|
|
return
|
|
|
|
|
|
|
|
from lxml import etree
|
|
|
|
|
|
|
|
# Create an HTML parser. If this fails, log and return no metadata.
|
|
|
|
parser = etree.HTMLParser(recover=True, encoding="utf-8")
|
|
|
|
|
|
|
|
# Attempt to parse the body. If this fails, log and return no metadata.
|
2023-05-31 13:06:57 -04:00
|
|
|
# TODO Develop of lxml-stubs has this correct.
|
|
|
|
tree = etree.fromstring(html_body, parser) # type: ignore[arg-type]
|
2021-09-21 12:09:57 -04:00
|
|
|
|
|
|
|
# The data was successfully parsed, but no tree was found.
|
|
|
|
if tree is None:
|
2023-05-31 13:06:57 -04:00
|
|
|
return # type: ignore[unreachable]
|
2021-09-21 12:09:57 -04:00
|
|
|
|
2021-09-22 09:45:20 -04:00
|
|
|
# Attempt to find interesting URLs (images, videos, embeds).
|
|
|
|
if "og:image" not in open_graph_response:
|
|
|
|
image_urls = _fetch_urls(tree, "img")
|
|
|
|
if image_urls:
|
|
|
|
open_graph_response["og:image"] = image_urls[0]
|
|
|
|
|
|
|
|
video_urls = _fetch_urls(tree, "video") + _fetch_urls(tree, "embed")
|
|
|
|
if video_urls:
|
|
|
|
open_graph_response["og:video"] = video_urls[0]
|
|
|
|
|
2021-12-13 12:55:07 -05:00
|
|
|
description = parse_html_description(tree)
|
2021-09-21 12:09:57 -04:00
|
|
|
if description:
|
|
|
|
open_graph_response["og:description"] = description
|