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

Do not override REMOTE_ADDR with X-Fowarded-For #633

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions gunicorn/config.py
Expand Up @@ -903,6 +903,21 @@ class XForwardedFor(Setting):
"""


class OverrideRemoteAddr(Setting):
name = "override_remote_addr"
section = "Server Mechanics"
cli = ["--override-remote-addr"]
validator = validate_bool
action = "store_true"
default = False
Copy link
Contributor

Choose a reason for hiding this comment

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

should not this option be True by default for downward compatibility?

Copy link
Owner

Choose a reason for hiding this comment

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

That's a good question , the code is here since a long time....

Copy link
Author

Choose a reason for hiding this comment

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

I am ok with that too, how do you wan't to call that option in that case ?

desc = """\
Override the REMOTE_ADDR using the X-Forwarded-For header

The settings XForwardedFor is also available to override
the name of the used header.
"""


class ForwardedAllowIPS(Setting):
name = "forwarded_allow_ips"
section = "Server Mechanics"
Expand Down
26 changes: 16 additions & 10 deletions gunicorn/http/wsgi.py
Expand Up @@ -80,10 +80,7 @@ def create(req, sock, client, server, cfg):

environ = default_environ(req, sock, cfg)

# authors should be aware that REMOTE_HOST and REMOTE_ADDR
# may not qualify the remote addr:
# http://www.ietf.org/rfc/rfc3875
forward = client or "127.0.0.1"
forward = None
url_scheme = "https" if cfg.is_ssl else "http"
script_name = os.environ.get("SCRIPT_NAME", "")

Expand Down Expand Up @@ -122,7 +119,12 @@ def create(req, sock, client, server, cfg):

environ['wsgi.url_scheme'] = url_scheme

if isinstance(forward, string_types):
# authors should be aware that REMOTE_HOST and REMOTE_ADDR
# may not qualify the remote addr:
# http://www.ietf.org/rfc/rfc3875

if forward and cfg.settings['override_remote_addr'].value:

# we only took the last one
# http://en.wikipedia.org/wiki/X-Forwarded-For
if forward.find(",") >= 0:
Expand All @@ -142,12 +144,16 @@ def create(req, sock, client, server, cfg):
else:
port = 80

remote = (host, port)
else:
remote = forward
environ['REMOTE_ADDR'] = host
environ['REMOTE_PORT'] = port

environ['REMOTE_ADDR'] = remote[0]
environ['REMOTE_PORT'] = str(remote[1])
else:
try:
environ['REMOTE_ADDR'] = client[0]
environ['REMOTE_PORT'] = str(client[1])
except IndexError:
# Client is empty if bound to a unix socket
environ['REMOTE_ADDR'] = 'unix:' + sock.getsockname()

if isinstance(server, string_types):
server = server.split(":")
Expand Down