Mercurial > prosody-modules
comparison mod_report_forward/mod_report_forward.lua @ 5857:ff90dad75352
mod_report_forward: Fixes for abuse contact address lookup in origin reporting
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sun, 03 Mar 2024 18:06:47 +0000 |
| parents | 6fe4dab27187 |
| children | fdff8cb54302 |
comparison
equal
deleted
inserted
replaced
| 5856:6fe4dab27187 | 5857:ff90dad75352 |
|---|---|
| 15 local cache_size = module:get_option_number("report_forward_contact_cache_size", 256); | 15 local cache_size = module:get_option_number("report_forward_contact_cache_size", 256); |
| 16 local report_to_origin = module:get_option_boolean("report_forward_to_origin", true); | 16 local report_to_origin = module:get_option_boolean("report_forward_to_origin", true); |
| 17 local contact_lookup_timeout = module:get_option_number("report_forward_contact_lookup_timeout", 180); | 17 local contact_lookup_timeout = module:get_option_number("report_forward_contact_lookup_timeout", 180); |
| 18 | 18 |
| 19 local body_template = module:get_option_string("report_forward_body_template", [[ | 19 local body_template = module:get_option_string("report_forward_body_template", [[ |
| 20 SPAM/ABUSE REPORT | 20 SPAM/ABUSE REPORT |
| 21 ----------------- | 21 ----------------- |
| 22 | 22 |
| 23 Reported JID: {reported_jid} | 23 Reported JID: {reported_jid} |
| 24 | 24 |
| 25 A user on our service has reported a message originating from the above JID on | 25 A user on our service has reported a message originating from the above JID on |
| 26 your server. | 26 your server. |
| 27 | 27 |
| 28 {reported_message_time&The reported message was sent at: {reported_message_time}} | 28 {reported_message_time&The reported message was sent at: {reported_message_time}} |
| 29 | 29 |
| 30 -- | 30 -- |
| 31 This message contains also machine-readable payloads, including XEP-0377, in case | 31 This message contains also machine-readable payloads, including XEP-0377, in case |
| 32 you want to automate handling of these reports. You can receive these reports | 32 you want to automate handling of these reports. You can receive these reports |
| 33 to a different address by setting 'spam-report-addresses' in your server | 33 to a different address by setting 'spam-report-addresses' in your server |
| 34 contact info configuration. For more information, see https://xmppbl.org/reports/ | 34 contact info configuration. For more information, see https://xmppbl.org/reports/ |
| 35 ]]):gsub("^%s+", ""):gsub("(%S)\n(%S)", "%1 %2"); | 35 ]]):gsub("^%s+", ""):gsub("(%S)\n(%S)", "%1 %2"); |
| 37 local report_addresses = require "util.cache".new(cache_size); | 37 local report_addresses = require "util.cache".new(cache_size); |
| 38 | 38 |
| 39 local function get_address(form, ...) | 39 local function get_address(form, ...) |
| 40 for i = 1, select("#", ...) do | 40 for i = 1, select("#", ...) do |
| 41 local field_var = select(i, ...); | 41 local field_var = select(i, ...); |
| 42 local field = form:get_child_with_attr("field", "jabber:x:data", "var", field_var); | 42 local field = form:get_child_with_attr("field", nil, "var", field_var); |
| 43 if field then | 43 if field then |
| 44 local parsed = url.parse(field:get_child_text("value")); | 44 local parsed = url.parse(field:get_child_text("value")); |
| 45 if parsed.scheme == "xmpp" and parsed.path and not parsed.query then | 45 if parsed.scheme == "xmpp" and parsed.path and not parsed.query then |
| 46 return parsed.path; | 46 return parsed.path; |
| 47 end | 47 end |
| 48 else | |
| 49 module:log("debug", "No field '%s'", field_var); | |
| 48 end | 50 end |
| 49 end | 51 end |
| 50 end | 52 end |
| 51 | 53 |
| 52 local function get_origin_report_address(reported_jid) | 54 local function get_origin_report_address(reported_jid) |
| 53 local host = jid.host(reported_jid); | 55 local host = jid.host(reported_jid); |
| 54 local address = report_addresses:get(host); | 56 local address = report_addresses:get(host); |
| 55 if address then return address; end | 57 if address then return address; end |
| 56 | 58 |
| 57 local contact_query = st.iq({ to = host, from = module.host, id = new_id() }) | 59 local contact_query = st.iq({ type = "get", to = host, from = module.host, id = new_id() }) |
| 58 :query("http://jabber.org/protocol/disco#info"); | 60 :query("http://jabber.org/protocol/disco#info"); |
| 59 | 61 |
| 60 return module:send_iq(contact_query, prosody.hosts[module.host], contact_lookup_timeout) | 62 return module:send_iq(contact_query, prosody.hosts[module.host], contact_lookup_timeout) |
| 61 :next(function (response) | 63 :next(function (result) |
| 62 if response.attr.type ~= "result" then return; end | 64 module:log("debug", "Processing contact form..."); |
| 65 local response = result.stanza; | |
| 66 if response.attr.type ~= "result" then | |
| 67 module:log("warn", "Failed to query contact addresses of %s: %s", host, response); | |
| 68 return; | |
| 69 end | |
| 63 | 70 |
| 64 for form in response.tags[1]:childtags("x", "jabber:x:data") do | 71 for form in response.tags[1]:childtags("x", "jabber:x:data") do |
| 65 local form_type = form:get_child_with_attr("field", nil, "var", "FORM_TYPE"); | 72 local form_type = form:get_child_with_attr("field", nil, "var", "FORM_TYPE"); |
| 66 if form_type == "http://jabber.org/network/serverinfo" then | 73 if form_type and form_type:get_child_text("value") == "http://jabber.org/network/serverinfo" then |
| 67 address = get_address(form, "spam-report-addresses", "abuse-addresses"); | 74 address = get_address(form, "spam-report-addresses", "abuse-addresses"); |
| 68 break; | 75 break; |
| 69 end | 76 end |
| 70 end | 77 end |
| 71 return address; | 78 return address; |
| 130 if not origin_report_address then | 137 if not origin_report_address then |
| 131 module:log("warn", "Couldn't report to origin: no contact address found for %s", jid.host(event.jid)); | 138 module:log("warn", "Couldn't report to origin: no contact address found for %s", jid.host(event.jid)); |
| 132 return; | 139 return; |
| 133 end | 140 end |
| 134 send_report(origin_report_address, message); | 141 send_report(origin_report_address, message); |
| 142 end):catch(function (e) | |
| 143 module:log("error", "Failed to report to origin server: %s", e); | |
| 135 end); | 144 end); |
| 136 end | 145 end |
| 137 end | 146 end |
| 138 | 147 |
| 139 module:hook("spam_reporting/abuse-report", forward_report, -1); | 148 module:hook("spam_reporting/abuse-report", forward_report, -1); |
