Comparison

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
comparison
equal deleted inserted replaced
4482:21698b960bd6 4483:c4f11a4b5ac7
1 local mod_muc = module:depends("muc") 1 local mod_muc = module:depends("muc")
2 local http = require "net.http" 2 local http = require "net.http"
3 local st = require "util.stanza" 3 local st = require "util.stanza"
4 local url_pattern = [[https?://%S+]] 4 local url_pattern = [[https?://%S+]]
5 local xmlns_fasten = "urn:xmpp:fasten:0"; 5 local domain_pattern = '^%w+://([^/]+)'
6 local xmlns_xhtml = "http://www.w3.org/1999/xhtml"; 6 local xmlns_fasten = "urn:xmpp:fasten:0"
7 local xmlns_xhtml = "http://www.w3.org/1999/xhtml"
8 local whitelist = module:get_option_set("ogp_domain_whitelist", {})
9
10
11 local function is_whitelisted(url)
12 if whitelist:empty() then
13 return true
14 end
15 local domain = url:match(domain_pattern)
16 if whitelist:contains(domain) then
17 return true;
18 end
19 return false
20 end
7 21
8 22
9 local function fetch_ogp_data(room, url, origin_id) 23 local function fetch_ogp_data(room, url, origin_id)
10 if not url then return; end 24 if not url or not is_whitelisted(url) then
25 return;
26 end
11 27
12 http.request( 28 http.request(
13 url, 29 url,
14 nil, 30 nil,
15 function(response_body, response_code, _) 31 function(response_body, response_code, _)