Malformed JSON gets 400

2019-08-17
Marnanel Thurman 2019-05-29 10:31:26 +01:00
rodzic 0713dec537
commit d93212320b
2 zmienionych plików z 28 dodań i 26 usunięć

Wyświetl plik

@ -292,6 +292,16 @@ class InboxView(django.views.View):
]: ]:
return HttpResponse( return HttpResponse(
status = 415, # unsupported media type status = 415, # unsupported media type
reason = 'Try application/activity+json',
)
try:
decoded_body = json.loads(
str(request.body, encoding='UTF-8'))
except json.decoder.JSONDecodeError:
return HttpResponse(
status = 415, # unsupported media type
reason = 'Invalid JSON',
) )
capture = django_kepi.validation.IncomingMessage( capture = django_kepi.validation.IncomingMessage(
@ -300,7 +310,7 @@ class InboxView(django.views.View):
path = request.path, path = request.path,
signature = request.META['HTTP_SIGNATURE'], signature = request.META['HTTP_SIGNATURE'],
content_type = request.META['CONTENT_TYPE'], content_type = request.META['CONTENT_TYPE'],
body = str(request.body, encoding='UTF-8'), body = str(decoded_body)
) )
capture.save() capture.save()
logger.debug('%s: received %s with headers %s at %s -- now validating', logger.debug('%s: received %s with headers %s at %s -- now validating',

Wyświetl plik

@ -54,7 +54,6 @@ class TestInbox(TestCase):
self._post_to_inbox(INBOX_PATH) self._post_to_inbox(INBOX_PATH)
def test_non_json(self): def test_non_json(self):
keys = json.load(open('tests/keys/keys-0001.json', 'r')) keys = json.load(open('tests/keys/keys-0001.json', 'r'))
body, headers = test_message_body_and_headers( body, headers = test_message_body_and_headers(
@ -84,35 +83,28 @@ class TestInbox(TestCase):
@httpretty.activate @httpretty.activate
def test_malformed_json(self): def test_malformed_json(self):
HUMAN_URL = 'https://users.example.com/my-dame' keys = json.load(open('tests/keys/keys-0001.json', 'r'))
ANIMAL_URL = 'https://animals.example.com/a-lame-tame-crane'
mock_remote_object(HUMAN_URL, ftype='Person') body, headers = test_message_body_and_headers(
mock_remote_object(ANIMAL_URL, ftype='Person') f_actor = REMOTE_FRED,
secret = keys['private'],
)
broken_json = json.dumps(body)[1:]
c = Client() c = Client()
result = c.post(
c.post('/sharedInbox', path = INBOX_PATH,
content_type = 'application/activity+json', content_type = headers['content-type'],
data = { data = broken_json,
"id": "https://example.net/hello-world", HTTP_DATE = headers['date'],
"actor": HUMAN_URL, HOST = headers['host'],
"object": ANIMAL_URL, HTTP_SIGNATURE = headers['signature'],
"type": "Like",
},
) )
return
self.assertTrue( self.assertEqual(
IncomingMessage.objects.all().exists()) result.status_code,
415, # unsupported media type
IncomingMessage.objects.all().delete()
text = text[1:] # remove leading {, so the JSON is invalid
c.post('/sharedInbox',
content_type = 'application/activity+json',
data = text,
) )
self.assertFalse( self.assertFalse(