From 5154bf831329056dc781121e5f576376a227ba63 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 11 Jul 2024 12:19:05 +0000 Subject: [PATCH] Prevent to fetch loops --- src/Model/Item.php | 4 +++ src/Protocol/ActivityPub/Processor.php | 38 ++++++++++++++------------ src/Protocol/ActivityPub/Receiver.php | 2 +- src/Worker/FetchMissingActivity.php | 4 +++ 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 14e2225d6a..a0816b7fa7 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -4126,6 +4126,10 @@ class Item return $item_id; } + if (ActivityPub\Processor::alreadyKnown($uri, '')) { + return 0; + } + $hookData = [ 'uri' => $uri, 'uid' => $uid, diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 923e46b473..110fc47e02 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -1702,7 +1702,9 @@ class Processor } Logger::debug('Fetch announced activity', ['type' => $type, 'id' => $object_id, 'actor' => $relay_actor, 'signer' => $signer]); - return self::fetchMissingActivity($object_id, $child, $relay_actor, $completion, $uid); + if (!self::alreadyKnown($object_id, $child['id'] ?? '')) { + return self::fetchMissingActivity($object_id, $child, $relay_actor, $completion, $uid); + } } $activity = $object; $ldactivity = $ldobject; @@ -1768,14 +1770,7 @@ class Processor if (is_array($reply)) { $ldobject = JsonLD::compact($reply); $id = JsonLD::fetchElement($ldobject, '@id'); - if ($id == $child['id']) { - Logger::debug('Incluced activity is currently processed', ['replies' => $url, 'id' => $id]); - continue; - } elseif (Item::searchByLink($id)) { - Logger::debug('Incluced activity already exists', ['replies' => $url, 'id' => $id]); - continue; - } elseif (Queue::exists($id, 'as:Create')) { - Logger::debug('Incluced activity is already queued', ['replies' => $url, 'id' => $id]); + if (Processor::alreadyKnown($id, $child['id'] ?? '')) { continue; } if (parse_url($id, PHP_URL_HOST) == parse_url($url, PHP_URL_HOST)) { @@ -1787,14 +1782,7 @@ class Processor } elseif (is_string($reply)) { $id = $reply; } - if ($id == $child['id']) { - Logger::debug('Activity is currently processed', ['replies' => $url, 'id' => $id]); - } elseif (Item::searchByLink($id)) { - Logger::debug('Activity already exists', ['replies' => $url, 'id' => $id]); - } elseif (Queue::exists($id, 'as:Create')) { - Logger::debug('Activity is already queued', ['replies' => $url, 'id' => $id]); - } else { - Logger::debug('Missing Activity will be fetched and processed', ['replies' => $url, 'id' => $id]); + if (!self::alreadyKnown($id, $child['id'] ?? '')) { self::fetchMissingActivity($id, $child, '', Receiver::COMPLETION_REPLIES); ++$fetched; } @@ -1802,6 +1790,22 @@ class Processor Logger::notice('Fetch replies - done', ['fetched' => $fetched, 'total' => count($replies), 'replies' => $url]); } + public static function alreadyKnown(string $id, string $child): bool + { + if ($id == $child) { + Logger::debug('Activity is currently processed', ['id' => $id, 'child' => $child]); + return true; + } elseif (Item::searchByLink($id)) { + Logger::debug('Activity already exists', ['id' => $id, 'child' => $child]); + return true; + } elseif (Queue::exists($id, 'as:Create')) { + Logger::debug('Activity is already queued', ['id' => $id, 'child' => $child]); + return true; + } + Logger::debug('Activity is unknown', ['id' => $id, 'child' => $child]); + return false; + } + private static function refetchObjectOnHostDifference(array $object, string $url): array { $ldobject = JsonLD::compact($object); diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index de105af8cb..bab386979a 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -828,7 +828,7 @@ class Receiver case 'as:Announce': if (in_array($object_data['object_type'], self::CONTENT_TYPES)) { - if (!Item::searchByLink($object_data['object_id'], $uid)) { + if (!Processor::alreadyKnown($object_data['object_id'], '')) { if (ActivityPub\Processor::fetchMissingActivity($object_data['object_id'], [], $object_data['actor'], self::COMPLETION_ANNOUNCE, $uid)) { Logger::debug('Created announced id', ['uid' => $uid, 'id' => $object_data['object_id']]); Queue::remove($object_data); diff --git a/src/Worker/FetchMissingActivity.php b/src/Worker/FetchMissingActivity.php index d45b5c966e..ce5de8ec39 100644 --- a/src/Worker/FetchMissingActivity.php +++ b/src/Worker/FetchMissingActivity.php @@ -41,6 +41,10 @@ class FetchMissingActivity public static function execute(string $url, array $child = [], string $relay_actor = '', int $completion = Receiver::COMPLETION_MANUAL) { Logger::info('Start fetching missing activity', ['url' => $url]); + if (ActivityPub\Processor::alreadyKnown($url, $child['id'] ?? '')) { + Logger::info('Activity is already known.', ['url' => $url]); + return; + } $result = ActivityPub\Processor::fetchMissingActivity($url, $child, $relay_actor, $completion); if ($result) { Logger::info('Successfully fetched missing activity', ['url' => $url]);