Changeset

4483:c4f11a4b5ac7

mod_ogp: Add the ability to whitelist domains
author JC Brand <jc@opkode.com>
date Tue, 02 Mar 2021 13:36:10 +0100
parents 4482:21698b960bd6
children 4484:6813a00878ea
files mod_ogp/README.markdown mod_ogp/mod_ogp.lua
diffstat 2 files changed, 38 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mod_ogp/README.markdown	Tue Mar 02 12:04:14 2021 +0100
+++ b/mod_ogp/README.markdown	Tue Mar 02 13:36:10 2021 +0100
@@ -6,13 +6,25 @@
 If it finds any, it sends a [XEP-0422 fastening](https://xmpp.org/extensions/xep-0422.html) applied to the original message that looks like:
 
 ```
-    <message id="example" from="chatroom@chatservice.example" to="chatroom@chatservice.example">
-        <apply-to xmlns="urn:xmpp:fasten:0" id="origin-id-X">
-            <meta xmlns="http://www.w3.org/1999/xhtml" property="og:title" content="The Rock"/>
-            <meta xmlns="http://www.w3.org/1999/xhtml" property="og:url" content="https://www.imdb.com/title/tt0117500/"/>
-            <meta xmlns="http://www.w3.org/1999/xhtml" property="og:image" content="https://ia.media-imdb.com/images/rock.jpg"/>
-        </apply-to>
-    </message>
+<message id="example" from="chatroom@muc.example.org" to="chatroom@muc.example.org">
+<apply-to xmlns="urn:xmpp:fasten:0" id="origin-id-X">
+<meta xmlns="http://www.w3.org/1999/xhtml" property="og:title" content="The Rock"/>
+<meta xmlns="http://www.w3.org/1999/xhtml" property="og:url" content="https://www.imdb.com/title/tt0117500/"/>
+<meta xmlns="http://www.w3.org/1999/xhtml" property="og:image" content="https://ia.media-imdb.com/images/rock.jpg"/>
+</apply-to>
+</message>
 ```
 
 The module is intentionally simple in the sense that it is basically a transport for https://ogp.me/
+
+Configuration
+-------------
+
+You can present a whitelist of domains for which OGP metadata will be fetched
+via the `ogp_domain_whitelist` setting.
+
+For example:
+
+    Component "muc.example.org" "muc"
+      modules_enabled = { "ogp" }
+      ogp_domain_whitelist = { "prosody.im" }
--- a/mod_ogp/mod_ogp.lua	Tue Mar 02 12:04:14 2021 +0100
+++ b/mod_ogp/mod_ogp.lua	Tue Mar 02 13:36:10 2021 +0100
@@ -2,12 +2,28 @@
 local http = require "net.http"
 local st = require "util.stanza"
 local url_pattern = [[https?://%S+]]
-local xmlns_fasten = "urn:xmpp:fasten:0";
-local xmlns_xhtml = "http://www.w3.org/1999/xhtml";
+local domain_pattern = '^%w+://([^/]+)'
+local xmlns_fasten = "urn:xmpp:fasten:0"
+local xmlns_xhtml = "http://www.w3.org/1999/xhtml"
+local whitelist = module:get_option_set("ogp_domain_whitelist", {})
+
+
+local function is_whitelisted(url)
+	if whitelist:empty() then
+		return true
+	end
+	local domain = url:match(domain_pattern)
+	if whitelist:contains(domain) then
+		return true;
+	end
+	return false
+end
 
 
 local function fetch_ogp_data(room, url, origin_id)
-	if not url then return; end
+	if not url or not is_whitelisted(url) then
+		return;
+	end
 
 	http.request(
 		url,