Changeset - 070b8c39736f
[Not reviewed]
default
0 5 0
Mads Kiilerich (mads) - 5 years ago 2021-05-09 22:40:56
mads@kiilerich.com
auth: only use X- headers instead of wsgi.url_scheme if explicitly told so in url_scheme_header - drop https_fixup setting

Before, several X- headers would be trusted to overrule the actual connection
protocol (http or https) seen by the Kallithea WSGI server. That was mainly
when https_fixup were set, but it incorrectly also kicked in if https_fixup or
use_htsts were configured. The ambiguity of which headers were used also made
it less reliable. The proxy server not only had to be configured to set one of
the headers correctly, it also had to make sure other headers were not passed
on from the client. It would thus in some cases be possible for clients to fake
the connection scheme, and thus potentially be possible to bypass restrictions
configured in Kallithea.

Fixed by making it configurable which WSGI environment variable to use for the
protocol. Users can configure url_scheme_header to for example
HTTP_X_FORWARDED_PROTO instead of using the default wsgi.url_scheme .

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 .

The semantics of the old https_fixup were unsafe, so it has been dropped.
Admins that are upgrading must change their configuration to use the new
url_scheme_header option.
5 files changed with 21 insertions and 19 deletions:
0 comments (0 inline, 0 general)
development.ini
Show inline comments
 
@@ -105,24 +105,27 @@ index_dir = %(here)s/data/index
 
archive_cache_dir = %(here)s/data/tarballcache
 

	
 
## change this to unique ID for security
 
#app_instance_uuid = VERY-SECRET
 
app_instance_uuid = development-not-secret
 

	
 
## 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
 

	
 
## WSGI environment variable to get the protocol (http or https) of the client connection (default wsgi.url_scheme)
 
#url_scheme_variable = HTTP_X_FORWARDED_PROTO
 

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

	
 
## use Strict-Transport-Security headers (default false)
 
#use_htsts = true
 

	
 
## number of commits stats will parse on each iteration
 
commit_parse_limit = 25
 

	
 
## Path to Python executable to be used for git hooks.
 
## This value will be written inside the git hook scripts as the text
 
## after '#!' (shebang). When empty or not defined, the value of
docs/setup.rst
Show inline comments
 
@@ -423,42 +423,40 @@ 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 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
 
the protocol of the client request in the ``X-Url-Scheme``,
 
``X-Forwarded-Scheme``, or ``X-Forwarded-Proto`` HTTP header,
 
Kallithea can be configured to trust these headers by setting::
 
the protocol of the client request in the ``X-Forwarded-Proto`` HTTP header,
 
Kallithea can be configured to trust that header by setting::
 

	
 
    https_fixup = true
 
    url_scheme_variable = HTTP_X_FORWARDED_PROTO
 

	
 

	
 
HTTPS support
 
-------------
 

	
 
Kallithea will by default generate URLs based on the WSGI environment.
 

	
 
Alternatively, you can use some special configuration settings to control
 
directly which scheme/protocol Kallithea will use when generating URLs:
 

	
 
- With ``https_fixup = true``, the scheme will be taken from the
 
  ``X-Url-Scheme``, ``X-Forwarded-Scheme`` or ``X-Forwarded-Proto`` HTTP header
 
  (default ``http``).
 
- With ``url_scheme_variable`` set, the scheme will be taken from that HTTP
 
  header.
 
- With ``force_https = true``, the scheme will be seen as ``https``.
 
- With ``use_htsts = true``, Kallithea will set ``Strict-Transport-Security`` when using https.
 

	
 
.. _nginx_virtual_host:
 

	
 

	
 
Nginx virtual host example
 
--------------------------
 

	
 
Sample config for Nginx using proxy:
 

	
 
.. code-block:: nginx
kallithea/config/application.py
Show inline comments
 
@@ -26,25 +26,25 @@ __all__ = ['make_app']
 

	
 

	
 
def wrap_app(app):
 
    """Wrap the TG WSGI application in Kallithea middleware"""
 
    config = app.config
 

	
 
    # we want our low level middleware to get to the request ASAP. We don't
 
    # need any stack middleware in them - especially no StatusCodeRedirect buffering
 
    app = SimpleHg(app, config)
 
    app = SimpleGit(app, config)
 

	
 
    # Enable https redirects based on HTTP_X_URL_SCHEME set by proxy
 
    if any(asbool(config.get(x)) for x in ['https_fixup', 'force_https', 'use_htsts']):
 
    if any(asbool(config.get(x)) for x in ['url_scheme_variable', 'force_https', 'use_htsts']):
 
        app = HttpsFixup(app, config)
 

	
 
    app = PermanentRepoUrl(app, config)
 

	
 
    # Optional and undocumented wrapper - gives more verbose request/response logging, but has a slight overhead
 
    if asbool(config.get('use_wsgi_wrapper')):
 
        app = RequestWrapper(app, config)
 

	
 
    return app
 

	
 

	
 
def make_app(global_conf, **app_conf):
kallithea/config/middleware/https_fixup.py
Show inline comments
 
@@ -17,24 +17,25 @@ kallithea.config.middleware.https_fixup
 

	
 
middleware to handle https correctly
 

	
 
This file was forked by the Kallithea project in July 2014.
 
Original author and date, and relevant copyright and licensing information is below:
 
:created_on: May 23, 2010
 
:author: marcink
 
:copyright: (c) 2013 RhodeCode GmbH, and others.
 
:license: GPLv3, see LICENSE.md for more details.
 
"""
 

	
 

	
 
import kallithea
 
from kallithea.lib.utils2 import asbool
 

	
 

	
 
class HttpsFixup(object):
 

	
 
    def __init__(self, app, config):
 
        self.application = app
 
        self.config = config
 

	
 
    def __call__(self, environ, start_response):
 
        self.__fixup(environ)
 
        debug = asbool(self.config.get('debug'))
 
@@ -45,29 +46,26 @@ class HttpsFixup(object):
 
                headers.append(('Strict-Transport-Security',
 
                                'max-age=8640000; includeSubDomains'))
 
            return start_response(status, headers, exc_info)
 

	
 
        return self.application(environ, custom_start_response)
 

	
 
    def __fixup(self, environ):
 
        """
 
        Function to fixup the environ as needed. In order to use this
 
        middleware you should set this header inside your
 
        proxy ie. nginx, apache etc.
 
        """
 
        # DETECT PROTOCOL !
 
        if 'HTTP_X_URL_SCHEME' in environ:
 
            proto = environ.get('HTTP_X_URL_SCHEME')
 
        elif 'HTTP_X_FORWARDED_SCHEME' in environ:
 
            proto = environ.get('HTTP_X_FORWARDED_SCHEME')
 
        elif 'HTTP_X_FORWARDED_PROTO' in environ:
 
            proto = environ.get('HTTP_X_FORWARDED_PROTO')
 
        else:
 
            proto = 'http'
 
        org_proto = proto
 
        proto = None
 

	
 
        # if we have force, just override
 
        if asbool(self.config.get('force_https')):
 
            proto = 'https'
 
        else:
 
            # get protocol from configured WSGI environment variable
 
            url_scheme_variable = kallithea.CONFIG.get('url_scheme_variable')
 
            if url_scheme_variable:
 
                proto = environ.get(url_scheme_variable)
 

	
 
        if proto:
 
            environ['wsgi._org_proto'] = environ.get('wsgi.url_scheme')
 
        environ['wsgi.url_scheme'] = proto
 
        environ['wsgi._org_proto'] = org_proto
kallithea/templates/ini/template.ini.mako
Show inline comments
 
@@ -168,24 +168,27 @@ index_dir = %(here)s/data/index
 
<%text>##</%text> uncomment and set this path to use archive download cache
 
archive_cache_dir = %(here)s/data/tarballcache
 

	
 
<%text>##</%text> change this to unique ID for security
 
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> WSGI environment variable to get the protocol (http or https) of the client connection (default wsgi.url_scheme)
 
#url_scheme_variable = HTTP_X_FORWARDED_PROTO
 

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

	
 
<%text>##</%text> use Strict-Transport-Security headers (default false)
 
#use_htsts = true
 

	
 
<%text>##</%text> number of commits stats will parse on each iteration
 
commit_parse_limit = 25
 

	
 
<%text>##</%text> Path to Python executable to be used for git hooks.
 
<%text>##</%text> This value will be written inside the git hook scripts as the text
 
<%text>##</%text> after '#!' (shebang). When empty or not defined, the value of
0 comments (0 inline, 0 general)