plugin XEP-0308: better author check:

Check full JID for MUC private messages.
This commit is contained in:
Goffi 2026-08-25 19:07:06 +02:00
commit 2f635798ac

View file

@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import time import time
from typing import cast
import uuid import uuid
from sqlalchemy.orm.attributes import flag_modified 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.core.log import getLogger
from libervia.backend.memory.sqla import History, Message, Subject, joinedload, select from libervia.backend.memory.sqla import History, Message, Subject, joinedload, select
from libervia.backend.models.core import MessageData, MessageEdition 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.common import data_format
from libervia.backend.tools.utils import aio from libervia.backend.tools.utils import aio
@ -44,7 +47,7 @@ PLUGIN_INFO = {
C.PI_IMPORT_NAME: "XEP-0308", C.PI_IMPORT_NAME: "XEP-0308",
C.PI_TYPE: "XEP", C.PI_TYPE: "XEP",
C.PI_PROTOCOLS: ["XEP-0308"], C.PI_PROTOCOLS: ["XEP-0308"],
C.PI_DEPENDENCIES: ["XEP-0334"], C.PI_DEPENDENCIES: ["XEP-0045", "XEP-0334"],
C.PI_MAIN: "XEP_0308", C.PI_MAIN: "XEP_0308",
C.PI_HANDLER: "yes", C.PI_HANDLER: "yes",
C.PI_DESCRIPTION: _("""Implementation of XEP-0308 (Last Message Correction)"""), C.PI_DESCRIPTION: _("""Implementation of XEP-0308 (Last Message Correction)"""),
@ -67,7 +70,8 @@ class XEP_0308:
method=self._message_edit, method=self._message_edit,
async_=True, 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): def get_handler(self, client):
return XEP_0308_handler() return XEP_0308_handler()
@ -189,13 +193,17 @@ class XEP_0308:
f"{message_elt.toXml()}" f"{message_elt.toXml()}"
) )
return False return False
# for groupchat, the correction must come from the same occupant, for other types, # For groupchat and MUC private messages the full JID must match.
# the resource may have changed (e.g. reconnection) # For a plain 1:1 message, the resource may have changed (e.g. reconnection), so
same_author = ( # only the bare JID is compared
edited_history.source_jid == from_jid is_muc = edited_history.type == C.MESS_TYPE_GROUPCHAT or self._m.is_joined_room(
if edited_history.type == C.MESS_TYPE_GROUPCHAT client, jid.JID(edited_history.source)
else edited_history.source_jid.userhost() == from_jid.userhost()
) )
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: if not same_author:
log.warning( log.warning(
"Can't apply correction: correction doesn't come from same author " "Can't apply correction: correction doesn't come from same author "
@ -279,6 +287,18 @@ class XEP_0308:
"it" "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( await self.update_history(
client, client,
edited_history, edited_history,
@ -293,18 +313,6 @@ class XEP_0308:
store=not is_group_chat, 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 = edited_history.serialise()
serialised["uid"] = str(uuid.uuid4()) serialised["uid"] = str(uuid.uuid4())
serialised["from"] = client.jid serialised["from"] = client.jid