Skip to content

Commit c5af4b6

Browse files
committed
Log if ActivityPub inbox request is invalid
1 parent 9aeedac commit c5af4b6

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

pod/activitypub/views.py

+23-16
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,29 @@ def inbox(request, username=None):
130130
https://www.w3.org/TR/activitypub/#inbox
131131
"""
132132

133-
data = json.loads(request.body.decode()) if request.body else None
134-
logger.warning("inbox query: %s", json.dumps(data, indent=True))
135-
136-
if (
137-
data["type"] in ("Announce", "Update", "Delete")
138-
and not settings.TEST_SETTINGS
139-
and not check_signatures(request)
140-
):
141-
return HttpResponse("Signature could not be verified", status=403)
142-
143-
if activitypub_task := TYPE_TASK.get(data["type"], None):
144-
activitypub_task.delay(username, data)
145-
else:
146-
logger.debug("Ignoring inbox action: %s", data["type"])
147-
148-
return HttpResponse(status=204)
133+
try:
134+
data = json.loads(request.body.decode())
135+
logger.warning("inbox query: %s", json.dumps(data, indent=True))
136+
137+
if (
138+
data["type"] in ("Announce", "Update", "Delete")
139+
and not settings.TEST_SETTINGS
140+
and not check_signatures(request)
141+
):
142+
return HttpResponse("Signature could not be verified", status=403)
143+
144+
if activitypub_task := TYPE_TASK.get(data["type"], None):
145+
activitypub_task.delay(username, data)
146+
else:
147+
logger.debug("Ignoring inbox action: %s", data["type"])
148+
149+
return HttpResponse(status=204)
150+
except (AttributeError, KeyError, UnicodeError, ValueError) as err:
151+
logger.error("ActivityPub inbox request body badly formatted and unusable: %s" % err)
152+
return HttpResponse(status=422)
153+
except Exception as err:
154+
logger.error("ActivityPub inbox request error: %s" % err)
155+
return HttpResponse(status=400)
149156

150157

151158
@csrf_exempt

0 commit comments

Comments
 (0)