Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/remote addr #232

Merged
merged 3 commits into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 13 additions & 4 deletions waitress/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,13 @@ def warn_unspecified_behavior(header):
forward_hop = forward_hop.strip()
forward_hop = undquote(forward_hop)

# Make sure that all IPv6 addresses are surrounded by brackets
# Make sure that all IPv6 addresses are surrounded by brackets,
# this is assuming that the IPv6 representation here does not
# include a port number.
Copy link
Member Author

Choose a reason for hiding this comment

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

https://tools.ietf.org/html/rfc5952#page-11 is a beast, and I am certainly not going to attempt to support every format out there.


if ":" in forward_hop and forward_hop[-1] != "]":
if "." not in forward_hop and (
":" in forward_hop and forward_hop[-1] != "]"
):
forwarded_for.append("[{}]".format(forward_hop))
else:
forwarded_for.append(forward_hop)
Expand Down Expand Up @@ -718,12 +722,17 @@ def warn_unspecified_behavior(header):
environ["SERVER_PORT"] = str(forwarded_port)

if client_addr:
def strip_brackets(addr):
if addr[0] == "[" and addr[-1] == "]":
return addr[1:-1]
return addr

if ":" in client_addr and client_addr[-1] != "]":
addr, port = client_addr.rsplit(":", 1)
environ["REMOTE_ADDR"] = addr.strip()
environ["REMOTE_ADDR"] = strip_brackets(addr.strip())
environ["REMOTE_PORT"] = port.strip()
else:
environ["REMOTE_ADDR"] = client_addr.strip()
environ["REMOTE_ADDR"] = strip_brackets(client_addr.strip())

return untrusted_headers

Expand Down
9 changes: 5 additions & 4 deletions waitress/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ def test_parse_proxy_headers_forwarded_for_v6_missing_brackets(self):
trusted_proxy_headers={'x-forwarded-for'}
)

self.assertEqual(environ['REMOTE_ADDR'], '[2001:db8::0]')
self.assertEqual(environ['REMOTE_ADDR'], '2001:db8::0')

def test_parse_proxy_headers_forwared_for_multiple(self):
inst = self._makeOne()
Expand Down Expand Up @@ -1029,7 +1029,7 @@ def test_parse_forwarded_multiple_proxies(self):
inst = self._makeOne()

headers = {
'FORWARDED': 'for="[2001:db8::1]";host="example.com:8443";proto="https", for=192.0.2.1;host="example.internal:8080"'
'FORWARDED': 'for="[2001:db8::1]:3821";host="example.com:8443";proto="https", for=192.0.2.1;host="example.internal:8080"'
}
environ = {}
inst.parse_proxy_headers(
Expand All @@ -1039,7 +1039,8 @@ def test_parse_forwarded_multiple_proxies(self):
trusted_proxy_headers={'forwarded'}
)

self.assertEqual(environ['REMOTE_ADDR'], '[2001:db8::1]')
self.assertEqual(environ['REMOTE_ADDR'], '2001:db8::1')
self.assertEqual(environ['REMOTE_PORT'], '3821')
self.assertEqual(environ['SERVER_NAME'], 'example.com')
self.assertEqual(environ['HTTP_HOST'], 'example.com:8443')
self.assertEqual(environ['SERVER_PORT'], '8443')
Expand All @@ -1059,7 +1060,7 @@ def test_parse_forwarded_multiple_proxies_minimal(self):
trusted_proxy_headers={'forwarded'}
)

self.assertEqual(environ['REMOTE_ADDR'], '[2001:db8::1]')
self.assertEqual(environ['REMOTE_ADDR'], '2001:db8::1')
self.assertEqual(environ['SERVER_NAME'], 'example.org')
self.assertEqual(environ['HTTP_HOST'], 'example.org')
self.assertEqual(environ['SERVER_PORT'], '443')
Expand Down