From 8850e2737498d2cc814f64cfe1a9f61af9c4c78b Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Thu, 27 Jul 2023 15:04:15 -0700 Subject: [PATCH] Activity.target_for: fix infinite loop when author is object id fixes #603 and https://console.cloud.google.com/errors/detail/CLPmoOqegtfm0AE;time=P30D?project=bridgy-federated --- activitypub.py | 4 +++- tests/test_activitypub.py | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/activitypub.py b/activitypub.py index db2f4c3..84cdf69 100644 --- a/activitypub.py +++ b/activitypub.py @@ -133,7 +133,9 @@ class ActivityPub(User, Protocol): for field in 'actor', 'author', 'attributedTo': inner_obj = as1.get_object(obj.as1, field) inner_id = inner_obj.get('id') or as1.get_url(inner_obj) - if not inner_id: + if (not inner_id + or inner_id == obj.as1.get('id') + or (obj.key and inner_id == obj.key.id())): continue # TODO: need a "soft" kwarg for load to suppress errors? diff --git a/tests/test_activitypub.py b/tests/test_activitypub.py index e4a5b85..7654fdc 100644 --- a/tests/test_activitypub.py +++ b/tests/test_activitypub.py @@ -1932,6 +1932,14 @@ class ActivityPubUtilsTest(TestCase): self.assertEqual('http://mas.to/inbox', ActivityPub.target_for(obj)) mock_get.assert_has_calls([self.as2_req('http://the/author')]) + @patch('requests.get') + def test_target_for_author_is_object_id(self, mock_get): + obj = self.store_object(id='http://the/author', our_as1={ + 'author': 'http://the/author', + }) + # test is that we short circuit out instead of infinite recursion + self.assertIsNone(ActivityPub.target_for(obj)) + @patch('requests.post') def test_send_blocklisted(self, mock_post): self.assertFalse(ActivityPub.send(Object(as2=NOTE),