From 6256030804c581d48d13113452165bd4053ba6c6 Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 29 Jul 2020 19:46:37 +0200 Subject: [PATCH 01/10] Revert "Corrected --proxy-headers client ip/host when using a unix socket (#636)" This reverts commit a796e1d4 --- uvicorn/protocols/utils.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/uvicorn/protocols/utils.py b/uvicorn/protocols/utils.py index b5cd72b50..a282c61eb 100644 --- a/uvicorn/protocols/utils.py +++ b/uvicorn/protocols/utils.py @@ -1,11 +1,6 @@ import socket import urllib -if hasattr(socket, "AF_UNIX"): - SUPPORTED_SOCKET_FAMILIES = (socket.AF_INET, socket.AF_INET6, socket.AF_UNIX) -else: - SUPPORTED_SOCKET_FAMILIES = (socket.AF_INET, socket.AF_INET6) - def get_remote_addr(transport): socket_info = transport.get_extra_info("socket") @@ -20,9 +15,8 @@ def get_remote_addr(transport): else: family = socket_info.family - if family in SUPPORTED_SOCKET_FAMILIES: + if family in (socket.AF_INET, socket.AF_INET6): return (str(info[0]), int(info[1])) - return None info = transport.get_extra_info("peername") if info is not None and isinstance(info, (list, tuple)) and len(info) == 2: @@ -35,7 +29,7 @@ def get_local_addr(transport): if socket_info is not None: info = socket_info.getsockname() family = socket_info.family - if family in SUPPORTED_SOCKET_FAMILIES: + if family in (socket.AF_INET, socket.AF_INET6): return (str(info[0]), int(info[1])) return None info = transport.get_extra_info("sockname") From 84c1bd8670bd74c3db32598f84a716f6b360aab9 Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 29 Jul 2020 20:24:13 +0200 Subject: [PATCH 02/10] Distinguish case fd/unix socket to return correctly client --- uvicorn/protocols/utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/uvicorn/protocols/utils.py b/uvicorn/protocols/utils.py index a282c61eb..bbe7e396d 100644 --- a/uvicorn/protocols/utils.py +++ b/uvicorn/protocols/utils.py @@ -17,6 +17,15 @@ def get_remote_addr(transport): if family in (socket.AF_INET, socket.AF_INET6): return (str(info[0]), int(info[1])) + elif family is socket.AF_UNIX: + if isinstance(info, tuple): + # fd case + # + return (str(info[0]), int(info[1])) + else: + # unix socket case + # + return None return None info = transport.get_extra_info("peername") if info is not None and isinstance(info, (list, tuple)) and len(info) == 2: From 4dc4643955b7a7d5de49a754520ac7447582e935 Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 29 Jul 2020 20:53:25 +0200 Subject: [PATCH 03/10] Handle windows case --- uvicorn/protocols/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uvicorn/protocols/utils.py b/uvicorn/protocols/utils.py index bbe7e396d..e702f512d 100644 --- a/uvicorn/protocols/utils.py +++ b/uvicorn/protocols/utils.py @@ -17,7 +17,7 @@ def get_remote_addr(transport): if family in (socket.AF_INET, socket.AF_INET6): return (str(info[0]), int(info[1])) - elif family is socket.AF_UNIX: + elif hasattr(socket, "AF_UNIX") and family is socket.AF_UNIX: if isinstance(info, tuple): # fd case # From cbc59fe47877ff275388d441d713cc348c909d33 Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 29 Jul 2020 21:04:22 +0200 Subject: [PATCH 04/10] Added test for AF_UNIX socket type Modified MockSocket peername to pass tuples instead of list because socket.getpeername() and socket.getsockname() return tuples --- tests/protocols/test_utils.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/protocols/test_utils.py b/tests/protocols/test_utils.py index 4f565d4f6..575b87873 100644 --- a/tests/protocols/test_utils.py +++ b/tests/protocols/test_utils.py @@ -29,12 +29,12 @@ def test_get_local_addr_with_socket(): assert get_local_addr(transport) is None transport = MockTransport( - {"socket": MockSocket(family=socket.AF_INET6, sockname=["::1", 123])} + {"socket": MockSocket(family=socket.AF_INET6, sockname=("::1", 123))} ) assert get_local_addr(transport) == ("::1", 123) transport = MockTransport( - {"socket": MockSocket(family=socket.AF_INET, sockname=["123.45.6.7", 123])} + {"socket": MockSocket(family=socket.AF_INET, sockname=("123.45.6.7", 123))} ) assert get_local_addr(transport) == ("123.45.6.7", 123) @@ -44,21 +44,24 @@ def test_get_remote_addr_with_socket(): assert get_remote_addr(transport) is None transport = MockTransport( - {"socket": MockSocket(family=socket.AF_INET6, peername=["::1", 123])} + {"socket": MockSocket(family=socket.AF_INET6, peername=("::1", 123))} ) assert get_remote_addr(transport) == ("::1", 123) transport = MockTransport( - {"socket": MockSocket(family=socket.AF_INET, peername=["123.45.6.7", 123])} + {"socket": MockSocket(family=socket.AF_INET, peername=("123.45.6.7", 123))} ) assert get_remote_addr(transport) == ("123.45.6.7", 123) + transport = MockTransport({"socket": MockSocket(family=socket.AF_UNIX, peername=("127.0.0.1", 8000))}) + assert get_remote_addr(transport) == ("127.0.0.1", 8000) + def test_get_local_addr(): transport = MockTransport({"sockname": "path/to/unix-domain-socket"}) assert get_local_addr(transport) is None - transport = MockTransport({"sockname": ["123.45.6.7", 123]}) + transport = MockTransport({"sockname": ("123.45.6.7", 123)}) assert get_local_addr(transport) == ("123.45.6.7", 123) @@ -66,5 +69,5 @@ def test_get_remote_addr(): transport = MockTransport({"peername": None}) assert get_remote_addr(transport) is None - transport = MockTransport({"peername": ["123.45.6.7", 123]}) + transport = MockTransport({"peername": ("123.45.6.7", 123)}) assert get_remote_addr(transport) == ("123.45.6.7", 123) From bc743e1368fdf84dfc598ef83cfd14b0a336865e Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 29 Jul 2020 21:07:24 +0200 Subject: [PATCH 05/10] Black --- tests/protocols/test_utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/protocols/test_utils.py b/tests/protocols/test_utils.py index 575b87873..d2846069d 100644 --- a/tests/protocols/test_utils.py +++ b/tests/protocols/test_utils.py @@ -53,7 +53,9 @@ def test_get_remote_addr_with_socket(): ) assert get_remote_addr(transport) == ("123.45.6.7", 123) - transport = MockTransport({"socket": MockSocket(family=socket.AF_UNIX, peername=("127.0.0.1", 8000))}) + transport = MockTransport( + {"socket": MockSocket(family=socket.AF_UNIX, peername=("127.0.0.1", 8000))} + ) assert get_remote_addr(transport) == ("127.0.0.1", 8000) From 9a8b9551c96688d36203a2f110f3c306e2db2fef Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 29 Jul 2020 21:22:47 +0200 Subject: [PATCH 06/10] Removed test, black works locally but not in CI.... --- tests/protocols/test_utils.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/protocols/test_utils.py b/tests/protocols/test_utils.py index d2846069d..779d9e780 100644 --- a/tests/protocols/test_utils.py +++ b/tests/protocols/test_utils.py @@ -53,11 +53,6 @@ def test_get_remote_addr_with_socket(): ) assert get_remote_addr(transport) == ("123.45.6.7", 123) - transport = MockTransport( - {"socket": MockSocket(family=socket.AF_UNIX, peername=("127.0.0.1", 8000))} - ) - assert get_remote_addr(transport) == ("127.0.0.1", 8000) - def test_get_local_addr(): transport = MockTransport({"sockname": "path/to/unix-domain-socket"}) From 253b7b7618b3147049557176268e75a29ad592fb Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 29 Jul 2020 21:37:55 +0200 Subject: [PATCH 07/10] Same deal on the server side of things --- tests/protocols/test_utils.py | 10 ++++++++++ uvicorn/protocols/utils.py | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/tests/protocols/test_utils.py b/tests/protocols/test_utils.py index 779d9e780..78ed0b3b7 100644 --- a/tests/protocols/test_utils.py +++ b/tests/protocols/test_utils.py @@ -38,6 +38,11 @@ def test_get_local_addr_with_socket(): ) assert get_local_addr(transport) == ("123.45.6.7", 123) + transport = MockTransport( + {"socket": MockSocket(family=socket.AF_UNIX, sockname=("127.0.0.1", 8000))} + ) + assert get_local_addr(transport) == ("127.0.0.1", 8000) + def test_get_remote_addr_with_socket(): transport = MockTransport({"socket": MockSocket(family=socket.AF_IPX)}) @@ -53,6 +58,11 @@ def test_get_remote_addr_with_socket(): ) assert get_remote_addr(transport) == ("123.45.6.7", 123) + transport = MockTransport( + {"socket": MockSocket(family=socket.AF_UNIX, peername=("127.0.0.1", 8000))} + ) + assert get_remote_addr(transport) == ("127.0.0.1", 8000) + def test_get_local_addr(): transport = MockTransport({"sockname": "path/to/unix-domain-socket"}) diff --git a/uvicorn/protocols/utils.py b/uvicorn/protocols/utils.py index e702f512d..1715eefcc 100644 --- a/uvicorn/protocols/utils.py +++ b/uvicorn/protocols/utils.py @@ -40,6 +40,15 @@ def get_local_addr(transport): family = socket_info.family if family in (socket.AF_INET, socket.AF_INET6): return (str(info[0]), int(info[1])) + elif hasattr(socket, "AF_UNIX") and family is socket.AF_UNIX: + if isinstance(info, tuple): + # fd case + # + return (str(info[0]), int(info[1])) + else: + # unix socket case + # + return None return None info = transport.get_extra_info("sockname") if info is not None and isinstance(info, (list, tuple)) and len(info) == 2: From 1ffa99677a1c251f123e517c05aa2b5a0c2e95e8 Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 29 Jul 2020 21:42:51 +0200 Subject: [PATCH 08/10] Test on AF_UNIX only if it is in socket --- tests/protocols/test_utils.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/protocols/test_utils.py b/tests/protocols/test_utils.py index 78ed0b3b7..0fff34a5d 100644 --- a/tests/protocols/test_utils.py +++ b/tests/protocols/test_utils.py @@ -38,10 +38,11 @@ def test_get_local_addr_with_socket(): ) assert get_local_addr(transport) == ("123.45.6.7", 123) - transport = MockTransport( - {"socket": MockSocket(family=socket.AF_UNIX, sockname=("127.0.0.1", 8000))} - ) - assert get_local_addr(transport) == ("127.0.0.1", 8000) + if hasattr(socket, "AF_UNIX"): + transport = MockTransport( + {"socket": MockSocket(family=socket.AF_UNIX, sockname=("127.0.0.1", 8000))} + ) + assert get_local_addr(transport) == ("127.0.0.1", 8000) def test_get_remote_addr_with_socket(): @@ -58,10 +59,11 @@ def test_get_remote_addr_with_socket(): ) assert get_remote_addr(transport) == ("123.45.6.7", 123) - transport = MockTransport( - {"socket": MockSocket(family=socket.AF_UNIX, peername=("127.0.0.1", 8000))} - ) - assert get_remote_addr(transport) == ("127.0.0.1", 8000) + if hasattr(socket, "AF_UNIX"): + transport = MockTransport( + {"socket": MockSocket(family=socket.AF_UNIX, peername=("127.0.0.1", 8000))} + ) + assert get_remote_addr(transport) == ("127.0.0.1", 8000) def test_get_local_addr(): From d43d0f82bf6e8596a1a8cb283b9941dd11d9dd86 Mon Sep 17 00:00:00 2001 From: euri10 Date: Fri, 31 Jul 2020 11:45:53 +0200 Subject: [PATCH 09/10] Simpler handling --- uvicorn/protocols/utils.py | 37 +++++++------------------------------ 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/uvicorn/protocols/utils.py b/uvicorn/protocols/utils.py index 1715eefcc..62e5f5ccb 100644 --- a/uvicorn/protocols/utils.py +++ b/uvicorn/protocols/utils.py @@ -1,32 +1,20 @@ -import socket import urllib def get_remote_addr(transport): socket_info = transport.get_extra_info("socket") + peer_info = transport.get_extra_info("peername") + print(socket_info) + print(peer_info) if socket_info is not None: try: info = socket_info.getpeername() + return (str(info[0]), int(info[1])) if isinstance(info, tuple) else None except OSError: # This case appears to inconsistently occur with uvloop # bound to a unix domain socket. - family = None - info = None - else: - family = socket_info.family + return None - if family in (socket.AF_INET, socket.AF_INET6): - return (str(info[0]), int(info[1])) - elif hasattr(socket, "AF_UNIX") and family is socket.AF_UNIX: - if isinstance(info, tuple): - # fd case - # - return (str(info[0]), int(info[1])) - else: - # unix socket case - # - return None - return None info = transport.get_extra_info("peername") if info is not None and isinstance(info, (list, tuple)) and len(info) == 2: return (str(info[0]), int(info[1])) @@ -37,19 +25,8 @@ def get_local_addr(transport): socket_info = transport.get_extra_info("socket") if socket_info is not None: info = socket_info.getsockname() - family = socket_info.family - if family in (socket.AF_INET, socket.AF_INET6): - return (str(info[0]), int(info[1])) - elif hasattr(socket, "AF_UNIX") and family is socket.AF_UNIX: - if isinstance(info, tuple): - # fd case - # - return (str(info[0]), int(info[1])) - else: - # unix socket case - # - return None - return None + + return (str(info[0]), int(info[1])) if isinstance(info, tuple) else None info = transport.get_extra_info("sockname") if info is not None and isinstance(info, (list, tuple)) and len(info) == 2: return (str(info[0]), int(info[1])) From 44f07fe982debfe0a1c03b82c42e455f9dfa812f Mon Sep 17 00:00:00 2001 From: euri10 Date: Fri, 31 Jul 2020 11:48:43 +0200 Subject: [PATCH 10/10] Removed debug leftovers --- uvicorn/protocols/utils.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/uvicorn/protocols/utils.py b/uvicorn/protocols/utils.py index 62e5f5ccb..c993f1521 100644 --- a/uvicorn/protocols/utils.py +++ b/uvicorn/protocols/utils.py @@ -3,9 +3,6 @@ def get_remote_addr(transport): socket_info = transport.get_extra_info("socket") - peer_info = transport.get_extra_info("peername") - print(socket_info) - print(peer_info) if socket_info is not None: try: info = socket_info.getpeername()