From 94fe247b17970f448c8d02335b31842888c54749 Mon Sep 17 00:00:00 2001 From: Michael Manfre Date: Sun, 15 Jan 2023 12:28:44 -0500 Subject: [PATCH] Handle errors when a Post references an invalid ancestor (#417) --- activities/models/post.py | 18 +++++++++++++----- activities/services/post.py | 8 +++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/activities/models/post.py b/activities/models/post.py index e20f690..b18e652 100644 --- a/activities/models/post.py +++ b/activities/models/post.py @@ -23,6 +23,7 @@ from activities.models.post_types import ( PostTypeDataDecoder, PostTypeDataEncoder, ) +from core.exceptions import capture_message from core.html import ContentRenderer, strip_html from core.ld import ( canonicalise, @@ -853,7 +854,12 @@ class Post(StatorModel): try: parent = cls.by_object_uri(post.in_reply_to) except cls.DoesNotExist: - cls.ensure_object_uri(post.in_reply_to, reason=post.object_uri) + try: + cls.ensure_object_uri(post.in_reply_to, reason=post.object_uri) + except ValueError: + capture_message( + f"Cannot fetch ancestor of Post={post.pk}, ancestor_uri={post.in_reply_to}" + ) else: parent.calculate_stats() return post @@ -905,8 +911,8 @@ class Post(StatorModel): Sees if the post is in our local set, and if not, schedules a fetch for it (in the background) """ - if not object_uri: - raise ValueError("No URI provided!") + if not object_uri or "://" not in object_uri: + raise ValueError("URI missing or invalid") try: cls.by_object_uri(object_uri) except cls.DoesNotExist: @@ -977,8 +983,10 @@ class Post(StatorModel): Handles an internal fetch-request inbox message """ try: - cls.by_object_uri(data["object"]["object"], fetch=True) - except cls.DoesNotExist: + uri = data["object"]["object"] + if "://" in uri: + cls.by_object_uri(uri, fetch=True) + except (cls.DoesNotExist, KeyError): pass ### OpenGraph API ### diff --git a/activities/services/post.py b/activities/services/post.py index f2f21ca..1631ce4 100644 --- a/activities/services/post.py +++ b/activities/services/post.py @@ -5,6 +5,7 @@ from activities.models import ( PostStates, TimelineEvent, ) +from core.exceptions import capture_message from users.models import Identity @@ -91,7 +92,12 @@ class PostService: reason = ancestor.object_uri ancestor = self.queryset().filter(object_uri=object_uri).first() if ancestor is None: - Post.ensure_object_uri(object_uri, reason=reason) + try: + Post.ensure_object_uri(object_uri, reason=reason) + except ValueError: + capture_message( + f"Cannot fetch ancestor Post={self.post.pk}, ancestor_uri={object_uri}" + ) break if ancestor.state in [PostStates.deleted, PostStates.deleted_fanned_out]: break