From 84466d4ae4d7ce199041a58b9ef8690db71db3df Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sat, 26 May 2018 12:16:07 +0200 Subject: [PATCH] Fixed #207: Do header transforms for WebSocket XFF right --- daphne/ws_protocol.py | 2 +- tests/test_websocket.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/daphne/ws_protocol.py b/daphne/ws_protocol.py index ba7bbb2..9edfce6 100755 --- a/daphne/ws_protocol.py +++ b/daphne/ws_protocol.py @@ -50,7 +50,7 @@ class WebSocketProtocol(WebSocketServerProtocol): if self.server.proxy_forwarded_address_header: self.client_addr = parse_x_forwarded_for( - self.clean_headers, + dict(self.clean_headers), self.server.proxy_forwarded_address_header, self.server.proxy_forwarded_port_header, self.client_addr diff --git a/tests/test_websocket.py b/tests/test_websocket.py index a55d889..0ae1a21 100644 --- a/tests/test_websocket.py +++ b/tests/test_websocket.py @@ -147,6 +147,27 @@ class TestWebsocket(DaphneTestCase): self.assert_valid_websocket_scope(scope, subprotocols=subprotocols) self.assert_valid_websocket_connect_message(messages[0]) + def test_xff(self): + """ + Tests that X-Forwarded-For headers get parsed right + """ + headers = [ + ["X-Forwarded-For", "10.1.2.3"], + ["X-Forwarded-Port", "80"], + ] + with DaphneTestingInstance(xff=True) as test_app: + test_app.add_send_messages([ + { + "type": "websocket.accept", + } + ]) + self.websocket_handshake(test_app, headers=headers) + # Validate the scope and messages we got + scope, messages = test_app.get_received() + self.assert_valid_websocket_scope(scope) + self.assert_valid_websocket_connect_message(messages[0]) + assert scope["client"] == ["10.1.2.3", 80] + @given( request_path=http_strategies.http_path(), request_params=http_strategies.query_params(),