Changeset - 3d7ba590f6f5
[Not reviewed]
default
0 4 0
Mads Kiilerich - 5 years ago 2021-05-09 22:34:02
mads@kiilerich.com
Grafted from: 9355431c99ac
auth: only use X- headers instead of REMOTE_ADDR if explicitly told so in remote_addr_header

Before, X-Forwarded-For (and others) headers would *always* be trusted blindly,
also in setups without a proxy server. It would thus in some cases be
possible for users to fake their IP, and thus potentially be possible to bypass
IP restrictions configured in Kallithea.

Fixed by making it configurable which WSGI environment variable to use for the
remote address. Users can configure remote_addr_header to for example
HTTP_X_FORWARDED_FOR instead of using the default REMOTE_ADDR.

This change is a bit similar to what is going on in the https_fixup middleware,
but is doing a bit more of what for example is happening in similar code in
werkzeug/middleware/proxy_fix.py .
4 files changed with 18 insertions and 16 deletions:
0 comments (0 inline, 0 general)
development.ini
Show inline comments
 
@@ -111,6 +111,9 @@ app_instance_uuid = development-not-secr
 
## cut off limit for large diffs (size in bytes)
 
cut_off_limit = 256000
 

	
 
## WSGI environment variable to get the IP address of the client (default REMOTE_ADDR)
 
#remote_addr_variable = HTTP_X_FORWARDED_FOR
 

	
 
## always pretend the client connected using HTTPS (default false)
 
#force_https = true
 

	
docs/setup.rst
Show inline comments
 
@@ -423,8 +423,12 @@ somehow pass the original information on
 
configured to pick that information up and trust it.
 

	
 
Kallithea will by default rely on its WSGI server to provide the IP of the
 
client in the WSGI environment as ``REMOTE_ADDR``, but it can also
 
get it from the ``X-Real-IP`` or ``X-Forwarded-For`` HTTP headers.
 
client in the WSGI environment as ``REMOTE_ADDR``, but it can be configured to
 
get it from an HTTP header that has been set by the proxy server. For
 
example, if the proxy server puts the client IP in the ``X-Forwarded-For``
 
HTTP header, set::
 

	
 
    remote_addr_variable = HTTP_X_FORWARDED_FOR
 

	
 
Kallithea will by default rely on finding the protocol (``http`` or ``https``)
 
in the WSGI environment as ``wsgi.url_scheme``. If the proxy server puts
kallithea/controllers/base.py
Show inline comments
 
@@ -81,20 +81,12 @@ def _filter_proxy(ip):
 

	
 

	
 
def get_ip_addr(environ):
 
    proxy_key = 'HTTP_X_REAL_IP'
 
    proxy_key2 = 'HTTP_X_FORWARDED_FOR'
 
    def_key = 'REMOTE_ADDR'
 

	
 
    ip = environ.get(proxy_key)
 
    if ip:
 
        return _filter_proxy(ip)
 

	
 
    ip = environ.get(proxy_key2)
 
    if ip:
 
        return _filter_proxy(ip)
 

	
 
    ip = environ.get(def_key, '0.0.0.0')
 
    return _filter_proxy(ip)
 
    """The web server will set REMOTE_ADDR to the unfakeable IP layer client IP address.
 
    If using a proxy server, make it possible to use another value, such as
 
    the X-Forwarded-For header, by setting `remote_addr_variable = HTTP_X_FORWARDED_FOR`.
 
    """
 
    remote_addr_variable = kallithea.CONFIG.get('remote_addr_variable', 'REMOTE_ADDR')
 
    return _filter_proxy(environ.get(remote_addr_variable, '0.0.0.0'))
 

	
 

	
 
def get_path_info(environ):
kallithea/templates/ini/template.ini.mako
Show inline comments
 
@@ -174,6 +174,9 @@ app_instance_uuid = ${uuid()}
 
<%text>##</%text> cut off limit for large diffs (size in bytes)
 
cut_off_limit = 256000
 

	
 
<%text>##</%text> WSGI environment variable to get the IP address of the client (default REMOTE_ADDR)
 
#remote_addr_variable = HTTP_X_FORWARDED_FOR
 

	
 
<%text>##</%text> always pretend the client connected using HTTPS (default false)
 
#force_https = true
 

	
0 comments (0 inline, 0 general)