From 5708d44a6eae3cf231e096a8ae2e7df6b50df051 Mon Sep 17 00:00:00 2001
From: Reid 'arrdem' McKenzie <me@arrdem.com>
Date: Sat, 26 Nov 2022 21:57:06 -0700
Subject: [PATCH] Handle missing bodies more gracefully

---
 .../activitypub_relay/src/python/relay/views.py    | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/projects/activitypub_relay/src/python/relay/views.py b/projects/activitypub_relay/src/python/relay/views.py
index 3dc05fa..779abcd 100644
--- a/projects/activitypub_relay/src/python/relay/views.py
+++ b/projects/activitypub_relay/src/python/relay/views.py
@@ -1,3 +1,5 @@
+import json
+from json.decoder import JSONDecodeError
 import logging
 
 from aiohttp.web import HTTPUnauthorized, Request
@@ -221,14 +223,20 @@ async def set_config(request: Request):
         return Response.new_error(403, "access denied", "json")
 
     # FIXME: config doesn't have a way to go from JSON or update, using dict stuff
-    new_config = await request.json()
+    text = await request.text()
+    try:
+        new_config = json.loads(text)
+    except JSONDecodeError as e:
+        logging.exception(f"Unable to load config {text!r}")
+        return Response.new_error(400, "bad request", "json")
 
     request.app.config.update(new_config)
 
-    # If there are pending follows which are NOW whitelisted, allow them
     if request.app.config.whitelist_enabled:
+        # If there are pending follows which are NOW whitelisted, allow them
         for domain in request.app.config.whitelist:
             if (pending_follow := request.app.database.get_request(domain, False)):
+                logging.info(f"Acknowledging queued follow request from {domain}...")
                 await misc.request(
                     actor.shared_inbox,
                     misc.Message.new_response(
@@ -249,6 +257,8 @@ async def set_config(request: Request):
 
                 request.app.database.del_request(domain)
 
+        # FIXME: If there are EXISTING follows which are NO LONGER allowed/are blacklisted, drop them
+
         request.app.database.save()
 
     request.app.config.save()