Skip to content

Commit

Permalink
Replace proxy_addr with option to trust proxies in 10.0.0.0/8.
Browse files Browse the repository at this point in the history
  • Loading branch information
spladug committed Jan 23, 2012
1 parent f52759f commit e7b672d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions r2/example.ini
Expand Up @@ -74,9 +74,9 @@ locale = C
# default site language (two letter character code)
lang = en
lang_override =
# if your webserver is a proxy and on a different instance, use
# X-forwarded-for and set this to the webserver's IP
proxy_addr =
# if your webserver is a proxy and on a different instance on the same 10.0.0.0/8 network
# set X-forwarded-for and set this to true
trust_local_proxies = false
# hash for validating HTTP_TRUE_CLIENT_IP_HASH
ip_hash =
# timezone for storing
Expand Down
2 changes: 1 addition & 1 deletion r2/r2/lib/app_globals.py
Expand Up @@ -98,6 +98,7 @@ class Globals(object):
'disable_ads',
'static_pre_gzipped',
'static_secure_pre_gzipped',
'trust_local_proxies',
]

tuple_props = ['stalecaches',
Expand All @@ -112,7 +113,6 @@ class Globals(object):
'allowed_css_linked_domains',
'authorized_cnames',
'hardcache_categories',
'proxy_addr',
's3_media_buckets',
'allowed_pay_countries',
'case_sensitive_domains']
Expand Down
8 changes: 7 additions & 1 deletion r2/r2/lib/base.py
Expand Up @@ -42,6 +42,12 @@
from r2.lib.utils import UrlParser, query_string
logging.getLogger('scgi-wsgi').setLevel(logging.CRITICAL)


def is_local_address(ip):
# TODO: support the /20 and /24 private networks? make this configurable?
return ip.startswith('10.')


class BaseController(WSGIController):
def try_pagecache(self):
pass
Expand All @@ -65,7 +71,7 @@ def __call__(self, environ, start_response):
and hashlib.md5(true_client_ip + g.ip_hash).hexdigest() \
== ip_hash.lower()):
request.ip = true_client_ip
elif remote_addr in g.proxy_addr and forwarded_for:
elif g.trust_local_proxies and forwarded_for and is_local_address(remote_addr):
request.ip = forwarded_for.split(',')[-1]
else:
request.ip = environ['REMOTE_ADDR']
Expand Down

0 comments on commit e7b672d

Please sign in to comment.