From 5d246548128253b07f24b4bfe3d1cbdcdcc62af1 Mon Sep 17 00:00:00 2001 From: MomentQYC <62551256+MomentQYC@users.noreply.github.com> Date: Wed, 27 Nov 2024 10:03:07 +0800 Subject: [PATCH 1/2] feat: support for setting UA for URL previews --- docs/usage/configuration/config_documentation.md | 11 +++++++++++ synapse/config/server.py | 7 +++++++ synapse/media/url_previewer.py | 5 ++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/usage/configuration/config_documentation.md b/docs/usage/configuration/config_documentation.md index 7a48d76bbb1..571631aca6f 100644 --- a/docs/usage/configuration/config_documentation.md +++ b/docs/usage/configuration/config_documentation.md @@ -774,6 +774,17 @@ Example configuration: ```yaml max_event_delay_duration: 24h ``` +--- +### `url_preview_user_agent` + +Setting the User-Agent for URL previews. + +Defaults to `Synapse (bot; +https://github.com/matrix-org/synapse)`. + +Example configuration: +```yaml +url_preview_user_agent: "Hello Matrix" +``` ## Homeserver blocking Useful options for Synapse admins. diff --git a/synapse/config/server.py b/synapse/config/server.py index ad7331de428..e5b3fe8aa91 100644 --- a/synapse/config/server.py +++ b/synapse/config/server.py @@ -788,6 +788,13 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None: else: self.max_event_delay_ms = None + self.url_preview_user_agent: str = ( + config.get("url_preview_user_agent") + or "Synapse (bot; +https://github.com/matrix-org/synapse)" + ) + if len(self.url_preview_user_agent.strip()) == 0: + raise ConfigError("The 'url_preview_user_agent' must be a valid User-Agent") + def has_tls_listener(self) -> bool: return any(listener.is_tls() for listener in self.listeners) diff --git a/synapse/media/url_previewer.py b/synapse/media/url_previewer.py index 2e65a047897..9935aca8e8c 100644 --- a/synapse/media/url_previewer.py +++ b/synapse/media/url_previewer.py @@ -168,6 +168,7 @@ def __init__( ): self.clock = hs.get_clock() self.filepaths = media_repo.filepaths + self.hs = hs self.max_spider_size = hs.config.media.max_spider_size self.server_name = hs.hostname self.store = hs.get_datastores().main @@ -464,9 +465,7 @@ async def _download_url(self, url: str, output_stream: BinaryIO) -> DownloadResu # Use a custom user agent for the preview because some sites will only return # Open Graph metadata to crawler user agents. Omit the Synapse version # string to avoid leaking information. - b"User-Agent": [ - "Synapse (bot; +https://github.com/matrix-org/synapse)" - ], + b"User-Agent": [self.hs.config.server.url_preview_user_agent], }, is_allowed_content_type=_is_previewable, ) From e18759a3e8053b1a5b550a67794e8dee95c1d644 Mon Sep 17 00:00:00 2001 From: MomentQYC <62551256+MomentQYC@users.noreply.github.com> Date: Wed, 27 Nov 2024 10:14:51 +0800 Subject: [PATCH 2/2] Create 17968.feature --- changelog.d/17968.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/17968.feature diff --git a/changelog.d/17968.feature b/changelog.d/17968.feature new file mode 100644 index 00000000000..023e766ae6a --- /dev/null +++ b/changelog.d/17968.feature @@ -0,0 +1 @@ +Add support for setting User-Agent for URL previewing.