Matrix and NodeInfo2 hostmeta parsers now clean the port out of the host name

merge-requests/159/merge
Jason Robinson 2019-12-29 23:08:05 +02:00
rodzic 8c4e4a2197
commit 557146055c
3 zmienionych plików z 74 dodań i 4 usunięć

Wyświetl plik

@ -2,6 +2,10 @@
## [0.20.0-dev] - unreleased
### Changed
* Matrix and NodeInfo2 hostmeta parsers now clean the port out of the host name.
### Fixed
* Don't crash loudly when fetching webfinger for Diaspora that does not contain XML.

Wyświetl plik

@ -125,8 +125,9 @@ def parse_mastodon_document(doc, host):
def parse_matrix_document(doc: Dict, host: str) -> Dict:
result = deepcopy(defaults)
result['host'] = host
result['name'] = host
host_without_port = host.split(':')[0]
result['host'] = host_without_port
result['name'] = host_without_port
result['protocols'] = ['matrix']
result['platform'] = f'matrix|{doc["server"]["name"].lower()}'
result['version'] = doc["server"]["version"]
@ -223,16 +224,18 @@ def parse_nodeinfo_document(doc, host):
def parse_nodeinfo2_document(doc, host):
result = deepcopy(defaults)
result['name'] = doc.get('server', {}).get('name', '') or ''
result['platform'] = doc.get('server', {}).get('software', 'unknown').lower()
result['version'] = doc.get('server', {}).get('version', '') or ''
# Ensure baseUrl is reported as the host we called
base_url = doc.get('server', {}).get('baseUrl', '').rstrip('/')
cleaned_base_url = re.sub(r'https?://', '', base_url)
cleaned_base_url = re.sub(r'https?://', '', base_url).split(':')[0]
if cleaned_base_url.startswith(host):
result['host'] = cleaned_base_url
else:
raise ValueError('NodeInfo2 baseUrl is outside called host.')
result['name'] = doc.get('server', {}).get('name', '') or ''
if not result['name']:
result['name'] = result['host']
result['open_signups'] = doc.get('openRegistrations', False)
inbound = doc.get('services', {}).get('inbound', [])
outbound = doc.get('services', {}).get('outbound', [])

Wyświetl plik

@ -119,6 +119,37 @@ class TestParseMastodonDocument:
class TestParseMatrixDocument:
@patch('federation.hostmeta.parsers.send_document', autospec=True, return_value=(403, None))
def test_parse_matrix_document__removes_port_from_host(self, mock_send):
result = parse_matrix_document(json.loads(MATRIX_SYNAPSE_DOC), 'feneas.org:8448')
assert result == {
'organization': {
'account': '',
'contact': '',
'name': '',
},
'host': 'feneas.org',
'name': 'feneas.org',
'open_signups': False,
'protocols': ['matrix'],
'relay': '',
'server_meta': {},
'services': [],
'platform': 'matrix|synapse',
'version': '0.33.8',
'features': {},
'activity': {
'users': {
'total': None,
'half_year': None,
'monthly': None,
'weekly': None,
},
'local_posts': None,
'local_comments': None,
},
}
@patch('federation.hostmeta.parsers.send_document', autospec=True, return_value=(403, None))
def test_parse_matrix_document__signups_closed(self, mock_send):
result = parse_matrix_document(json.loads(MATRIX_SYNAPSE_DOC), 'feneas.org')
@ -323,6 +354,38 @@ class TestParseNodeInfo2Document:
},
}
def test_parse_nodeinfo2_10_document__cleans_port_from_host(self):
response = json.loads(NODEINFO2_10_DOC)
response["server"]["baseUrl"] = "https://example.com:5221"
result = parse_nodeinfo2_document(response, 'example.com')
assert result == {
'organization': {
'account': 'https://example.com/u/admin',
'contact': 'foobar@example.com',
'name': 'Example organization',
},
'host': 'example.com',
'name': 'Example server',
'open_signups': True,
'protocols': ["diaspora", "zot"],
'relay': "tags",
'server_meta': {},
'services': ["facebook", "gnusocial", "twitter"],
'platform': 'example',
'version': '0.5.0',
'features': {},
'activity': {
'users': {
'total': 123,
'half_year': 42,
'monthly': 23,
'weekly': 10,
},
'local_posts': 500,
'local_comments': 1000,
},
}
class TestParseStatisticsJSONDocument:
def test_parse_statisticsjson_document(self):