From 2f635798ace2e263fc7d51a4324c12d77f90bd2f Mon Sep 17 00:00:00 2001 From: Goffi Date: Tue, 25 Aug 2026 19:07:06 +0200 Subject: [PATCH] plugin XEP-0308: better author check: Check full JID for MUC private messages. --- libervia/backend/plugins/plugin_xep_0308.py | 48 ++++++++++++--------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/libervia/backend/plugins/plugin_xep_0308.py b/libervia/backend/plugins/plugin_xep_0308.py index f42e1e55..72702b48 100644 --- a/libervia/backend/plugins/plugin_xep_0308.py +++ b/libervia/backend/plugins/plugin_xep_0308.py @@ -16,6 +16,7 @@ # along with this program. If not, see . import time +from typing import cast import uuid from sqlalchemy.orm.attributes import flag_modified @@ -33,6 +34,8 @@ from libervia.backend.core.i18n import _ from libervia.backend.core.log import getLogger from libervia.backend.memory.sqla import History, Message, Subject, joinedload, select from libervia.backend.models.core import MessageData, MessageEdition +from libervia.backend.plugins.plugin_xep_0045 import XEP_0045 +from libervia.backend.plugins.plugin_xep_0334 import XEP_0334 from libervia.backend.tools.common import data_format from libervia.backend.tools.utils import aio @@ -44,7 +47,7 @@ PLUGIN_INFO = { C.PI_IMPORT_NAME: "XEP-0308", C.PI_TYPE: "XEP", C.PI_PROTOCOLS: ["XEP-0308"], - C.PI_DEPENDENCIES: ["XEP-0334"], + C.PI_DEPENDENCIES: ["XEP-0045", "XEP-0334"], C.PI_MAIN: "XEP_0308", C.PI_HANDLER: "yes", C.PI_DESCRIPTION: _("""Implementation of XEP-0308 (Last Message Correction)"""), @@ -67,7 +70,8 @@ class XEP_0308: method=self._message_edit, async_=True, ) - self._h = host.plugins["XEP-0334"] + self._m = cast(XEP_0045, host.plugins["XEP-0045"]) + self._h = cast(XEP_0334, host.plugins["XEP-0334"]) def get_handler(self, client): return XEP_0308_handler() @@ -189,13 +193,17 @@ class XEP_0308: f"{message_elt.toXml()}" ) return False - # for groupchat, the correction must come from the same occupant, for other types, - # the resource may have changed (e.g. reconnection) - same_author = ( - edited_history.source_jid == from_jid - if edited_history.type == C.MESS_TYPE_GROUPCHAT - else edited_history.source_jid.userhost() == from_jid.userhost() + # For groupchat and MUC private messages the full JID must match. + # For a plain 1:1 message, the resource may have changed (e.g. reconnection), so + # only the bare JID is compared + is_muc = edited_history.type == C.MESS_TYPE_GROUPCHAT or self._m.is_joined_room( + client, jid.JID(edited_history.source) ) + + if is_muc: + same_author = edited_history.source_jid == from_jid + else: + same_author = edited_history.source_jid.userhost() == from_jid.userhost() if not same_author: log.warning( "Can't apply correction: correction doesn't come from same author " @@ -279,6 +287,18 @@ class XEP_0308: "it" ) + ref_id = edited_history.stanza_id if is_group_chat else edited_history.origin_id + if not ref_id: + orig_data = edited_history.serialise() + orig_data["from"] = jid.JID(orig_data["from"]) + orig_data["to"] = jid.JID(orig_data["to"]) + log.warning( + f"Can't correct message {message_id}: the original message has no usable " + f"id (stanza_id={edited_history.stanza_id}, " + f"origin_id={edited_history.origin_id}), ignoring the edit." + ) + return + await self.update_history( client, edited_history, @@ -293,18 +313,6 @@ class XEP_0308: store=not is_group_chat, ) - ref_id = edited_history.stanza_id if is_group_chat else edited_history.origin_id - if not ref_id: - orig_data = edited_history.serialise() - orig_data["from"] = jid.JID(orig_data["from"]) - orig_data["to"] = jid.JID(orig_data["to"]) - log.warning( - f"Can't correct message {message_id}: the original message has no usable " - f"id (stanza_id={edited_history.stanza_id}, " - f"origin_id={edited_history.origin_id}), ignoring the edit." - ) - return - serialised = edited_history.serialise() serialised["uid"] = str(uuid.uuid4()) serialised["from"] = client.jid