From 0fa0b3a5f6918d695a02ed5cc56fa791cb402ad7 Mon Sep 17 00:00:00 2001 From: Goffi Date: Mon, 12 Jun 2023 14:50:43 +0200 Subject: [PATCH] component AP gateway: fix handling of XMPP comments authors: the gateway was supposing that comments where emitted from PEP of author. While this is the case for most blog posts, it's not for comments. Instead the component is now using `author_jid` which is retrieved by XEP-0277 plugin, and reject the item if the auhor is not verified (i.e. if `publisher` attribute is not set by XMPP service). --- .../plugin_comp_ap_gateway/__init__.py | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/libervia/backend/plugins/plugin_comp_ap_gateway/__init__.py b/libervia/backend/plugins/plugin_comp_ap_gateway/__init__.py index 58612a72..02e80a13 100644 --- a/libervia/backend/plugins/plugin_comp_ap_gateway/__init__.py +++ b/libervia/backend/plugins/plugin_comp_ap_gateway/__init__.py @@ -125,6 +125,10 @@ RE_PERCENT_ENC = re.compile(f"%{HEXA_ENC}") RE_ALLOWED_UNQUOTED = re.compile(r"^[a-zA-Z0-9_-]+$") +class MissingLocalPartError(Exception): + """JID local part is missing""" + + class APGateway: IMPORT_NAME = IMPORT_NAME # show data send or received through HTTP, used for debugging @@ -668,7 +672,7 @@ class APGateway: user = self.period_encode(user) if not user: - raise exceptions.InternalError("there should be a user part") + raise MissingLocalPartError("there should be a local part") if node: account_elts.extend((node, "---")) @@ -2094,9 +2098,25 @@ class APGateway: raise exceptions.InternalError( "node or service is missing in mb_data" ) - target_ap_account = await self.get_ap_account_from_jid_and_node( - service, node - ) + try: + target_ap_account = await self.get_ap_account_from_jid_and_node( + service, node + ) + except MissingLocalPartError: + # we have no local part, this is probably a comment + try: + service = jid.JID(mb_data["author_jid"]) + if not mb_data["author_jid_verified"]: + raise KeyError + except KeyError: + raise exceptions.DataError( + "Can't identify post author. Be sure to use a pubsub service " + "setting the 'publisher' attribute." + ) + else: + target_ap_account = await self.get_ap_account_from_jid_and_node( + service, node + ) if self.is_virtual_jid(service): # service is a proxy JID for AP account actor_data = await self.get_ap_actor_data_from_account(target_ap_account)