diff --git a/CHANGELOG.md b/CHANGELOG.md index f03eaab..1d5964d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,10 @@ * Add missing `response.raise_for_status()` call to the `fetch_document` network helper when fetching with given URL. Error status was already being raised correctly when - fetching by domain and path. + fetching by domain and path. + +* Don't crash when parsing an invalid NodeInfo document where the usage dictionary + is not following specification. ## [0.19.0] - 2019-12-15 diff --git a/federation/hostmeta/parsers.py b/federation/hostmeta/parsers.py index 460888c..52d0269 100644 --- a/federation/hostmeta/parsers.py +++ b/federation/hostmeta/parsers.py @@ -40,6 +40,8 @@ defaults = { def int_or_none(value): + if isinstance(value, dict): + return None if value is None or value == '': return None try: diff --git a/federation/tests/fixtures/hostmeta.py b/federation/tests/fixtures/hostmeta.py index 1afacec..b756304 100644 --- a/federation/tests/fixtures/hostmeta.py +++ b/federation/tests/fixtures/hostmeta.py @@ -70,6 +70,13 @@ NODEINFO_20_DOC = '{"version":"2.0","software":{"name":"diaspora","version":"0.7 '22,"localComments":17671},"metadata":{"nodeName":"I Like Toast","xmppChat":false,"camo":{"' \ 'markdown":false,"opengraph":false,"remotePods":false},"adminAccount":"podmin"}}' +# Some Pleroma instances don't conform to NodeInfo spec +NODEINFO_21_DOC_INVALID_USAGE_COUNTS = \ + '{"version":"2.1","software":{"name":"pleroma","version":"0.7.4.0-pd0313756"},"protocols":' \ + '["activitypub"],"services":{"inbound":[],"outbound":[]},"openRegistrations"' \ + ':true,"usage":{"users":{"total":348},"localPosts":{"all":3,"direct":1}},' \ + '"metadata":{}}' + # Buggy NodeInfo well known found in certain older Hubzilla versions NODEINFO_WELL_KNOWN_BUGGY = '{"links":{"rel":"http:\/\/nodeinfo.diaspora.software\/ns\/schema\/1.0","href":"h' \ 'ttps:\/\/example.com\/nodeinfo\/1.0"},"0":{"rel":"http:\/\/nodeinfo.diaspo' \ diff --git a/federation/tests/hostmeta/test_parsers.py b/federation/tests/hostmeta/test_parsers.py index 904a809..404ff8b 100644 --- a/federation/tests/hostmeta/test_parsers.py +++ b/federation/tests/hostmeta/test_parsers.py @@ -6,7 +6,8 @@ from federation.hostmeta.parsers import ( parse_mastodon_document, parse_matrix_document) from federation.tests.fixtures.hostmeta import ( NODEINFO2_10_DOC, NODEINFO_10_DOC, NODEINFO_20_DOC, STATISTICS_JSON_DOC, MASTODON_DOC, MASTODON_ACTIVITY_DOC, - MASTODON_RC_DOC, MASTODON_DOC_NULL_CONTACT, MATRIX_SYNAPSE_DOC, PLEROMA_MASTODON_API_DOC) + MASTODON_RC_DOC, MASTODON_DOC_NULL_CONTACT, MATRIX_SYNAPSE_DOC, PLEROMA_MASTODON_API_DOC, + NODEINFO_21_DOC_INVALID_USAGE_COUNTS) class TestIntOrNone: @@ -260,6 +261,36 @@ class TestParseNodeInfoDocument: }, } + def test_parse_nodeinfo_21_document__invalid_usage_counts(self): + result = parse_nodeinfo_document(json.loads(NODEINFO_21_DOC_INVALID_USAGE_COUNTS), 'pleroma.local') + assert result == { + 'organization': { + 'account': '', + 'contact': '', + 'name': '', + }, + 'host': 'pleroma.local', + 'name': 'pleroma.local', + 'open_signups': True, + 'protocols': ["activitypub"], + 'relay': '', + 'server_meta': {}, + 'services': [], + 'platform': 'pleroma', + 'version': '0.7.4.0-pd0313756', + 'features': {}, + 'activity': { + 'users': { + 'total': 348, + 'half_year': None, + 'monthly': None, + 'weekly': None, + }, + 'local_posts': None, + 'local_comments': None, + }, + } + class TestParseNodeInfo2Document: def test_parse_nodeinfo2_10_document(self):