Hi, I added another permission to https://modules.prosody.im/mod_restrict_federation.html and wanted to ask if thats ok for the existing module. I would like to use the same module, since its basically the same checks that are nessesary for it. Otherwise I could add a new, similar module too of course.
The new permission shall be similar to https://github.com/snikket-im/snikket-server/blob/master/ansible/files/restricted_users.pfw but without the need for mod_firewall, it adds a second permission to only allow federation to jids in the roster. I have the patch attached. Greetings Menel -- You received this message because you are subscribed to the Google Groups "prosody-dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/prosody-dev/3D8231A4-3A10-4689-B527-FC444267B1A3%40snikket.de.
mod_restrict_federation.lua
Description: Binary data
# HG changeset patch # User Menel <[email protected]> # Date 1795373851 -3600 # Sun Nov 22 19:57:31 2026 +0100 # Node ID 2add67a4d3bae35834d97961453d77b2dac4832b # Parent c37c141629017e699741dd233ddf875d1357502f mod_restrict_federation: Add a second permission to only federate to roster contacts diff --git a/mod_restrict_federation/README.md b/mod_restrict_federation/README.md --- a/mod_restrict_federation/README.md +++ b/mod_restrict_federation/README.md @@ -34,11 +34,13 @@ modules_enabled = { } ``` -Any user without the `xmpp:federate` permission will be unable to communicate -with remote domains. By default this module does not grant this permission to +Any user without the `xmpp:federate` or `xmpp:federate-roster` permission will be unable to communicate +with remote domains. By default this module does not grant these permissions to any role, meaning all users will be restricted. +`xmpp:federate` will grant the same permission to federate unlimited, like all roles have without this module. +`xmpp:federate-roster` will grant the permission to federate only to jids that are in the contact list of the user, not to rooms or strangers. -To grant permission to certain roles, you can use the `add_permission` +To grant permissions to certain roles, you can use the `add_permissions` configuration option (assuming you are using Prosody's [default authorization module](https://prosody.im/doc/modules/mod_authz_internal)): @@ -53,6 +55,34 @@ add_permissions = { } ``` +Add `xmpp:federate-roster` to a custom role: + +``` {.lua} +-- Allow registered users to federate, and a custom role "proteced-local" +-- to federate to contacts only +-- The custom role "limited-local" will not be able to federate. +add_permissions = { + ["prosody:registered"] = { + "xmpp:federate"; + ["protected-local"] = { + "xmpp:federate-roster"; + }; +} + +custom_roles = { + { + name = "limited-local"; + priority = 16; + inherits = { "prosody:guest" }; + }; + { + name = "protected-local"; + priority = 20; + inherits = { "limited-local" }; + }; +} +``` + Compatibility ============= diff --git a/mod_restrict_federation/mod_restrict_federation.lua b/mod_restrict_federation/mod_restrict_federation.lua --- a/mod_restrict_federation/mod_restrict_federation.lua +++ b/mod_restrict_federation/mod_restrict_federation.lua @@ -1,7 +1,21 @@ -local jid_node = require "prosody.util.jid".node; +local jid = require "prosody.util.jid"; +local jid_node, jid_bare = jid.node, jid.bare; local st = require "prosody.util.stanza"; local get_user_role = require "prosody.core.usermanager".get_user_role; +local load_roster = require "prosody.core.rostermanager".load_roster; + +-- Check whether the remote contact is in the local user's roster. +local function is_contact_in_roster(username, contact_jid) + if not username or not contact_jid then + return false; + end + + local contact_bare = jid_bare(contact_jid); + + local roster = load_roster(username, module.host); + return roster and roster[contact_bare] ~= nil; +end function check_outgoing_stanza(event) local origin, stanza = event.origin, event.stanza; @@ -22,6 +36,7 @@ function check_outgoing_stanza(event) -- Allow responses to iqs return; end + local payload = stanza.tags[1]; -- Allow push notification registration if payload.name == "command" and payload.attr.xmlns == "http://jabber.org/protocol/commands" then @@ -33,10 +48,20 @@ function check_outgoing_stanza(event) end end + local err_reply_txt + -- Allow remote contacts in the roster if the sender has the permission. + if module:could("xmpp:federate-roster", event) then + if is_contact_in_roster(origin.username, stanza.attr.to) then + return; + end + err_reply_txt = "Communication with remote domains is not permitted. Remote users must be added as contact first" + else + err_reply_txt = "Communication with remote domains is not permitted" + end -- Block module:log("debug", "Forbidding outgoing %s stanza from <%s> to <%s>", stanza.name, stanza.attr.from, stanza.attr.to); - local err_reply = st.error_reply(event.stanza, "auth", "policy-violation", "Communication with remote domains is not permitted"); + local err_reply = st.error_reply(stanza, "auth", "policy-violation", err_reply_txt); origin.send(err_reply); return true; end @@ -74,6 +99,12 @@ function check_incoming_stanza(event) return; end + -- Allow remote contacts in the roster if the recipient has the permission: + if recipient_role:may("xmpp:federate-roster") + and is_contact_in_roster(recipient_username, stanza.attr.from) then + return; + end + -- Block module:log("debug", "Forbidding incoming %s stanza from <%s> to <%s>", stanza.name, stanza.attr.from, stanza.attr.to); local err_reply = st.error_reply(event.stanza, "cancel", "service-unavailable");
