Diff

mod_ogp/mod_ogp.lua @ 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
parent 4482:21698b960bd6
child 4504:0136c98f574c
line wrap: on
line diff
--- 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,