mirror of
https://github.com/jointakahe/takahe.git
synced 2024-11-22 07:10:59 +00:00
Capture and don't thrash on badly formatted AP messages
This commit is contained in:
parent
709f2527ac
commit
c93a27e418
4 changed files with 149 additions and 133 deletions
|
@ -25,7 +25,7 @@ from activities.models.post_types import (
|
||||||
PostTypeDataEncoder,
|
PostTypeDataEncoder,
|
||||||
QuestionData,
|
QuestionData,
|
||||||
)
|
)
|
||||||
from core.exceptions import capture_message
|
from core.exceptions import ActivityPubFormatError, capture_message
|
||||||
from core.html import ContentRenderer, FediverseHtmlParser
|
from core.html import ContentRenderer, FediverseHtmlParser
|
||||||
from core.ld import (
|
from core.ld import (
|
||||||
canonicalise,
|
canonicalise,
|
||||||
|
@ -916,6 +916,8 @@ class Post(StatorModel):
|
||||||
focal_x, focal_y = None, None
|
focal_x, focal_y = None, None
|
||||||
mimetype = attachment.get("mediaType")
|
mimetype = attachment.get("mediaType")
|
||||||
if not mimetype or not isinstance(mimetype, str):
|
if not mimetype or not isinstance(mimetype, str):
|
||||||
|
if "url" not in attachment:
|
||||||
|
raise ActivityPubFormatError("No URL present on attachment")
|
||||||
mimetype, _ = mimetypes.guess_type(attachment["url"])
|
mimetype, _ = mimetypes.guess_type(attachment["url"])
|
||||||
if not mimetype:
|
if not mimetype:
|
||||||
mimetype = "application/octet-stream"
|
mimetype = "application/octet-stream"
|
||||||
|
|
|
@ -9,6 +9,12 @@ class ActivityPubError(BaseException):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class ActivityPubFormatError(ActivityPubError):
|
||||||
|
"""
|
||||||
|
A problem with an ActivityPub message's format/keys
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
class ActorMismatchError(ActivityPubError):
|
class ActorMismatchError(ActivityPubError):
|
||||||
"""
|
"""
|
||||||
The actor is not authorised to do the action we saw
|
The actor is not authorised to do the action we saw
|
||||||
|
|
|
@ -6,6 +6,8 @@ from dateutil import parser
|
||||||
from pyld import jsonld
|
from pyld import jsonld
|
||||||
from pyld.jsonld import JsonLdError
|
from pyld.jsonld import JsonLdError
|
||||||
|
|
||||||
|
from core.exceptions import ActivityPubFormatError
|
||||||
|
|
||||||
schemas = {
|
schemas = {
|
||||||
"www.w3.org/ns/activitystreams": {
|
"www.w3.org/ns/activitystreams": {
|
||||||
"contentType": "application/ld+json",
|
"contentType": "application/ld+json",
|
||||||
|
@ -695,7 +697,7 @@ def get_value_or_map(data, key, map_key):
|
||||||
if "und" in map_key:
|
if "und" in map_key:
|
||||||
return data[map_key]["und"]
|
return data[map_key]["und"]
|
||||||
return list(data[map_key].values())[0]
|
return list(data[map_key].values())[0]
|
||||||
raise KeyError(f"Cannot find {key} or {map_key}")
|
raise ActivityPubFormatError(f"Cannot find {key} or {map_key}")
|
||||||
|
|
||||||
|
|
||||||
def media_type_from_filename(filename):
|
def media_type_from_filename(filename):
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
from core.exceptions import ActivityPubError
|
||||||
from stator.models import State, StateField, StateGraph, StatorModel
|
from stator.models import State, StateField, StateGraph, StatorModel
|
||||||
|
|
||||||
|
|
||||||
class InboxMessageStates(StateGraph):
|
class InboxMessageStates(StateGraph):
|
||||||
received = State(try_interval=300, delete_after=86400 * 3)
|
received = State(try_interval=300, delete_after=86400 * 3)
|
||||||
processed = State(externally_progressed=True, delete_after=86400)
|
processed = State(externally_progressed=True, delete_after=86400)
|
||||||
purge = State(delete_after=24 * 60 * 60) # Delete after release (back compat)
|
errored = State(externally_progressed=True, delete_after=86400)
|
||||||
|
|
||||||
received.transitions_to(processed)
|
received.transitions_to(processed)
|
||||||
processed.transitions_to(purge) # Delete after release (back compat)
|
received.transitions_to(errored)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def handle_received(cls, instance: "InboxMessage"):
|
def handle_received(cls, instance: "InboxMessage"):
|
||||||
|
@ -17,6 +18,7 @@ class InboxMessageStates(StateGraph):
|
||||||
from users.models import Block, Follow, Identity, Report
|
from users.models import Block, Follow, Identity, Report
|
||||||
from users.services import IdentityService
|
from users.services import IdentityService
|
||||||
|
|
||||||
|
try:
|
||||||
match instance.message_type:
|
match instance.message_type:
|
||||||
case "follow":
|
case "follow":
|
||||||
Follow.handle_request_ap(instance.message)
|
Follow.handle_request_ap(instance.message)
|
||||||
|
@ -139,7 +141,9 @@ class InboxMessageStates(StateGraph):
|
||||||
case "fetchpost":
|
case "fetchpost":
|
||||||
Post.handle_fetch_internal(instance.message["object"])
|
Post.handle_fetch_internal(instance.message["object"])
|
||||||
case "cleartimeline":
|
case "cleartimeline":
|
||||||
TimelineEvent.handle_clear_timeline(instance.message["object"])
|
TimelineEvent.handle_clear_timeline(
|
||||||
|
instance.message["object"]
|
||||||
|
)
|
||||||
case "addfollow":
|
case "addfollow":
|
||||||
IdentityService.handle_internal_add_follow(
|
IdentityService.handle_internal_add_follow(
|
||||||
instance.message["object"]
|
instance.message["object"]
|
||||||
|
@ -151,6 +155,8 @@ class InboxMessageStates(StateGraph):
|
||||||
case unknown:
|
case unknown:
|
||||||
raise ValueError(f"Cannot handle activity of type {unknown}")
|
raise ValueError(f"Cannot handle activity of type {unknown}")
|
||||||
return cls.processed
|
return cls.processed
|
||||||
|
except ActivityPubError:
|
||||||
|
return cls.errored
|
||||||
|
|
||||||
|
|
||||||
class InboxMessage(StatorModel):
|
class InboxMessage(StatorModel):
|
||||||
|
|
Loading…
Reference in a new issue