Skip to content

Commit

Permalink
return 400 on bad mf2-json input, not 500
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed May 28, 2018
1 parent 8d51bfd commit 490e8b8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
7 changes: 5 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,11 @@ def fetch_mf2_func(url):
_, doc = self._fetch(url)
return mf2py.parse(doc=doc, url=url)

actor = microformats2.find_author(mf2, fetch_mf2_func=fetch_mf2_func)
title = microformats2.get_title(mf2)
try:
actor = microformats2.find_author(mf2, fetch_mf2_func=fetch_mf2_func)
title = microformats2.get_title(mf2)
except (KeyError, ValueError) as e:
raise exc.HTTPBadRequest('Could not parse %s as %s: %s' % (url, input, e))

if input in ('as1', 'activitystreams'):
activities = body_items
Expand Down
2 changes: 1 addition & 1 deletion granary/microformats2.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ def find_author(parsed, **kwargs):


def get_title(mf2):
"""Returns the author of a page as a ActivityStreams actor dict.
"""Returns an mf2 object's title, ie its name.
Args:
mf2: dict, parsed mf2 object (ie return value from mf2py.parse())
Expand Down
24 changes: 24 additions & 0 deletions test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,3 +575,27 @@ def test_hub(self):
headers = resp.headers.getall('Link')
self.assertIn('<http://a/hub>; rel="hub"', headers)
self.assertIn('<%s>; rel="self"' % self_url, headers)


def test_bad_mf2_json_input_400s(self):
"""If a user sends JSON Feed input, but claims it's mf2 JSON, return 400.
https://console.cloud.google.com/errors/COyl7MTulffpuAE
"""
self.expect_requests_get('http://some/jf2', {
'data': {
'type': 'feed',
'items': [{
'type': 'entry',
'published': '2018-05-24T08:58:44+02:00',
'url': 'https://realize.be/notes/1463',
'content': {
'text': 'foo',
'html': '<p>bar</p>',
},
}],
}})
self.mox.ReplayAll()
resp = app.application.get_response('/url?url=http://some/jf2&input=mf2-json')
self.assert_equals(400, resp.status_int)

0 comments on commit 490e8b8

Please sign in to comment.