Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix Host header sent by MatrixFederationAgent #4468

Merged
merged 2 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions synapse/http/federation/matrix_federation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from twisted.internet import defer
from twisted.internet.endpoints import HostnameEndpoint, wrapClientTLS
from twisted.web.client import URI, Agent, HTTPConnectionPool
from twisted.web.http_headers import Headers
from twisted.web.iweb import IAgent

from synapse.http.endpoint import parse_server_name
Expand Down Expand Up @@ -109,6 +110,15 @@ def request(self, method, uri, headers=None, bodyProducer=None):
else:
target = pick_server_from_list(server_list)

# make sure that the Host header is set correctly
if headers is None:
headers = Headers()
else:
headers = headers.copy()

if not headers.hasHeader(b'host'):
headers.addRawHeader(b'host', server_name_bytes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming header names are case insensitive here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, twisted sorts that out for us.


class EndpointFactory(object):
@staticmethod
def endpointForURI(_uri):
Expand Down
1 change: 0 additions & 1 deletion synapse/http/matrixfederationclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ def _send_request(

headers_dict = {
b"User-Agent": [self.version_string_bytes],
b"Host": [destination_bytes],
}

with limiter:
Expand Down
16 changes: 16 additions & 0 deletions tests/http/federation/test_matrix_federation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ def test_get(self):
request = http_server.requests[0]
self.assertEqual(request.method, b'GET')
self.assertEqual(request.path, b'/foo/bar')
self.assertEqual(
request.requestHeaders.getRawHeaders(b'host'),
[b'testserv:8448']
)
content = request.content.read()
self.assertEqual(content, b'')

Expand Down Expand Up @@ -195,6 +199,10 @@ def test_get_ip_address(self):
request = http_server.requests[0]
self.assertEqual(request.method, b'GET')
self.assertEqual(request.path, b'/foo/bar')
self.assertEqual(
request.requestHeaders.getRawHeaders(b'host'),
[b'1.2.3.4'],
)

# finish the request
request.finish()
Expand Down Expand Up @@ -235,6 +243,10 @@ def test_get_hostname_no_srv(self):
request = http_server.requests[0]
self.assertEqual(request.method, b'GET')
self.assertEqual(request.path, b'/foo/bar')
self.assertEqual(
request.requestHeaders.getRawHeaders(b'host'),
[b'testserv'],
)

# finish the request
request.finish()
Expand Down Expand Up @@ -276,6 +288,10 @@ def test_get_hostname_srv(self):
request = http_server.requests[0]
self.assertEqual(request.method, b'GET')
self.assertEqual(request.path, b'/foo/bar')
self.assertEqual(
request.requestHeaders.getRawHeaders(b'host'),
[b'testserv'],
)

# finish the request
request.finish()
Expand Down
2 changes: 1 addition & 1 deletion tests/http/test_fedclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def make_homeserver(self, reactor, clock):
return hs

def prepare(self, reactor, clock, homeserver):

self.cl = MatrixFederationHttpClient(self.hs)
self.reactor.lookups["testserv"] = "1.2.3.4"

Expand Down Expand Up @@ -95,6 +94,7 @@ def do_request():

# that should have made it send the request to the transport
self.assertRegex(transport.value(), b"^GET /foo/bar")
self.assertRegex(transport.value(), b"Host: testserv:8008")

# Deferred is still without a result
self.assertNoResult(test_d)
Expand Down