Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

feat(prosody): add mod_muc_resource_validate module (#17316)

bgrozev6d26c687bb69
diff --git a/resources/prosody-plugins/mod_muc_resource_validate.lua b/resources/prosody-plugins/mod_muc_resource_validate.lua
new file mode 100644
index 0000000000..c0b7c5d36a
--- /dev/null
+++ b/resources/prosody-plugins/mod_muc_resource_validate.lua
@@ -0,0 +1,93 @@
+-- Validates the resourcepart of client JIDs when joining a MUC.
+-- Copyright (C) 2024-present 8x8, Inc.
+--
+-- Hooks into muc-occupant-pre-join and rejects joins whose MUC resource is invalid.
+--
+-- A valid resource consists only of alphanumerics and underscores and must
+-- NOT start with an underscore: ^[a-zA-Z0-9][a-zA-Z0-9_]*$
+--
+-- When "anonymous_strict" mode is enabled, users on domains whose
+-- authentication provider is considered anonymous are additionally required
+-- to use a MUC resource equal to the first 8 characters of their real JID
+-- username (the UUID prefix assigned by the server).
+-- The check is dynamic: the auth provider is read from prosody.hosts at
+-- join time, so no static domain list is needed.
+--
+-- Example configuration (place under the MUC component):
+--
+-- Component "conference.meet.jitsi" "muc"
+-- modules_enabled = { "muc_resource_validate" }
+-- -- Enable strict anonymous-user check
+-- anonymous_strict = true
+-- -- Optional: extend the list of auth methods treated as anonymous
+-- anonymous_auth_methods = { "anonymous", "jitsi-anonymous", "token", "custom-anon" }
+
+local jid_split = require "util.jid".split;
+local st = require "util.stanza";
+local util = module:require "util";
+local is_healthcheck_room = util.is_healthcheck_room;
+
+-- Valid resource: starts with alphanumeric, followed by zero-or-more alphanumerics / underscores.
+local VALID_RESOURCE_PATTERN = "^[a-zA-Z0-9][a-zA-Z0-9_]*$";
+
+local anonymous_strict;
+local anonymous_auth_methods;
+
+local function load_config()
+ anonymous_strict = module:get_option_boolean("anonymous_strict", false);
+ anonymous_auth_methods = module:get_option_set(
+ "anonymous_auth_methods", { "anonymous", "jitsi-anonymous", "token" });
+end
+load_config();
+
+-- Returns true when the domain's authentication provider is considered anonymous.
+-- Prosody's usermanager sets host.users.name to the value of the
+-- "authentication" config option.
+local function is_anonymous_domain(domain)
+ local host_obj = prosody.hosts[domain];
+ if not host_obj or not host_obj.users then
+ return false;
+ end
+ return anonymous_auth_methods:contains(host_obj.users.name);
+end
+
+-- Returns true (halts event processing) when the join should be rejected.
+local function check_resource(event)
+ local room, origin, stanza = event.room, event.origin, event.stanza;
+
+ if is_healthcheck_room(room.jid) then
+ return;
+ end
+
+ local user, domain = jid_split(stanza.attr.from);
+ local _, _, muc_resource = jid_split(stanza.attr.to);
+
+ if user == nil then
+ return;
+ end
+
+ -- 1. Validate the resource part of the MUC JID (the occupant nickname).
+ if not muc_resource or not muc_resource:match(VALID_RESOURCE_PATTERN) then
+ module:log("warn", "Rejecting join with invalid MUC resource from %s (muc_resource: %s)",
+ stanza.attr.from, tostring(muc_resource));
+ origin.send(st.error_reply(stanza, "cancel", "not-allowed", "Invalid resource"));
+ return true;
+ end
+
+ -- 2. Anonymous strict mode: MUC resource must equal the 8-char UUID prefix of the real JID username.
+ if anonymous_strict and is_anonymous_domain(domain) then
+ local uuid_prefix = user:sub(1, 8);
+ if muc_resource ~= uuid_prefix then
+ module:log("warn", "Rejecting anonymous user: MUC resource %s does not match UUID prefix %s (from %s)",
+ muc_resource, uuid_prefix, stanza.attr.from);
+ origin.send(st.error_reply(stanza, "cancel", "not-allowed", "Invalid anonymous user ID"));
+ return true;
+ end
+ end
+end
+
+module:log("info", "module loaded (anonymous_strict=%s)", tostring(anonymous_strict));
+
+module:hook("muc-occupant-pre-join", check_resource, 11);
+
+module:hook_global("config-reloaded", load_config);
diff --git a/resources/prosody-plugins/mod_visitors_component.lua b/resources/prosody-plugins/mod_visitors_component.lua
index 98948af1ba..98708f9472 100644
--- a/resources/prosody-plugins/mod_visitors_component.lua
+++ b/resources/prosody-plugins/mod_visitors_component.lua
@@ -20,6 +20,7 @@ local process_host_module = util.process_host_module;
local respond_iq_result = util.respond_iq_result;
local split_string = util.split_string;
local new_id = require 'util.id'.medium;
+local uuid_generate = require 'util.uuid'.generate;
local json = require 'cjson.safe';
local inspect = require 'inspect';
@@ -116,7 +117,7 @@ local function request_promotion_received(room, from_jid, from_vnode, nick, time
if time and time > 0 and force_promote then
-- we are in auto-allow mode, let's reply with accept
-- we store where the request is coming from so we can send back the response
- local username = new_id():lower();
+ local username = uuid_generate();
visitors_promotion_map[room.jid][username] = {
from = from_vnode;
jid = from_jid;
@@ -385,7 +386,7 @@ local function process_promotion_response(room, id, approved)
end
-- lets reply to participant that requested promotion
- local username = new_id():lower();
+ local username = uuid_generate();
visitors_promotion_map[room.jid][username] = {
from = visitors_promotion_requests[room.jid][id].from;
jid = id;