diff sat/plugins/plugin_comp_ap_gateway/http_server.py @ 3870:bd84d289fc94

component AP gateway: fix item ID for XMPP items on Announce: if an Announced item is from XMPP, the AP id must be parsed to retrieve the XMPP original ID. rel 370
author Goffi <goffi@goffi.org>
date Fri, 22 Jul 2022 15:42:59 +0200
parents c0bcbcf5b4b7
children 2e4a0f6050bd
line wrap: on
line diff
--- a/sat/plugins/plugin_comp_ap_gateway/http_server.py	Thu Jul 21 18:07:35 2022 +0200
+++ b/sat/plugins/plugin_comp_ap_gateway/http_server.py	Fri Jul 22 15:42:59 2022 +0200
@@ -288,6 +288,33 @@
                 # from post author in this case
                 sender_jid = await self.apg.getJIDFromId(sender)
                 repeater_jid = await self.apg.getJIDFromId(signing_actor)
+                repeated_item_id = obj["id"]
+                if self.apg.isLocalURL(repeated_item_id):
+                    # the repeated object is from XMPP, we need to parse the URL to find
+                    # the right ID
+                    url_type, url_args = self.apg.parseAPURL(repeated_item_id)
+                    if url_type != "item":
+                        raise exceptions.DataError(
+                            "local URI is not an item: {repeated_id}"
+                        )
+                    try:
+                        url_account, url_item_id = url_args
+                        if not url_account or not url_item_id:
+                            raise ValueError
+                    except (RuntimeError, ValueError):
+                        raise exceptions.DataError(
+                            "local URI is invalid: {repeated_id}"
+                        )
+                    else:
+                        url_jid, url_node = await self.apg.getJIDAndNode(url_account)
+                        if ((url_jid != sender_jid
+                             or url_node and url_node != self.apg._m.namespace)):
+                            raise exceptions.DataError(
+                                "announced ID doesn't match sender ({sender}): "
+                                f"[repeated_item_id]"
+                            )
+
+                    repeated_item_id = url_item_id
 
                 obj["_repeated"] = {
                     "by": repeater_jid.full(),
@@ -296,7 +323,7 @@
                         "pubsub",
                         path=sender_jid.full(),
                         node=self.apg._m.namespace,
-                        item=obj["id"]
+                        item=repeated_item_id
                     )
                 }
                 # we must use activity's id and targets, not the original item ones