Skip to content

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

10.7 KB
Raw Blame
-- Injects a <permissions> element into a participant's self-presence to
-- communicate which features they are allowed to use.
--
-- The <permissions> element is only injected when the server is the authoritative
-- source of that information.  When a participant authenticated with a JWT that
-- already contains context.features, the client reads those features directly
-- from the token; no <permissions> element is sent.  This means:
--
--   * Token user WITH context.features  → no injection (client uses token features)
--   * Token user WITHOUT context.features, becomes moderator → inject default_permissions
--   * Anonymous/non-token user, becomes moderator → inject default_permissions
--   * Non-moderator of any kind → no injection
--
-- There is no mechanism for a client to explicitly request its permissions;
-- <permissions> is only ever pushed by the server, either on the initial
-- self-presence when a moderator joins (if the conditions above are met), or
-- when an affiliation change triggers force_permissions_update (e.g. another
-- moderator grants owner to this participant).
--
-- auto loaded by meeting_id
local filters = require 'util.filters';
local jid = require 'util.jid';

local util = module:require 'util';
local is_admin = util.is_admin;
local get_room_from_jid = util.get_room_from_jid;
local is_healthcheck_room = util.is_healthcheck_room;
local room_jid_match_rewrite = util.room_jid_match_rewrite;
local is_focus = util.is_focus;
local presence_check_status = util.presence_check_status;
local get_occupant_by_real_jid = util.get_occupant_by_real_jid;

local MUC_NS = 'http://jabber.org/protocol/muc';

local muc_domain_prefix = module:get_option_string('muc_mapper_domain_prefix', 'conference');

local muc_domain_base = module:get_option_string('muc_mapper_domain_base');
if not muc_domain_base then
    module:log('warn', 'No "muc_domain_base" option set, disabling module.');
    return ;
end

-- only the visitor prosody has main_domain setting
local is_visitor_prosody = module:get_option_string('main_domain') ~= nil;

-- load it only on the main muc component as it is loaded by muc_meeting_id which is loaded and for the breakout room muc
if muc_domain_prefix..'.'..muc_domain_base ~= module.host or is_visitor_prosody then
    return;
end

local sessions = prosody.full_sessions;
local default_permissions;

-- Returns a copy of a feature table. Sessions must not share one table, because
-- a change to the features of one session must not change the features of
-- another session.
local function copy_features(features)
    local res = {};

    for k, v in pairs(features) do
        res[k] = v;
    end

    return res;
end

local function load_config()
    default_permissions = module:get_option('jitsi_default_permissions', {
        livestreaming = true;
        recording = true;
        transcription = true;
        ['outbound-call'] = true;
        ['sip-outbound-call'] = true;
        ['create-polls'] = true;
        ['send-groupchat'] = true;
        ['send-private-message'] = true;
        ['share-video'] = true;
        flip = true;
        ['live-translation'] = true;
        ['live-translation-subscribe'] = true;
    });
end
load_config();

function process_set_affiliation(event)
    local actor, affiliation, jid, previous_affiliation, room
        = event.actor, event.affiliation, event.jid, event.previous_affiliation, event.room;
    local actor_session = sessions[actor];

    if is_admin(jid) or is_healthcheck_room(room.jid) or not actor or not previous_affiliation
        or not actor_session or not actor_session.jitsi_meet_context_features then
        return;
    end

    -- An affiliation belongs to a bare JID, and a bare JID can have more than one
    -- live session (multiple devices/tabs). Resolve them all, but only touch a
    -- session that is actually an occupant of this room or one of its own breakout
    -- rooms: an affiliation set on this room must not reach a session that happens
    -- to share the same bare JID while sitting in an unrelated conference.
    local bare_session = prosody.bare_sessions[jid];
    if not bare_session then
        return;
    end

    -- Any transition into owner is a grant. Any transition out of owner is a revoke.
    -- This also covers the admin and outcast affiliations, which the client does not
    -- use but which a raw stanza can set.
    local granting = affiliation == 'owner' and previous_affiliation ~= 'owner';
    local revoking = previous_affiliation == 'owner' and affiliation ~= 'owner';

    for _, occupant_session in pairs(bare_session.sessions) do
        if get_occupant_by_real_jid(room, occupant_session.full_jid) then
            if granting then
                -- Keep the features the session had before the grant, so that a later
                -- revoke can put them back. The value false marks a session that had no
                -- features, which is not the same as a session that had an empty table.
                if occupant_session.pre_grant_jitsi_meet_context_features == nil then
                    occupant_session.pre_grant_jitsi_meet_context_features
                        = occupant_session.jitsi_meet_context_features or false;
                end

                occupant_session.jitsi_meet_context_features
                    = copy_features(actor_session.jitsi_meet_context_features);
                if actor_session.jitsi_meet_context_user then
                    occupant_session.granted_jitsi_meet_context_user_id = actor_session.jitsi_meet_context_user['id']
                        or actor_session.granted_jitsi_meet_context_user_id;
                end
                occupant_session.granted_jitsi_meet_context_group_id = actor_session.jitsi_meet_context_group
                    or actor_session.granted_jitsi_meet_context_group_id;
                -- even if token and features are set we may want to re-send permissions
                occupant_session.force_permissions_update = true;
            elseif revoking then
                occupant_session.granted_jitsi_meet_context_user_id = nil;
                occupant_session.granted_jitsi_meet_context_group_id = nil;

                local pre_grant = occupant_session.pre_grant_jitsi_meet_context_features;

                if pre_grant ~= nil then
                    -- The session received a grant before. Put back the features it had
                    -- then. A token session gets its token features again, and any other
                    -- session gets no features and falls back to a check of its current role.
                    occupant_session.jitsi_meet_context_features = pre_grant or nil;
                    occupant_session.pre_grant_jitsi_meet_context_features = nil;
                elseif not occupant_session.auth_token then
                    occupant_session.jitsi_meet_context_features = nil;
                end
            end
        end
    end
end

-- Detects when sending self-presence because of role change
-- we can end up here because of the following cases:
-- 1. user joins the room and is granted moderator by another moderator or jicofo
-- 2. Some module changes the role of the user by using set_affiliation method
-- In cases where authentication is 'anonymous', 'jitsi-anonymous', 'internal_hashed', 'internal_plain', 'cyrus' we
-- want to send default permissions all to indicate UI that everything is allowed (to not relay on the UI to check
-- is participant moderator or not), to allow finer control over the permissions.
-- In case the authentication is 'token' based we want to send permissions only if the token of the user does not include
-- features in the user.context.
-- In case of allowners we want to send the permissions, no matter of the authentication method.
-- In case permissions were granted we want to send the granted permissions in all cases except when the user is
-- using token that has features pre-defined (authentication is 'token').
function filter_stanza(stanza, session)
    if not stanza.attr or not stanza.attr.to or stanza.name ~= 'presence'
        or stanza.attr.type == 'unavailable' or is_focus(stanza.attr.from) then
        return stanza;
    end

    local bare_to = jid.bare(stanza.attr.to);
    if is_admin(bare_to) then
        return stanza;
    end

    local muc_x = stanza:get_child('x', MUC_NS..'#user');
    if not muc_x or not presence_check_status(muc_x, '110') then
        return stanza;
    end

    local room = get_room_from_jid(room_jid_match_rewrite(jid.bare(stanza.attr.from)));

    if not room or is_healthcheck_room(room.jid)  then
        return stanza;
    end

    if not room.send_default_permissions_to then
        room.send_default_permissions_to = {};
    end

    if not session.force_permissions_update then
        if session.auth_token and session.jitsi_meet_context_features then -- token and features are set so skip
            room.send_default_permissions_to[bare_to] = nil;
            return stanza;
        end

        -- we are sending permissions only when becoming a member
        local is_moderator = false;
        for item in muc_x:childtags('item') do
            if item.attr.role == 'moderator' then
                is_moderator = true;
                break;
            end
        end

        if not is_moderator then
            return stanza;
        end

        if not room.send_default_permissions_to[bare_to] then
            return stanza;
        end
    end

    session.force_permissions_update = false;

    if not session.jitsi_meet_context_features then
        session.jitsi_meet_context_features = copy_features(default_permissions);
    end

    room.send_default_permissions_to[bare_to] = nil;

    stanza:tag('permissions', { xmlns='http://jitsi.org/jitmeet' });
    for k, v in pairs(session.jitsi_meet_context_features) do
        local val = tostring(v);
        stanza:tag('p', { name = k, val = val }):up();
    end
    stanza:up();

    return stanza;
end

-- we need to indicate that we will send permissions if we need to
-- we need to handle granted features and stuff in the pre-set hook so they are unavailable
-- when the self presence is set, so we can update the client, the checks
-- whether the actor is allowed to set the affiliation are done before pre-set hook is fired
module:hook('muc-pre-set-affiliation', function(event)
    local jid, room = event.jid, event.room;

    if not room.send_default_permissions_to then
        room.send_default_permissions_to = {};
    end
    room.send_default_permissions_to[jid] = true;

    process_set_affiliation(event);
end);

function filter_session(session)
    -- domain mapper is filtering on default priority 0
    -- allowners is -1 and we need it after that
    filters.add_filter(session, 'stanzas/out', filter_stanza, -2);
end

-- enable filtering presences
filters.add_filter_hook(filter_session);